1 | /* |
---|
2 | * Copyright (c) 2015, 2016 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #if HAVE_STRUCT__THREAD_QUEUE_QUEUE |
---|
20 | |
---|
21 | #include <sys/lock.h> |
---|
22 | #include <errno.h> |
---|
23 | |
---|
24 | #include <rtems/score/atomic.h> |
---|
25 | #include <rtems/score/chainimpl.h> |
---|
26 | #include <rtems/score/threadimpl.h> |
---|
27 | #include <rtems/score/threadqimpl.h> |
---|
28 | |
---|
29 | #define FUTEX_TQ_OPERATIONS &_Thread_queue_Operations_FIFO |
---|
30 | |
---|
31 | typedef struct { |
---|
32 | Thread_queue_Syslock_queue Queue; |
---|
33 | } Futex_Control; |
---|
34 | |
---|
35 | RTEMS_STATIC_ASSERT( |
---|
36 | offsetof( Futex_Control, Queue ) |
---|
37 | == offsetof( struct _Futex_Control, _Queue ), |
---|
38 | FUTEX_CONTROL_QUEUE |
---|
39 | ); |
---|
40 | |
---|
41 | RTEMS_STATIC_ASSERT( |
---|
42 | sizeof( Futex_Control ) == sizeof( struct _Futex_Control ), |
---|
43 | FUTEX_CONTROL_SIZE |
---|
44 | ); |
---|
45 | |
---|
46 | static Futex_Control *_Futex_Get( struct _Futex_Control *_futex ) |
---|
47 | { |
---|
48 | return (Futex_Control *) _futex; |
---|
49 | } |
---|
50 | |
---|
51 | static Thread_Control *_Futex_Queue_acquire( |
---|
52 | Futex_Control *futex, |
---|
53 | Thread_queue_Context *queue_context |
---|
54 | ) |
---|
55 | { |
---|
56 | Thread_Control *executing; |
---|
57 | |
---|
58 | _ISR_lock_ISR_disable( &queue_context->Lock_context ); |
---|
59 | executing = _Thread_Executing; |
---|
60 | _Thread_queue_Queue_acquire_critical( |
---|
61 | &futex->Queue.Queue, |
---|
62 | &executing->Potpourri_stats, |
---|
63 | &queue_context->Lock_context |
---|
64 | ); |
---|
65 | |
---|
66 | return executing; |
---|
67 | } |
---|
68 | |
---|
69 | static void _Futex_Queue_release( |
---|
70 | Futex_Control *futex, |
---|
71 | Thread_queue_Context *queue_context |
---|
72 | ) |
---|
73 | { |
---|
74 | _Thread_queue_Queue_release( |
---|
75 | &futex->Queue.Queue, |
---|
76 | &queue_context->Lock_context |
---|
77 | ); |
---|
78 | } |
---|
79 | |
---|
80 | int _Futex_Wait( struct _Futex_Control *_futex, int *uaddr, int val ) |
---|
81 | { |
---|
82 | Futex_Control *futex; |
---|
83 | Thread_queue_Context queue_context; |
---|
84 | Thread_Control *executing; |
---|
85 | int eno; |
---|
86 | |
---|
87 | futex = _Futex_Get( _futex ); |
---|
88 | executing = _Futex_Queue_acquire( futex, &queue_context ); |
---|
89 | |
---|
90 | if ( *uaddr == val ) { |
---|
91 | _Thread_queue_Context_set_expected_level( &queue_context, 1 ); |
---|
92 | _Thread_queue_Context_set_no_timeout( &queue_context ); |
---|
93 | _Thread_queue_Enqueue_critical( |
---|
94 | &futex->Queue.Queue, |
---|
95 | FUTEX_TQ_OPERATIONS, |
---|
96 | executing, |
---|
97 | STATES_WAITING_FOR_SYS_LOCK_FUTEX, |
---|
98 | &queue_context |
---|
99 | ); |
---|
100 | eno = 0; |
---|
101 | } else { |
---|
102 | _Futex_Queue_release( futex, &queue_context ); |
---|
103 | eno = EWOULDBLOCK; |
---|
104 | } |
---|
105 | |
---|
106 | return eno; |
---|
107 | } |
---|
108 | |
---|
109 | typedef struct { |
---|
110 | Thread_queue_Context Base; |
---|
111 | int count; |
---|
112 | } Futex_Context; |
---|
113 | |
---|
114 | static Thread_Control *_Futex_Flush_filter( |
---|
115 | Thread_Control *the_thread, |
---|
116 | Thread_queue_Queue *queue, |
---|
117 | Thread_queue_Context *queue_context |
---|
118 | ) |
---|
119 | { |
---|
120 | Futex_Context *context; |
---|
121 | |
---|
122 | context = (Futex_Context *) queue_context; |
---|
123 | |
---|
124 | if ( context->count <= 0 ) { |
---|
125 | return NULL; |
---|
126 | } |
---|
127 | |
---|
128 | --context->count; |
---|
129 | |
---|
130 | return the_thread; |
---|
131 | } |
---|
132 | |
---|
133 | int _Futex_Wake( struct _Futex_Control *_futex, int count ) |
---|
134 | { |
---|
135 | Futex_Control *futex; |
---|
136 | Futex_Context context; |
---|
137 | |
---|
138 | futex = _Futex_Get( _futex ); |
---|
139 | _Futex_Queue_acquire( futex, &context.Base ); |
---|
140 | |
---|
141 | /* |
---|
142 | * For some synchronization objects like barriers the _Futex_Wake() must be |
---|
143 | * called in the fast path. Normally there are no threads on the queue, so |
---|
144 | * check this condition early. |
---|
145 | */ |
---|
146 | if ( __predict_true( _Thread_queue_Is_empty( &futex->Queue.Queue ) ) ) { |
---|
147 | _Futex_Queue_release( futex, &context.Base ); |
---|
148 | return 0; |
---|
149 | } |
---|
150 | |
---|
151 | context.count = count; |
---|
152 | return (int) _Thread_queue_Flush_critical( |
---|
153 | &futex->Queue.Queue, |
---|
154 | FUTEX_TQ_OPERATIONS, |
---|
155 | _Futex_Flush_filter, |
---|
156 | &context.Base |
---|
157 | ); |
---|
158 | } |
---|
159 | |
---|
160 | #endif /* HAVE_STRUCT__THREAD_QUEUE_QUEUE */ |
---|