source: rtems/cpukit/score/include/rtems/score/coremuteximpl.h @ 2bca05f7

4.115
Last change on this file since 2bca05f7 was 2bca05f7, checked in by Sebastian Huber <sebastian.huber@…>, on 08/01/13 at 12:55:03

score: Delete SYSTEM_STATE_BEGIN_MULTITASKING

Nothing happened between the SYSTEM_STATE_BEGIN_MULTITASKING to
SYSTEM_STATE_UP transition.

  • Property mode set to 100644
File size: 16.7 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup ScoreMutex
5 *
6 * @brief CORE Mutex Implementation
7 */
8
9/*
10 *  COPYRIGHT (c) 1989-2009.
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#ifndef _RTEMS_SCORE_COREMUTEXIMPL_H
19#define _RTEMS_SCORE_COREMUTEXIMPL_H
20
21#include <rtems/score/coremutex.h>
22#include <rtems/score/chainimpl.h>
23#include <rtems/score/sysstate.h>
24#include <rtems/score/threadimpl.h>
25#include <rtems/score/threadqimpl.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31/**
32 * @addtogroup ScoreMutex
33 */
34/**@{**/
35
36/**
37 *  @brief Callout which provides to support global/multiprocessor operations.
38 *
39 *  The following type defines the callout which the API provides
40 *  to support global/multiprocessor operations on mutexes.
41 */
42typedef void ( *CORE_mutex_API_mp_support_callout )(
43                 Thread_Control *,
44                 Objects_Id
45             );
46
47/**
48 *  @brief The possible Mutex handler return statuses.
49 *
50 *  This enumerated type defines the possible Mutex handler return statuses.
51 */
52typedef enum {
53  /** This status indicates that the operation completed successfully. */
54  CORE_MUTEX_STATUS_SUCCESSFUL,
55  /** This status indicates that the calling task did not want to block
56   *  and the operation was unable to complete immediately because the
57   *  resource was unavailable.
58   */
59  CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT,
60#if defined(RTEMS_POSIX_API)
61  /** This status indicates that an attempt was made to relock a mutex
62   *  for which nesting is not configured.
63   */
64  CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED,
65#endif
66  /** This status indicates that an attempt was made to release a mutex
67   *  by a thread other than the thread which locked it.
68   */
69  CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE,
70  /** This status indicates that the thread was blocked waiting for an
71   *  operation to complete and the mutex was deleted.
72   */
73  CORE_MUTEX_WAS_DELETED,
74  /** This status indicates that the calling task was willing to block
75   *  but the operation was unable to complete within the time allotted
76   *  because the resource never became available.
77   */
78  CORE_MUTEX_TIMEOUT,
79
80#if defined(__RTEMS_STRICT_ORDER_MUTEX__)
81  /** This status indicates that a thread not release the mutex which has
82   *  the priority inheritance property in a right order.
83   */
84  CORE_MUTEX_RELEASE_NOT_ORDER,
85#endif
86
87  /** This status indicates that a thread of logically greater importance
88   *  than the ceiling priority attempted to lock this mutex.
89   */
90  CORE_MUTEX_STATUS_CEILING_VIOLATED
91
92}   CORE_mutex_Status;
93
94/**
95 *  @brief The last status value.
96 *
97 *  This is the last status value.
98 */
99#define CORE_MUTEX_STATUS_LAST CORE_MUTEX_STATUS_CEILING_VIOLATED
100
101/**
102 *  This is the value of a mutex when it is unlocked.
103 */
104#define CORE_MUTEX_UNLOCKED 1
105
106/**
107 *  This is the value of a mutex when it is locked.
108 */
109#define CORE_MUTEX_LOCKED   0
110
111/**
112 *  @brief Initializes the mutex based on the parameters passed.
113 *
114 *  This routine initializes the mutex based on the parameters passed.
115 *
116 *  @param[in,out] the_mutex is the mutex to initalize
117 *  @param[in,out] executing The currently executing thread.
118 *  @param[in] the_mutex_attributes is the attributes associated with this
119 *         mutex instance
120 *  @param[in] initial_lock is the initial value of the mutex
121 *
122 *  @retval This method returns CORE_MUTEX_STATUS_SUCCESSFUL if successful.
123 */
124CORE_mutex_Status _CORE_mutex_Initialize(
125  CORE_mutex_Control           *the_mutex,
126  Thread_Control               *executing,
127  const CORE_mutex_Attributes  *the_mutex_attributes,
128  uint32_t                      initial_lock
129);
130
131/**
132 *  @brief Attempt to receive a unit from the_mutex.
133 *
134 *  This routine attempts to receive a unit from the_mutex.
135 *  If a unit is available or if the wait flag is false, then the routine
136 *  returns.  Otherwise, the calling task is blocked until a unit becomes
137 *  available.
138 *
139 *  @param[in,out] executing The currently executing thread.
140 *  @param[in,out] the_mutex is the mutex to attempt to lock
141 *  @param[in] level is the interrupt level
142 *
143 *  @retval This routine returns 0 if "trylock" can resolve whether or not
144 *  the mutex is immediately obtained or there was an error attempting to
145 *  get it.  It returns 1 to indicate that the caller cannot obtain
146 *  the mutex and will have to block to do so.
147 *
148 *  @note  For performance reasons, this routine is implemented as
149 *         a macro that uses two support routines.
150 */
151
152RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body(
153  CORE_mutex_Control  *the_mutex,
154  Thread_Control      *executing,
155  ISR_Level            level
156);
157
158#if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
159  /**
160   *  @brief Interrupt trylock CORE mutex seize.
161   *
162   *  When doing test coverage analysis or trying to minimize the code
163   *  space for RTEMS, it is often helpful to not inline this method
164   *  multiple times.  It is fairly large and has a high branch complexity
165   *  which makes it harder to get full binary test coverage.
166   *
167   *  @param[in] the_mutex will attempt to lock
168   *  @param[in] level_p is the interrupt level
169   */
170  int _CORE_mutex_Seize_interrupt_trylock(
171    CORE_mutex_Control  *the_mutex,
172    Thread_Control      *executing,
173    ISR_Level            level
174  );
175#else
176  /**
177   *  The default is to favor speed and inlining this definitely saves
178   *  a few instructions.  This is very important for mutex performance.
179   *
180   *  @param[in] _mutex will attempt to lock
181   *  @param[in] _level is the interrupt level
182   */
183  #define _CORE_mutex_Seize_interrupt_trylock( _mutex, _executing, _level ) \
184     _CORE_mutex_Seize_interrupt_trylock_body( _mutex, _executing, _level )
185#endif
186
187/**
188 *  @brief Performs the blocking portion of a mutex obtain.
189 *
190 *  This routine performs the blocking portion of a mutex obtain.
191 *  It is an actual subroutine and is not implemented as something
192 *  that may be inlined.
193 *
194 *  @param[in,out] the_mutex is the mutex to attempt to lock
195 *  @param[in,out] executing The currently executing thread.
196 *  @param[in] timeout is the maximum number of ticks to block
197 */
198void _CORE_mutex_Seize_interrupt_blocking(
199  CORE_mutex_Control  *the_mutex,
200  Thread_Control      *executing,
201  Watchdog_Interval    timeout
202);
203/**
204 *  @brief Verifies that a mutex blocking seize is performed safely.
205 *
206 *  This macro is to verify that a mutex blocking seize is
207 *  performed from a safe system state.  For example, one
208 *  cannot block inside an isr.
209 *
210 *  @retval this method returns true if dispatch is in an unsafe state.
211 */
212#ifdef RTEMS_SMP
213  #define _CORE_mutex_Check_dispatch_for_seize(_wait) \
214      (_Thread_Dispatch_get_disable_level() != 1 \
215        && (_wait) \
216        && (_System_state_Get() >= SYSTEM_STATE_UP))
217#else
218  #define _CORE_mutex_Check_dispatch_for_seize(_wait) \
219      (!_Thread_Dispatch_is_enabled() \
220        && (_wait) \
221        && (_System_state_Get() >= SYSTEM_STATE_UP))
222#endif
223
224/**
225 *  @brief Attempt to obtain the mutex.
226 *
227 *  This routine attempts to obtain the mutex.  If the mutex is available,
228 *  then it will return immediately.  Otherwise, it will invoke the
229 *  support routine @a _Core_mutex_Seize_interrupt_blocking.
230 *
231 *  @param[in] _the_mutex is the mutex to attempt to lock
232 *  @param[in] _id is the Id of the owning API level Semaphore object
233 *  @param[in] _wait is true if the thread is willing to wait
234 *  @param[in] _timeout is the maximum number of ticks to block
235 *  @param[in] _level is a temporary variable used to contain the ISR
236 *         disable level cookie
237 *
238 *  @note If the mutex is called from an interrupt service routine,
239 *        with context switching disabled, or before multitasking,
240 *        then a fatal error is generated.
241 *
242 *  The logic on this routine is as follows:
243 *
244 *  * If incorrect system state
245 *      return an error
246 *  * If mutex is available without any contention or blocking
247 *      obtain it with interrupts disabled and returned
248 *  * If the caller is willing to wait
249 *      then they are blocked.
250 */
251RTEMS_INLINE_ROUTINE void _CORE_mutex_Seize_body(
252  CORE_mutex_Control  *the_mutex,
253  Thread_Control      *executing,
254  Objects_Id           id,
255  bool                 wait,
256  Watchdog_Interval    timeout,
257  ISR_Level            level
258)
259{
260  if ( _CORE_mutex_Check_dispatch_for_seize( wait ) ) {
261    _Internal_error_Occurred(
262      INTERNAL_ERROR_CORE,
263      false,
264      INTERNAL_ERROR_MUTEX_OBTAIN_FROM_BAD_STATE
265    );
266  }
267  if ( _CORE_mutex_Seize_interrupt_trylock( the_mutex, executing, level ) ) {
268    if ( !wait ) {
269      _ISR_Enable( level );
270      executing->Wait.return_code =
271        CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT;
272    } else {
273      _Thread_queue_Enter_critical_section( &the_mutex->Wait_queue );
274      executing->Wait.queue = &the_mutex->Wait_queue;
275      executing->Wait.id = id;
276      _Thread_Disable_dispatch();
277      _ISR_Enable( level );
278      _CORE_mutex_Seize_interrupt_blocking( the_mutex, executing, timeout );
279    }
280  }
281}
282
283/**
284 *  This method is used to obtain a core mutex.
285 *
286 *  @param[in] _the_mutex is the mutex to attempt to lock
287 *  @param[in] _id is the Id of the owning API level Semaphore object
288 *  @param[in] _wait is true if the thread is willing to wait
289 *  @param[in] _timeout is the maximum number of ticks to block
290 *  @param[in] _level is a temporary variable used to contain the ISR
291 *         disable level cookie
292 */
293#if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
294  void _CORE_mutex_Seize(
295    CORE_mutex_Control  *_the_mutex,
296    Thread_Control      *_executing,
297    Objects_Id           _id,
298    bool                 _wait,
299    Watchdog_Interval    _timeout,
300    ISR_Level            _level
301  );
302#else
303  #define _CORE_mutex_Seize( _executing, _mtx, _id, _wait, _timeout, _level ) \
304     _CORE_mutex_Seize_body( _executing, _mtx, _id, _wait, _timeout, _level )
305#endif
306
307/**
308 *  @brief Frees a unit to the mutex.
309 *
310 *  This routine frees a unit to the mutex.  If a task was blocked waiting for
311 *  a unit from this mutex, then that task will be readied and the unit
312 *  given to that task.  Otherwise, the unit will be returned to the mutex.
313 *
314 *  @param[in] the_mutex is the mutex to surrender
315 *  @param[in] id is the id of the RTEMS Object associated with this mutex
316 *  @param[in] api_mutex_mp_support is the routine that will be called when
317 *         unblocking a remote mutex
318 *
319 *  @retval an indication of whether the routine succeeded or failed
320 */
321CORE_mutex_Status _CORE_mutex_Surrender(
322  CORE_mutex_Control                *the_mutex,
323  Objects_Id                         id,
324  CORE_mutex_API_mp_support_callout  api_mutex_mp_support
325);
326
327/**
328 *  @brief Flush all waiting threads.
329 *
330 *  This routine assists in the deletion of a mutex by flushing the associated
331 *  wait queue.
332 *
333 *  @param[in] the_mutex is the mutex to flush
334 *  @param[in] remote_extract_callout is the routine to invoke when a remote
335 *         thread is extracted
336 *  @param[in] status is the status value which each unblocked thread will
337 *         return to its caller.
338 */
339void _CORE_mutex_Flush(
340  CORE_mutex_Control         *the_mutex,
341  Thread_queue_Flush_callout  remote_extract_callout,
342  uint32_t                    status
343);
344
345/**
346 * @brief Is mutex locked.
347 *
348 * This routine returns true if the mutex specified is locked and false
349 * otherwise.
350 *
351 * @param[in] the_mutex is the mutex to check.
352 *
353 * @retval true The mutex is locked.
354 * @retval false The mutex is not locked.
355 */
356RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_locked(
357  CORE_mutex_Control  *the_mutex
358)
359{
360  return the_mutex->lock == CORE_MUTEX_LOCKED;
361}
362
363/**
364 * @brief Does core mutex use FIFO blocking.
365 *
366 * This routine returns true if the mutex's wait discipline is FIFO and false
367 * otherwise.
368 *
369 * @param[in] the_attribute is the attribute set of the mutex.
370 *
371 * @retval true The mutex is using FIFO blocking order.
372 * @retval false The mutex is not using FIFO blocking order.
373 */
374RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_fifo(
375  const CORE_mutex_Attributes *the_attribute
376)
377{
378  return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_FIFO;
379}
380
381/**
382 * @brief Doex core mutex use priority blocking.
383 *
384 * This routine returns true if the mutex's wait discipline is PRIORITY and
385 * false otherwise.
386 *
387 * @param[in] the_attribute is the attribute set of the mutex.
388 *
389 * @retval true The mutex is using priority blocking order.
390 * @retval false The mutex is not using priority blocking order.
391 *
392 */
393RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_priority(
394  CORE_mutex_Attributes *the_attribute
395)
396{
397  return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_PRIORITY;
398}
399
400/**
401 * @brief Does mutex use priority inheritance.
402 *
403 * This routine returns true if the mutex's wait discipline is
404 * INHERIT_PRIORITY and false otherwise.
405 *
406 * @param[in] the_attribute is the attribute set of the mutex.
407 *
408 * @retval true The mutex is using priority inheritance.
409 * @retval false The mutex is not using priority inheritance.
410 */
411RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_inherit_priority(
412  CORE_mutex_Attributes *the_attribute
413)
414{
415  return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
416}
417
418/**
419 * @brief Does mutex use priority ceiling.
420 *
421 * This routine returns true if the mutex's wait discipline is
422 * PRIORITY_CEILING and false otherwise.
423 *
424 * @param[in] the_attribute is the attribute set of the mutex.
425 *
426 * @retval true The mutex is using priority ceiling.
427 * @retval false The mutex is not using priority ceiling.
428 */
429RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_priority_ceiling(
430  CORE_mutex_Attributes *the_attribute
431)
432{
433  return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
434}
435
436/*
437 *  Seize Mutex with Quick Success Path
438 *
439 *  NOTE: There is no MACRO version of this routine.  A body is in
440 *  coremutexseize.c that is duplicated from the .inl by hand.
441 *
442 *  NOTE: The Doxygen for this routine is in the .h file.
443 */
444
445RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body(
446  CORE_mutex_Control  *the_mutex,
447  Thread_Control      *executing,
448  ISR_Level            level
449)
450{
451  /* disabled when you get here */
452
453  executing->Wait.return_code = CORE_MUTEX_STATUS_SUCCESSFUL;
454  if ( !_CORE_mutex_Is_locked( the_mutex ) ) {
455    the_mutex->lock       = CORE_MUTEX_LOCKED;
456    the_mutex->holder     = executing;
457    the_mutex->holder_id  = executing->Object.id;
458    the_mutex->nest_count = 1;
459    if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
460         _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ){
461
462#ifdef __RTEMS_STRICT_ORDER_MUTEX__
463       _Chain_Prepend_unprotected( &executing->lock_mutex,
464                                   &the_mutex->queue.lock_queue );
465       the_mutex->queue.priority_before = executing->current_priority;
466#endif
467
468      executing->resource_count++;
469    }
470
471    if ( !_CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
472      _ISR_Enable( level );
473      return 0;
474    } /* else must be CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING
475       *
476       * we possibly bump the priority of the current holder -- which
477       * happens to be _Thread_Executing.
478       */
479    {
480      Priority_Control  ceiling;
481      Priority_Control  current;
482
483      ceiling = the_mutex->Attributes.priority_ceiling;
484      current = executing->current_priority;
485      if ( current == ceiling ) {
486        _ISR_Enable( level );
487        return 0;
488      }
489
490      if ( current > ceiling ) {
491        _Thread_Disable_dispatch();
492        _ISR_Enable( level );
493        _Thread_Change_priority(
494          the_mutex->holder,
495          the_mutex->Attributes.priority_ceiling,
496         false
497        );
498        _Thread_Enable_dispatch();
499        return 0;
500      }
501      /* if ( current < ceiling ) */ {
502        executing->Wait.return_code = CORE_MUTEX_STATUS_CEILING_VIOLATED;
503        the_mutex->lock       = CORE_MUTEX_UNLOCKED;
504        the_mutex->nest_count = 0;     /* undo locking above */
505        executing->resource_count--;   /* undo locking above */
506        _ISR_Enable( level );
507        return 0;
508      }
509    }
510    return 0;
511  }
512
513  /*
514   *  At this point, we know the mutex was not available.  If this thread
515   *  is the thread that has locked the mutex, let's see if we are allowed
516   *  to nest access.
517   */
518  if ( _Thread_Is_executing( the_mutex->holder ) ) {
519    switch ( the_mutex->Attributes.lock_nesting_behavior ) {
520      case CORE_MUTEX_NESTING_ACQUIRES:
521        the_mutex->nest_count++;
522        _ISR_Enable( level );
523        return 0;
524      #if defined(RTEMS_POSIX_API)
525        case CORE_MUTEX_NESTING_IS_ERROR:
526          executing->Wait.return_code = CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED;
527          _ISR_Enable( level );
528          return 0;
529      #endif
530      case CORE_MUTEX_NESTING_BLOCKS:
531        break;
532    }
533  }
534
535  /*
536   *  The mutex is not available and the caller must deal with the possibility
537   *  of blocking.
538   */
539  return 1;
540}
541
542/** @} */
543
544#ifdef __cplusplus
545}
546#endif
547
548#endif
549/* end of include file */
Note: See TracBrowser for help on using the repository browser.