gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
IIOThreaded.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 
18 #ifndef IIOTHREADED_H_
19 #define IIOTHREADED_H_
20 
21 #include "IIO.H"
22 #include "Thread.H"
23 #include "mffm/LinkList.H"
24 
25 class IIOThreaded : public IIO, public ThreadedMethod, public Cond {
26 
27 // Mutex fillRegion, emptyRegion;
28 // Mutex emptyRegion;
29 
31  Eigen::Array<unsigned short int, Eigen::Dynamic, Eigen::Dynamic> *fullBuffer;
32 
36  void *threadMain(void) {
37  struct timespec lockStart, lockStop;
38 
39  fullBuffer=NULL;
40  int nframes=getReadArraySampleCount(*buffers.current());
41  while (1) {
42  if( clock_gettime( CLOCK_REALTIME, &lockStart) == -1 ) {
43  cout<<"clock lockStart get time error"<<endl;
44  }
45 
46  int ret=read(nframes, *buffers.current());
47  if (ret!=NO_ERROR)
48  break;
49 
50  // store the pointer to the full buffer, switch to the empty buffer and indicate to any external thread that the full buffer is ready for reading.
51  fullBuffer=buffers.current(); // store a pointer to the full buffer
52  buffers.next(); // switch to the empty buffer.
53 
54  lock(); // lock the mutex, indicate the condition and wake the thread.
55  bufFull=true;
56  signal(); // Wake the WaitingThread
57  unLock(); // Unlock so the WaitingThread can continue.
58 
59  if( clock_gettime( CLOCK_REALTIME, &lockStop) == -1 ) {
60  cout<<"clock lockStop get time error"<<endl;
61  }
62  double duration = 1.e3*( lockStop.tv_sec - lockStart.tv_sec ) + (double)( lockStop.tv_nsec - lockStart.tv_nsec )/1.e6;
63  cout<<"thread duration = "<<duration<<'\n';
64 // if (duration<1800)
65 // usleep(2000);
66 
67  }
68 
69  cout<<"IIO read thread stopped due to error"<<endl;
70  return NULL;
71  }
72 
80  int resizeBuffers(int N, int ch) {
81  // if the data matrix is larger in columns then the number of capture channels, then resize it.
82 
83  // ensure that the buffer exist
84  while (buffers.getCount()<2) {
85  buffers.add(new Eigen::Array<unsigned short int, Eigen::Dynamic, Eigen::Dynamic>);
86  if (!buffers.current()) {
87  buffers.remove();
89  }
90  }
91  int retVal;
92  for (int i=0; i<buffers.getCount(); i++) {
93  if ((retVal=getReadArray(N, *buffers.next()))!=NO_ERROR)
94  return retVal;
95  int cols=(int)ceil((float)ch/(float)operator[](0).getChCnt()); // check whether we require less then the available number of channels
96  if (cols<buffers.current()->cols())
97  buffers.current()->resize(buffers.current()->rows(), cols);
98  }
99  bufferInfo();
100  //setChannelBufferCnt(N*2);
101  return NO_ERROR;
102  }
103 
104 public:
105  bool bufFull;
106 
108  bufFull=false;
109  fullBuffer=NULL;
110  }
111 
112  virtual ~IIOThreaded() {
113  while (buffers.getCount()) // remove and delete any buffers which are present
114  delete buffers.remove();
115  }
116 
118  return resizeBuffers(N, ch);
119  }
120 
121  void bufferInfo() {
122  cout<<"IIOThreaded :: there are "<<buffers.getCount()<<" buffers."<<endl;
123  for (int i=0; i<buffers.getCount(); i++)
124  cout<<"\tbuffer "<<i<<" has "<<buffers.grab(i+1)->cols()<<" channels with N="<<buffers.grab(i+1)->rows()<<endl;
125  }
126 
130  Eigen::Array<unsigned short int, Eigen::Dynamic, Eigen::Dynamic> *getFullBuffer() {
131  return fullBuffer;
132  }
133 
134 // int fillRegionLock(void){
135 // return fillRegion.lock();
136 // }
137 //
138 // int emptyRegionLock(bool lock){
139 // if (lock)
140 // return emptyRegion.lock();
141 // else
142 // return emptyRegion.unLock();
143 // }
144 };
145 
146 #endif // IIOTHREADED_H_
bool bufFull
Indicates when the read has completed and a buffer is full.
Definition: IIOThreaded.H:105
int N
Eigen::Array< unsigned short int, Eigen::Dynamic, Eigen::Dynamic > * getFullBuffer()
Definition: IIOThreaded.H:130
void signal()
Definition: Thread.H:369
int setSampleCountChannelCount(uint N, uint ch)
Definition: IIOThreaded.H:117
virtual int evaluateError(int errorNum)
Definition: Debug.H:132
Definition: Thread.H:340
int getReadArraySampleCount(Eigen::Array< TYPE, Eigen::Dynamic, Eigen::Dynamic > &array)
Definition: IIO.H:241
#define NO_ERROR
There is no error.
Definition: Debug.H:33
int read(uint N, const Eigen::Array< TYPE, Eigen::Dynamic, Eigen::Dynamic > &array)
Definition: IIO.H:254
The iio_channel_info structure is external.
Definition: IIO.H:72
unsigned int uint
Definition: Box.H:28
int getReadArray(uint N, Eigen::Array< TYPE, Eigen::Dynamic, Eigen::Dynamic > &array)
Definition: IIO.H:222
void * threadMain(void)
Definition: IIOThreaded.H:36
Eigen::Array< unsigned short int, Eigen::Dynamic, Eigen::Dynamic > * fullBuffer
Definition: IIOThreaded.H:31
int resizeBuffers(int N, int ch)
Definition: IIOThreaded.H:80
int unLock()
Definition: Thread.H:325
void bufferInfo()
Definition: IIOThreaded.H:121
virtual ~IIOThreaded()
Definition: IIOThreaded.H:112
#define IIODEVICE_RESIZEBUF_NEW_ERROR
One of the devices has a different buffer size to the other devices.
Definition: IIOChannel.H:42
int lock()
Definition: Thread.H:295
LinkList< Eigen::Array< unsigned short int, Eigen::Dynamic, Eigen::Dynamic > * > buffers
The load and unload buffers.
Definition: IIOThreaded.H:30
gtkIOStream: /tmp/gtkiostream/include/IIO/IIOThreaded.H Source File
GTK+ IOStream  Beta