Xmipp  v3.23.11-Nereus
xmipp_threads.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: J.M. de la Rosa Trevin (jmdelarosa@cnb.csic.es)
3  *
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef THREADS_T
27 #define THREADS_T
28 
29 #include <pthread.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 
33 class ThreadManager;
34 class ThreadArgument;
35 class Condition;
36 
37 /* Prototype of functions for threads works. */
38 typedef void (*ThreadFunction) (ThreadArgument &arg);
39 
40 //TODO (MARIANA) Please give more documentation and in a good structure e.g. @name (see args.h as example)
41 
45 
52 class Mutex
53 {
54 private:
55  pthread_mutex_t mutex; //our pthread mutex
56 
57 public:
62  Mutex();
63 
65  virtual ~Mutex();
66 
72  virtual void lock();
73 
78  virtual void unlock();
79 
80  friend class Condition;
81 }
82 ;//end of class Mutex
83 
88 class Condition
89 {
90 private:
91  Mutex *mutex; //our pthread mutex
92  pthread_cond_t cond; //our pthread cond
93 
94 public:
99  Condition();
100 
102  ~Condition();
103 
106  void lock();
107 
110  void unlock();
111 
117  void wait();
118 
123  void signal();
124 
126  void broadcast();
127 
128 }
129 ;//end of class Condition
130 
150 class Barrier
151 {
152 private:
153  int needed;
154  int called;
155  Condition * condition;
156 
157 public:
174  Barrier(int numberOfThreads);
175 
177  ~Barrier();
178 
183  void wait();
184 
185 }
186 ;//end of class Barrier
187 
192 class Thread
193 {
194 private:
195  pthread_t thId;
196 
197 public:
202  Thread();
203 
205  virtual ~Thread();
206 
210  virtual void run() = 0;
211 
215  void start();
216 }
217 ;//end of class Condition
218 
223 void * _singleThreadMain(void * data);
224 
228 void * _threadMain(void * data);
229 
240 {
241 public:
242  int threads;
243 private:
244  pthread_t * ids;
245  ThreadArgument * arguments;
246  Barrier * barrier;
247  ThreadFunction workFunction;
250  bool started;
251  void * workClass;
252 
256  void createThreads();
257 
258 public:
262  void setData(void * data, int nThread = -1);
263 
265  ThreadManager(int numberOfThreads, void * workClass = NULL);
266 
268  ~ThreadManager();
269 
315  void run(ThreadFunction function, void * data = NULL);
316 
318  void runAsync(ThreadFunction function, void * data = NULL);
319 
321  void wait();
322 
325  friend void * _threadMain(void * data);
326 
328  int getNumberOfThreads() {return threads;}
329 }
330 ;//end of class ThreadManager
331 
338 {
339 private:
340  ThreadManager * manager;
341 public:
342  int thread_id;
343  int threads;
344  void * workClass;
345  void * data; // Pointer to void *
346 
347  ThreadArgument();
348  ThreadArgument(int id, ThreadManager * manager = NULL, void * data = NULL);
349 
350  friend class ThreadManager;
351  friend void * _threadMain(void * data);
352  int getNumberOfThreads() {return manager->getNumberOfThreads();}
353 };
354 
367 {
368 protected:
369  //How many tasks give in each request
370  size_t blockSize;
371  //The number of tasks that have been assigned
373 
374 public:
375  //The total number of tasks to be distributed
382  ParallelTaskDistributor(size_t nTasks, size_t bSize);
383 
387  {}
388  ;
389 
395  void clear();
396 
398  void setBlockSize(size_t bSize);
399 
401  int getBlockSize() const;
402 
427  bool getTasks(size_t &first, size_t &last); // False = no more jobs, true = more jobs
428  /* This function set the number of completed tasks.
429  * Usually this not need to be called. Its more useful
430  * for restarting work, when usually the master detect
431  * the number of tasks already done.
432  */
433  bool setAssignedTasks(size_t tasks);
434 
435 
436 protected:
437  //Virtual functions that should be implemented in
438  //subclasses, providing a mechanism of lock and
439  //the specific way of distribute tasks.
440  virtual void lock() = 0;
441  virtual void unlock() = 0;
442  virtual bool distribute(size_t &first, size_t &last) = 0;
443 
444 }
445 ;//class ParallelTaskDistributor
446 
452 {
453 public:
454  ThreadTaskDistributor(size_t nTasks, size_t bSize):ParallelTaskDistributor(nTasks, bSize)
455  {}
457  {}
458  ;
459 protected:
460  Mutex mutex;
461  virtual void lock();
462  virtual void unlock();
463  virtual bool distribute(size_t &first, size_t &last);
464 public:
465  virtual void reset() { setAssignedTasks(0); };
466 }
467 ;//end of class ThreadTaskDistributor
468 
472 typedef struct mybarrier_t
473 {
475  int needed;
477  int called;
479  pthread_mutex_t mutex;
481  pthread_cond_t cond;
482 }
483 barrier_t;
484 
486 int barrier_init(barrier_t *barrier, int needed);
492 
493 #endif
int barrier_init(barrier_t *barrier, int needed)
Mutex mutex
void * _threadMain(void *data)
friend class Condition
Definition: xmipp_threads.h:80
virtual ~ThreadTaskDistributor()
int getNumberOfThreads()
int threads
number of working threads.
int called
How many threads already arrived.
int barrier_destroy(barrier_t *barrier)
int getNumberOfThreads()
void * _singleThreadMain(void *data)
virtual ~Mutex()
glob_log first
Barrier * barrier
void(* ThreadFunction)(ThreadArgument &arg)
Definition: xmipp_threads.h:38
int threads
Number of threads.
int needed
How many threads should be awaited.
void * workClass
The class in which threads will be working.
pthread_cond_t cond
Condition on which the threads are waiting.
Mutex mutex
Mutex to synchronize access to critical region.
ThreadTaskDistributor(size_t nTasks, size_t bSize)
virtual ~ParallelTaskDistributor()
pthread_mutex_t mutex
Mutex to update this structure.
int thread_id
The thread id.
virtual void unlock()
int barrier_wait(barrier_t *barrier)
virtual void reset()
virtual void lock()
struct mybarrier_t barrier_t