summaryrefslogtreecommitdiffstats
path: root/c/src/exec/score/src/coremutex.c
blob: dac5a5172b036f05319f86976862400cb5eb0f53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 *  Mutex Handler
 *
 *  DESCRIPTION:
 *
 *  This package is the implementation of the Mutex Handler.
 *  This handler provides synchronization and mutual exclusion capabilities.
 *
 *  COPYRIGHT (c) 1989-1998.
 *  On-Line Applications Research Corporation (OAR).
 *  Copyright assigned to U.S. Government, 1994.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $Id$
 */

#include <rtems/system.h>
#include <rtems/score/isr.h>
#include <rtems/score/coremutex.h>
#include <rtems/score/states.h>
#include <rtems/score/thread.h>
#include <rtems/score/threadq.h>

/*PAGE
 *
 *  _CORE_mutex_Initialize
 *
 *  This routine initializes a mutex at create time and set the control
 *  structure according to the values passed.
 *
 *  Input parameters: 
 *    the_mutex             - the mutex control block to initialize
 *    the_class             - the API class of the object
 *    the_mutex_attributes  - the mutex attributes specified at create time
 *    initial_lock          - mutex initial lock or unlocked status
 *    proxy_extract_callout - MP specific extract callout
 *
 *  Output parameters:  NONE
 */

void _CORE_mutex_Initialize(
  CORE_mutex_Control           *the_mutex,
  Objects_Classes               the_class,
  CORE_mutex_Attributes        *the_mutex_attributes,
  unsigned32                    initial_lock,
  Thread_queue_Extract_callout  proxy_extract_callout
)
{

/* Add this to the RTEMS environment later ????????? 
  rtems_assert( initial_lock == CORE_MUTEX_LOCKED ||
                initial_lock == CORE_MUTEX_UNLOCKED );
 */

  the_mutex->Attributes = *the_mutex_attributes;
  the_mutex->lock          = initial_lock;

  if ( initial_lock == CORE_MUTEX_LOCKED ) {
    the_mutex->nest_count = 1;
    the_mutex->holder     = _Thread_Executing;
    the_mutex->holder_id  = _Thread_Executing->Object.id;
    _Thread_Executing->resource_count++;
  } else {
    the_mutex->nest_count = 0;
    the_mutex->holder     = NULL;
    the_mutex->holder_id  = 0;
  }

  _Thread_queue_Initialize(
    &the_mutex->Wait_queue,
    the_class,
    _CORE_mutex_Is_fifo( the_mutex_attributes ) ?
      THREAD_QUEUE_DISCIPLINE_FIFO : THREAD_QUEUE_DISCIPLINE_PRIORITY,
    STATES_WAITING_FOR_MUTEX,
    proxy_extract_callout,
    CORE_MUTEX_TIMEOUT
  );
}

/*PAGE
 *
 *  _CORE_mutex_Seize
 *
 *  This routine attempts to allocate a mutex to the calling thread.
 *
 *  Input parameters:
 *    the_mutex - pointer to mutex control block
 *    id        - id of object to wait on
 *    wait      - TRUE if wait is allowed, FALSE otherwise
 *    timeout   - number of ticks to wait (0 means forever)
 *
 *  Output parameters:  NONE
 *
 *  INTERRUPT LATENCY:
 *    available
 *    wait
 */

void _CORE_mutex_Seize(
  CORE_mutex_Control  *the_mutex,
  Objects_Id           id,
  boolean              wait,
  Watchdog_Interval    timeout
)
{
  Thread_Control *executing;
  ISR_Level       level;

  executing = _Thread_Executing;
  switch ( the_mutex->Attributes.discipline ) {
    case CORE_MUTEX_DISCIPLINES_FIFO:
    case CORE_MUTEX_DISCIPLINES_PRIORITY:
    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
      break;
    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
      if ( executing->current_priority <
                              the_mutex->Attributes.priority_ceiling) {
        executing->Wait.return_code = CORE_MUTEX_STATUS_CEILING_VIOLATED;
        return;
      }
  }
  executing->Wait.return_code = CORE_MUTEX_STATUS_SUCCESSFUL;
  _ISR_Disable( level );
  if ( ! _CORE_mutex_Is_locked( the_mutex ) ) {
    the_mutex->lock       = CORE_MUTEX_LOCKED;
    the_mutex->holder     = executing;
    the_mutex->holder_id  = executing->Object.id;
    the_mutex->nest_count = 1;
    executing->resource_count++;
    _ISR_Enable( level );
    switch ( the_mutex->Attributes.discipline ) {
      case CORE_MUTEX_DISCIPLINES_FIFO:
      case CORE_MUTEX_DISCIPLINES_PRIORITY:
      case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
        /* already the highest priority */
        break;
      case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
      if ( the_mutex->Attributes.priority_ceiling <
                                           executing->current_priority ) {
        _Thread_Change_priority(
          the_mutex->holder,
          the_mutex->Attributes.priority_ceiling,
          FALSE
        );
      }
    }
    return;
  }

  if ( _Objects_Are_ids_equal(
              _Thread_Executing->Object.id, the_mutex->holder_id ) ) {
    if ( _CORE_mutex_Is_nesting_allowed( &the_mutex->Attributes ) )
      the_mutex->nest_count++;
    else 
      executing->Wait.return_code = CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED;

    _ISR_Enable( level );
    return;
  }

  if ( !wait ) {
    _ISR_Enable( level );
    executing->Wait.return_code = CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT;
    return;
  }

  _Thread_queue_Enter_critical_section( &the_mutex->Wait_queue );
  executing->Wait.queue = &the_mutex->Wait_queue;
  executing->Wait.id    = id;
  _ISR_Enable( level );

  switch ( the_mutex->Attributes.discipline ) {
    case CORE_MUTEX_DISCIPLINES_FIFO:
    case CORE_MUTEX_DISCIPLINES_PRIORITY:
    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
      break;
    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
      if ( the_mutex->holder->current_priority > executing->current_priority ) {
        _Thread_Change_priority(
          the_mutex->holder,
          executing->current_priority,
          FALSE
        );
      }
      break;
  }

  _Thread_queue_Enqueue( &the_mutex->Wait_queue, timeout );

  if ( _Thread_Executing->Wait.return_code == CORE_MUTEX_STATUS_SUCCESSFUL ) {
    switch ( the_mutex->Attributes.discipline ) {
      case CORE_MUTEX_DISCIPLINES_FIFO:
      case CORE_MUTEX_DISCIPLINES_PRIORITY:
      case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
        break;
      case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
        if ( the_mutex->Attributes.priority_ceiling <
                                           executing->current_priority ) {
          _Thread_Change_priority(
            executing,
            the_mutex->Attributes.priority_ceiling,
            FALSE
          );
        };
        break;
    }
  }
}

