Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
glibc/nptl/DESIGN-sem.txt
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 lines (37 sloc)
789 Bytes
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
Semaphores pseudocode | |
============================== | |
int sem_wait(sem_t * sem); | |
int sem_trywait(sem_t * sem); | |
int sem_post(sem_t * sem); | |
int sem_getvalue(sem_t * sem, int * sval); | |
struct sem_t { | |
unsigned int count; | |
- current semaphore count, also used as a futex | |
} | |
sem_wait(sem_t *sem) | |
{ | |
for (;;) { | |
if (atomic_decrement_if_positive(sem->count)) | |
break; | |
futex_wait(&sem->count, 0) | |
} | |
} | |
sem_post(sem_t *sem) | |
{ | |
n = atomic_increment(sem->count); | |
// Pass the new value of sem->count | |
futex_wake(&sem->count, n + 1); | |
} | |
sem_trywait(sem_t *sem) | |
{ | |
if (atomic_decrement_if_positive(sem->count)) { | |
return 0; | |
} else { | |
return EAGAIN; | |
} | |
} | |
sem_getvalue(sem_t *sem, int *sval) | |
{ | |
*sval = sem->count; | |
read_barrier(); | |
} |