12 #include "../stdafx.h" 17 #if defined(__APPLE__) 18 #include "../os/macosx/macos.h" 21 #include "../safeguards.h" 42 self_destruct(self_destruct),
45 pthread_create(&this->thread, NULL, &
stThreadProc,
this);
50 assert(pthread_self() == this->thread);
58 assert(pthread_self() != this->thread);
59 pthread_join(this->thread, NULL);
70 #if defined(__GLIBC__) 71 #if __GLIBC_PREREQ(2, 12) 73 pthread_setname_np(pthread_self(), self->name);
77 #if defined(__APPLE__) 78 MacOSSetThreadName(self->name);
92 this->
proc(this->param);
99 pthread_detach(pthread_self());
108 if (thread != NULL) *thread = to;
126 pthread_mutexattr_init(&this->attr);
127 pthread_mutexattr_settype(&this->attr, PTHREAD_MUTEX_ERRORCHECK);
128 pthread_mutex_init(&this->mutex, &this->attr);
129 pthread_cond_init(&this->condition, NULL);
134 int err = pthread_cond_destroy(&this->condition);
135 assert(err != EBUSY);
136 err = pthread_mutex_destroy(&this->mutex);
137 assert(err != EBUSY);
140 bool IsOwnedByCurrentThread()
const 142 return this->owner == pthread_self();
148 if (this->IsOwnedByCurrentThread()) {
149 if (!allow_recursive) NOT_REACHED();
151 int err = pthread_mutex_lock(&this->mutex);
153 assert(this->recursive_count == 0);
154 this->owner = pthread_self();
156 this->recursive_count++;
161 assert(this->IsOwnedByCurrentThread());
162 if (!allow_recursive && this->recursive_count != 1) NOT_REACHED();
163 this->recursive_count--;
164 if (this->recursive_count != 0)
return;
166 int err = pthread_mutex_unlock(&this->mutex);
172 uint old_recursive_count = this->recursive_count;
173 this->recursive_count = 0;
175 int err = pthread_cond_wait(&this->condition, &this->mutex);
177 this->owner = pthread_self();
178 this->recursive_count = old_recursive_count;
183 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.