1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2007. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <assert.h> |
---|
17 | #include <errno.h> |
---|
18 | #include <pthread.h> |
---|
19 | |
---|
20 | #include <rtems/system.h> |
---|
21 | #include <rtems/score/coremutex.h> |
---|
22 | #include <rtems/score/watchdog.h> |
---|
23 | #include <rtems/posix/mutex.h> |
---|
24 | #include <rtems/posix/priority.h> |
---|
25 | #include <rtems/posix/time.h> |
---|
26 | |
---|
27 | /*PAGE |
---|
28 | * |
---|
29 | * 11.3.2 Initializing and Destroying a Mutex, P1003.1c/Draft 10, p. 87 |
---|
30 | * |
---|
31 | * NOTE: XXX Could be optimized so all the attribute error checking |
---|
32 | * is not performed when attr is NULL. |
---|
33 | */ |
---|
34 | |
---|
35 | int pthread_mutex_init( |
---|
36 | pthread_mutex_t *mutex, |
---|
37 | const pthread_mutexattr_t *attr |
---|
38 | ) |
---|
39 | { |
---|
40 | POSIX_Mutex_Control *the_mutex; |
---|
41 | CORE_mutex_Attributes *the_mutex_attr; |
---|
42 | const pthread_mutexattr_t *the_attr; |
---|
43 | CORE_mutex_Disciplines the_discipline; |
---|
44 | #if 0 |
---|
45 | register POSIX_Mutex_Control *mutex_in_use; |
---|
46 | Objects_Locations location; |
---|
47 | #endif |
---|
48 | |
---|
49 | if ( attr ) the_attr = attr; |
---|
50 | else the_attr = &_POSIX_Mutex_Default_attributes; |
---|
51 | |
---|
52 | /* Check for NULL mutex */ |
---|
53 | |
---|
54 | if ( !mutex ) |
---|
55 | return EINVAL; |
---|
56 | |
---|
57 | /* |
---|
58 | * This code should eventually be removed. |
---|
59 | * |
---|
60 | * Although the POSIX specification says: |
---|
61 | * |
---|
62 | * "Attempting to initialize an already initialized mutex results |
---|
63 | * in undefined behavior." |
---|
64 | * |
---|
65 | * Trying to keep the caller from doing the create when *mutex |
---|
66 | * is actually a valid ID causes grief. All it takes is the wrong |
---|
67 | * value in an uninitialized variable to make this fail. As best |
---|
68 | * I can tell, RTEMS was the only pthread implementation to choose |
---|
69 | * this option for "undefined behavior" and doing so has created |
---|
70 | * portability problems. In particular, Rosimildo DaSilva |
---|
71 | * <rdasilva@connecttel.com> saw seemingly random failures in the |
---|
72 | * RTEMS port of omniORB2 when this code was enabled. |
---|
73 | * |
---|
74 | * Joel Sherrill <joel@OARcorp.com> 14 May 1999 |
---|
75 | */ |
---|
76 | #if 0 |
---|
77 | /* avoid infinite recursion on call to this routine in _POSIX_Mutex_Get */ |
---|
78 | |
---|
79 | if ( *mutex != PTHREAD_MUTEX_INITIALIZER ) { |
---|
80 | |
---|
81 | /* EBUSY if *mutex is a valid id */ |
---|
82 | |
---|
83 | mutex_in_use = _POSIX_Mutex_Get( mutex, &location ); |
---|
84 | switch ( location ) { |
---|
85 | case OBJECTS_LOCAL: |
---|
86 | _Thread_Enable_dispatch(); |
---|
87 | return EBUSY; |
---|
88 | #if defined(RTEMS_MULTIPROCESSING) |
---|
89 | case OBJECTS_REMOTE: |
---|
90 | #endif |
---|
91 | case OBJECTS_ERROR: |
---|
92 | break; |
---|
93 | } |
---|
94 | } |
---|
95 | #endif |
---|
96 | |
---|
97 | if ( !the_attr->is_initialized ) |
---|
98 | return EINVAL; |
---|
99 | |
---|
100 | /* |
---|
101 | * XXX: Be careful about attributes when global!!! |
---|
102 | */ |
---|
103 | |
---|
104 | assert( the_attr->process_shared == PTHREAD_PROCESS_PRIVATE ); |
---|
105 | |
---|
106 | if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED ) |
---|
107 | return ENOSYS; |
---|
108 | |
---|
109 | /* |
---|
110 | * Determine the discipline of the mutex |
---|
111 | */ |
---|
112 | |
---|
113 | switch ( the_attr->protocol ) { |
---|
114 | case PTHREAD_PRIO_NONE: |
---|
115 | the_discipline = CORE_MUTEX_DISCIPLINES_FIFO; |
---|
116 | break; |
---|
117 | case PTHREAD_PRIO_INHERIT: |
---|
118 | the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT; |
---|
119 | break; |
---|
120 | case PTHREAD_PRIO_PROTECT: |
---|
121 | the_discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING; |
---|
122 | break; |
---|
123 | default: |
---|
124 | return EINVAL; |
---|
125 | } |
---|
126 | |
---|
127 | if ( !_POSIX_Priority_Is_valid( the_attr->prio_ceiling ) ) |
---|
128 | return EINVAL; |
---|
129 | |
---|
130 | _Thread_Disable_dispatch(); |
---|
131 | |
---|
132 | the_mutex = _POSIX_Mutex_Allocate(); |
---|
133 | |
---|
134 | if ( !the_mutex ) { |
---|
135 | _Thread_Enable_dispatch(); |
---|
136 | return EAGAIN; |
---|
137 | } |
---|
138 | |
---|
139 | the_mutex->process_shared = the_attr->process_shared; |
---|
140 | |
---|
141 | the_mutex_attr = &the_mutex->Mutex.Attributes; |
---|
142 | |
---|
143 | if ( the_attr->recursive ) |
---|
144 | the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES; |
---|
145 | else |
---|
146 | the_mutex_attr->lock_nesting_behavior = CORE_MUTEX_NESTING_IS_ERROR; |
---|
147 | the_mutex_attr->only_owner_release = true; |
---|
148 | the_mutex_attr->priority_ceiling = |
---|
149 | _POSIX_Priority_To_core( the_attr->prio_ceiling ); |
---|
150 | the_mutex_attr->discipline = the_discipline; |
---|
151 | |
---|
152 | /* |
---|
153 | * Must be initialized to unlocked. |
---|
154 | */ |
---|
155 | |
---|
156 | _CORE_mutex_Initialize( |
---|
157 | &the_mutex->Mutex, |
---|
158 | the_mutex_attr, |
---|
159 | CORE_MUTEX_UNLOCKED |
---|
160 | ); |
---|
161 | |
---|
162 | _Objects_Open_u32( &_POSIX_Mutex_Information, &the_mutex->Object, 0 ); |
---|
163 | |
---|
164 | *mutex = the_mutex->Object.id; |
---|
165 | |
---|
166 | _Thread_Enable_dispatch(); |
---|
167 | return 0; |
---|
168 | } |
---|