Xenomai
3.0.5
|
Cobalt/POSIX semaphore services. More...
Functions | |
int | sem_init (sem_t *sem, int pshared, unsigned int value) |
Initialize an unnamed semaphore. More... | |
int | sem_destroy (sem_t *sem) |
Destroy an unnamed semaphore. More... | |
int | sem_post (sem_t *sem) |
Post a semaphore. More... | |
int | sem_trywait (sem_t *sem) |
Attempt to decrement a semaphore. More... | |
int | sem_wait (sem_t *sem) |
Decrement a semaphore. More... | |
int | sem_timedwait (sem_t *sem, const struct timespec *abs_timeout) |
Attempt to decrement a semaphore with a time limit. More... | |
int | sem_close (sem_t *sem) |
Close a named semaphore. More... | |
int | sem_unlink (const char *name) |
Unlink a named semaphore. More... | |
Cobalt/POSIX semaphore services.
Semaphores are counters for resources shared between threads. The basic operations on semaphores are: increment the counter atomically, and wait until the counter is non-null and decrement it atomically.
Semaphores have a maximum value past which they cannot be incremented. The macro SEM_VALUE_MAX is defined to be this maximum value.
int sem_close | ( | sem_t * | sem | ) |
Close a named semaphore.
This service closes the semaphore sem. The semaphore is destroyed only when unlinked with a call to the sem_unlink() service and when each call to sem_open() matches a call to this service.
When a semaphore is destroyed, the memory it used is returned to the system heap, so that further references to this semaphore are not guaranteed to fail, as is the case for unnamed semaphores.
This service fails if sem is an unnamed semaphore.
sem | the semaphore to be closed. |
0 | on success; |
-1 | with errno set if:
|
int sem_destroy | ( | sem_t * | sem | ) |
Destroy an unnamed semaphore.
This service destroys the semaphore sem. Threads currently blocked on sem are unblocked and the service they called return -1 with errno set to EINVAL. The semaphore is then considered invalid by all semaphore services (they all fail with errno set to EINVAL) except sem_init().
This service fails if sem is a named semaphore.
sem | the semaphore to be destroyed. |
always | 0 on success. If SEM_WARNDEL was mentioned in sem_init_np(), the semaphore is deleted as requested and a strictly positive value is returned to warn the caller if threads were pending on it, otherwise zero is returned. If SEM_NOBUSYDEL was mentioned in sem_init_np(), sem_destroy() may succeed only if no thread is waiting on the semaphore to delete, otherwise -EBUSY is returned. |
-1 | with errno set if:
|
int sem_init | ( | sem_t * | sem, |
int | pshared, | ||
unsigned int | value | ||
) |
Initialize an unnamed semaphore.
This service initializes the semaphore sm, with the value value.
This service fails if sm is already initialized or is a named semaphore.
sem | the semaphore to be initialized; |
pshared | if zero, means that the new semaphore may only be used by threads in the same process as the thread calling sem_init(); if non zero, means that the new semaphore may be used by any thread that has access to the memory where the semaphore is allocated. |
value | the semaphore initial value. |
0 | on success, |
-1 | with errno set if:
|
int sem_post | ( | sem_t * | sem | ) |
Post a semaphore.
This service posts the semaphore sem.
If no thread is currently blocked on this semaphore, its count is incremented unless "pulse" mode is enabled for it (see sem_init_np(), SEM_PULSE). If a thread is blocked on the semaphore, the thread heading the wait queue is unblocked.
sem | the semaphore to be signaled. |
0 | on success; |
-1 | with errno set if:
|
int sem_timedwait | ( | sem_t * | sem, |
const struct timespec * | abs_timeout | ||
) |
Attempt to decrement a semaphore with a time limit.
This service is equivalent to sem_wait(), except that the caller is only blocked until the timeout abs_timeout expires.
sem | the semaphore to be decremented; |
abs_timeout | the timeout, expressed as an absolute value of the relevant clock for the semaphore, either CLOCK_MONOTONIC if SEM_RAWCLOCK was mentioned via sem_init_np(), or CLOCK_REALTIME otherwise. |
0 | on success; |
-1 | with errno set if:
|
References sem_trywait().
int sem_trywait | ( | sem_t * | sem | ) |
Attempt to decrement a semaphore.
This service is equivalent to sem_wait(), except that it returns immediately if the semaphore sem is currently depleted, and that it is not a cancellation point.
sem | the semaphore to be decremented. |
0 | on success; |
-1 | with errno set if:
|
Referenced by sem_timedwait(), and sem_wait().
int sem_unlink | ( | const char * | name | ) |
Unlink a named semaphore.
This service unlinks the semaphore named name. This semaphore is not destroyed until all references obtained with sem_open() are closed by calling sem_close(). However, the unlinked semaphore may no longer be reached with the sem_open() service.
When a semaphore is destroyed, the memory it used is returned to the system heap, so that further references to this semaphore are not guaranteed to fail, as is the case for unnamed semaphores.
name | the name of the semaphore to be unlinked. |
0 | on success; |
-1 | with errno set if:
|
int sem_wait | ( | sem_t * | sem | ) |
Decrement a semaphore.
This service decrements the semaphore sem if it is currently if its value is greater than 0. If the semaphore's value is currently zero, the calling thread is suspended until the semaphore is posted, or a signal is delivered to the calling thread.
This service is a cancellation point for Cobalt threads (created with the pthread_create() service). When such a thread is cancelled while blocked in a call to this service, the semaphore state is left unchanged before the cancellation cleanup handlers are called.
sem | the semaphore to be decremented. |
0 | on success; |
-1 | with errno set if:
|
References sem_trywait().