File: Thread.hpp

package info (click to toggle)
freefem3d 1.0pre10-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 24,816 kB
  • ctags: 8,361
  • sloc: cpp: 57,201; sh: 8,788; yacc: 2,975; makefile: 1,148; ansic: 508; perl: 110
file content (495 lines) | stat: -rw-r--r-- 7,198 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//  This file is part of ff3d - http://www.freefem.org/ff3d
//  Copyright (C) 2003 Pascal Hav
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2, or (at your option)
//  any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//  $Id: Thread.hpp,v 1.6 2007/05/20 23:02:46 delpinux Exp $

/**
 * @file   Thread.hpp
 * @author Pascal Have
 * @date   Tue Oct  7 21:35:03 2003
 * 
 * @brief Thread, Semaphore and Mutex classes
 * 
 * @note If the system does not support pthread, a none threaded
 * version is used (see second part of this file)
 * 
 */

#ifndef THREAD_HPP
#define THREAD_HPP

#include <cstddef>
#include <config.h>
#include <Assert.hpp>

//! Virtual Runnable Function
/*! Used as template independent virtual class */
class Runnable {
public:
  //! Destructor 
  virtual ~Runnable() { }
  
  //! Run function
  virtual void run() = 0;

  //! Exit function
  virtual void clean() { }
};


/** 
 * We first define the pthread versions of those classes
 */
#ifdef HAVE_PTHREAD
#include <pthread.h>
#include <semaphore.h>

extern "C" {  
  //! C function called when cleaning a thread
  inline void clean_function(void * t) {
    reinterpret_cast<Runnable *>(t)->clean();
  }

  //! C function called for starting a thread class
  inline void * thread_function(void * t) {
    Runnable *f = reinterpret_cast<Runnable *>(t);
    ASSERT(f != NULL);
    pthread_cleanup_push(clean_function,f);
    f->run(); // Appel par virtual
    pthread_cleanup_pop(0);
    pthread_exit(NULL);
    return NULL;
  }
}

//! Main Thread class
class Thread
  : public Runnable
{
protected:
  pthread_t thread;		/**< The thread struture */

public:
  /** 
   * Starts the Tread
   * 
   * @return true if anything was ok
   */
  bool start()
  {
    if (thread) return false;
#ifndef NDEBUG
    const int ret = 
#endif /* NDEBUG */
      pthread_create(&thread,NULL,
		     thread_function,
		     this);
    ASSERT(ret == 0);
    return true;
  }

  /** 
   * Cancels the current thread
   * 
   */
  void cancel() const
  {
    pthread_cancel(thread);
  }

  /** 
   * Changes cancellable state of a thread
   * 
   * @param b true or false
   */
  static inline void cancellable(const bool& b)
  {
    pthread_setcancelstate((b)?PTHREAD_CANCEL_ENABLE:PTHREAD_CANCEL_DISABLE
			   ,NULL);
  }

  /** 
   * Forces cancelation of cancel request.
   * 
   */
  static inline void cancellable()
  {
    pthread_testcancel();
  }

  /** 
   * 
   * Waits for thread to finish
   * 
   * @return true if thread was already joined
   */
  inline bool join()
  {
    if (thread) {
      void * ret;
      pthread_join(thread,&ret);
      return false;
    }
    return true;
  }

  /** 
   * Gets the Id of the Thread
   * 
   */
  inline const pthread_t& getId() const
  {
    return thread;
  }

  /** 
   * Constructor
   * 
   */
  Thread()
    : thread(0)
  {
    ;
  }

  /** 
   * Thread destructir
   * 
   */
  virtual ~Thread()
  {
    this->join();
  }
};

//! Mutex class
class Mutex
{
protected:
  pthread_mutex_t mutex;	/**< the mutex */

public:
  /** 
   * Locks the mutex resource
   * 
   */
  inline void lock()
  {
    pthread_mutex_lock(&mutex); 
  }

  /** 
   * Unlocks mutex resource
   * 
   */
  inline void unlock()
  {
    pthread_mutex_unlock(&mutex);
  }

