Eucalyptus analysis Three Lock

Source: Internet
Author: User
Tags mutex posix pthread mutex

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-&GT;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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.