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 <pthread.h> |
---|
17 | #include <errno.h> |
---|
18 | |
---|
19 | #include <rtems/system.h> |
---|
20 | #include <rtems/score/object.h> |
---|
21 | #include <rtems/score/states.h> |
---|
22 | #include <rtems/score/watchdog.h> |
---|
23 | #include <rtems/posix/cond.h> |
---|
24 | #include <rtems/posix/time.h> |
---|
25 | #include <rtems/posix/mutex.h> |
---|
26 | |
---|
27 | /*PAGE |
---|
28 | * |
---|
29 | * _POSIX_Condition_variables_Wait_support |
---|
30 | * |
---|
31 | * A support routine which implements guts of the blocking, non-blocking, and |
---|
32 | * timed wait version of condition variable wait routines. |
---|
33 | */ |
---|
34 | |
---|
35 | int _POSIX_Condition_variables_Wait_support( |
---|
36 | pthread_cond_t *cond, |
---|
37 | pthread_mutex_t *mutex, |
---|
38 | Watchdog_Interval timeout, |
---|
39 | bool already_timedout |
---|
40 | ) |
---|
41 | { |
---|
42 | register POSIX_Condition_variables_Control *the_cond; |
---|
43 | Objects_Locations location; |
---|
44 | int status; |
---|
45 | int mutex_status; |
---|
46 | |
---|
47 | if ( !_POSIX_Mutex_Get( mutex, &location ) ) { |
---|
48 | return EINVAL; |
---|
49 | } |
---|
50 | |
---|
51 | _Thread_Unnest_dispatch(); |
---|
52 | |
---|
53 | the_cond = _POSIX_Condition_variables_Get( cond, &location ); |
---|
54 | switch ( location ) { |
---|
55 | |
---|
56 | case OBJECTS_LOCAL: |
---|
57 | |
---|
58 | if ( the_cond->Mutex && ( the_cond->Mutex != *mutex ) ) { |
---|
59 | _Thread_Enable_dispatch(); |
---|
60 | return EINVAL; |
---|
61 | } |
---|
62 | |
---|
63 | (void) pthread_mutex_unlock( mutex ); |
---|
64 | /* XXX ignore this for now since behavior is undefined |
---|
65 | if ( mutex_status ) { |
---|
66 | _Thread_Enable_dispatch(); |
---|
67 | return EINVAL; |
---|
68 | } |
---|
69 | */ |
---|
70 | |
---|
71 | if ( !already_timedout ) { |
---|
72 | the_cond->Mutex = *mutex; |
---|
73 | |
---|
74 | _Thread_queue_Enter_critical_section( &the_cond->Wait_queue ); |
---|
75 | _Thread_Executing->Wait.return_code = 0; |
---|
76 | _Thread_Executing->Wait.queue = &the_cond->Wait_queue; |
---|
77 | _Thread_Executing->Wait.id = *cond; |
---|
78 | |
---|
79 | _Thread_queue_Enqueue( &the_cond->Wait_queue, timeout ); |
---|
80 | |
---|
81 | _Thread_Enable_dispatch(); |
---|
82 | |
---|
83 | /* |
---|
84 | * Switch ourself out because we blocked as a result of the |
---|
85 | * _Thread_queue_Enqueue. |
---|
86 | */ |
---|
87 | |
---|
88 | status = _Thread_Executing->Wait.return_code; |
---|
89 | if ( status && status != ETIMEDOUT ) |
---|
90 | return status; |
---|
91 | |
---|
92 | } else { |
---|
93 | _Thread_Enable_dispatch(); |
---|
94 | status = ETIMEDOUT; |
---|
95 | } |
---|
96 | |
---|
97 | /* |
---|
98 | * When we get here the dispatch disable level is 0. |
---|
99 | */ |
---|
100 | |
---|
101 | mutex_status = pthread_mutex_lock( mutex ); |
---|
102 | if ( mutex_status ) |
---|
103 | return EINVAL; |
---|
104 | |
---|
105 | return status; |
---|
106 | |
---|
107 | #if defined(RTEMS_MULTIPROCESSING) |
---|
108 | case OBJECTS_REMOTE: |
---|
109 | #endif |
---|
110 | case OBJECTS_ERROR: |
---|
111 | break; |
---|
112 | } |
---|
113 | |
---|
114 | return EINVAL; |
---|
115 | } |
---|