Migrated mutex_t to INIT/METHOD macros.

This commit is contained in:
Tobias Brunner
2011-10-03 15:45:17 +02:00
parent 7fa43a9036
commit f5808078cb
+54 -70
View File
@@ -96,11 +96,8 @@ struct private_condvar_t {
}; };
METHOD(mutex_t, lock, void,
/** private_mutex_t *this)
* Implementation of mutex_t.lock.
*/
static void lock(private_mutex_t *this)
{ {
int err; int err;
@@ -113,10 +110,8 @@ static void lock(private_mutex_t *this)
profiler_end(&this->profile); profiler_end(&this->profile);
} }
/** METHOD(mutex_t, unlock, void,
* Implementation of mutex_t.unlock. private_mutex_t *this)
*/
static void unlock(private_mutex_t *this)
{ {
int err; int err;
@@ -127,10 +122,8 @@ static void unlock(private_mutex_t *this)
} }
} }
/** METHOD(mutex_t, lock_r, void,
* Implementation of mutex_t.lock. private_r_mutex_t *this)
*/
static void lock_r(private_r_mutex_t *this)
{ {
pthread_t self = pthread_self(); pthread_t self = pthread_self();
@@ -151,10 +144,8 @@ static void lock_r(private_r_mutex_t *this)
} }
} }
/** METHOD(mutex_t, unlock_r, void,
* Implementation of mutex_t.unlock. private_r_mutex_t *this)
*/
static void unlock_r(private_r_mutex_t *this)
{ {
uintptr_t times; uintptr_t times;
@@ -169,20 +160,16 @@ static void unlock_r(private_r_mutex_t *this)
} }
} }
/** METHOD(mutex_t, mutex_destroy, void,
* Implementation of mutex_t.destroy private_mutex_t *this)
*/
static void mutex_destroy(private_mutex_t *this)
{ {
profiler_cleanup(&this->profile); profiler_cleanup(&this->profile);
pthread_mutex_destroy(&this->mutex); pthread_mutex_destroy(&this->mutex);
free(this); free(this);
} }
/** METHOD(mutex_t, mutex_destroy_r, void,
* Implementation of mutex_t.destroy for recursive mutex' private_r_mutex_t *this)
*/
static void mutex_destroy_r(private_r_mutex_t *this)
{ {
profiler_cleanup(&this->generic.profile); profiler_cleanup(&this->generic.profile);
pthread_mutex_destroy(&this->generic.mutex); pthread_mutex_destroy(&this->generic.mutex);
@@ -199,31 +186,39 @@ mutex_t *mutex_create(mutex_type_t type)
{ {
case MUTEX_TYPE_RECURSIVE: case MUTEX_TYPE_RECURSIVE:
{ {
private_r_mutex_t *this = malloc_thing(private_r_mutex_t); private_r_mutex_t *this;
this->generic.public.lock = (void(*)(mutex_t*))lock_r; INIT(this,
this->generic.public.unlock = (void(*)(mutex_t*))unlock_r; .generic = {
this->generic.public.destroy = (void(*)(mutex_t*))mutex_destroy_r; .public = {
.lock = _lock_r,
.unlock = _unlock_r,
.destroy = _mutex_destroy_r,
},
.recursive = TRUE,
},
);
pthread_mutex_init(&this->generic.mutex, NULL); pthread_mutex_init(&this->generic.mutex, NULL);
pthread_key_create(&this->times, NULL); pthread_key_create(&this->times, NULL);
this->generic.recursive = TRUE;
profiler_init(&this->generic.profile); profiler_init(&this->generic.profile);
this->thread = 0;
return &this->generic.public; return &this->generic.public;
} }
case MUTEX_TYPE_DEFAULT: case MUTEX_TYPE_DEFAULT:
default: default:
{ {
private_mutex_t *this = malloc_thing(private_mutex_t); private_mutex_t *this;
this->public.lock = (void(*)(mutex_t*))lock; INIT(this,
this->public.unlock = (void(*)(mutex_t*))unlock; .public = {
this->public.destroy = (void(*)(mutex_t*))mutex_destroy; .lock = _lock,
.unlock = _unlock,
.destroy = _mutex_destroy,
},
);
pthread_mutex_init(&this->mutex, NULL); pthread_mutex_init(&this->mutex, NULL);
this->recursive = FALSE;
profiler_init(&this->profile); profiler_init(&this->profile);
return &this->public; return &this->public;
@@ -232,11 +227,8 @@ mutex_t *mutex_create(mutex_type_t type)
} }
METHOD(condvar_t, wait_, void,
/** private_condvar_t *this, private_mutex_t *mutex)
* Implementation of condvar_t.wait.
*/
static void _wait(private_condvar_t *this, private_mutex_t *mutex)
{ {
if (mutex->recursive) if (mutex->recursive)
{ {
@@ -258,11 +250,8 @@ static void _wait(private_condvar_t *this, private_mutex_t *mutex)
#define pthread_cond_timedwait pthread_cond_timedwait_monotonic #define pthread_cond_timedwait pthread_cond_timedwait_monotonic
#endif #endif
/** METHOD(condvar_t, timed_wait_abs, bool,
* Implementation of condvar_t.timed_wait_abs. private_condvar_t *this, private_mutex_t *mutex, timeval_t time)
*/
static bool timed_wait_abs(private_condvar_t *this, private_mutex_t *mutex,
timeval_t time)
{ {
struct timespec ts; struct timespec ts;
bool timed_out; bool timed_out;
@@ -287,11 +276,8 @@ static bool timed_wait_abs(private_condvar_t *this, private_mutex_t *mutex,
return timed_out; return timed_out;
} }
/** METHOD(condvar_t, timed_wait, bool,
* Implementation of condvar_t.timed_wait. private_condvar_t *this, private_mutex_t *mutex, u_int timeout)
*/
static bool timed_wait(private_condvar_t *this, private_mutex_t *mutex,
u_int timeout)
{ {
timeval_t tv; timeval_t tv;
u_int s, ms; u_int s, ms;
@@ -312,26 +298,20 @@ static bool timed_wait(private_condvar_t *this, private_mutex_t *mutex,
return timed_wait_abs(this, mutex, tv); return timed_wait_abs(this, mutex, tv);
} }
/** METHOD(condvar_t, signal_, void,
* Implementation of condvar_t.signal. private_condvar_t *this)
*/
static void _signal(private_condvar_t *this)
{ {
pthread_cond_signal(&this->condvar); pthread_cond_signal(&this->condvar);
} }
/** METHOD(condvar_t, broadcast, void,
* Implementation of condvar_t.broadcast. private_condvar_t *this)
*/
static void broadcast(private_condvar_t *this)
{ {
pthread_cond_broadcast(&this->condvar); pthread_cond_broadcast(&this->condvar);
} }
/** METHOD(condvar_t, condvar_destroy, void,
* Implementation of condvar_t.destroy private_condvar_t *this)
*/
static void condvar_destroy(private_condvar_t *this)
{ {
pthread_cond_destroy(&this->condvar); pthread_cond_destroy(&this->condvar);
free(this); free(this);
@@ -347,14 +327,18 @@ condvar_t *condvar_create(condvar_type_t type)
case CONDVAR_TYPE_DEFAULT: case CONDVAR_TYPE_DEFAULT:
default: default:
{ {
private_condvar_t *this = malloc_thing(private_condvar_t); private_condvar_t *this;
this->public.wait = (void(*)(condvar_t*, mutex_t *mutex))_wait; INIT(this,
this->public.timed_wait = (bool(*)(condvar_t*, mutex_t *mutex, u_int timeout))timed_wait; .public = {
this->public.timed_wait_abs = (bool(*)(condvar_t*, mutex_t *mutex, timeval_t time))timed_wait_abs; .wait = (void*)_wait_,
this->public.signal = (void(*)(condvar_t*))_signal; .timed_wait = (void*)_timed_wait,
this->public.broadcast = (void(*)(condvar_t*))broadcast; .timed_wait_abs = (void*)_timed_wait_abs,
this->public.destroy = (void(*)(condvar_t*))condvar_destroy; .signal = _signal_,
.broadcast = _broadcast,
.destroy = _condvar_destroy,
}
);
#ifdef HAVE_PTHREAD_CONDATTR_INIT #ifdef HAVE_PTHREAD_CONDATTR_INIT
{ {