My Project
thread.h
Go to the documentation of this file.
1 #ifndef _THREAD_H
2 #define _THREAD_H
3 
4 #include <limits.h>
5 #include <pthread.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <cstddef>
9 #include <exception>
10 
11 typedef pthread_t Thread;
12 
13 void ThreadError(const char *message);
14 
15 class ConditionVariable;
16 
17 class Lock {
18 private:
19  pthread_mutex_t mutex;
20  friend class ConditionVariable;
22  int locked;
23  bool recursive;
24  void resume_lock(int l) {
25  owner = pthread_self();
26  locked = l;
27  }
28  int break_lock() {
29  extern pthread_t no_thread;
30  int result = locked;
31  owner = no_thread;
32  locked = 0;
33  return result;
34  }
35 public:
36  Lock(bool rec = false) {
37  extern pthread_t no_thread;
38  pthread_mutex_init(&mutex, NULL);
39  locked = 0;
40  recursive = rec;
41  owner = no_thread;
42  }
43  ~Lock() {
44  pthread_mutex_destroy(&mutex);
45  }
46  void lock() {
47  Thread self = pthread_self();
48  if (owner == self) {
49  if (locked && !recursive)
50  ThreadError("locking mutex twice");
51  }
52  else
53  pthread_mutex_lock(&mutex);
54  owner = self;
55  locked++;
56  }
57  void unlock() {
58  extern pthread_t no_thread;
59  Thread self = pthread_self();
60  if (owner != self)
61  ThreadError("unlocking unowned lock");
62  locked--;
63  if (locked == 0) {
64  owner = no_thread;
65  pthread_mutex_unlock(&mutex);
66  }
67  }
68  bool is_locked() {
69  return locked != 0 && owner == pthread_self();
70  }
71 };
72 
74  friend class Lock;
75 private:
76  pthread_cond_t condition;
78  int waiting;
79  friend class Semaphore;
81 public:
82  ConditionVariable(Lock *lock_init) : waiting(0), lock(lock_init) {
83  pthread_cond_init(&condition, NULL);
84  }
86  pthread_cond_destroy(&condition);
87  }
88  void wait() {
89  if (!lock->is_locked())
90  ThreadError("waited on condition without locked mutex");
91  waiting++;
92  int l = lock->break_lock();
93  pthread_cond_wait(&condition, &lock->mutex);
94  waiting--;
95  lock->resume_lock(l);
96  }
97  void signal() {
98  if (!lock->is_locked())
99  ThreadError("signaled condition without locked mutex");
100  if (waiting)
101  pthread_cond_signal(&condition);
102  }
103  void broadcast() {
104  if (!lock->is_locked())
105  ThreadError("signaled condition without locked mutex");
106  if (waiting)
107  pthread_cond_broadcast(&condition);
108  }
109 };
110 
111 class Semaphore {
112 private:
115  unsigned count;
116  unsigned waiting;
117 public:
118  Semaphore() : lock(), cond(&lock), count(0), waiting(0) {
119  }
120  Semaphore(unsigned count0) : lock(), cond(&lock), count(count0), waiting(0) {
121  }
122  void wait();
123  void post();
124 };
125 
126 #endif // _THREAD_H
int l
Definition: cfEzgcd.cc:100
void wait()
Definition: thread.h:88
ConditionVariable(Lock *lock_init)
Definition: thread.h:82
~ConditionVariable()
Definition: thread.h:85
pthread_cond_t condition
Definition: thread.h:76
void broadcast()
Definition: thread.h:103
void signal()
Definition: thread.h:97
Lock * lock
Definition: thread.h:77
Definition: thread.h:17
bool is_locked()
Definition: thread.h:68
int break_lock()
Definition: thread.h:28
Thread owner
Definition: thread.h:21
~Lock()
Definition: thread.h:43
void lock()
Definition: thread.h:46
void unlock()
Definition: thread.h:57
Lock(bool rec=false)
Definition: thread.h:36
pthread_mutex_t mutex
Definition: thread.h:19
int locked
Definition: thread.h:22
bool recursive
Definition: thread.h:23
void resume_lock(int l)
Definition: thread.h:24
ConditionVariable cond
Definition: thread.h:114
Lock lock
Definition: thread.h:113
unsigned waiting
Definition: thread.h:116
void wait()
Definition: thread.cc:23
void post()
Definition: thread.cc:33
Semaphore(unsigned count0)
Definition: thread.h:120
unsigned count
Definition: thread.h:115
Semaphore()
Definition: thread.h:118
return result
Definition: facAbsBiFact.cc:75
void message(int i, int *reduc, int *olddeg, kStrategy strat, int red_result)
Definition: kutil.cc:7730
#define NULL
Definition: omList.c:12
pthread_t no_thread
Definition: thread.cc:16
void ThreadError(const char *message)
Definition: thread.cc:18
pthread_t Thread
Definition: thread.h:11