source: rtems/cpukit/score/include/rtems/score/coremutex.h @ 4f5baff

4.115
Last change on this file since 4f5baff was 4f5baff, checked in by Sebastian Huber <sebastian.huber@…>, on 06/05/13 at 15:13:14

score: Simplify _CORE_mutex_Seize_interrupt_try*

  • Property mode set to 100644
File size: 15.6 KB
Line 
1/**
2 *  @file  rtems/score/coremutex.h
3 *
4 *  @brief Constants and Structures Associated with the Mutex Handler
5 *
6 *  This include file contains all the constants and structures associated
7 *  with the Mutex Handler.  A mutex is an enhanced version of the standard
8 *  Dijkstra binary semaphore used to provide synchronization and mutual
9 *  exclusion capabilities.
10 */
11
12/*
13 *  COPYRIGHT (c) 1989-2011.
14 *  On-Line Applications Research Corporation (OAR).
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.com/license/LICENSE.
19 */
20
21#ifndef _RTEMS_SCORE_COREMUTEX_H
22#define _RTEMS_SCORE_COREMUTEX_H
23/**
24 *  @defgroup ScoreMutex Mutex Handler
25 *
26 *  @ingroup Score
27 *
28 *  This handler encapsulates functionality which provides the foundation
29 *  Mutex services used in all of the APIs supported by RTEMS.
30 */
31/**@{*/
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37#include <rtems/score/thread.h>
38#include <rtems/score/threadq.h>
39#include <rtems/score/priority.h>
40#include <rtems/score/watchdog.h>
41#include <rtems/score/interr.h>
42#include <rtems/score/sysstate.h>
43
44/**
45 *  @brief Callout which provides to support global/multiprocessor operations.
46 *
47 *  The following type defines the callout which the API provides
48 *  to support global/multiprocessor operations on mutexes.
49 */
50typedef void ( *CORE_mutex_API_mp_support_callout )(
51                 Thread_Control *,
52                 Objects_Id
53             );
54
55/**
56 *  @brief The blocking disciplines for a mutex.
57 *
58 *  This enumerated type defines the blocking disciplines for a mutex.
59 */
60typedef enum {
61  /** This specifies that threads will wait for the mutex in FIFO order. */
62  CORE_MUTEX_DISCIPLINES_FIFO,
63  /** This specifies that threads will wait for the mutex in priority order.  */
64  CORE_MUTEX_DISCIPLINES_PRIORITY,
65  /** This specifies that threads will wait for the mutex in priority order.
66   *  Additionally, the Priority Inheritance Protocol will be in effect.
67   */
68  CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT,
69  /** This specifies that threads will wait for the mutex in priority order.
70   *  Additionally, the Priority Ceiling Protocol will be in effect.
71   */
72  CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING
73}   CORE_mutex_Disciplines;
74
75/**
76 *  @brief The possible Mutex handler return statuses.
77 *
78 *  This enumerated type defines the possible Mutex handler return statuses.
79 */
80typedef enum {
81  /** This status indicates that the operation completed successfully. */
82  CORE_MUTEX_STATUS_SUCCESSFUL,
83  /** This status indicates that the calling task did not want to block
84   *  and the operation was unable to complete immediately because the
85   *  resource was unavailable.
86   */
87  CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT,
88#if defined(RTEMS_POSIX_API)
89  /** This status indicates that an attempt was made to relock a mutex
90   *  for which nesting is not configured.
91   */
92  CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED,
93#endif
94  /** This status indicates that an attempt was made to release a mutex
95   *  by a thread other than the thread which locked it.
96   */
97  CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE,
98  /** This status indicates that the thread was blocked waiting for an
99   *  operation to complete and the mutex was deleted.
100   */
101  CORE_MUTEX_WAS_DELETED,
102  /** This status indicates that the calling task was willing to block
103   *  but the operation was unable to complete within the time allotted
104   *  because the resource never became available.
105   */
106  CORE_MUTEX_TIMEOUT,
107
108#if defined(__RTEMS_STRICT_ORDER_MUTEX__)
109  /** This status indicates that a thread not release the mutex which has
110   *  the priority inheritance property in a right order.
111   */
112  CORE_MUTEX_RELEASE_NOT_ORDER,
113#endif
114
115  /** This status indicates that a thread of logically greater importance
116   *  than the ceiling priority attempted to lock this mutex.
117   */
118  CORE_MUTEX_STATUS_CEILING_VIOLATED
119
120}   CORE_mutex_Status;
121
122/**
123 *  @brief The last status value.
124 *
125 *  This is the last status value.
126 */
127#define CORE_MUTEX_STATUS_LAST CORE_MUTEX_STATUS_CEILING_VIOLATED
128
129/**
130 *  @brief The possible behaviors for lock nesting.
131 *
132 *  This enumerated type defines the possible behaviors for
133 *  lock nesting.
134 */
135typedef enum {
136  /**
137   *    This sequence has no blocking or errors:
138   *
139   *         + lock(m)
140   *         + lock(m)
141   *         + unlock(m)
142   *         + unlock(m)
143   */
144  CORE_MUTEX_NESTING_ACQUIRES,
145#if defined(RTEMS_POSIX_API)
146  /**
147   *    This sequence returns an error at the indicated point:
148   *
149   *        + lock(m)
150   *        + lock(m)   - already locked error
151   *        + unlock(m)
152   */
153  CORE_MUTEX_NESTING_IS_ERROR,
154#endif
155  /**
156   *    This sequence performs as indicated:
157   *        + lock(m)
158   *        + lock(m)   - deadlocks or timeouts
159   *        + unlock(m) - releases
160   */
161  CORE_MUTEX_NESTING_BLOCKS
162}  CORE_mutex_Nesting_behaviors;
163
164/**
165 *  This is the value of a mutex when it is unlocked.
166 */
167#define CORE_MUTEX_UNLOCKED 1
168
169/**
170 *  This is the value of a mutex when it is locked.
171 */
172#define CORE_MUTEX_LOCKED   0
173
174/**
175 *  @brief The control block used to manage attributes of each mutex.
176 *
177 *  The following defines the control block used to manage the
178 *  attributes of each mutex.
179 */
180typedef struct {
181  /** This field determines what the behavior of this mutex instance will
182   *  be when attempting to acquire the mutex when it is already locked.
183   */
184  CORE_mutex_Nesting_behaviors lock_nesting_behavior;
185  /** When this field is true, then only the thread that locked the mutex
186   *  is allowed to unlock it.
187   */
188  bool                         only_owner_release;
189  /** This field indicates whether threads waiting on the mutex block in
190   *  FIFO or priority order.
191   */
192  CORE_mutex_Disciplines       discipline;
193  /** This field contains the ceiling priority to be used if that protocol
194   *  is selected.
195   */
196  Priority_Control             priority_ceiling;
197}   CORE_mutex_Attributes;
198
199#ifdef __RTEMS_STRICT_ORDER_MUTEX__
200/**
201 * @brief The control block to manage lock chain of priority inheritance mutex.
202 *
203 * The following defines the control block used to manage lock chain of
204 * priority inheritance mutex.
205 */
206  typedef struct{
207    /** This field is a chian of locked mutex by a thread,new mutex will
208     *  be added to the head of queue, and the mutex which will be released
209     *  must be the head of queue.
210     */
211    Chain_Node                lock_queue;
212    /** This field is the priority of thread before locking this mutex
213     *
214     */
215    Priority_Control          priority_before;
216  }  CORE_mutex_order_list;
217#endif
218
219/**
220 *  @brief Control block used to manage each mutex.
221 *
222 *  The following defines the control block used to manage each mutex.
223 */
224typedef struct {
225  /** This field is the Waiting Queue used to manage the set of tasks
226   *  which are blocked waiting to lock the mutex.
227   */
228  Thread_queue_Control    Wait_queue;
229  /** This element is the set of attributes which define this instance's
230   *  behavior.
231   */
232  CORE_mutex_Attributes   Attributes;
233  /** This element contains the current state of the mutex.
234   */
235  uint32_t                lock;
236  /** This element contains the number of times the mutex has been acquired
237   *  nested.  This must be zero (0) before the mutex is actually unlocked.
238   */
239  uint32_t                nest_count;
240  /** This is the number of waiting threads. */
241  uint32_t                blocked_count;
242  /** This element points to the thread which is currently holding this mutex.
243   *  The holder is the last thread to successfully lock the mutex and which
244   *  has not unlocked it.  If the thread is not locked, there is no holder.
245   */
246  Thread_Control         *holder;
247  /** This element contains the object Id of the holding thread.  */
248  Objects_Id              holder_id;
249#ifdef __RTEMS_STRICT_ORDER_MUTEX__
250  /** This field is used to manipulate the priority inheritance mutex queue*/
251  CORE_mutex_order_list   queue;
252#endif
253
254}   CORE_mutex_Control;
255
256/**
257 *  @brief Initializes the mutex based on the parameters passed.
258 *
259 *  This routine initializes the mutex based on the parameters passed.
260 *
261 *  @param[in] the_mutex is the mutex to initalize
262 *  @param[in] the_mutex_attributes is the attributes associated with this
263 *         mutex instance
264 *  @param[in] initial_lock is the initial value of the mutex
265 *
266 *  @retval This method returns CORE_MUTEX_STATUS_SUCCESSFUL if successful.
267 */
268CORE_mutex_Status _CORE_mutex_Initialize(
269  CORE_mutex_Control           *the_mutex,
270  CORE_mutex_Attributes        *the_mutex_attributes,
271  uint32_t                      initial_lock
272);
273
274#ifndef __RTEMS_APPLICATION__
275/**
276 *  @brief Attempt to receive a unit from the_mutex.
277 *
278 *  This routine attempts to receive a unit from the_mutex.
279 *  If a unit is available or if the wait flag is false, then the routine
280 *  returns.  Otherwise, the calling task is blocked until a unit becomes
281 *  available.
282 *
283 *  @param[in] the_mutex is the mutex to attempt to lock
284 *  @param[in] level is the interrupt level
285 *
286 *  @retval This routine returns 0 if "trylock" can resolve whether or not
287 *  the mutex is immediately obtained or there was an error attempting to
288 *  get it.  It returns 1 to indicate that the caller cannot obtain
289 *  the mutex and will have to block to do so.
290 *
291 *  @note  For performance reasons, this routine is implemented as
292 *         a macro that uses two support routines.
293 */
294
295RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body(
296  CORE_mutex_Control  *the_mutex,
297  ISR_Level            level
298);
299
300#if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
301  /**
302   *  @brief Interrupt trylock CORE mutex seize.
303   *
304   *  When doing test coverage analysis or trying to minimize the code
305   *  space for RTEMS, it is often helpful to not inline this method
306   *  multiple times.  It is fairly large and has a high branch complexity
307   *  which makes it harder to get full binary test coverage.
308   *
309   *  @param[in] the_mutex will attempt to lock
310   *  @param[in] level_p is the interrupt level
311   */
312  int _CORE_mutex_Seize_interrupt_trylock(
313    CORE_mutex_Control  *the_mutex,
314    ISR_Level            level
315  );
316#else
317  /**
318   *  The default is to favor speed and inlining this definitely saves
319   *  a few instructions.  This is very important for mutex performance.
320   *
321   *  @param[in] _mutex will attempt to lock
322   *  @param[in] _level is the interrupt level
323   */
324  #define _CORE_mutex_Seize_interrupt_trylock( _mutex, _level ) \
325     _CORE_mutex_Seize_interrupt_trylock_body( _mutex, _level )
326#endif
327
328/**
329 *  @brief Performs the blocking portion of a mutex obtain.
330 *
331 *  This routine performs the blocking portion of a mutex obtain.
332 *  It is an actual subroutine and is not implemented as something
333 *  that may be inlined.
334 *
335 *  @param[in] the_mutex is the mutex to attempt to lock
336 *  @param[in] timeout is the maximum number of ticks to block
337 */
338void _CORE_mutex_Seize_interrupt_blocking(
339  CORE_mutex_Control  *the_mutex,
340  Watchdog_Interval    timeout
341);
342/**
343 *  @brief Verifies that a mutex blocking seize is performed safely.
344 *
345 *  This macro is to verify that a mutex blocking seize is
346 *  performed from a safe system state.  For example, one
347 *  cannot block inside an isr.
348 *
349 *  @retval this method returns true if dispatch is in an unsafe state.
350 */
351#ifdef RTEMS_SMP
352  #define _CORE_mutex_Check_dispatch_for_seize(_wait) 0
353#else
354  #define _CORE_mutex_Check_dispatch_for_seize(_wait) \
355      (_Thread_Dispatch_in_critical_section() \
356        && (_wait) \
357        && (_System_state_Get() >= SYSTEM_STATE_BEGIN_MULTITASKING))
358#endif
359
360/**
361 *  @brief Attempt to obtain the mutex.
362 *
363 *  This routine attempts to obtain the mutex.  If the mutex is available,
364 *  then it will return immediately.  Otherwise, it will invoke the
365 *  support routine @a _Core_mutex_Seize_interrupt_blocking.
366 *
367 *  @param[in] _the_mutex is the mutex to attempt to lock
368 *  @param[in] _id is the Id of the owning API level Semaphore object
369 *  @param[in] _wait is true if the thread is willing to wait
370 *  @param[in] _timeout is the maximum number of ticks to block
371 *  @param[in] _level is a temporary variable used to contain the ISR
372 *         disable level cookie
373 *
374 *  @note If the mutex is called from an interrupt service routine,
375 *        with context switching disabled, or before multitasking,
376 *        then a fatal error is generated.
377 *
378 *  The logic on this routine is as follows:
379 *
380 *  * If incorrect system state
381 *      return an error
382 *  * If mutex is available without any contention or blocking
383 *      obtain it with interrupts disabled and returned
384 *  * If the caller is willing to wait
385 *      then they are blocked.
386 */
387#define _CORE_mutex_Seize_body( \
388  _the_mutex, _id, _wait, _timeout, _level ) \
389  do { \
390    if ( _CORE_mutex_Check_dispatch_for_seize(_wait) ) { \
391        _Internal_error_Occurred( \
392           INTERNAL_ERROR_CORE, \
393           false, \
394           INTERNAL_ERROR_MUTEX_OBTAIN_FROM_BAD_STATE \
395           ); \
396    } \
397    if ( _CORE_mutex_Seize_interrupt_trylock( _the_mutex, _level ) ) {  \
398      if ( !(_wait) ) { \
399        _ISR_Enable( _level ); \
400        _Thread_Executing->Wait.return_code = \
401          CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT; \
402      } else { \
403        _Thread_queue_Enter_critical_section( &(_the_mutex)->Wait_queue ); \
404        _Thread_Executing->Wait.queue = &(_the_mutex)->Wait_queue; \
405        _Thread_Executing->Wait.id    = _id; \
406        _Thread_Disable_dispatch(); \
407        _ISR_Enable( _level ); \
408       _CORE_mutex_Seize_interrupt_blocking( _the_mutex, _timeout ); \
409      } \
410    } \
411  } while (0)
412
413/**
414 *  This method is used to obtain a core mutex.
415 *
416 *  @param[in] _the_mutex is the mutex to attempt to lock
417 *  @param[in] _id is the Id of the owning API level Semaphore object
418 *  @param[in] _wait is true if the thread is willing to wait
419 *  @param[in] _timeout is the maximum number of ticks to block
420 *  @param[in] _level is a temporary variable used to contain the ISR
421 *         disable level cookie
422 */
423#if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
424  void _CORE_mutex_Seize(
425    CORE_mutex_Control  *_the_mutex,
426    Objects_Id           _id,
427    bool                 _wait,
428    Watchdog_Interval    _timeout,
429    ISR_Level            _level
430  );
431#else
432  #define _CORE_mutex_Seize( _the_mutex, _id, _wait, _timeout, _level ) \
433     _CORE_mutex_Seize_body( _the_mutex, _id, _wait, _timeout, _level )
434#endif
435
436/**
437 *  @brief Frees a unit to the mutex.
438 *
439 *  This routine frees a unit to the mutex.  If a task was blocked waiting for
440 *  a unit from this mutex, then that task will be readied and the unit
441 *  given to that task.  Otherwise, the unit will be returned to the mutex.
442 *
443 *  @param[in] the_mutex is the mutex to surrender
444 *  @param[in] id is the id of the RTEMS Object associated with this mutex
445 *  @param[in] api_mutex_mp_support is the routine that will be called when
446 *         unblocking a remote mutex
447 *
448 *  @retval an indication of whether the routine succeeded or failed
449 */
450CORE_mutex_Status _CORE_mutex_Surrender(
451  CORE_mutex_Control                *the_mutex,
452  Objects_Id                         id,
453  CORE_mutex_API_mp_support_callout  api_mutex_mp_support
454);
455
456/**
457 *  @brief Flush all waiting threads.
458 *
459 *  This routine assists in the deletion of a mutex by flushing the associated
460 *  wait queue.
461 *
462 *  @param[in] the_mutex is the mutex to flush
463 *  @param[in] remote_extract_callout is the routine to invoke when a remote
464 *         thread is extracted
465 *  @param[in] status is the status value which each unblocked thread will
466 *         return to its caller.
467 */
468void _CORE_mutex_Flush(
469  CORE_mutex_Control         *the_mutex,
470  Thread_queue_Flush_callout  remote_extract_callout,
471  uint32_t                    status
472);
473
474#include <rtems/score/coremutex.inl>
475#endif
476
477#ifdef __cplusplus
478}
479#endif
480
481/**@}*/
482
483#endif
484/*  end of include file */
Note: See TracBrowser for help on using the repository browser.