RTEMS
semobtain.c
Go to the documentation of this file.
1 
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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include <rtems/rtems/semimpl.h>
23 #include <rtems/rtems/statusimpl.h>
24 
25 THREAD_QUEUE_OBJECT_ASSERT(
27  Core_control.Wait_queue,
28  SEMAPHORE_CONTROL_GENERIC
29 );
30 
31 THREAD_QUEUE_OBJECT_ASSERT(
33  Core_control.Mutex.Recursive.Mutex.Wait_queue,
34  SEMAPHORE_CONTROL_MUTEX
35 );
36 
37 THREAD_QUEUE_OBJECT_ASSERT(
39  Core_control.Semaphore.Wait_queue,
40  SEMAPHORE_CONTROL_SEMAPHORE
41 );
42 
43 #if defined(RTEMS_SMP)
44 THREAD_QUEUE_OBJECT_ASSERT(
46  Core_control.MRSP.Wait_queue,
47  SEMAPHORE_CONTROL_MRSP
48 );
49 #endif
50 
52  rtems_id id,
53  rtems_option option_set,
54  rtems_interval timeout
55 )
56 {
57  Semaphore_Control *the_semaphore;
58  Thread_queue_Context queue_context;
59  Thread_Control *executing;
60  bool wait;
61  uintptr_t flags;
62  Semaphore_Variant variant;
63  Status_Control status;
64 
65  the_semaphore = _Semaphore_Get( id, &queue_context );
66 
67  if ( the_semaphore == NULL ) {
68 #if defined(RTEMS_MULTIPROCESSING)
69  return _Semaphore_MP_Obtain( id, option_set, timeout );
70 #else
71  return RTEMS_INVALID_ID;
72 #endif
73  }
74 
75  executing = _Thread_Executing;
76  wait = !_Options_Is_no_wait( option_set );
77 
78  if ( wait ) {
79  _Thread_queue_Context_set_enqueue_timeout_ticks( &queue_context, timeout );
80  } else {
82  }
83 
84  flags = _Semaphore_Get_flags( the_semaphore );
85  variant = _Semaphore_Get_variant( flags );
86 
87  switch ( variant ) {
88  case SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY:
90  &the_semaphore->Core_control.Mutex.Recursive,
91  CORE_MUTEX_TQ_PRIORITY_INHERIT_OPERATIONS,
92  executing,
93  wait,
95  &queue_context
96  );
97  break;
98  case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING:
100  &the_semaphore->Core_control.Mutex,
101  executing,
102  wait,
104  &queue_context
105  );
106  break;
107  case SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL:
109  &the_semaphore->Core_control.Mutex.Recursive,
110  _Semaphore_Get_operations( flags ),
111  executing,
112  wait,
114  &queue_context
115  );
116  break;
117 #if defined(RTEMS_SMP)
118  case SEMAPHORE_VARIANT_MRSP:
119  status = _MRSP_Seize(
120  &the_semaphore->Core_control.MRSP,
121  executing,
122  wait,
123  &queue_context
124  );
125  break;
126 #endif
127  default:
128  _Assert(
129  variant == SEMAPHORE_VARIANT_SIMPLE_BINARY
130  || variant == SEMAPHORE_VARIANT_COUNTING
131  );
132  status = _CORE_semaphore_Seize(
133  &the_semaphore->Core_control.Semaphore,
134  _Semaphore_Get_operations( flags ),
135  executing,
136  wait,
137  &queue_context
138  );
139  break;
140  }
141 
142  return _Status_Get( status );
143 }
Thread_queue_Control Wait_queue
The thread queue of this mutex.
Definition: coremutex.h:62
Thread queue context for the thread queue methods.
Definition: threadq.h:198
static __inline__ void _Thread_queue_Context_set_enqueue_timeout_ticks(Thread_queue_Context *queue_context, Watchdog_Interval ticks)
Sets the enqueue callout to add a relative monotonic timeout in ticks.
Definition: threadqimpl.h:262
uint32_t rtems_option
This type is used to represent an option set.
Definition: options.h:121
CORE_ceiling_mutex_Control Mutex
Definition: semdata.h:71
Thread_queue_Control Wait_queue
The thread queue to manage ownership and waiting threads.
Definition: mrsp.h:66
CORE_recursive_mutex_Control Recursive
The plain recursive mutex.
Definition: coremutex.h:87
Thread_queue_Control Wait_queue
The thread queue present in all other variants.
Definition: semdata.h:65
static __inline__ Status_Control _CORE_recursive_mutex_Seize_nested(CORE_recursive_mutex_Control *the_mutex)
Seizes the recursive mutex nested.
static __inline__ Status_Control _CORE_ceiling_mutex_Seize(CORE_ceiling_mutex_Control *the_mutex, Thread_Control *executing, bool wait, Status_Control(*nested)(CORE_recursive_mutex_Control *), Thread_queue_Context *queue_context)
Seizes the ceiling mutex.
Classic Options Implementation.
RTEMS_INLINE_ROUTINE bool _Options_Is_no_wait(rtems_option option_set)
Checks if the RTEMS_NO_WAIT option is enabled in option_set.
Definition: optionsimpl.h:40
static __inline__ Status_Control _MRSP_Seize(MRSP_Control *mrsp, Thread_Control *executing, bool wait, Thread_queue_Context *queue_context)
Seizes the MrsP control.
Definition: mrspimpl.h:406
static __inline__ void _Thread_queue_Context_set_enqueue_do_nothing_extra(Thread_queue_Context *queue_context)
Sets the do nothing enqueue callout in the thread queue context.
Definition: threadqimpl.h:245
Classic Semaphore Manager Implementation.
rtems_status_code
This enumeration provides status codes for directives of the Classic API.
Definition: status.h:82
Classic Status Implementation.
rtems_status_code rtems_semaphore_obtain(rtems_id id, rtems_option option_set, rtems_interval timeout)
%
Definition: semobtain.c:51
CORE_mutex_Control Mutex
The plain non-recursive mutex.
Definition: coremutex.h:72
static __inline__ Status_Control _CORE_recursive_mutex_Seize(CORE_recursive_mutex_Control *the_mutex, const Thread_queue_Operations *operations, Thread_Control *executing, bool wait, Status_Control(*nested)(CORE_recursive_mutex_Control *), Thread_queue_Context *queue_context)
Seizes the recursive mutex.
Watchdog_Interval rtems_interval
This type is used to represent clock tick intervals.
Definition: types.h:90
This status code indicates that an object identifier was invalid.
Definition: status.h:106
Objects_Id rtems_id
Values of this type identify an RTEMS object.
Definition: types.h:99
static __inline__ Status_Control _CORE_semaphore_Seize(CORE_semaphore_Control *the_semaphore, const Thread_queue_Operations *operations, Thread_Control *executing, bool wait, Thread_queue_Context *queue_context)
Seizes the semaphore.
Definition: coresemimpl.h:202
CORE_semaphore_Control Semaphore
Definition: semdata.h:77
union Semaphore_Control::@8 Core_control
Semaphore_Variant
Classic semaphore variants.
Definition: semimpl.h:42
Thread_queue_Control Wait_queue
Definition: coresem.h:52
#define _Assert(_e)
Assertion similar to assert() controlled via RTEMS_DEBUG instead of NDEBUG.
Definition: assert.h:100