source: rtems/c/src/exec/posix/src/cond.c @ 95b76bc

4.104.114.84.95
Last change on this file since 95b76bc was 95b76bc, checked in by Mark Johannes <Mark.Johannes@…>, on 08/13/96 at 21:10:06

pthread_codn_destroy: EBUSY case moved and uses Thread_queue_First

  • Property mode set to 100644
File size: 10.0 KB
Line 
1/*
2 *  $Id$
3 */
4
5#include <pthread.h>
6#include <errno.h>
7
8#include <rtems/system.h>
9#include <rtems/score/object.h>
10#include <rtems/score/states.h>
11#include <rtems/score/watchdog.h>
12#include <rtems/posix/cond.h>
13#include <rtems/posix/time.h>
14#include <rtems/posix/mutex.h>
15
16/*
17 *  TEMPORARY
18 */
19
20void _POSIX_Condition_variables_MP_Send_process_packet (
21  POSIX_Condition_variables_MP_Remote_operations  operation,
22  Objects_Id                        condition_variables_id,
23  Objects_Name                      name,
24  Objects_Id                        proxy_id
25)
26{
27  (void) POSIX_MP_NOT_IMPLEMENTED();
28}
29
30void _POSIX_Condition_variables_MP_Send_extract_proxy(
31  Thread_Control *the_thread
32)
33{
34  (void) POSIX_MP_NOT_IMPLEMENTED();
35}
36
37/*
38 *  END OF TEMPORARY
39 */
40
41/*PAGE
42 *
43 *  The default condition variable attributes structure.
44 */
45 
46const pthread_condattr_t _POSIX_Condition_variables_Default_attributes = {
47  TRUE,                      /* is_initialized */
48  PTHREAD_PROCESS_PRIVATE    /* process_shared */
49};
50 
51/*PAGE
52 *
53 *  _POSIX_Condition_variables_Manager_initialization
54 *
55 *  This routine initializes all condition variable manager related data
56 *  structures.
57 *
58 *  Input parameters:
59 *    maximum_condition_variables - maximum configured condition_variables
60 *
61 *  Output parameters:  NONE
62 */
63 
64void _POSIX_Condition_variables_Manager_initialization(
65  unsigned32 maximum_condition_variables
66)
67{
68  _Objects_Initialize_information(
69    &_POSIX_Condition_variables_Information,
70    OBJECTS_POSIX_CONDITION_VARIABLES,
71    TRUE,
72    maximum_condition_variables,
73    sizeof( POSIX_Condition_variables_Control ),
74    FALSE,
75    0,
76    FALSE
77  );
78}
79
80/*PAGE
81 *
82 *  11.4.1 Condition Variable Initialization Attributes,
83 *            P1003.1c/Draft 10, p. 96
84 */
85 
86int pthread_condattr_init(
87  pthread_condattr_t *attr
88)
89{
90  if ( !attr )
91    return EINVAL;
92
93  *attr = _POSIX_Condition_variables_Default_attributes;
94  return 0;
95}
96 
97/*PAGE
98 *
99 *  11.4.1 Condition Variable Initialization Attributes,
100 *            P1003.1c/Draft 10, p. 96
101 */
102 
103int pthread_condattr_destroy(
104  pthread_condattr_t *attr
105)
106{
107  if ( !attr || attr->is_initialized == FALSE )
108    return EINVAL;
109
110  attr->is_initialized = FALSE;
111  return 0;
112}
113 
114/*PAGE
115 *
116 *  11.4.1 Condition Variable Initialization Attributes,
117 *            P1003.1c/Draft 10, p. 96
118 */
119 
120int pthread_condattr_getpshared(
121  const pthread_condattr_t *attr,
122  int                      *pshared
123)
124{
125  if ( !attr )
126    return EINVAL;
127
128  *pshared = attr->process_shared;
129  return 0;
130}
131 
132/*PAGE
133 *
134 *  11.4.1 Condition Variable Initialization Attributes,
135 *            P1003.1c/Draft 10, p. 96
136 */
137 
138int pthread_condattr_setpshared(
139  pthread_condattr_t *attr,
140  int                 pshared
141)
142{
143  if ( !attr )
144    return EINVAL;
145
146  switch ( pshared ) {
147    case PTHREAD_PROCESS_SHARED:
148    case PTHREAD_PROCESS_PRIVATE:
149      attr->process_shared = pshared;
150      return 0;
151
152    default:
153      return EINVAL;
154  }
155}
156 
157/*PAGE
158 *
159 *  11.4.2 Initializing and Destroying a Condition Variable,
160 *         P1003.1c/Draft 10, p. 87
161 */
162 
163int pthread_cond_init(
164  pthread_cond_t           *cond,
165  const pthread_condattr_t *attr
166)
167{
168  POSIX_Condition_variables_Control   *the_cond;
169  const pthread_condattr_t            *the_attr;
170 
171  if ( attr ) the_attr = attr;
172  else        the_attr = &_POSIX_Condition_variables_Default_attributes;
173 
174  /*
175   *  XXX: Be careful about attributes when global!!!
176   */
177 
178  if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED )
179    return POSIX_MP_NOT_IMPLEMENTED();
180 
181  if ( !the_attr->is_initialized )
182    return EINVAL;
183 
184  _Thread_Disable_dispatch();
185 
186  the_cond = _POSIX_Condition_variables_Allocate();
187 
188  if ( !the_cond ) {
189    _Thread_Enable_dispatch();
190    return ENOMEM;
191  }
192 
193  if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED &&
194     !( _Objects_MP_Allocate_and_open( &_POSIX_Condition_variables_Information,
195                0, the_cond->Object.id, FALSE ) ) ) {
196    _POSIX_Condition_variables_Free( the_cond );
197    _Thread_Enable_dispatch();
198    return EAGAIN;
199  }
200 
201  the_cond->process_shared  = the_attr->process_shared;
202
203  the_cond->Mutex = POSIX_CONDITION_VARIABLES_NO_MUTEX;
204
205/* XXX some more initialization might need to go here */
206  _Thread_queue_Initialize(
207    &the_cond->Wait_queue,
208    OBJECTS_POSIX_CONDITION_VARIABLES,
209    THREAD_QUEUE_DISCIPLINE_FIFO,
210    STATES_WAITING_FOR_CONDITION_VARIABLE,
211    _POSIX_Condition_variables_MP_Send_extract_proxy,
212    ETIMEDOUT
213  );
214
215  _Objects_Open(
216    &_POSIX_Condition_variables_Information,
217    &the_cond->Object,
218    0
219  );
220 
221  *cond = the_cond->Object.id;
222 
223  if ( the_attr->process_shared == PTHREAD_PROCESS_SHARED )
224    _POSIX_Condition_variables_MP_Send_process_packet(
225      POSIX_CONDITION_VARIABLES_MP_ANNOUNCE_CREATE,
226      the_cond->Object.id,
227      0,                         /* Name not used */
228      0                          /* Not used */
229    );
230 
231  _Thread_Enable_dispatch();
232
233  return 0;
234}
235 
236/*PAGE
237 *
238 *  11.4.2 Initializing and Destroying a Condition Variable,
239 *         P1003.1c/Draft 10, p. 87
240 */
241 
242int pthread_cond_destroy(
243  pthread_cond_t           *cond
244)
245{
246  register POSIX_Condition_variables_Control *the_cond;
247  Objects_Locations                           location;
248 
249  the_cond = _POSIX_Condition_variables_Get( cond, &location );
250  switch ( location ) {
251    case OBJECTS_ERROR:
252      return EINVAL;
253    case OBJECTS_REMOTE:
254      _Thread_Dispatch();
255      return POSIX_MP_NOT_IMPLEMENTED();
256      return EINVAL;
257    case OBJECTS_LOCAL:
258 
259      if ( _Thread_queue_First( &the_cond->Wait_queue ) )
260        return EBUSY;
261 
262      _Objects_Close(
263        &_POSIX_Condition_variables_Information,
264        &the_cond->Object
265      );
266 
267      _POSIX_Condition_variables_Free( the_cond );
268 
269      if ( the_cond->process_shared == PTHREAD_PROCESS_SHARED ) {
270 
271        _Objects_MP_Close(
272          &_POSIX_Condition_variables_Information,
273          the_cond->Object.id
274        );
275 
276        _POSIX_Condition_variables_MP_Send_process_packet(
277          POSIX_CONDITION_VARIABLES_MP_ANNOUNCE_DELETE,
278          the_cond->Object.id,
279          0,                         /* Not used */
280          0                          /* Not used */
281        );
282      }
283      _Thread_Enable_dispatch();
284      return 0;
285  }
286  return POSIX_BOTTOM_REACHED();
287}
288 
289/*PAGE
290 *
291 *  _POSIX_Condition_variables_Signal_support
292 *
293 *  A support routine which implements guts of the broadcast and single task
294 *  wake up version of the "signal" operation.
295 */
296 
297int _POSIX_Condition_variables_Signal_support(
298  pthread_cond_t            *cond,
299  boolean                    is_broadcast
300)
301{
302  register POSIX_Condition_variables_Control *the_cond;
303  Objects_Locations                           location;
304  Thread_Control                             *the_thread;
305 
306  the_cond = _POSIX_Condition_variables_Get( cond, &location );
307  switch ( location ) {
308    case OBJECTS_ERROR:
309      return EINVAL;
310    case OBJECTS_REMOTE:
311      _Thread_Dispatch();
312      return POSIX_MP_NOT_IMPLEMENTED();
313      return EINVAL;
314    case OBJECTS_LOCAL:
315 
316      do {
317        the_thread = _Thread_queue_Dequeue( &the_cond->Wait_queue );
318        if ( !the_thread )
319          the_cond->Mutex = POSIX_CONDITION_VARIABLES_NO_MUTEX;
320      } while ( is_broadcast && the_thread );
321
322      _Thread_Enable_dispatch();
323
324      return 0;
325  }
326  return POSIX_BOTTOM_REACHED();
327}
328
329/*PAGE
330 *
331 *  11.4.3 Broadcasting and Signaling a Condition, P1003.1c/Draft 10, p. 101
332 */
333 
334int pthread_cond_signal(
335  pthread_cond_t   *cond
336)
337{
338  return _POSIX_Condition_variables_Signal_support( cond, FALSE );
339}
340 
341/*PAGE
342 *
343 *  11.4.3 Broadcasting and Signaling a Condition, P1003.1c/Draft 10, p. 101
344 */
345 
346int pthread_cond_broadcast(
347  pthread_cond_t   *cond
348)
349{
350  return _POSIX_Condition_variables_Signal_support( cond, TRUE );
351}
352 
353/*PAGE
354 *
355 *  _POSIX_Condition_variables_Wait_support
356 *
357 *  A support routine which implements guts of the blocking, non-blocking, and
358 *  timed wait version of condition variable wait routines.
359 */
360 
361int _POSIX_Condition_variables_Wait_support(
362  pthread_cond_t            *cond,
363  pthread_mutex_t           *mutex,
364  Watchdog_Interval          timeout
365)
366{
367  register POSIX_Condition_variables_Control *the_cond;
368  Objects_Locations                           location;
369  int                                         status;
370 
371  the_cond = _POSIX_Condition_variables_Get( cond, &location );
372  switch ( location ) {
373    case OBJECTS_ERROR:
374      return EINVAL;
375    case OBJECTS_REMOTE:
376      _Thread_Dispatch();
377      return POSIX_MP_NOT_IMPLEMENTED();
378      return EINVAL;
379    case OBJECTS_LOCAL:
380 
381      if ( the_cond->Mutex && ( the_cond->Mutex != *mutex ) ) {
382        _Thread_Enable_dispatch();
383        return EINVAL;
384      }
385 
386      the_cond->Mutex = *mutex;
387 
388      status = pthread_mutex_unlock( mutex );
389      if ( status ) {
390        _Thread_Enable_dispatch();
391        return EINVAL;
392      }
393 
394      _Thread_queue_Enter_critical_section( &the_cond->Wait_queue );
395      _Thread_Executing->Wait.return_code = 0;
396      _Thread_Executing->Wait.queue       = &the_cond->Wait_queue;
397      _Thread_Executing->Wait.id          = *cond;
398
399      _Thread_queue_Enqueue( &the_cond->Wait_queue, timeout );
400
401      _Thread_Enable_dispatch();
402
403      /*
404       *  Switch ourself out because we blocked as a result of the
405       *  _Thread_queue_Enqueue.
406       */
407
408      if ( _Thread_Executing->Wait.return_code )
409        return _Thread_Executing->Wait.return_code;
410
411      status = pthread_mutex_lock( mutex );
412      if ( status )
413        return EINVAL;
414   
415      return _Thread_Executing->Wait.return_code;
416  }
417  return POSIX_BOTTOM_REACHED();
418}
419
420/*PAGE
421 *
422 *  11.4.4 Waiting on a Condition, P1003.1c/Draft 10, p. 105
423 */
424 
425int pthread_cond_wait(
426  pthread_cond_t     *cond,
427  pthread_mutex_t    *mutex
428)
429{
430  return _POSIX_Condition_variables_Wait_support(
431    cond,
432    mutex,
433    THREAD_QUEUE_WAIT_FOREVER
434  );
435}
436 
437/*PAGE
438 *
439 *  11.4.4 Waiting on a Condition, P1003.1c/Draft 10, p. 105
440 */
441 
442int pthread_cond_timedwait(
443  pthread_cond_t        *cond,
444  pthread_mutex_t       *mutex,
445  const struct timespec *abstime
446)
447{
448  return _POSIX_Condition_variables_Wait_support(
449    cond,
450    mutex,
451    _POSIX_Timespec_to_interval( abstime )
452  );
453}
Note: See TracBrowser for help on using the repository browser.