1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Unlock a Semaphore |
---|
5 | * @ingroup POSIX_SEMAPHORE |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2014. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/posix/semaphoreimpl.h> |
---|
22 | |
---|
23 | #include <limits.h> |
---|
24 | |
---|
25 | int sem_post( sem_t *_sem ) |
---|
26 | { |
---|
27 | Sem_Control *sem; |
---|
28 | ISR_Level level; |
---|
29 | Thread_queue_Context queue_context; |
---|
30 | Thread_queue_Heads *heads; |
---|
31 | unsigned int count; |
---|
32 | |
---|
33 | POSIX_SEMAPHORE_VALIDATE_OBJECT( _sem ); |
---|
34 | |
---|
35 | sem = _Sem_Get( &_sem->_Semaphore ); |
---|
36 | _Thread_queue_Context_initialize( &queue_context ); |
---|
37 | _Thread_queue_Context_ISR_disable( &queue_context, level ); |
---|
38 | _Sem_Queue_acquire_critical( sem, &queue_context ); |
---|
39 | |
---|
40 | heads = sem->Queue.Queue.heads; |
---|
41 | count = sem->count; |
---|
42 | |
---|
43 | if ( RTEMS_PREDICT_TRUE( heads == NULL && count < SEM_VALUE_MAX ) ) { |
---|
44 | sem->count = count + 1; |
---|
45 | _Sem_Queue_release( sem, level, &queue_context ); |
---|
46 | return 0; |
---|
47 | } |
---|
48 | |
---|
49 | if ( RTEMS_PREDICT_TRUE( heads != NULL ) ) { |
---|
50 | const Thread_queue_Operations *operations; |
---|
51 | Thread_Control *first; |
---|
52 | |
---|
53 | _Thread_queue_Context_set_ISR_level( &queue_context, level ); |
---|
54 | operations = SEMAPHORE_TQ_OPERATIONS; |
---|
55 | first = ( *operations->first )( heads ); |
---|
56 | |
---|
57 | _Thread_queue_Extract_critical( |
---|
58 | &sem->Queue.Queue, |
---|
59 | operations, |
---|
60 | first, |
---|
61 | &queue_context |
---|
62 | ); |
---|
63 | return 0; |
---|
64 | } |
---|
65 | |
---|
66 | _Sem_Queue_release( sem, level, &queue_context ); |
---|
67 | rtems_set_errno_and_return_minus_one( EOVERFLOW ); |
---|
68 | } |
---|