1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Initialize a Core Mutex |
---|
5 | * @ingroup ScoreMutex |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-1999. |
---|
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.com/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/isr.h> |
---|
23 | #include <rtems/score/coremutex.h> |
---|
24 | #include <rtems/score/states.h> |
---|
25 | #include <rtems/score/thread.h> |
---|
26 | #include <rtems/score/threadq.h> |
---|
27 | |
---|
28 | CORE_mutex_Status _CORE_mutex_Initialize( |
---|
29 | CORE_mutex_Control *the_mutex, |
---|
30 | CORE_mutex_Attributes *the_mutex_attributes, |
---|
31 | uint32_t initial_lock |
---|
32 | ) |
---|
33 | { |
---|
34 | |
---|
35 | /* Add this to the RTEMS environment later ????????? |
---|
36 | rtems_assert( initial_lock == CORE_MUTEX_LOCKED || |
---|
37 | initial_lock == CORE_MUTEX_UNLOCKED ); |
---|
38 | */ |
---|
39 | |
---|
40 | the_mutex->Attributes = *the_mutex_attributes; |
---|
41 | the_mutex->lock = initial_lock; |
---|
42 | the_mutex->blocked_count = 0; |
---|
43 | |
---|
44 | if ( initial_lock == CORE_MUTEX_LOCKED ) { |
---|
45 | the_mutex->nest_count = 1; |
---|
46 | the_mutex->holder = _Thread_Executing; |
---|
47 | the_mutex->holder_id = _Thread_Executing->Object.id; |
---|
48 | if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) || |
---|
49 | _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) { |
---|
50 | |
---|
51 | if ( _Thread_Executing->current_priority < |
---|
52 | the_mutex->Attributes.priority_ceiling ) |
---|
53 | return CORE_MUTEX_STATUS_CEILING_VIOLATED; |
---|
54 | #ifdef __RTEMS_STRICT_ORDER_MUTEX__ |
---|
55 | _Chain_Prepend_unprotected( &_Thread_Executing->lock_mutex, |
---|
56 | &the_mutex->queue.lock_queue ); |
---|
57 | the_mutex->queue.priority_before = _Thread_Executing->current_priority; |
---|
58 | #endif |
---|
59 | |
---|
60 | _Thread_Executing->resource_count++; |
---|
61 | } |
---|
62 | } else { |
---|
63 | the_mutex->nest_count = 0; |
---|
64 | the_mutex->holder = NULL; |
---|
65 | the_mutex->holder_id = 0; |
---|
66 | } |
---|
67 | |
---|
68 | _Thread_queue_Initialize( |
---|
69 | &the_mutex->Wait_queue, |
---|
70 | _CORE_mutex_Is_fifo( the_mutex_attributes ) ? |
---|
71 | THREAD_QUEUE_DISCIPLINE_FIFO : THREAD_QUEUE_DISCIPLINE_PRIORITY, |
---|
72 | STATES_WAITING_FOR_MUTEX, |
---|
73 | CORE_MUTEX_TIMEOUT |
---|
74 | ); |
---|
75 | |
---|
76 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
77 | } |
---|