Created
January 30, 2011 17:06
-
-
Save GaretJax/803019 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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