*/
static thread_value_t *current_thread;
+
#ifndef HAVE_PTHREAD_CANCEL
/* if pthread_cancel is not available, we emulate it using a signal */
#define SIG_CANCEL (SIGRTMIN+7)
free(this);
}
-/**
- * Implementation of thread_t.cancel.
- */
-static void cancel(private_thread_t *this)
+METHOD(thread_t, cancel, void,
+ private_thread_t *this)
{
this->mutex->lock(this->mutex);
if (pthread_equal(this->thread_id, pthread_self()))
this->mutex->unlock(this->mutex);
}
-/**
- * Implementation of thread_t.kill.
- */
-static void _kill(private_thread_t *this, int sig)
+METHOD(thread_t, kill_, void,
+ private_thread_t *this, int sig)
{
this->mutex->lock(this->mutex);
if (pthread_equal(this->thread_id, pthread_self()))
this->mutex->unlock(this->mutex);
}
-/**
- * Implementation of thread_t.detach.
- */
-static void detach(private_thread_t *this)
+METHOD(thread_t, detach, void,
+ private_thread_t *this)
{
this->mutex->lock(this->mutex);
pthread_detach(this->thread_id);
thread_destroy(this);
}
-/**
- * Implementation of thread_t.join.
- */
-static void *join(private_thread_t *this)
+METHOD(thread_t, join, void*,
+ private_thread_t *this)
{
pthread_t thread_id;
void *val;
*/
static private_thread_t *thread_create_internal()
{
- private_thread_t *this = malloc_thing(private_thread_t);
-
- this->public.cancel = (void(*)(thread_t*))cancel;
- this->public.kill = (void(*)(thread_t*,int))_kill;
- this->public.detach = (void(*)(thread_t*))detach;
- this->public.join = (void*(*)(thread_t*))join;
-
- this->id = 0;
- this->thread_id = 0;
- this->main = NULL;
- this->arg = NULL;
- this->cleanup_handlers = linked_list_create();
- this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
+ private_thread_t *this;
+
+ INIT(this,
+ .public = {
+ .cancel = _cancel,
+ .kill = _kill_,
+ .detach = _detach,
+ .join = _join,
+ },
+ .cleanup_handlers = linked_list_create(),
+ .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
+ );
sem_init(&this->created, FALSE, 0);
- this->detached_or_joined = FALSE;
- this->terminated = FALSE;
return this;
}
private_thread_t *this = (private_thread_t*)thread_current();
cleanup_handler_t *handler;
+ INIT(handler,
+ .cleanup = cleanup,
+ .arg = arg,
+ );
+
this->mutex->lock(this->mutex);
- handler = malloc_thing(cleanup_handler_t);
- handler->cleanup = cleanup;
- handler->arg = arg;
this->cleanup_handlers->insert_last(this->cleanup_handlers, handler);
this->mutex->unlock(this->mutex);
}