minix/lib/libmthread/event.c

127 lines
2.9 KiB
C
Raw Normal View History

#include <minix/mthread.h>
#include "global.h"
/*===========================================================================*
* mthread_event_init *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
int mthread_event_init(event)
mthread_event_t *event; /* The event to be initialized */
{
/* Initialize an event object.
*/
int r;
if (!event)
return EINVAL;
r = mthread_mutex_init(&event->mutex, NULL);
if (r != 0)
return r;
r = mthread_cond_init(&event->cond, NULL);
if (r != 0)
mthread_mutex_destroy(&event->mutex);
return r;
}
/*===========================================================================*
* mthread_event_destroy *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
int mthread_event_destroy(event)
mthread_event_t *event; /* The event to be destroyed */
{
/* Destroy an event object.
*/
int r;
if (!event)
return EINVAL;
r = mthread_cond_destroy(&event->cond);
if (r != 0)
return r;
return mthread_mutex_destroy(&event->mutex);
}
/*===========================================================================*
* mthread_event_wait *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
int mthread_event_wait(event)
mthread_event_t *event; /* The event to be waited on */
{
/* Wait for an event, blocking the current thread in the process.
*/
int r;
if (!event)
return EINVAL;
r = mthread_mutex_lock(&event->mutex);
if (r != 0)
return r;
r = mthread_cond_wait(&event->cond, &event->mutex);
if (r != 0) {
mthread_mutex_unlock(&event->mutex);
return r;
}
return mthread_mutex_unlock(&event->mutex);
}
/*===========================================================================*
* mthread_event_fire *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
int mthread_event_fire(event)
mthread_event_t *event; /* The event to be fired */
{
/* Fire an event, waking up any thread blocked on it.
*/
int r;
if (!event)
return EINVAL;
r = mthread_mutex_lock(&event->mutex);
if (r != 0)
return r;
r = mthread_cond_signal(&event->cond);
if (r != 0) {
mthread_mutex_unlock(&event->mutex);
return r;
}
return mthread_mutex_unlock(&event->mutex);
}
2011-12-01 14:52:25 +01:00
/*===========================================================================*
* mthread_event_fire_all *
*===========================================================================*/
2012-03-25 20:25:53 +02:00
int mthread_event_fire_all(event)
2011-12-01 14:52:25 +01:00
mthread_event_t *event; /* The event to be fired */
{
/* Fire an event, waking up any thread blocked on it.
*/
int r;
if (!event)
return EINVAL;
r = mthread_mutex_lock(&event->mutex);
if (r != 0)
return r;
r = mthread_cond_broadcast(&event->cond);
if (r != 0) {
mthread_mutex_unlock(&event->mutex);
return r;
}
return mthread_mutex_unlock(&event->mutex);
}