source: rtems/cpukit/score/include/rtems/score/coremutex.h @ d4dc7c8

4.115
Last change on this file since d4dc7c8 was d7c3883, checked in by Jennifer Averett <Jennifer.Averett@…>, on 04/21/11 at 19:05:15

2011-04-21 Jennifer Averett <Jennifer.Averett@…

PR 1777/cpukit

  • libcsupport/src/malloc_deferred.c, libcsupport/src/realloc.c, score/Makefile.am, score/cpu/lm32/irq.c, score/cpu/nios2/irq.c, score/include/rtems/score/coremutex.h, score/include/rtems/score/thread.h, score/inline/rtems/score/thread.inl, score/src/heapfree.c, score/src/pheapwalk.c, score/src/smp.c, score/src/threaddispatch.c: Consolidated access to _Thread_Dispatch_disable_level.
  • score/src/threaddisabledispatch.c, score/src/threadenabledispatch.c: New files.
  • Property mode set to 100644
File size: 14.8 KB
Line 
1/**
2 *  @file  rtems/score/coremutex.h
3 *
4 *  This include file contains all the constants and structures associated
5 *  with the Mutex Handler.  A mutex is an enhanced version of the standard
6 *  Dijkstra binary semaphore used to provide synchronization and mutual
7 *  exclusion capabilities.
8 */
9
10/*
11 *  COPYRIGHT (c) 1989-2009.
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.com/license/LICENSE.
17 *
18 *  $Id$
19 */
20
21#ifndef _RTEMS_SCORE_COREMUTEX_H
22#define _RTEMS_SCORE_COREMUTEX_H
23
24
25/**
26 *  @defgroup ScoreMutex Mutex Handler
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 MP Support Callback Prototype
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 Blocking Disciplines Enumerated Type
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 Mutex method 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 Core Mutex Last Status
124 *
125 *  This is the last status value.
126 */
127#define CORE_MUTEX_STATUS_LAST CORE_MUTEX_STATUS_CEILING_VIOLATED
128
129/**
130 *  @brief Mutex Lock Nesting Behavior Enumeration
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 Core Mutex Attributes
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/*@brief Core Mutex Lock_Chain Struct
201 *
202 * The following defines the control block used to manage lock chain of
203 * priority inheritance mutex.
204 */
205  typedef struct{
206    /** This field is a chian of locked mutex by a thread,new mutex will
207     *  be added to the head of queue, and the mutex which will be released
208     *  must be the head of queue.
209     */
210    Chain_Node                lock_queue;
211    /** This field is the priority of thread before locking this mutex
212     *
213     */
214    Priority_Control          priority_before;
215  }  CORE_mutex_order_list;
216#endif
217
218/**
219 *  @brief Core Mutex Control Structure
220 *
221 *  The following defines the control block used to manage each mutex.
222 */
223typedef struct {
224  /** This field is the Waiting Queue used to manage the set of tasks
225   *  which are blocked waiting to lock the mutex.
226   */
227  Thread_queue_Control    Wait_queue;
228  /** This element is the set of attributes which define this instance's
229   *  behavior.
230   */
231  CORE_mutex_Attributes   Attributes;
232  /** This element contains the current state of the mutex.
233   */
234  uint32_t                lock;
235  /** This element contains the number of times the mutex has been acquired
236   *  nested.  This must be zero (0) before the mutex is actually unlocked.
237   */
238  uint32_t                nest_count;
239  /** This is the number of waiting threads. */
240  uint32_t                blocked_count;
241  /** This element points to the thread which is currently holding this mutex.
242   *  The holder is the last thread to successfully lock the mutex and which
243   *  has not unlocked it.  If the thread is not locked, there is no holder.
244   */
245  Thread_Control         *holder;
246  /** This element contains the object Id of the holding thread.  */
247  Objects_Id              holder_id;
248#ifdef __RTEMS_STRICT_ORDER_MUTEX__
249  /** This field is used to manipulate the priority inheritance mutex queue*/
250  CORE_mutex_order_list   queue;
251#endif
252
253}   CORE_mutex_Control;
254
255/**
256 *  @brief Initialize a Core Mutex
257 *
258 *  This routine initializes the mutex based on the parameters passed.
259 *
260 *  @param[in] the_mutex is the mutex to initalize
261 *  @param[in] the_mutex_attributes is the attributes associated with this
262 *         mutex instance
263 *  @param[in] initial_lock is the initial value of the mutex
264 *
265 *  @return This method returns CORE_MUTEX_STATUS_SUCCESSFUL if successful.
266 */
267CORE_mutex_Status _CORE_mutex_Initialize(
268  CORE_mutex_Control           *the_mutex,
269  CORE_mutex_Attributes        *the_mutex_attributes,
270  uint32_t                      initial_lock
271);
272
273#ifndef __RTEMS_APPLICATION__
274/**
275 *  @brief Seize Mutex with Quick Success Path
276 *
277 *  This routine attempts to receive a unit from the_mutex.
278 *  If a unit is available or if the wait flag is false, then the routine
279 *  returns.  Otherwise, the calling task is blocked until a unit becomes
280 *  available.
281 *
282 *  @param[in] the_mutex is the mutex to attempt to lock
283 *  @param[in] level_p is the interrupt level holder
284 *
285 *  @return This routine returns 0 if "trylock" can resolve whether or not
286 *  the mutex is immediately obtained or there was an error attempting to
287 *  get it.  It returns 1 to indicate that the caller cannot obtain
288 *  the mutex and will have to block to do so.
289 *
290 *  @note  For performance reasons, this routine is implemented as
291 *         a macro that uses two support routines.
292 */
293
294RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body(
295  CORE_mutex_Control  *the_mutex,
296  ISR_Level           *level_p
297);
298
299#if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
300  /**
301   *  When doing test coverage analysis or trying to minimize the code
302   *  space for RTEMS, it is often helpful to not inline this method
303   *  multiple times.  It is fairly large and has a high branch complexity
304   *  which makes it harder to get full binary test coverage.
305   *
306   *  @param[in] the_mutex will attempt to lock
307   *  @param[in] level_p is the interrupt level holder
308   */
309  int _CORE_mutex_Seize_interrupt_trylock(
310    CORE_mutex_Control  *the_mutex,
311    ISR_Level           *level_p
312  );
313#else
314  /**
315   *  The default is to favor speed and inlining this definitely saves
316   *  a few instructions.  This is very important for mutex performance.
317   *
318   *  @param[in] _mutex will attempt to lock
319   *  @param[in] _level_p is the interrupt level holder
320   */
321  #define _CORE_mutex_Seize_interrupt_trylock( _mutex, _level_p ) \
322     _CORE_mutex_Seize_interrupt_trylock_body( _mutex, _level_p )
323#endif
324
325/**
326 *  @brief Seize Mutex with Blocking
327 *
328 *  This routine performs the blocking portion of a mutex obtain.
329 *  It is an actual subroutine and is not implemented as something
330 *  that may be inlined.
331 *
332 *  @param[in] the_mutex is the mutex to attempt to lock
333 *  @param[in] timeout is the maximum number of ticks to block
334 */
335void _CORE_mutex_Seize_interrupt_blocking(
336  CORE_mutex_Control  *the_mutex,
337  Watchdog_Interval    timeout
338);
339
340/**
341 *  @brief Sieze Interrupt Wrapper
342 *
343 *  This routine attempts to obtain the mutex.  If the mutex is available,
344 *  then it will return immediately.  Otherwise, it will invoke the
345 *  support routine @a _Core_mutex_Seize_interrupt_blocking.
346 *
347 *  @param[in] _the_mutex is the mutex to attempt to lock
348 *  @param[in] _id is the Id of the owning API level Semaphore object
349 *  @param[in] _wait is true if the thread is willing to wait
350 *  @param[in] _timeout is the maximum number of ticks to block
351 *  @param[in] _level is a temporary variable used to contain the ISR
352 *         disable level cookie
353 *
354 *  @note If the mutex is called from an interrupt service routine,
355 *        with context switching disabled, or before multitasking,
356 *        then a fatal error is generated.
357 *
358 *  The logic on this routine is as follows:
359 *
360 *  * If incorrect system state
361 *      return an error
362 *  * If mutex is available without any contention or blocking
363 *      obtain it with interrupts disabled and returned
364 *  * If the caller is willing to wait
365 *      then they are blocked.
366 */
367
368#define _CORE_mutex_Seize_body( \
369  _the_mutex, _id, _wait, _timeout, _level ) \
370  do { \
371    if ( _Thread_Dispatch_in_critical_section() \
372        && (_wait) \
373        && (_System_state_Get() >= SYSTEM_STATE_BEGIN_MULTITASKING ) \
374       ) { \
375        _Internal_error_Occurred( \
376           INTERNAL_ERROR_CORE, \
377           false, \
378           INTERNAL_ERROR_MUTEX_OBTAIN_FROM_BAD_STATE \
379           ); \
380    } \
381    if ( _CORE_mutex_Seize_interrupt_trylock( _the_mutex, &_level ) ) {  \
382      if ( !_wait ) { \
383        _ISR_Enable( _level ); \
384        _Thread_Executing->Wait.return_code = \
385          CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT; \
386      } else { \
387        _Thread_queue_Enter_critical_section( &(_the_mutex)->Wait_queue ); \
388        _Thread_Executing->Wait.queue = &(_the_mutex)->Wait_queue; \
389        _Thread_Executing->Wait.id    = _id; \
390        _Thread_Disable_dispatch(); \
391        _ISR_Enable( _level ); \
392       _CORE_mutex_Seize_interrupt_blocking( _the_mutex, _timeout ); \
393      } \
394    } \
395  } while (0)
396
397/**
398 *  This method is used to obtain a core mutex.
399 *
400 *  @param[in] _the_mutex is the mutex to attempt to lock
401 *  @param[in] _id is the Id of the owning API level Semaphore object
402 *  @param[in] _wait is true if the thread is willing to wait
403 *  @param[in] _timeout is the maximum number of ticks to block
404 *  @param[in] _level is a temporary variable used to contain the ISR
405 *         disable level cookie
406 */
407#if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
408  void _CORE_mutex_Seize(
409    CORE_mutex_Control  *_the_mutex,
410    Objects_Id           _id,
411    bool                 _wait,
412    Watchdog_Interval    _timeout,
413    ISR_Level            _level
414  );
415#else
416  #define _CORE_mutex_Seize( _the_mutex, _id, _wait, _timeout, _level ) \
417     _CORE_mutex_Seize_body( _the_mutex, _id, _wait, _timeout, _level )
418#endif
419
420/**
421 *  @brief Surrender the Mutex
422 *
423 *  This routine frees a unit to the mutex.  If a task was blocked waiting for
424 *  a unit from this mutex, then that task will be readied and the unit
425 *  given to that task.  Otherwise, the unit will be returned to the mutex.
426 *
427 *  @param[in] the_mutex is the mutex to surrender
428 *  @param[in] id is the id of the RTEMS Object associated with this mutex
429 *  @param[in] api_mutex_mp_support is the routine that will be called when
430 *         unblocking a remote mutex
431 *
432 *  @return an indication of whether the routine succeeded or failed
433 */
434CORE_mutex_Status _CORE_mutex_Surrender(
435  CORE_mutex_Control                *the_mutex,
436  Objects_Id                         id,
437  CORE_mutex_API_mp_support_callout  api_mutex_mp_support
438);
439
440/**
441 *  @brief Flush all waiting threads
442 *
443 *  This routine assists in the deletion of a mutex by flushing the associated
444 *  wait queue.
445 *
446 *  @param[in] the_mutex is the mutex to flush
447 *  @param[in] remote_extract_callout is the routine to invoke when a remote
448 *         thread is extracted
449 *  @param[in] status is the status value which each unblocked thread will
450 *         return to its caller.
451 */
452void _CORE_mutex_Flush(
453  CORE_mutex_Control         *the_mutex,
454  Thread_queue_Flush_callout  remote_extract_callout,
455  uint32_t                    status
456);
457
458#include <rtems/score/coremutex.inl>
459#endif
460
461#ifdef __cplusplus
462}
463#endif
464
465/**@}*/
466
467#endif
468/*  end of include file */
Note: See TracBrowser for help on using the repository browser.