12 #include "../stdafx.h" 17 #include "../safeguards.h" 38 self_destruct(self_destruct),
41 pthread_create(&this->thread, NULL, &
stThreadProc,
this);
46 assert(pthread_self() == this->thread);
54 assert(pthread_self() != this->thread);
55 pthread_join(this->thread, NULL);
66 #if defined(__GLIBC__) 67 #if __GLIBC_PREREQ(2, 12) 69 pthread_setname_np(pthread_self(), self->name);
85 this->
proc(this->param);
92 pthread_detach(pthread_self());
101 if (thread != NULL) *thread = to;
119 pthread_mutexattr_init(&this->attr);
120 pthread_mutexattr_settype(&this->attr, PTHREAD_MUTEX_ERRORCHECK);
121 pthread_mutex_init(&this->mutex, &this->attr);
122 pthread_cond_init(&this->condition, NULL);
127 int err = pthread_cond_destroy(&this->condition);
128 assert(err != EBUSY);
129 err = pthread_mutex_destroy(&this->mutex);
130 assert(err != EBUSY);
133 bool IsOwnedByCurrentThread()
const 135 return this->owner == pthread_self();
141 if (this->IsOwnedByCurrentThread()) {
142 if (!allow_recursive) NOT_REACHED();
144 int err = pthread_mutex_lock(&this->mutex);
146 assert(this->recursive_count == 0);
147 this->owner = pthread_self();
149 this->recursive_count++;
154 assert(this->IsOwnedByCurrentThread());
155 if (!allow_recursive && this->recursive_count != 1) NOT_REACHED();
156 this->recursive_count--;
157 if (this->recursive_count != 0)
return;
159 int err = pthread_mutex_unlock(&this->mutex);
165 uint old_recursive_count = this->recursive_count;
166 this->recursive_count = 0;
168 int err = pthread_cond_wait(&this->condition, &this->mutex);
170 this->owner = pthread_self();
171 this->recursive_count = old_recursive_count;
176 int err = pthread_cond_signal(&this->condition);
void Join()
Join this thread.
void(* OTTDThreadFunc)(void *)
Definition of all thread entry functions.
void ThreadProc()
A new thread is created, and this function is called.
POSIX pthread version for ThreadObject.
void SendSignal()
Send a signal and wake the 'thread' that was waiting for it.
pthread_mutexattr_t attr
Attributes set for the mutex.
pthread_t thread
System thread identifier.
pthread_t owner
Owning thread of the mutex.
static ThreadMutex * New()
Create a new mutex.
bool self_destruct
Free ourselves when done?
OTTDThreadFunc proc
External thread procedure.
pthread_mutex_t mutex
The actual mutex.
uint recursive_count
Recursive lock count.
void EndCritical(bool allow_recursive=false)
End of the critical section.
POSIX pthread version of ThreadMutex.
void WaitForSignal()
Wait for a signal to be send.
pthread_cond_t condition
Data for conditional waiting.
ThreadObject_pthread(OTTDThreadFunc proc, void *param, bool self_destruct, const char *name)
Create a pthread and start it, calling proc(param).
void BeginCritical(bool allow_recursive=false)
Begin the critical section.
const char * name
Name for the thread.
static void * stThreadProc(void *thr)
On thread creation, this function is called, which calls the real startup function.
void * param
Parameter for the external thread procedure.
A Thread Object which works on all our supported OSes.
Signal used for signalling we knowingly want to end the thread.
static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread=NULL, const char *name=NULL)
Create a thread; proc will be called as first function inside the thread, with optional params...
bool Exit()
Exit this thread.