  /** 
   * Tries to lock mutex resource
   * 
   * 
   * @return 0 if locking was possible
   */
  inline int trylock()
  {
    return pthread_mutex_trylock(&mutex);
  }

  /** 
   * Constructor
   * 
   */
  Mutex()
  { 
    pthread_mutex_init(&mutex,NULL);
  }

  /** 
   * Destructor
   * 
   */
  ~Mutex()
  {
    pthread_mutex_destroy(&mutex);
  }
};

//! Semaphore class
class Semaphore
{
protected:
  sem_t semaphore;		/**< semaphore structure */

public:

  /** 
   * Waits for resource
   * 
   */
  inline void wait()
  {
    sem_wait(&semaphore);
  }

  /** 
   * Increments semaphore counter (decrease priority)
   * 
   */
  inline void post()
  {
    sem_post(&semaphore);
  }

  /** 
   * Tries to get resource
   * 
   * 
   * @return 0 if not reserved
   */
  inline int trywait()
  {
    return sem_trywait(&semaphore);
  }

  /** 
   * Constructor
   * 
   */
  Semaphore()
  { 
    sem_init(&semaphore,
	     0, // Interprocessus?
	     1  // Number of threads which can access simultaneously to the protected zone
	     );
  }

  /** 
   * Destructor
   * 
   */
  ~Semaphore()
  {
    sem_destroy(&semaphore);
  }
};

#else  // HAVE_PTHREAD

/** 
 * We now define the unthreaded versions of those classes.
 *
 * We only provide here the same interface as above without anyother
 * effort.
 */

//! Main Thread class
class Thread
  : public Runnable
{
public:
  typedef size_t Id;		/**< Defines Thread Id */

public:
  /** 
   * Starts the Tread
   * 
   * @return true if anything was ok
   */
  bool start()
  {
    this->run();
    return true;
  }

  /** 
   * Cancels the current thread
   * 
   */
  void cancel() const
  {
    ;
  }

  /** 
   * Changes cancellable state of a thread
   * 
   * @param b true or false
   */
  static inline void cancellable(const bool& b)
  {
    ;
  }

  /** 
   * Forces cancelation of cancel request.
   * 
   */
  static inline void cancellable()
  {
    ;
  }

  /** 
   * 
   * Waits for thread to finish
   * 
   * @return true if thread was already joined
   */
  inline bool join()
  {
    return true;
  }

  /** 
   * Gets the Id of the Thread
   * 
   */
  inline Id getId() const
  {
    return 0;
  }

  /** 
   * Constructor
   * 
   */
  Thread()
  {
    ;
  }

  /** 
   * Thread destructir
   * 
   */
  virtual ~Thread()
  {
    ;
  }
};

//! Mutex class
class Mutex
{
public:
  /** 
   * Locks the mutex resource
   * 
   */
  inline void lock()
  {
    ;
  }

  /** 
   * Unlocks mutex resource
   * 
   */
  inline void unlock()
  {
    ;
  }

  /** 
   * Tries to lock mutex resource
   * 
   * 
   * @return 0 if locking was possible
   */
  inline int trylock()
  {
    return 0;
  }

  /** 
   * Constructor
   * 
   */
  Mutex()
  { 
    ;
  }

  /** 
   * Destructor
   * 
   */
  ~Mutex()
  {
    ;
  }
};

//! Semaphore class
class Semaphore
{
public:
  /** 
   * Waits for resource
   * 
   */
  inline void wait()
  {
    ;
  }

  /** 
   * Increments semaphore counter (decrease priority)
   * 
   */
  inline void post()
  {
    ;
  }

  /** 
   * Tries to get resource
   * 
   * 
   * @return 0 if not reserved
   */
  inline int trywait()
  {
    return 0;
  }

  /** 
   * Constructor
   * 
   */
  Semaphore()
  { 
    ;
  }

  /** 
   * Destructor
   * 
   */
  ~Semaphore()
  {
    ;
  }
};


#endif // HAVE_PTHREAD

#endif /* THREAD_HPP */