1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * This include file contains the static inline implementation of the private |
---|
5 | * inlined routines for POSIX condition variables. |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2013. |
---|
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.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef _RTEMS_POSIX_CONDIMPL_H |
---|
18 | #define _RTEMS_POSIX_CONDIMPL_H |
---|
19 | |
---|
20 | #include <errno.h> |
---|
21 | #include <pthread.h> |
---|
22 | |
---|
23 | #include <rtems/score/percpu.h> |
---|
24 | #include <rtems/score/threadqimpl.h> |
---|
25 | |
---|
26 | #ifdef __cplusplus |
---|
27 | extern "C" { |
---|
28 | #endif |
---|
29 | |
---|
30 | typedef struct { |
---|
31 | unsigned long flags; |
---|
32 | Thread_queue_Syslock_queue Queue; |
---|
33 | pthread_mutex_t *mutex; |
---|
34 | } POSIX_Condition_variables_Control; |
---|
35 | |
---|
36 | #define POSIX_CONDITION_VARIABLES_CLOCK_MONOTONIC 0x1UL |
---|
37 | |
---|
38 | #define POSIX_CONDITION_VARIABLES_FLAGS_MASK 0x1UL |
---|
39 | |
---|
40 | #define POSIX_CONDITION_VARIABLES_MAGIC 0x18dfb1feUL |
---|
41 | |
---|
42 | /** |
---|
43 | * Constant to indicate condition variable does not currently have |
---|
44 | * a mutex assigned to it. |
---|
45 | */ |
---|
46 | #define POSIX_CONDITION_VARIABLES_NO_MUTEX NULL |
---|
47 | |
---|
48 | #define POSIX_CONDITION_VARIABLES_TQ_OPERATIONS &_Thread_queue_Operations_FIFO |
---|
49 | |
---|
50 | #define POSIX_CONDITION_VARIABLE_OF_THREAD_QUEUE_QUEUE( queue ) \ |
---|
51 | RTEMS_CONTAINER_OF( \ |
---|
52 | queue, POSIX_Condition_variables_Control, Queue.Queue ) |
---|
53 | |
---|
54 | /** |
---|
55 | * The default condition variable attributes structure. |
---|
56 | */ |
---|
57 | extern const pthread_condattr_t _POSIX_Condition_variables_Default_attributes; |
---|
58 | |
---|
59 | static inline POSIX_Condition_variables_Control *_POSIX_Condition_variables_Get( |
---|
60 | pthread_cond_t *cond |
---|
61 | ) |
---|
62 | { |
---|
63 | return (POSIX_Condition_variables_Control *) cond; |
---|
64 | } |
---|
65 | |
---|
66 | RTEMS_INLINE_ROUTINE void _POSIX_Condition_variables_Initialize( |
---|
67 | POSIX_Condition_variables_Control *the_cond, |
---|
68 | const pthread_condattr_t *the_attr |
---|
69 | ) |
---|
70 | { |
---|
71 | unsigned long flags; |
---|
72 | |
---|
73 | _Thread_queue_Queue_initialize( &the_cond->Queue.Queue, NULL ); |
---|
74 | the_cond->mutex = POSIX_CONDITION_VARIABLES_NO_MUTEX; |
---|
75 | |
---|
76 | flags = (uintptr_t) the_cond ^ POSIX_CONDITION_VARIABLES_MAGIC; |
---|
77 | flags &= ~POSIX_CONDITION_VARIABLES_FLAGS_MASK; |
---|
78 | |
---|
79 | if ( the_attr->clock == CLOCK_MONOTONIC ) { |
---|
80 | flags |= POSIX_CONDITION_VARIABLES_CLOCK_MONOTONIC; |
---|
81 | } |
---|
82 | |
---|
83 | the_cond->flags = flags; |
---|
84 | } |
---|
85 | |
---|
86 | RTEMS_INLINE_ROUTINE void _POSIX_Condition_variables_Destroy( |
---|
87 | POSIX_Condition_variables_Control *the_cond |
---|
88 | ) |
---|
89 | { |
---|
90 | the_cond->flags = ~the_cond->flags; |
---|
91 | } |
---|
92 | |
---|
93 | RTEMS_INLINE_ROUTINE clockid_t _POSIX_Condition_variables_Get_clock( |
---|
94 | unsigned long flags |
---|
95 | ) |
---|
96 | { |
---|
97 | if ( ( flags & POSIX_CONDITION_VARIABLES_CLOCK_MONOTONIC ) != 0 ) { |
---|
98 | return CLOCK_MONOTONIC; |
---|
99 | } |
---|
100 | |
---|
101 | return CLOCK_REALTIME; |
---|
102 | } |
---|
103 | |
---|
104 | RTEMS_INLINE_ROUTINE Thread_Control *_POSIX_Condition_variables_Acquire( |
---|
105 | POSIX_Condition_variables_Control *the_cond, |
---|
106 | Thread_queue_Context *queue_context |
---|
107 | ) |
---|
108 | { |
---|
109 | ISR_Level level; |
---|
110 | Thread_Control *executing; |
---|
111 | |
---|
112 | _Thread_queue_Context_ISR_disable( queue_context, level ); |
---|
113 | _Thread_queue_Context_set_ISR_level( queue_context, level ); |
---|
114 | executing = _Thread_Executing; |
---|
115 | _Thread_queue_Queue_acquire_critical( |
---|
116 | &the_cond->Queue.Queue, |
---|
117 | &executing->Potpourri_stats, |
---|
118 | &queue_context->Lock_context.Lock_context |
---|
119 | ); |
---|
120 | |
---|
121 | return executing; |
---|
122 | } |
---|
123 | |
---|
124 | RTEMS_INLINE_ROUTINE void _POSIX_Condition_variables_Release( |
---|
125 | POSIX_Condition_variables_Control *the_cond, |
---|
126 | Thread_queue_Context *queue_context |
---|
127 | ) |
---|
128 | { |
---|
129 | _Thread_queue_Queue_release( |
---|
130 | &the_cond->Queue.Queue, |
---|
131 | &queue_context->Lock_context.Lock_context |
---|
132 | ); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * @brief Implements wake up version of the "signal" operation. |
---|
137 | * |
---|
138 | * A support routine which implements guts of the broadcast and single task |
---|
139 | * wake up version of the "signal" operation. |
---|
140 | */ |
---|
141 | int _POSIX_Condition_variables_Signal_support( |
---|
142 | pthread_cond_t *cond, |
---|
143 | bool is_broadcast |
---|
144 | ); |
---|
145 | |
---|
146 | /** |
---|
147 | * @brief POSIX condition variables wait support. |
---|
148 | * |
---|
149 | * A support routine which implements guts of the blocking, non-blocking, and |
---|
150 | * timed wait version of condition variable wait routines. |
---|
151 | */ |
---|
152 | int _POSIX_Condition_variables_Wait_support( |
---|
153 | pthread_cond_t *cond, |
---|
154 | pthread_mutex_t *mutex, |
---|
155 | const struct timespec *abstime |
---|
156 | ); |
---|
157 | |
---|
158 | bool _POSIX_Condition_variables_Auto_initialization( |
---|
159 | POSIX_Condition_variables_Control *the_cond |
---|
160 | ); |
---|
161 | |
---|
162 | #define POSIX_CONDITION_VARIABLES_VALIDATE_OBJECT( the_cond, flags ) \ |
---|
163 | do { \ |
---|
164 | if ( ( the_cond ) == NULL ) { \ |
---|
165 | return EINVAL; \ |
---|
166 | } \ |
---|
167 | flags = ( the_cond )->flags; \ |
---|
168 | if ( \ |
---|
169 | ( ( (uintptr_t) ( the_cond ) ^ POSIX_CONDITION_VARIABLES_MAGIC ) \ |
---|
170 | & ~POSIX_CONDITION_VARIABLES_FLAGS_MASK ) \ |
---|
171 | != ( flags & ~POSIX_CONDITION_VARIABLES_FLAGS_MASK ) \ |
---|
172 | ) { \ |
---|
173 | if ( !_POSIX_Condition_variables_Auto_initialization( the_cond ) ) { \ |
---|
174 | return EINVAL; \ |
---|
175 | } \ |
---|
176 | } \ |
---|
177 | } while ( 0 ) |
---|
178 | |
---|
179 | #ifdef __cplusplus |
---|
180 | } |
---|
181 | #endif |
---|
182 | |
---|
183 | #endif |
---|
184 | /* end of include file */ |
---|