File: Thread.hpp

package info (click to toggle)
freefem3d 1.0pre5-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,516 kB
  • ctags: 6,368
  • sloc: cpp: 37,565; sh: 8,535; yacc: 2,728; ansic: 830; makefile: 339; perl: 110
file content (168 lines) | stat: -rw-r--r-- 3,753 bytes parent folder | download
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
//  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.1 2003/10/07 19:39:04 opkod Exp $

/**
 * @file   Thread.hpp
 * @author Pascal Have
 * @date   Tue Oct  7 21:35:03 2003
 * 
 * @brief  Thread, Semaphore and Mutex classes
 * 
 * 
 */

#ifndef THREAD_HPP
#define THREAD_HPP

#include <cassert>
#include <pthread.h>
#include <semaphore.h>

//! 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() { }
};

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);
    delete f; // destruction de l'argument forcment dynamique
    pthread_exit(NULL);
  }
}

//! Thread Proxy class
template<typename T>
class ThreadFunction : public Runnable {
protected:
  T & _t;
public:
  ThreadFunction(T * const t) : _t(*t) { }
  virtual ~ThreadFunction() { }
  inline void run() { _t.run(); }
  inline void clean() { _t.clean(); }
};

//! Main Thread class
class Thread : public Runnable {
protected:
  pthread_t thread;
public:
  Thread() { }
  virtual ~Thread() { join(); }

  void start() {
#ifndef NDEBUG
    const int ret = 
#endif /* NDEBUG */
      pthread_create(&thread,NULL,
		     thread_function,
		     new ThreadFunction<Thread>(this));
    assert(ret == 0);
  }

  void cancel() const {
    pthread_cancel(thread);
  }

  static inline void cancellable(const bool b) {
    pthread_setcancelstate((b)?PTHREAD_CANCEL_ENABLE:PTHREAD_CANCEL_DISABLE
			   ,NULL);
  }
  
  static inline void cancellable() {
    pthread_testcancel();
  }

  inline void join() const {
    void * ret;
    pthread_join(thread,&ret);
  }

  inline unsigned getId() const {
    return thread;
  }
};

class Mutex {
protected:
  pthread_mutex_t mutex;
public:
  Mutex() { 
    pthread_mutex_init(&mutex,NULL);
  }

  ~Mutex() {
    pthread_mutex_destroy(&mutex);
  }
  inline void lock() { 
    pthread_mutex_lock(&mutex); 
  }
  inline void unlock() {
    pthread_mutex_unlock(&mutex);
  }
  inline int trylock() {
    return pthread_mutex_trylock(&mutex);
  }
};

class Semaphore {
protected:
  sem_t semaphore;
public:
  Semaphore() { 
    sem_init(&semaphore,
	     0, // Interprocessus?
	     1  // Number of threads which can access simultaneously to the protected zone
	     );
  }
  ~Semaphore() {
    sem_destroy(&semaphore);
  }
  inline void wait() {
    sem_wait(&semaphore);
  }
  inline void post() {
    sem_post(&semaphore);
  }
  inline int trywait() {
    return sem_trywait(&semaphore);
  }
};

#endif /* THREAD_HPP */