|
|
@ -25,16 +25,18 @@ private:
|
|
|
|
OTTDThreadFunc proc; ///< External thread procedure.
|
|
|
|
OTTDThreadFunc proc; ///< External thread procedure.
|
|
|
|
void *param; ///< Parameter for the external thread procedure.
|
|
|
|
void *param; ///< Parameter for the external thread procedure.
|
|
|
|
bool self_destruct; ///< Free ourselves when done?
|
|
|
|
bool self_destruct; ///< Free ourselves when done?
|
|
|
|
|
|
|
|
const char *name; ///< Name for the thread
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Create a pthread and start it, calling proc(param).
|
|
|
|
* Create a pthread and start it, calling proc(param).
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
ThreadObject_pthread(OTTDThreadFunc proc, void *param, bool self_destruct) :
|
|
|
|
ThreadObject_pthread(OTTDThreadFunc proc, void *param, bool self_destruct, const char *name) :
|
|
|
|
thread(0),
|
|
|
|
thread(0),
|
|
|
|
proc(proc),
|
|
|
|
proc(proc),
|
|
|
|
param(param),
|
|
|
|
param(param),
|
|
|
|
self_destruct(self_destruct)
|
|
|
|
self_destruct(self_destruct),
|
|
|
|
|
|
|
|
name(name)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
pthread_create(&this->thread, NULL, &stThreadProc, this);
|
|
|
|
pthread_create(&this->thread, NULL, &stThreadProc, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -60,7 +62,15 @@ private:
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static void *stThreadProc(void *thr)
|
|
|
|
static void *stThreadProc(void *thr)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
((ThreadObject_pthread *)thr)->ThreadProc();
|
|
|
|
ThreadObject_pthread *self = (ThreadObject_pthread *) thr;
|
|
|
|
|
|
|
|
#if defined(__GLIBC__)
|
|
|
|
|
|
|
|
#if __GLIBC_PREREQ(2, 12)
|
|
|
|
|
|
|
|
if (self->name) {
|
|
|
|
|
|
|
|
pthread_setname_np(pthread_self(), self->name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
self->ThreadProc();
|
|
|
|
pthread_exit(NULL);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -85,9 +95,9 @@ private:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread)
|
|
|
|
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ThreadObject *to = new ThreadObject_pthread(proc, param, thread == NULL);
|
|
|
|
ThreadObject *to = new ThreadObject_pthread(proc, param, thread == NULL, name);
|
|
|
|
if (thread != NULL) *thread = to;
|
|
|
|
if (thread != NULL) *thread = to;
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|