source: rtems/cpukit/score/src/corespinlockwait.c @ 25f5730f

4.115
Last change on this file since 25f5730f was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 3.3 KB
Line 
1/*
2 *  @file
3 *
4 *  @brief Wait for Spinlock
5 *  @ingroup ScoreSpinlock
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2009.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/system.h>
22#include <rtems/score/corespinlockimpl.h>
23#include <rtems/score/thread.h>
24#include <rtems/score/threaddispatch.h>
25
26/*
27 *  _CORE_spinlock_Wait
28 *
29 *  This function waits for the spinlock to become available.  Optionally,
30 *  a limit may be placed on the duration of the spin.
31 *
32 *  Input parameters:
33 *    the_spinlock - the spinlock control block to initialize
34 *    wait         - true if willing to wait
35 *    timeout      - the maximum number of ticks to spin (0 is forever)
36 *
37 *  Output parameters:  NONE
38 */
39
40CORE_spinlock_Status _CORE_spinlock_Wait(
41  CORE_spinlock_Control  *the_spinlock,
42  bool                    wait,
43  Watchdog_Interval       timeout
44)
45{
46  ISR_Level level;
47  #if defined(FUNCTIONALITY_NOT_CURRENTLY_USED_BY_ANY_API)
48    Watchdog_Interval       limit = _Watchdog_Ticks_since_boot + timeout;
49  #endif
50
51  _ISR_Disable( level );
52    if ( (the_spinlock->lock == CORE_SPINLOCK_LOCKED) &&
53         (the_spinlock->holder == _Thread_Executing->Object.id) ) {
54      _ISR_Enable( level );
55      return CORE_SPINLOCK_HOLDER_RELOCKING;
56    }
57    the_spinlock->users += 1;
58    for ( ;; ) {
59      if ( the_spinlock->lock == CORE_SPINLOCK_UNLOCKED ) {
60        the_spinlock->lock = CORE_SPINLOCK_LOCKED;
61        the_spinlock->holder = _Thread_Executing->Object.id;
62        _ISR_Enable( level );
63        return CORE_SPINLOCK_SUCCESSFUL;
64      }
65
66      /*
67       *  Spinlock is unavailable.  If not willing to wait, return.
68       */
69      if ( !wait ) {
70        the_spinlock->users -= 1;
71        _ISR_Enable( level );
72        return CORE_SPINLOCK_UNAVAILABLE;
73      }
74
75      #if defined(FUNCTIONALITY_NOT_CURRENTLY_USED_BY_ANY_API)
76        /*
77         *  They are willing to wait but there could be a timeout.
78         */
79        if ( timeout && (limit <= _Watchdog_Ticks_since_boot) ) {
80          the_spinlock->users -= 1;
81          _ISR_Enable( level );
82          return CORE_SPINLOCK_TIMEOUT;
83        }
84      #endif
85
86      /*
87       *  The thread is willing to spin so let's set things up so
88       *  another thread has a chance of running.  This spinlock has
89       *  to be released by either another thread or an ISR.  Since
90       *  POSIX does not say anything about ISRs, that implies that
91       *  another thread must be able to run while spinning.  We are
92       *  not blocking so that implies we are at least preemptible
93       *  and possibly time-sliced.
94       *
95       *  So first, we will enable interrpts to allow for them to happen.
96       *  Then we will "flash" the thread dispatching critical section
97       *  so other threads have a chance to run.
98       *
99       *  A spinlock cannot be deleted while it is being used so we are
100       *  safe from deletion.
101       */
102
103       _ISR_Enable( level );
104       /* An ISR could occur here */
105
106       _Thread_Enable_dispatch();
107       /* Another thread could get dispatched here */
108
109       /* Reenter the critical sections so we can attempt the lock again. */
110       _Thread_Disable_dispatch();
111
112       _ISR_Disable( level );
113    }
114
115}
Note: See TracBrowser for help on using the repository browser.