/*
 *  _CORE_mutex_Surrender
 *
 *  DESCRIPTION:
 *
 *  This routine frees a unit to the mutex.  If a task was blocked waiting for
 *  a unit from this mutex, then that task will be readied and the unit
 *  given to that task.  Otherwise, the unit will be returned to the mutex.
 *
 *  Input parameters:
 *    the_mutex            - the mutex to be flushed
 *    id                   - id of parent mutex
 *    api_mutex_mp_support - api dependent MP support actions
 *
 *  Output parameters:
 *    CORE_MUTEX_STATUS_SUCCESSFUL - if successful
 *    core error code              - if unsuccessful
 */

CORE_mutex_Status _CORE_mutex_Surrender(
  CORE_mutex_Control                *the_mutex,
  Objects_Id                         id,
  CORE_mutex_API_mp_support_callout  api_mutex_mp_support
)
{
  Thread_Control *the_thread;
  Thread_Control *executing;

  executing = _Thread_Executing;

  /*
   *  The following code allows a thread (or ISR) other than the thread
   *  which acquired the mutex to release that mutex.  This is only
   *  allowed when the mutex in quetion is FIFO or simple Priority
   *  discipline.  But Priority Ceiling or Priority Inheritance mutexes
   *  must be released by the thread which acquired them.
   */ 

  if ( !_Objects_Are_ids_equal(
           _Thread_Executing->Object.id, the_mutex->holder_id ) ) {

    switch ( the_mutex->Attributes.discipline ) {
      case CORE_MUTEX_DISCIPLINES_FIFO:
      case CORE_MUTEX_DISCIPLINES_PRIORITY:
        break;
      case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
      case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
        return( CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE );
        break;
    }
  }

  the_mutex->nest_count--;

  if ( the_mutex->nest_count != 0 )
    return( CORE_MUTEX_STATUS_SUCCESSFUL );

  _Thread_Executing->resource_count--;
  the_mutex->holder    = NULL;
  the_mutex->holder_id = 0;

  /*
   *  Whether or not someone is waiting for the mutex, an
   *  inherited priority must be lowered if this is the last
   *  mutex (i.e. resource) this task has.
   */

  switch ( the_mutex->Attributes.discipline ) {
    case CORE_MUTEX_DISCIPLINES_FIFO:
    case CORE_MUTEX_DISCIPLINES_PRIORITY:
      break;
    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
      if ( executing->resource_count == 0 && 
           executing->real_priority != executing->current_priority ) {
         _Thread_Change_priority( executing, executing->real_priority, TRUE );
      }
      break;
  }


  if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) {

    if ( !_Objects_Is_local_id( the_thread->Object.id ) ) {
      
      the_mutex->holder     = NULL;
      the_mutex->holder_id  = the_thread->Object.id;
      the_mutex->nest_count = 1;

      ( *api_mutex_mp_support)( the_thread, id );

    } else {

      the_mutex->holder     = the_thread;
      the_mutex->holder_id  = the_thread->Object.id;
      the_thread->resource_count++;
      the_mutex->nest_count = 1;

     /*
      *  No special action for priority inheritance or priority ceiling
      *  because the_thread is guaranteed to be the highest priority
      *  thread waiting for the mutex.
      */
    }
  } else
    the_mutex->lock = CORE_MUTEX_UNLOCKED;

  return( CORE_MUTEX_STATUS_SUCCESSFUL );
}

/*PAGE
 *
 *  _CORE_mutex_Flush
 *
 *  This function a flushes the mutex's task wait queue.
 *
 *  Input parameters:
 *    the_mutex              - the mutex to be flushed
 *    remote_extract_callout - function to invoke remotely
 *    status                 - status to pass to thread
 *
 *  Output parameters:  NONE
 */

void _CORE_mutex_Flush(
  CORE_mutex_Control         *the_mutex,
  Thread_queue_Flush_callout  remote_extract_callout,
  unsigned32                  status
)
{
  _Thread_queue_Flush(
    &the_mutex->Wait_queue,
    remote_extract_callout,
    status
  );
}