Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
#ifndef _THREAD_H
#define _THREAD_H
#include <limits.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <cstddef>
#include <exception>
typedef pthread_t Thread;
void ThreadError(const char *message);
class ConditionVariable;
class Lock {
private:
pthread_mutex_t mutex;
friend class ConditionVariable;
Thread owner;
int locked;
bool recursive;
void resume_lock(int l) {
owner = pthread_self();
locked = l;
}
int break_lock() {
extern pthread_t no_thread;
int result = locked;
owner = no_thread;
locked = 0;
return result;
}
public:
Lock(bool rec = false) {
extern pthread_t no_thread;
pthread_mutex_init(&mutex, NULL);
locked = 0;
recursive = rec;
owner = no_thread;
}
~Lock() {
pthread_mutex_destroy(&mutex);
}
void lock() {
Thread self = pthread_self();
if (owner == self) {
if (locked && !recursive)
ThreadError("locking mutex twice");
}
else
pthread_mutex_lock(&mutex);
owner = self;
locked++;
}
void unlock() {
extern pthread_t no_thread;
Thread self = pthread_self();
if (owner != self)
ThreadError("unlocking unowned lock");
locked--;
if (locked == 0) {
owner = no_thread;
pthread_mutex_unlock(&mutex);
}
}
bool is_locked() {
return locked != 0 && owner == pthread_self();
}
};
class ConditionVariable {
friend class Lock;
private:
pthread_cond_t condition;
Lock *lock;
int waiting;
friend class Semaphore;
ConditionVariable() { }
public:
ConditionVariable(Lock *lock_init) : waiting(0), lock(lock_init) {
pthread_cond_init(&condition, NULL);
}
~ConditionVariable() {
pthread_cond_destroy(&condition);
}
void wait() {
if (!lock->is_locked())
ThreadError("waited on condition without locked mutex");
waiting++;
int l = lock->break_lock();
pthread_cond_wait(&condition, &lock->mutex);
waiting--;
lock->resume_lock(l);
}
void signal() {
if (!lock->is_locked())
ThreadError("signaled condition without locked mutex");
if (waiting)
pthread_cond_signal(&condition);
}
void broadcast() {
if (!lock->is_locked())
ThreadError("signaled condition without locked mutex");
if (waiting)
pthread_cond_broadcast(&condition);
}
};
class Semaphore {
private:
Lock lock;
ConditionVariable cond;
unsigned count;
unsigned waiting;
public:
Semaphore() : lock(), cond(&lock), count(0), waiting(0) {
}
Semaphore(unsigned count0) : lock(), cond(&lock), count(count0), waiting(0) {
}
void wait();
void post();
};
#endif // _THREAD_H
l
int l
Definition cfEzgcd.cc:100
ConditionVariable
Definition thread.h:73
ConditionVariable::waiting
int waiting
Definition thread.h:78
ConditionVariable::ConditionVariable
ConditionVariable()
Definition thread.h:80
ConditionVariable::wait
void wait()
Definition thread.h:88
ConditionVariable::ConditionVariable
ConditionVariable(Lock *lock_init)
Definition thread.h:82
ConditionVariable::~ConditionVariable
~ConditionVariable()
Definition thread.h:85
ConditionVariable::Lock
friend class Lock
Definition thread.h:74
ConditionVariable::condition
pthread_cond_t condition
Definition thread.h:76
ConditionVariable::broadcast
void broadcast()
Definition thread.h:103
ConditionVariable::signal
void signal()
Definition thread.h:97
ConditionVariable::lock
Lock * lock
Definition thread.h:77
ConditionVariable::Semaphore
friend class Semaphore
Definition thread.h:79
Lock
Definition thread.h:17
Lock::is_locked
bool is_locked()
Definition thread.h:68
Lock::break_lock
int break_lock()
Definition thread.h:28
Lock::owner
Thread owner
Definition thread.h:21
Lock::~Lock
~Lock()
Definition thread.h:43
Lock::lock
void lock()
Definition thread.h:46
Lock::unlock
void unlock()
Definition thread.h:57
Lock::Lock
Lock(bool rec=false)
Definition thread.h:36
Lock::mutex
pthread_mutex_t mutex
Definition thread.h:19
Lock::locked
int locked
Definition thread.h:22
Lock::ConditionVariable
friend class ConditionVariable
Definition thread.h:20
Lock::recursive
bool recursive
Definition thread.h:23
Lock::resume_lock
void resume_lock(int l)
Definition thread.h:24
Semaphore::cond
ConditionVariable cond
Definition thread.h:114
Semaphore::lock
Lock lock
Definition thread.h:113
Semaphore::waiting
unsigned waiting
Definition thread.h:116
Semaphore::wait
void wait()
Definition thread.cc:23
Semaphore::post
void post()
Definition thread.cc:33
Semaphore::Semaphore
Semaphore(unsigned count0)
Definition thread.h:120
Semaphore::count
unsigned count
Definition thread.h:115
Semaphore::Semaphore
Semaphore()
Definition thread.h:118
result
return result
Definition facAbsBiFact.cc:76
message
void message(int i, int *olddeg, int *reduc, kStrategy strat, int red_result)
Definition kutil.cc:7463
NULL
#define NULL
Definition omList.c:12
no_thread
pthread_t no_thread
Definition thread.cc:16
ThreadError
void ThreadError(const char *message)
Definition thread.cc:18
Thread
pthread_t Thread
Definition thread.h:11