1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief POSIX Condition Variables Wait Support |
---|
5 | * @ingroup POSIX_COND_VARS |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2007. |
---|
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 <pthread.h> |
---|
22 | #include <errno.h> |
---|
23 | |
---|
24 | #include <rtems/system.h> |
---|
25 | #include <rtems/score/object.h> |
---|
26 | #include <rtems/score/states.h> |
---|
27 | #include <rtems/score/watchdog.h> |
---|
28 | #include <rtems/posix/cond.h> |
---|
29 | #include <rtems/posix/time.h> |
---|
30 | #include <rtems/posix/mutex.h> |
---|
31 | |
---|
32 | int _POSIX_Condition_variables_Wait_support( |
---|
33 | pthread_cond_t *cond, |
---|
34 | pthread_mutex_t *mutex, |
---|
35 | Watchdog_Interval timeout, |
---|
36 | bool already_timedout |
---|
37 | ) |
---|
38 | { |
---|
39 | register POSIX_Condition_variables_Control *the_cond; |
---|
40 | POSIX_Mutex_Control *the_mutex; |
---|
41 | Objects_Locations location; |
---|
42 | int status; |
---|
43 | int mutex_status; |
---|
44 | |
---|
45 | the_mutex = _POSIX_Mutex_Get( mutex, &location ); |
---|
46 | if ( !the_mutex ) { |
---|
47 | return EINVAL; |
---|
48 | } |
---|
49 | |
---|
50 | _Objects_Put_without_thread_dispatch( &the_mutex->Object ); |
---|
51 | |
---|
52 | the_cond = _POSIX_Condition_variables_Get( cond, &location ); |
---|
53 | switch ( location ) { |
---|
54 | |
---|
55 | case OBJECTS_LOCAL: |
---|
56 | |
---|
57 | if ( the_cond->Mutex && ( the_cond->Mutex != *mutex ) ) { |
---|
58 | _Objects_Put( &the_cond->Object ); |
---|
59 | return EINVAL; |
---|
60 | } |
---|
61 | |
---|
62 | (void) pthread_mutex_unlock( mutex ); |
---|
63 | /* XXX ignore this for now since behavior is undefined |
---|
64 | if ( mutex_status ) { |
---|
65 | _Objects_Put( &the_cond->Object ); |
---|
66 | return EINVAL; |
---|
67 | } |
---|
68 | */ |
---|
69 | |
---|
70 | if ( !already_timedout ) { |
---|
71 | the_cond->Mutex = *mutex; |
---|
72 | |
---|
73 | _Thread_queue_Enter_critical_section( &the_cond->Wait_queue ); |
---|
74 | _Thread_Executing->Wait.return_code = 0; |
---|
75 | _Thread_Executing->Wait.queue = &the_cond->Wait_queue; |
---|
76 | _Thread_Executing->Wait.id = *cond; |
---|
77 | |
---|
78 | _Thread_queue_Enqueue( &the_cond->Wait_queue, timeout ); |
---|
79 | |
---|
80 | _Objects_Put( &the_cond->Object ); |
---|
81 | |
---|
82 | /* |
---|
83 | * Switch ourself out because we blocked as a result of the |
---|
84 | * _Thread_queue_Enqueue. |
---|
85 | */ |
---|
86 | |
---|
87 | /* |
---|
88 | * If the thread is interrupted, while in the thread queue, by |
---|
89 | * a POSIX signal, then pthread_cond_wait returns spuriously, |
---|
90 | * according to the POSIX standard. It means that pthread_cond_wait |
---|
91 | * returns a success status, except for the fact that it was not |
---|
92 | * woken up a pthread_cond_signal or a pthread_cond_broadcast. |
---|
93 | */ |
---|
94 | status = _Thread_Executing->Wait.return_code; |
---|
95 | if ( status == EINTR ) |
---|
96 | status = 0; |
---|
97 | |
---|
98 | } else { |
---|
99 | _Objects_Put( &the_cond->Object ); |
---|
100 | status = ETIMEDOUT; |
---|
101 | } |
---|
102 | |
---|
103 | /* |
---|
104 | * When we get here the dispatch disable level is 0. |
---|
105 | */ |
---|
106 | |
---|
107 | mutex_status = pthread_mutex_lock( mutex ); |
---|
108 | if ( mutex_status ) |
---|
109 | return EINVAL; |
---|
110 | |
---|
111 | return status; |
---|
112 | |
---|
113 | #if defined(RTEMS_MULTIPROCESSING) |
---|
114 | case OBJECTS_REMOTE: |
---|
115 | #endif |
---|
116 | case OBJECTS_ERROR: |
---|
117 | break; |
---|
118 | } |
---|
119 | |
---|
120 | return EINVAL; |
---|
121 | } |
---|