source: rtems/cpukit/score/include/rtems/score/coresemimpl.h @ 114e408

5
Last change on this file since 114e408 was 114e408, checked in by Sebastian Huber <sebastian.huber@…>, on 08/22/16 at 11:17:05

score: Simplify thread queue acquire/release

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/**
2 * @file
3 *
4 * @brief Inlined Routines Associated with the SuperCore Semaphore
5 *
6 * This include file contains all of the inlined routines associated
7 * with the SuperCore semaphore.
8 */
9
10/*
11 *  COPYRIGHT (c) 1989-2006.
12 *  On-Line Applications Research Corporation (OAR).
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.rtems.org/license/LICENSE.
17 */
18
19#ifndef _RTEMS_SCORE_CORESEMIMPL_H
20#define _RTEMS_SCORE_CORESEMIMPL_H
21
22#include <rtems/score/coresem.h>
23#include <rtems/score/objectimpl.h>
24#include <rtems/score/threaddispatch.h>
25#include <rtems/score/threadimpl.h>
26#include <rtems/score/threadqimpl.h>
27#include <rtems/score/statesimpl.h>
28#include <rtems/score/status.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/**
35 * @addtogroup ScoreSemaphore
36 */
37/**@{**/
38
39/**
40 *  @brief Initialize the semaphore based on the parameters passed.
41 *
42 *  This package is the implementation of the CORE Semaphore Handler.
43 *  This core object utilizes standard Dijkstra counting semaphores to provide
44 *  synchronization and mutual exclusion capabilities.
45 *
46 *  This routine initializes the semaphore based on the parameters passed.
47 *
48 *  @param[in] the_semaphore is the semaphore to initialize
49 *  @param[in] initial_value is the initial count of the semaphore
50 */
51void _CORE_semaphore_Initialize(
52  CORE_semaphore_Control *the_semaphore,
53  uint32_t                initial_value
54);
55
56RTEMS_INLINE_ROUTINE void _CORE_semaphore_Acquire_critical(
57  CORE_semaphore_Control *the_semaphore,
58  Thread_queue_Context   *queue_context
59)
60{
61  _Thread_queue_Acquire_critical( &the_semaphore->Wait_queue, queue_context );
62}
63
64RTEMS_INLINE_ROUTINE void _CORE_semaphore_Release(
65  CORE_semaphore_Control *the_semaphore,
66  Thread_queue_Context   *queue_context
67)
68{
69  _Thread_queue_Release( &the_semaphore->Wait_queue, queue_context );
70}
71
72RTEMS_INLINE_ROUTINE void _CORE_semaphore_Destroy(
73  CORE_semaphore_Control        *the_semaphore,
74  const Thread_queue_Operations *operations,
75  Thread_queue_Context          *queue_context
76)
77{
78  _Thread_queue_Flush_critical(
79    &the_semaphore->Wait_queue.Queue,
80    operations,
81    _Thread_queue_Flush_status_object_was_deleted,
82    queue_context
83  );
84  _Thread_queue_Destroy( &the_semaphore->Wait_queue );
85}
86
87/**
88 *  @brief Surrender a unit to a semaphore.
89 *
90 *  This routine frees a unit to the semaphore.  If a task was blocked waiting
91 *  for a unit from this semaphore, then that task will be readied and the unit
92 *  given to that task.  Otherwise, the unit will be returned to the semaphore.
93 *
94 *  @param[in] the_semaphore is the semaphore to surrender
95 *  @param[in] operations The thread queue operations.
96 *  @param[in] queue_context is a temporary variable used to contain the ISR
97 *        disable level cookie
98 *
99 *  @retval an indication of whether the routine succeeded or failed
100 */
101RTEMS_INLINE_ROUTINE Status_Control _CORE_semaphore_Surrender(
102  CORE_semaphore_Control        *the_semaphore,
103  const Thread_queue_Operations *operations,
104  uint32_t                       maximum_count,
105  Thread_queue_Context          *queue_context
106)
107{
108  Thread_Control *the_thread;
109  Status_Control  status;
110
111  status = STATUS_SUCCESSFUL;
112
113  _CORE_semaphore_Acquire_critical( the_semaphore, queue_context );
114
115  the_thread = _Thread_queue_First_locked(
116    &the_semaphore->Wait_queue,
117    operations
118  );
119  if ( the_thread != NULL ) {
120    _Thread_queue_Extract_critical(
121      &the_semaphore->Wait_queue.Queue,
122      operations,
123      the_thread,
124      queue_context
125    );
126  } else {
127    if ( the_semaphore->count < maximum_count )
128      the_semaphore->count += 1;
129    else
130      status = STATUS_MAXIMUM_COUNT_EXCEEDED;
131
132    _CORE_semaphore_Release( the_semaphore, queue_context );
133  }
134
135  return status;
136}
137
138/**
139 * This routine returns the current count associated with the semaphore.
140 *
141 * @param[in] the_semaphore is the semaphore to obtain the count of
142 *
143 * @return the current count of this semaphore
144 */
145RTEMS_INLINE_ROUTINE uint32_t  _CORE_semaphore_Get_count(
146  const CORE_semaphore_Control *the_semaphore
147)
148{
149  return the_semaphore->count;
150}
151
152/**
153 * This routine attempts to receive a unit from the_semaphore.
154 * If a unit is available or if the wait flag is false, then the routine
155 * returns.  Otherwise, the calling task is blocked until a unit becomes
156 * available.
157 *
158 * @param[in] the_semaphore is the semaphore to obtain
159 * @param[in] operations The thread queue operations.
160 * @param[in] executing The currently executing thread.
161 * @param[in] wait is true if the thread is willing to wait
162 * @param[in] queue_context is a temporary variable used to contain the ISR
163 *        disable level cookie
164 */
165RTEMS_INLINE_ROUTINE Status_Control _CORE_semaphore_Seize(
166  CORE_semaphore_Control        *the_semaphore,
167  const Thread_queue_Operations *operations,
168  Thread_Control                *executing,
169  bool                           wait,
170  Thread_queue_Context          *queue_context
171)
172{
173  _Assert( _ISR_Get_level() != 0 );
174
175  _CORE_semaphore_Acquire_critical( the_semaphore, queue_context );
176  if ( the_semaphore->count != 0 ) {
177    the_semaphore->count -= 1;
178    _CORE_semaphore_Release( the_semaphore, queue_context );
179    return STATUS_SUCCESSFUL;
180  }
181
182  if ( !wait ) {
183    _CORE_semaphore_Release( the_semaphore, queue_context );
184    return STATUS_UNSATISFIED;
185  }
186
187  _Thread_queue_Context_set_expected_level( queue_context, 1 );
188  _Thread_queue_Enqueue_critical(
189    &the_semaphore->Wait_queue.Queue,
190    operations,
191    executing,
192    STATES_WAITING_FOR_SEMAPHORE,
193    queue_context
194  );
195  return _Thread_Wait_get_status( executing );
196}
197
198/** @} */
199
200#ifdef __cplusplus
201}
202#endif
203
204#endif
205/* end of include file */
Note: See TracBrowser for help on using the repository browser.