Eucalyptus blockade has no level, no type, only one, is the mutex, is the mutex, the condition variable encapsulated together, to achieve the operating system of the PV operation,
Directly look at the following function, you can directly understand
typedef struct SEM_STRUCT {
int sysv;
sem_t * POSIX;
pthread_mutex_t Mutex;
pthread_cond_t cond;
int Usemutex, mutwaiters, Mutcount;
char * name;
} sem;
SEM * SEM_ALLOC (const int val, const char * name)
{
Declare_arg;
SEM * s = malloc (sizeof (SEM));
if (s==null) return NULL;
Bzero (S, sizeof (SEM));
S->SYSV =-1;
if (name &&!strcmp (name, "Mutex")) {/* use pthread Mutex */
S->usemutex = 1;
S->mutcount = val;
s->mutwaiters = 0;
Pthread_mutex_init (& (S->mutex), NULL);
Pthread_cond_init (& (S->cond), NULL);
} else if (name) {/* named semaphores */
if (sem_unlink (name) = = 0) {/* Clean up in case previous SEM holder crashed */
LOGPRINTFL (Eucainfo, "Sem_alloc (): Cleaning up old semaphore%s/n", name);
}
if (S->posix = Sem_open (name, O_creat | O_excl, 0644, Val)) ==sem_failed) {
Free (s);
return NULL;
}
S->name = StrDup (name);
} else {/* SYS V IPC semaphores */
S->SYSV = Semget (ipc_private,/* PRIVATE to process & children */
1,/* only need one */
Ipc_creat | Ipc_excl | S_IRUSR | S_IWUSR/* user-only */);
if (s->sysv<0) {
Free (s);
return NULL;
}
/* Set the value */
Arg.val = val;
if (Semctl (S->SYSV, 0, setval, arg) = =-1) {
Free (s);
return NULL;
}
}
return s;
}
int sem_p (SEM * s)
{
int RC;
if (s && s->usemutex) {
rc = Pthread_mutex_lock (& (S->mutex));
s->mutwaiters++;
while (S->mutcount = = 0) {
Pthread_cond_wait (& (S->cond), & (S->mutex));
}
s->mutwaiters--;
s->mutcount--;
rc = Pthread_mutex_unlock (& (S->mutex));
return (RC);
}
if (s && s->posix) {
Return sem_wait (S->posix);
}
if (s && s->sysv > 0) {
struct SEMBUF sb = {0,-1, 0};
Return Semop (S->SYSV, &SB, 1);
}
return-1;
}
int Sem_v (SEM * s)
{
int RC;
if (s && s->usemutex) {
rc = Pthread_mutex_lock (& (S->mutex));
if (S->mutwaiters > 0) {
rc = Pthread_cond_signal (& (S->cond));
}
s->mutcount++;
rc = Pthread_mutex_unlock (& (S->mutex));
return (RC);
}
if (s && s->posix) {
Return Sem_post (S->posix);
}
if (s && s->sysv > 0) {
struct SEMBUF sb = {0, 1, 0};
Return Semop (S->SYSV, &SB, 1);
}
return-1;
}
void Sem_free (SEM * s)
{
Declare_arg;
if (s && s->posix) {
Sem_close (S->posix);
Sem_unlink (S->name);
Free (s->name);
}
if (s && s->sysv > 0) {
Semctl (S->SYSV, 0, Ipc_rmid, ARG); /* Todo:check return */
}
Free (s);
}