1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Core Semaphore Seize |
---|
5 | * |
---|
6 | * @ingroup ScoreSemaphore |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2008. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.com/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #if HAVE_CONFIG_H |
---|
19 | #include "config.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <rtems/system.h> |
---|
23 | #include <rtems/score/isr.h> |
---|
24 | #include <rtems/score/coresem.h> |
---|
25 | #include <rtems/score/states.h> |
---|
26 | #include <rtems/score/thread.h> |
---|
27 | #include <rtems/score/threadq.h> |
---|
28 | |
---|
29 | #if defined(RTEMS_SCORE_CORESEM_ENABLE_SEIZE_BODY) |
---|
30 | |
---|
31 | void _CORE_semaphore_Seize( |
---|
32 | CORE_semaphore_Control *the_semaphore, |
---|
33 | Objects_Id id, |
---|
34 | bool wait, |
---|
35 | Watchdog_Interval timeout |
---|
36 | ) |
---|
37 | { |
---|
38 | Thread_Control *executing; |
---|
39 | ISR_Level level; |
---|
40 | |
---|
41 | executing = _Thread_Executing; |
---|
42 | executing->Wait.return_code = CORE_SEMAPHORE_STATUS_SUCCESSFUL; |
---|
43 | _ISR_Disable( level ); |
---|
44 | if ( the_semaphore->count != 0 ) { |
---|
45 | the_semaphore->count -= 1; |
---|
46 | _ISR_Enable( level ); |
---|
47 | return; |
---|
48 | } |
---|
49 | |
---|
50 | /* |
---|
51 | * If the semaphore was not available and the caller was not willing |
---|
52 | * to block, then return immediately with a status indicating that |
---|
53 | * the semaphore was not available and the caller never blocked. |
---|
54 | */ |
---|
55 | if ( !wait ) { |
---|
56 | _ISR_Enable( level ); |
---|
57 | executing->Wait.return_code = CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT; |
---|
58 | return; |
---|
59 | } |
---|
60 | |
---|
61 | /* |
---|
62 | * If the semaphore is not available and the caller is willing to |
---|
63 | * block, then we now block the caller with optional timeout. |
---|
64 | */ |
---|
65 | _Thread_queue_Enter_critical_section( &the_semaphore->Wait_queue ); |
---|
66 | executing->Wait.queue = &the_semaphore->Wait_queue; |
---|
67 | executing->Wait.id = id; |
---|
68 | _ISR_Enable( level ); |
---|
69 | _Thread_queue_Enqueue( &the_semaphore->Wait_queue, timeout ); |
---|
70 | } |
---|
71 | #endif |
---|