source: rtems/cpukit/score/include/rtems/score/coremuteximpl.h @ 1e1a91ed

5
Last change on this file since 1e1a91ed was 1e1a91ed, checked in by Sebastian Huber <sebastian.huber@…>, on 03/23/16 at 09:01:31

score: Remove Thread_queue_Queue::operations field

Remove the Thread_queue_Queue::operations field to reduce the size of
this structure. Add a thread queue operations parameter to the
_Thread_queue_First(), _Thread_queue_First_locked(),
_Thread_queue_Enqueue(), _Thread_queue_Dequeue() and
_Thread_queue_Flush() functions. This is a preparation patch to reduce
the size of several synchronization objects.

  • Property mode set to 100644
File size: 17.1 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.org/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 *  @brief Initializes the mutex based on the parameters passed.
103 *
104 *  This routine initializes the mutex based on the parameters passed.
105 *
106 *  @param[in,out] the_mutex is the mutex to initalize
107 *  @param[in,out] executing The currently executing thread.
108 *  @param[in] the_mutex_attributes is the attributes associated with this
109 *         mutex instance
110 *  @param[in] initially_locked If true, then the mutex is initially locked by
111 *  the executing thread.
112 *
113 *  @retval This method returns CORE_MUTEX_STATUS_SUCCESSFUL if successful.
114 */
115CORE_mutex_Status _CORE_mutex_Initialize(
116  CORE_mutex_Control           *the_mutex,
117  Thread_Control               *executing,
118  const CORE_mutex_Attributes  *the_mutex_attributes,
119  bool                          initially_locked
120);
121
122RTEMS_INLINE_ROUTINE void _CORE_mutex_Destroy( CORE_mutex_Control *the_mutex )
123{
124  _Thread_queue_Destroy( &the_mutex->Wait_queue );
125}
126
127/**
128 *  @brief Attempt to receive a unit from the_mutex.
129 *
130 *  This routine attempts to receive a unit from the_mutex.
131 *  If a unit is available or if the wait flag is false, then the routine
132 *  returns.  Otherwise, the calling task is blocked until a unit becomes
133 *  available.
134 *
135 *  @param[in,out] executing The currently executing thread.
136 *  @param[in,out] the_mutex is the mutex to attempt to lock
137 *  @param[in] lock_context is the interrupt level
138 *
139 *  @retval This routine returns 0 if "trylock" can resolve whether or not
140 *  the mutex is immediately obtained or there was an error attempting to
141 *  get it.  It returns 1 to indicate that the caller cannot obtain
142 *  the mutex and will have to block to do so.
143 *
144 *  @note  For performance reasons, this routine is implemented as
145 *         a macro that uses two support routines.
146 */
147
148RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body(
149  CORE_mutex_Control  *the_mutex,
150  Thread_Control      *executing,
151  ISR_lock_Context    *lock_context
152);
153
154#if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
155  /**
156   *  @brief Interrupt trylock CORE mutex seize.
157   *
158   *  When doing test coverage analysis or trying to minimize the code
159   *  space for RTEMS, it is often helpful to not inline this method
160   *  multiple times.  It is fairly large and has a high branch complexity
161   *  which makes it harder to get full binary test coverage.
162   *
163   *  @param[in] the_mutex will attempt to lock
164   *  @param[in] _executing points to the executing thread
165   *  @param[in] level_p is the interrupt level
166   */
167  int _CORE_mutex_Seize_interrupt_trylock(
168    CORE_mutex_Control  *the_mutex,
169    Thread_Control      *executing,
170    ISR_lock_Context    *lock_context
171  );
172#else
173  /**
174   *  The default is to favor speed and inlining this definitely saves
175   *  a few instructions.  This is very important for mutex performance.
176   *
177   *  @param[in] _mutex will attempt to lock
178   *  @param[in] _executing points to the executing thread
179   *  @param[in] _lock_context is the interrupt level
180   */
181  #define _CORE_mutex_Seize_interrupt_trylock( _mutex, _executing, _lock_context ) \
182     _CORE_mutex_Seize_interrupt_trylock_body( _mutex, _executing, _lock_context )
183#endif
184
185/**
186 *  @brief Performs the blocking portion of a mutex obtain.
187 *
188 *  This routine performs the blocking portion of a mutex obtain.
189 *  It is an actual subroutine and is not implemented as something
190 *  that may be inlined.
191 *
192 *  @param[in,out] the_mutex is the mutex to attempt to lock
193 *  @param[in,out] executing The currently executing thread.
194 *  @param[in] timeout is the maximum number of ticks to block
195 *  @param[in] lock_context is the interrupt level
196 */
197void _CORE_mutex_Seize_interrupt_blocking(
198  CORE_mutex_Control  *the_mutex,
199  Thread_Control      *executing,
200  Watchdog_Interval    timeout,
201  ISR_lock_Context    *lock_context
202);
203
204/**
205 *  @brief Verifies that a mutex blocking seize is performed safely.
206 *
207 *  This macro is to verify that a mutex blocking seize is
208 *  performed from a safe system state.  For example, one
209 *  cannot block inside an isr.
210 *
211 *  @retval this method returns true if dispatch is in an unsafe state.
212 */
213#define _CORE_mutex_Check_dispatch_for_seize(_wait) \
214  (!_Thread_Dispatch_is_enabled() \
215    && (_wait) \
216    && (_System_state_Get() >= SYSTEM_STATE_UP))
217
218/**
219 *  @brief Attempt to obtain the mutex.
220 *
221 *  This routine attempts to obtain the mutex.  If the mutex is available,
222 *  then it will return immediately.  Otherwise, it will invoke the
223 *  support routine @a _Core_mutex_Seize_interrupt_blocking.
224 *
225 *  @param[in] the_mutex is the mutex to attempt to lock
226 *  @param[in] id is the Id of the owning API level Semaphore object
227 *  @param[in] wait is true if the thread is willing to wait
228 *  @param[in] timeout is the maximum number of ticks to block
229 *  @param[in] lock_context is a temporary variable used to contain the ISR
230 *         disable level cookie
231 *
232 *  @note If the mutex is called from an interrupt service routine,
233 *        with context switching disabled, or before multitasking,
234 *        then a fatal error is generated.
235 *
236 *  The logic on this routine is as follows:
237 *
238 *  * If incorrect system state
239 *      return an error
240 *  * If mutex is available without any contention or blocking
241 *      obtain it with interrupts disabled and returned
242 *  * If the caller is willing to wait
243 *      then they are blocked.
244 */
245RTEMS_INLINE_ROUTINE void _CORE_mutex_Seize_body(
246  CORE_mutex_Control  *the_mutex,
247  Thread_Control      *executing,
248  Objects_Id           id,
249  bool                 wait,
250  Watchdog_Interval    timeout,
251  ISR_lock_Context    *lock_context
252)
253{
254  if ( _CORE_mutex_Check_dispatch_for_seize( wait ) ) {
255    _Terminate(
256      INTERNAL_ERROR_CORE,
257      false,
258      INTERNAL_ERROR_MUTEX_OBTAIN_FROM_BAD_STATE
259    );
260  }
261  _Thread_queue_Acquire_critical( &the_mutex->Wait_queue, lock_context );
262  if ( _CORE_mutex_Seize_interrupt_trylock( the_mutex, executing, lock_context ) ) {
263    if ( !wait ) {
264      _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
265      executing->Wait.return_code =
266        CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT;
267    } else {
268      executing->Wait.id = id;
269      _CORE_mutex_Seize_interrupt_blocking(
270        the_mutex,
271        executing,
272        timeout,
273        lock_context
274      );
275    }
276  }
277}
278
279/**
280 *  This method is used to obtain a core mutex.
281 *
282 *  @param[in] _the_mutex is the mutex to attempt to lock
283 *  @param[in] _executing The currently executing thread.
284 *  @param[in] _id is the Id of the owning API level Semaphore object
285 *  @param[in] _wait is true if the thread is willing to wait
286 *  @param[in] _timeout is the maximum number of ticks to block
287 *  @param[in] _lock_context is a temporary variable used to contain the ISR
288 *         disable level cookie
289 */
290#if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__)
291  void _CORE_mutex_Seize(
292    CORE_mutex_Control  *_the_mutex,
293    Thread_Control      *_executing,
294    Objects_Id           _id,
295    bool                 _wait,
296    Watchdog_Interval    _timeout,
297    ISR_lock_Context    *_lock_context
298  );
299#else
300  #define _CORE_mutex_Seize( \
301      _the_mutex, _executing, _id, _wait, _timeout, _lock_context ) \
302       _CORE_mutex_Seize_body( \
303         _the_mutex, _executing, _id, _wait, _timeout, _lock_context )
304#endif
305
306/**
307 *  @brief Frees a unit to the mutex.
308 *
309 *  This routine frees a unit to the mutex.  If a task was blocked waiting for
310 *  a unit from this mutex, then that task will be readied and the unit
311 *  given to that task.  Otherwise, the unit will be returned to the mutex.
312 *
313 *  @param[in] the_mutex is the mutex to surrender
314 *  @param[in] id is the id of the RTEMS Object associated with this mutex
315 *  @param[in] api_mutex_mp_support is the routine that will be called when
316 *         unblocking a remote mutex
317 *  @param[in] lock_context is the interrupt level
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  ISR_lock_Context                  *lock_context
326);
327
328/**
329 *  @brief Flush all waiting threads.
330 *
331 *  This routine assists in the deletion of a mutex by flushing the associated
332 *  wait queue.
333 *
334 *  @param[in] the_mutex is the mutex to flush
335 *  @param[in] remote_extract_callout is the routine to invoke when a remote
336 *         thread is extracted
337 *  @param[in] status is the status value which each unblocked thread will
338 *         return to its caller.
339 */
340void _CORE_mutex_Flush(
341  CORE_mutex_Control         *the_mutex,
342  Thread_queue_Flush_callout  remote_extract_callout,
343  uint32_t                    status
344);
345
346/**
347 * @brief Is mutex locked.
348 *
349 * This routine returns true if the mutex specified is locked and false
350 * otherwise.
351 *
352 * @param[in] the_mutex is the mutex to check.
353 *
354 * @retval true The mutex is locked.
355 * @retval false The mutex is not locked.
356 */
357RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_locked(
358  const CORE_mutex_Control *the_mutex
359)
360{
361  return the_mutex->holder != NULL;
362}
363
364/**
365 * @brief Does core mutex use FIFO blocking.
366 *
367 * This routine returns true if the mutex's wait discipline is FIFO and false
368 * otherwise.
369 *
370 * @param[in] the_attribute is the attribute set of the mutex.
371 *
372 * @retval true The mutex is using FIFO blocking order.
373 * @retval false The mutex is not using FIFO blocking order.
374 */
375RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_fifo(
376  const CORE_mutex_Attributes *the_attribute
377)
378{
379  return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_FIFO;
380}
381
382/**
383 * @brief Doex core mutex use priority blocking.
384 *
385 * This routine returns true if the mutex's wait discipline is PRIORITY and
386 * false otherwise.
387 *
388 * @param[in] the_attribute is the attribute set of the mutex.
389 *
390 * @retval true The mutex is using priority blocking order.
391 * @retval false The mutex is not using priority blocking order.
392 *
393 */
394RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_priority(
395  const CORE_mutex_Attributes *the_attribute
396)
397{
398  return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_PRIORITY;
399}
400
401/**
402 * @brief Does mutex use priority inheritance.
403 *
404 * This routine returns true if the mutex's wait discipline is
405 * INHERIT_PRIORITY and false otherwise.
406 *
407 * @param[in] the_attribute is the attribute set of the mutex.
408 *
409 * @retval true The mutex is using priority inheritance.
410 * @retval false The mutex is not using priority inheritance.
411 */
412RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_inherit_priority(
413  const CORE_mutex_Attributes *the_attribute
414)
415{
416  return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
417}
418
419/**
420 * @brief Does mutex use priority ceiling.
421 *
422 * This routine returns true if the mutex's wait discipline is
423 * PRIORITY_CEILING and false otherwise.
424 *
425 * @param[in] the_attribute is the attribute set of the mutex.
426 *
427 * @retval true The mutex is using priority ceiling.
428 * @retval false The mutex is not using priority ceiling.
429 */
430RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_priority_ceiling(
431  const CORE_mutex_Attributes *the_attribute
432)
433{
434  return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
435}
436
437/*
438 *  Seize Mutex with Quick Success Path
439 *
440 *  NOTE: There is no MACRO version of this routine.  A body is in
441 *  coremutexseize.c that is duplicated from the .inl by hand.
442 *
443 *  NOTE: The Doxygen for this routine is in the .h file.
444 */
445
446RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body(
447  CORE_mutex_Control  *the_mutex,
448  Thread_Control      *executing,
449  ISR_lock_Context    *lock_context
450)
451{
452  /* disabled when you get here */
453
454  executing->Wait.return_code = CORE_MUTEX_STATUS_SUCCESSFUL;
455  if ( !_CORE_mutex_Is_locked( the_mutex ) ) {
456    the_mutex->holder     = executing;
457    the_mutex->nest_count = 1;
458    if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
459         _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ){
460
461#ifdef __RTEMS_STRICT_ORDER_MUTEX__
462       _Chain_Prepend_unprotected( &executing->lock_mutex,
463                                   &the_mutex->queue.lock_queue );
464       the_mutex->queue.priority_before = executing->current_priority;
465#endif
466
467      executing->resource_count++;
468    }
469
470    if ( !_CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
471      _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
472      return 0;
473    } /* else must be CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING
474       *
475       * we possibly bump the priority of the current holder -- which
476       * happens to be _Thread_Executing.
477       */
478    {
479      Priority_Control  ceiling;
480      Priority_Control  current;
481
482      ceiling = the_mutex->Attributes.priority_ceiling;
483      current = executing->current_priority;
484      if ( current == ceiling ) {
485        _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
486        return 0;
487      }
488
489      if ( current > ceiling ) {
490        Per_CPU_Control *cpu_self;
491
492        cpu_self = _Thread_Dispatch_disable_critical( lock_context );
493        _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
494        _Thread_Raise_priority( executing, ceiling );
495        _Thread_Dispatch_enable( cpu_self );
496        return 0;
497      }
498      /* if ( current < ceiling ) */ {
499        executing->Wait.return_code = CORE_MUTEX_STATUS_CEILING_VIOLATED;
500        the_mutex->holder = NULL;
501        the_mutex->nest_count = 0;     /* undo locking above */
502        executing->resource_count--;   /* undo locking above */
503        _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
504        return 0;
505      }
506    }
507    return 0;
508  }
509
510  /*
511   *  At this point, we know the mutex was not available.  If this thread
512   *  is the thread that has locked the mutex, let's see if we are allowed
513   *  to nest access.
514   */
515  if ( _Thread_Is_executing( the_mutex->holder ) ) {
516    switch ( the_mutex->Attributes.lock_nesting_behavior ) {
517      case CORE_MUTEX_NESTING_ACQUIRES:
518        the_mutex->nest_count++;
519        _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
520        return 0;
521      #if defined(RTEMS_POSIX_API)
522        case CORE_MUTEX_NESTING_IS_ERROR:
523          executing->Wait.return_code = CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED;
524          _Thread_queue_Release( &the_mutex->Wait_queue, lock_context );
525          return 0;
526      #endif
527      case CORE_MUTEX_NESTING_BLOCKS:
528        break;
529    }
530  }
531
532  /*
533   *  The mutex is not available and the caller must deal with the possibility
534   *  of blocking.
535   */
536  return 1;
537}
538
539/** @} */
540
541#ifdef __cplusplus
542}
543#endif
544
545#endif
546/* end of include file */
Note: See TracBrowser for help on using the repository browser.