Skip to content

Instantly share code, notes, and snippets.

@GaretJax
Created January 30, 2011 17:06
Show Gist options
  • Save GaretJax/803019 to your computer and use it in GitHub Desktop.
Save GaretJax/803019 to your computer and use it in GitHub Desktop.
#include "sync.h"
void monitor_acquire(Monitor * monitor) {
P(monitor->mutex);
}
void monitor_release(Monitor * monitor) {
if (sem_getvalue(monitor->next) > 0) {
V(monitor->next);
} else {
V(monitor->mutex);
}
}
#include "sync.h"
void condition_wait(Condition * cond) {
if (sem_getvalue(cond->monitor->next) > 0) {
V(cond->monitor->next);
} else {
V(cond->monitor->mutex);
}
P(cond->queue);
P(cond->monitor->next);
}
void condition_signal(Condition * cond) {
if (sem_getvalue(cond->queue) > 0) {
V(cond->queue);
}
}
/**
* A monitor implementing the SIGNAL & CONTINUE model.
*/
typedef struct {
int * waiting;
sem_t * mutex;
sem_t * next;
} Monitor;
/**
* A condition bound to a monitor.
*
* This condition uses a shared memory segment to hold a counter and a named
* semaphore to queue requests.
*/
typedef struct {
int * waiting;
Monitor * monitor;
sem_t * queue;
} Condition;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment