Simultaneous.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2005 by Haydon Knight
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 #ifndef __baseband_dsp_Simultaneous_h_
10 #define __baseband_dsp_Simultaneous_h_
11 
12 #include "dsp/BasicBuffer.h"
13 #include "dsp/Operation.h"
14 #include "dsp/Buffer.h"
15 #include "dsp/TimeSeries.h"
16 
17 #include <queue>
18 
19 namespace dsp {
20 
25  class Simultaneous : public Operation {
26 
27  public:
28 
29  TimeSeries* last_on;
30 
32  Simultaneous();
33 
35  virtual ~Simultaneous();
36 
38  Operation* get_op(){ return op; }
39 
41  bool has_op(){ return op; }
42 
44  void set_op(Operation* _op){ op = _op; }
45 
47  template<class T>
48  T* get();
49 
51  void stop_thread();
52 
53  protected:
54 
56  virtual void operation ();
57 
62  bool new_data();
63 
66 
71 
73  void start_thread();
74 
76  void destroy_thread();
77 
79  static void* op_loop(Simultaneous* thiz);
80 
82  void wait_to_synch();
83 
85  bool can_run();
86 
88  void checks();
89 
91  void run_op();
92 
94  bool op_uses_an_input();
95 
97  bool op_uses_an_output();
98 
100  void setup_buffers();
101 
103  template<class T>
104  bool newify_buffers(vector<Reference::To<BasicBuffer> >& buffers);
105 
107  bool newify_input_buffers();
108 
110  bool newify_output_buffers();
111 
113  bool newify_those_buffers(string typestring,
114  vector<Reference::To<BasicBuffer> >& buffers);
115 
120 
122  string get_input_typestring();
123 
125  string get_output_typestring();
126 
128  void initialise_op_input();
130  void initialise_op_output();
131 
133  Reference::To<BasicBuffer> new_buffer(string typestring);
134 
137  Reference::To<BasicBuffer> pop_full_input_buffer();
138  Reference::To<BasicBuffer> pop_free_output_buffer();
139  Reference::To<BasicBuffer> pop_full_output_buffer();
140 
143  void wait_for_full_input_buffer();
144  void wait_for_free_output_buffer();
145  void wait_for_full_output_buffer();
146 
148  void init();
149 
152 
154  bool thread_running;
155 
157  vector<Reference::To<BasicBuffer> > input_buffers;
158  vector<Reference::To<BasicBuffer> > output_buffers;
159 
161  std::queue<Reference::To<BasicBuffer> > free_input_buffers;
162  std::queue<Reference::To<BasicBuffer> > full_input_buffers;
163  std::queue<Reference::To<BasicBuffer> > free_output_buffers;
164  std::queue<Reference::To<BasicBuffer> > full_output_buffers;
165 
168  Reference::To<BasicBuffer> op_output;
169 
171  pthread_mutex_t free_input_buffers_mutex;
172  pthread_mutex_t full_input_buffers_mutex;
173  pthread_mutex_t free_output_buffers_mutex;
174  pthread_mutex_t full_output_buffers_mutex;
175 
176  pthread_mutex_t free_input_mutex;
177  pthread_mutex_t full_input_mutex;
178  pthread_mutex_t free_output_mutex;
179  pthread_mutex_t full_output_mutex;
180 
181  pthread_cond_t free_input_cond;
182  pthread_cond_t full_input_cond;
183  pthread_cond_t free_output_cond;
184  pthread_cond_t full_output_cond;
185 
186  pthread_mutex_t synch_mutex;
187  pthread_cond_t synch_cond;
188 
189  pthread_mutex_t running_mutex;
190  pthread_cond_t running_cond;
191 
193  float wait_time;
194 
196  bool use_threads;
197 
199  pthread_t* op_thread;
200 
202  bool keep_running;
203 
205  bool synch;
206  };
207 
208 }
209 
211 template<class T>
213  return dynamic_cast<T*>( op.get() );
214 }
215 
217 template<class T>
219  for( unsigned i=0; i<buffers.size(); i++)
220  buffers[i] = new Buffer<T>;
221 
222  return true;
223 }
224 
225 #endif
static void * op_loop(Simultaneous *thiz)
This function runs in the extra thread.
virtual void open(const std::string &id)
Prepare the appropriate Input and Unpacker.
Definition: IOManager.C:38
void initialise_op_input()
Initialises the 'op_input' variable.
void wait_to_synch()
Makes the thread wait for the call to operate() before running.
bool use_threads
Whether to use threads [true].
Definition: Simultaneous.h:206
virtual void operation()
Does the work.
Produces TimeSeries data by integrating an Input with an Unpacker.
Definition: IOManager.h:26
bool newify_input_buffers()
Tries to work out what sort of buffers the input_buffers should be.
Contains all Baseband Data Reduction Library classes.
Definition: ASCIIObservation.h:17
Detects phase-coherent TimeSeries data.
Definition: Detection.h:38
static bool verbose
Verbosity flag.
Definition: Observation.h:39
bool has_op()
Inquire whether an Operation is being stored.
Definition: Simultaneous.h:51
Reference::To< BasicBuffer > new_buffer(string typestring)
Returns a new Buffer of the requisite type.
Data as a function of pulse phase.
Definition: PhaseSeries.h:28
void run_op()
Calls op->operate() after setting up the input/output buffers.
bool new_data()
Puts the new input data into the input ringbuffer and puts the output data processed in the extra thr...
static bool verbose
void set(Pulsar::Archive *archive, const PhaseSeries *phase)
Set the Pulsar::Archive with the PhaseSeries data.
Definition: Archiver.C:477
void set_op(Operation *_op)
Set the Operation.
Definition: Simultaneous.h:54
Reference::To< Operation > op
The class being run simultaneously.
Definition: Simultaneous.h:161
void initialise_op_output()
Initialises the 'op_output' variable.
bool thread_running
True if the thread has been started up.
Definition: Simultaneous.h:164
virtual void load(TimeSeries *data)
Load and convert the next block of data.
Definition: UnpackerSource.h:254
static void set_verbosity(unsigned level)
Simultaneous()
Null constructor.
Fold TimeSeries data into phase-averaged profile(s)
Definition: Fold.h:34
Operation(const char *name)
All sub-classes must specify a unique name.
Definition: Operation.C:46
static bool record_time
Global flag enables stopwatch to record the time spent operating.
Definition: Operation.h:42
Reference::To< BasicBuffer > pop_free_input_buffer()
pops a free/full input/output buffer as soon as one is available
void checks()
Checks stuff before operate()
void setup_buffers()
Makes sure the buffers point to something sane.
std::queue< Reference::To< BasicBuffer > > free_input_buffers
Ordered list of who is to be used next.
Definition: Simultaneous.h:171
Defines the interface by which operations are performed on data.
Definition: Operation.h:37
Container of weighted time-major order floating point data.
Definition: WeightedTimeSeries.h:26
T * get()
Convenience method to return the Operation.
Definition: Simultaneous.h:212
virtual void set_source(const std::string &name)
Set the source name.
Definition: Observation.h:120
bool newify_those_buffers(string typestring, vector< Reference::To< BasicBuffer > > &buffers)
Worker function for newify_input_buffers() and newify_output_buffers()
virtual bool eod() const =0
End of data.
void wait_for_free_input_buffer()
Waits around until a free/full input/output buffer is available.
string get_input_typestring()
Returns a string describing the input type.
bool copy_input_into_ringbuffer()
Copies the new input data into the input ringbuffer if necessary.
bool copy_ringbuffer_into_output()
Puts the output data processed in the extra thread into the output so that it's ready to be processed...
bool get_detected() const
Returns true if state is Detected, Coherence, or Stokes.
Definition: Observation.C:144
bool can_run()
Returns whether or not the user has said to stop.
void init()
Called by constructor to initialise stuff.
static bool verbose
Global verbosity flag.
Definition: Operation.h:48
void start_thread()
Spawns the extra thread if using threads.
Reference::To< BasicBuffer > setup_op_output()
Gives the Operation an output if it needs one.
bool verbose
bool op_uses_an_input()
Returns true if the Operation needs an input to be set.
float wait_time
Time-out time on call to pthread_cond_wait (in seconds) [1.0].
Definition: Simultaneous.h:203
bool keep_running
Whether the user wants to finish up or keep running [true].
Definition: Simultaneous.h:212
Class to unload PhaseSeries data in a Pulsar::Archive.
Definition: Archiver.h:43
void stop_thread()
Prompts the thread to exit.
Reference::To< BasicBuffer > op_input
Has the actual Buffers that the Operation was originally set up with.
Definition: Simultaneous.h:177
pthread_mutex_t free_input_buffers_mutex
Mutexes and conds.
Definition: Simultaneous.h:181
An extension of dsp::RingBuffer to allow any class to run in a separate thread Be wary of use in save...
Definition: Simultaneous.h:30
Operation * get_op()
Retrieve a pointer to the Operation.
Definition: Simultaneous.h:48
virtual ~Simultaneous()
Virtual destructor.
void detect(Profile *input)
vector< Reference::To< BasicBuffer > > input_buffers
Buffers for storing data
Definition: Simultaneous.h:167
bool newify_output_buffers()
Tries to work out what sort of buffers the input_buffers should be.
pthread_t * op_thread
The thread object.
Definition: Simultaneous.h:209
void destroy_thread()
Destroys the extra thread.
const InputType * get_input() const
Return pointer to the appropriate InputType.
Definition: UnpackerSource.h:77
static unsigned verbose
Verbose flag.
Definition: Archiver.h:48
bool synch
Whether to synchronise work with the main thread [false].
Definition: Simultaneous.h:215
Reference::To< BasicBuffer > setup_op_input()
Gives the Operation an input if it needs one.
string get_output_typestring()
Returns a string describing the output type.
bool op_uses_an_output()
Returns true if the Operation needs an output to be set.
virtual void set_block_size(uint64_t _size)
Set the number of time samples to load on each load_block.
Definition: Input.C:399
bool newify_buffers(vector< Reference::To< BasicBuffer > > &buffers)
Worker function that actually calls 'new' for each element in the vector.
Definition: Simultaneous.h:218

Generated using doxygen 1.8.17