1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup rtems_bsd_rtems |
---|
5 | * |
---|
6 | * @brief TODO. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2009, 2010 embedded brains GmbH. |
---|
11 | * All rights reserved. |
---|
12 | * |
---|
13 | * embedded brains GmbH |
---|
14 | * Obere Lagerstr. 30 |
---|
15 | * 82178 Puchheim |
---|
16 | * Germany |
---|
17 | * <rtems@embedded-brains.de> |
---|
18 | * |
---|
19 | * Redistribution and use in source and binary forms, with or without |
---|
20 | * modification, are permitted provided that the following conditions |
---|
21 | * are met: |
---|
22 | * 1. Redistributions of source code must retain the above copyright |
---|
23 | * notice, this list of conditions and the following disclaimer. |
---|
24 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
25 | * notice, this list of conditions and the following disclaimer in the |
---|
26 | * documentation and/or other materials provided with the distribution. |
---|
27 | * |
---|
28 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
38 | * SUCH DAMAGE. |
---|
39 | */ |
---|
40 | |
---|
41 | /* Necessary to obtain some internal functions */ |
---|
42 | #define __RTEMS_VIOLATE_KERNEL_VISIBILITY__ |
---|
43 | |
---|
44 | #include <freebsd/machine/rtems-bsd-config.h> |
---|
45 | |
---|
46 | #include <rtems/posix/cond.h> |
---|
47 | |
---|
48 | #include <freebsd/sys/param.h> |
---|
49 | #include <freebsd/sys/types.h> |
---|
50 | #include <freebsd/sys/systm.h> |
---|
51 | #include <freebsd/sys/lock.h> |
---|
52 | #include <freebsd/sys/mutex.h> |
---|
53 | #include <freebsd/sys/condvar.h> |
---|
54 | |
---|
55 | RTEMS_CHAIN_DEFINE_EMPTY(rtems_bsd_condvar_chain); |
---|
56 | |
---|
57 | void |
---|
58 | cv_init(struct cv *cv, const char *desc) |
---|
59 | { |
---|
60 | int rv = pthread_cond_init(&cv->cv_id, NULL); |
---|
61 | |
---|
62 | BSD_ASSERT_RV(rv); |
---|
63 | |
---|
64 | cv->cv_description = desc; |
---|
65 | |
---|
66 | rtems_chain_append(&rtems_bsd_condvar_chain, &cv->cv_node); |
---|
67 | } |
---|
68 | |
---|
69 | void |
---|
70 | cv_destroy(struct cv *cv) |
---|
71 | { |
---|
72 | int rv = pthread_cond_destroy(&cv->cv_id); |
---|
73 | |
---|
74 | BSD_ASSERT_RV(rv); |
---|
75 | |
---|
76 | rtems_chain_extract(&cv->cv_node); |
---|
77 | } |
---|
78 | |
---|
79 | static int _cv_wait_support(struct cv *cv, struct lock_object *lock, int timo, bool relock) |
---|
80 | { |
---|
81 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
82 | int eno = 0; |
---|
83 | Objects_Locations location = OBJECTS_ERROR; |
---|
84 | POSIX_Condition_variables_Control *pcv = _POSIX_Condition_variables_Get(&cv->cv_id, &location); |
---|
85 | |
---|
86 | if (location == OBJECTS_LOCAL) { |
---|
87 | if (pcv->Mutex != POSIX_CONDITION_VARIABLES_NO_MUTEX && pcv->Mutex != lock->lo_id) { |
---|
88 | _Thread_Enable_dispatch(); |
---|
89 | |
---|
90 | BSD_ASSERT(false); |
---|
91 | |
---|
92 | return EINVAL; |
---|
93 | } |
---|
94 | |
---|
95 | sc = rtems_semaphore_release(lock->lo_id); |
---|
96 | if (sc != RTEMS_SUCCESSFUL) { |
---|
97 | _Thread_Enable_dispatch(); |
---|
98 | |
---|
99 | BSD_ASSERT(false); |
---|
100 | |
---|
101 | return EINVAL; |
---|
102 | } |
---|
103 | |
---|
104 | pcv->Mutex = lock->lo_id; |
---|
105 | |
---|
106 | _Thread_queue_Enter_critical_section(&pcv->Wait_queue); |
---|
107 | _Thread_Executing->Wait.return_code = 0; |
---|
108 | _Thread_Executing->Wait.queue = &pcv->Wait_queue; |
---|
109 | _Thread_Executing->Wait.id = cv->cv_id; |
---|
110 | |
---|
111 | /* FIXME: Integer conversion */ |
---|
112 | _Thread_queue_Enqueue(&pcv->Wait_queue, (Watchdog_Interval) timo); |
---|
113 | |
---|
114 | DROP_GIANT(); |
---|
115 | |
---|
116 | _Thread_Enable_dispatch(); |
---|
117 | |
---|
118 | PICKUP_GIANT(); |
---|
119 | |
---|
120 | eno = (int) _Thread_Executing->Wait.return_code; |
---|
121 | if (eno != 0) { |
---|
122 | if (eno == ETIMEDOUT) { |
---|
123 | eno = EWOULDBLOCK; |
---|
124 | } else { |
---|
125 | BSD_ASSERT(false); |
---|
126 | |
---|
127 | eno = EINVAL; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | if (relock) { |
---|
132 | sc = rtems_semaphore_obtain(lock->lo_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
133 | if (sc != RTEMS_SUCCESSFUL) { |
---|
134 | BSD_ASSERT(false); |
---|
135 | |
---|
136 | eno = EINVAL; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | return eno; |
---|
141 | } |
---|
142 | |
---|
143 | BSD_PANIC("unexpected object location"); |
---|
144 | } |
---|
145 | |
---|
146 | void |
---|
147 | _cv_wait(struct cv *cv, struct lock_object *lock) |
---|
148 | { |
---|
149 | _cv_wait_support(cv, lock, 0, true); |
---|
150 | } |
---|
151 | |
---|
152 | void |
---|
153 | _cv_wait_unlock(struct cv *cv, struct lock_object *lock) |
---|
154 | { |
---|
155 | _cv_wait_support(cv, lock, 0, false); |
---|
156 | } |
---|
157 | |
---|
158 | int |
---|
159 | _cv_timedwait(struct cv *cv, struct lock_object *lock, int timo) |
---|
160 | { |
---|
161 | if (timo <= 0) { |
---|
162 | timo = 1; |
---|
163 | } |
---|
164 | |
---|
165 | return _cv_wait_support(cv, lock, timo, true); |
---|
166 | } |
---|
167 | |
---|
168 | void |
---|
169 | cv_signal(struct cv *cv) |
---|
170 | { |
---|
171 | int rv = pthread_cond_signal(&cv->cv_id); |
---|
172 | |
---|
173 | BSD_ASSERT_RV(rv); |
---|
174 | } |
---|
175 | |
---|
176 | void |
---|
177 | cv_broadcastpri(struct cv *cv, int pri) |
---|
178 | { |
---|
179 | int rv = 0; |
---|
180 | |
---|
181 | BSD_ASSERT(pri == 0); |
---|
182 | |
---|
183 | rv = pthread_cond_broadcast(&cv->cv_id); |
---|
184 | BSD_ASSERT_RV(rv); |
---|
185 | } |
---|