]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2000-2009 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | * | |
31 | */ | |
32 | /* | |
33 | * File: kern/sync_sema.c | |
34 | * Author: Joseph CaraDonna | |
35 | * | |
36 | * Contains RT distributed semaphore synchronization services. | |
37 | */ | |
38 | ||
39 | #include <mach/mach_types.h> | |
91447636 | 40 | #include <mach/mach_traps.h> |
1c79356b A |
41 | #include <mach/kern_return.h> |
42 | #include <mach/semaphore.h> | |
43 | #include <mach/sync_policy.h> | |
91447636 | 44 | #include <mach/task.h> |
1c79356b A |
45 | |
46 | #include <kern/misc_protos.h> | |
47 | #include <kern/sync_sema.h> | |
48 | #include <kern/spl.h> | |
49 | #include <kern/ipc_kobject.h> | |
50 | #include <kern/ipc_sync.h> | |
51 | #include <kern/ipc_tt.h> | |
52 | #include <kern/thread.h> | |
53 | #include <kern/clock.h> | |
54 | #include <ipc/ipc_port.h> | |
55 | #include <ipc/ipc_space.h> | |
56 | #include <kern/host.h> | |
57 | #include <kern/wait_queue.h> | |
58 | #include <kern/zalloc.h> | |
59 | #include <kern/mach_param.h> | |
60 | ||
9bccf70c | 61 | static unsigned int semaphore_event; |
cf7d32b8 | 62 | #define SEMAPHORE_EVENT CAST_EVENT64_T(&semaphore_event) |
1c79356b A |
63 | |
64 | zone_t semaphore_zone; | |
b0d623f7 | 65 | unsigned int semaphore_max; |
1c79356b | 66 | |
91447636 A |
67 | /* Forward declarations */ |
68 | ||
69 | ||
70 | kern_return_t | |
71 | semaphore_wait_trap_internal( | |
72 | mach_port_name_t name, | |
73 | void (*caller_cont)(kern_return_t)); | |
74 | ||
75 | kern_return_t | |
76 | semaphore_wait_signal_trap_internal( | |
77 | mach_port_name_t wait_name, | |
78 | mach_port_name_t signal_name, | |
79 | void (*caller_cont)(kern_return_t)); | |
80 | ||
81 | kern_return_t | |
82 | semaphore_timedwait_trap_internal( | |
83 | mach_port_name_t name, | |
84 | unsigned int sec, | |
85 | clock_res_t nsec, | |
86 | void (*caller_cont)(kern_return_t)); | |
87 | ||
88 | kern_return_t | |
89 | semaphore_timedwait_signal_trap_internal( | |
90 | mach_port_name_t wait_name, | |
91 | mach_port_name_t signal_name, | |
92 | unsigned int sec, | |
93 | clock_res_t nsec, | |
94 | void (*caller_cont)(kern_return_t)); | |
95 | ||
2d21ac55 A |
96 | kern_return_t |
97 | semaphore_signal_internal_trap(mach_port_name_t sema_name); | |
91447636 A |
98 | |
99 | kern_return_t | |
100 | semaphore_signal_internal( | |
101 | semaphore_t semaphore, | |
102 | thread_t thread, | |
103 | int options); | |
104 | ||
105 | kern_return_t | |
106 | semaphore_convert_wait_result( | |
107 | int wait_result); | |
108 | ||
109 | void | |
110 | semaphore_wait_continue(void); | |
111 | ||
b0d623f7 | 112 | static kern_return_t |
91447636 A |
113 | semaphore_wait_internal( |
114 | semaphore_t wait_semaphore, | |
115 | semaphore_t signal_semaphore, | |
b0d623f7 A |
116 | uint64_t deadline, |
117 | int option, | |
91447636 A |
118 | void (*caller_cont)(kern_return_t)); |
119 | ||
b0d623f7 A |
120 | static __inline__ uint64_t |
121 | semaphore_deadline( | |
122 | unsigned int sec, | |
123 | clock_res_t nsec) | |
124 | { | |
125 | uint64_t abstime; | |
126 | ||
127 | nanoseconds_to_absolutetime((uint64_t)sec * NSEC_PER_SEC + nsec, &abstime); | |
128 | clock_absolutetime_interval_to_deadline(abstime, &abstime); | |
129 | ||
130 | return (abstime); | |
131 | } | |
132 | ||
1c79356b A |
133 | /* |
134 | * ROUTINE: semaphore_init [private] | |
135 | * | |
136 | * Initialize the semaphore mechanisms. | |
137 | * Right now, we only need to initialize the semaphore zone. | |
138 | */ | |
139 | void | |
140 | semaphore_init(void) | |
141 | { | |
142 | semaphore_zone = zinit(sizeof(struct semaphore), | |
143 | semaphore_max * sizeof(struct semaphore), | |
144 | sizeof(struct semaphore), | |
145 | "semaphores"); | |
0b4c1975 | 146 | zone_change(semaphore_zone, Z_NOENCRYPT, TRUE); |
1c79356b A |
147 | } |
148 | ||
149 | /* | |
150 | * Routine: semaphore_create | |
151 | * | |
152 | * Creates a semaphore. | |
153 | * The port representing the semaphore is returned as a parameter. | |
154 | */ | |
155 | kern_return_t | |
156 | semaphore_create( | |
157 | task_t task, | |
158 | semaphore_t *new_semaphore, | |
159 | int policy, | |
160 | int value) | |
161 | { | |
162 | semaphore_t s = SEMAPHORE_NULL; | |
b0d623f7 | 163 | kern_return_t kret; |
1c79356b A |
164 | |
165 | ||
b0d623f7 A |
166 | *new_semaphore = SEMAPHORE_NULL; |
167 | if (task == TASK_NULL || value < 0 || policy > SYNC_POLICY_MAX) | |
1c79356b | 168 | return KERN_INVALID_ARGUMENT; |
1c79356b A |
169 | |
170 | s = (semaphore_t) zalloc (semaphore_zone); | |
171 | ||
b0d623f7 | 172 | if (s == SEMAPHORE_NULL) |
1c79356b | 173 | return KERN_RESOURCE_SHORTAGE; |
b0d623f7 A |
174 | |
175 | kret = wait_queue_init(&s->wait_queue, policy); /* also inits lock */ | |
176 | if (kret != KERN_SUCCESS) { | |
177 | zfree(semaphore_zone, s); | |
178 | return kret; | |
1c79356b A |
179 | } |
180 | ||
1c79356b | 181 | s->count = value; |
b0d623f7 | 182 | s->ref_count = (task == kernel_task) ? 1 : 2; |
1c79356b A |
183 | |
184 | /* | |
185 | * Create and initialize the semaphore port | |
186 | */ | |
187 | s->port = ipc_port_alloc_kernel(); | |
188 | if (s->port == IP_NULL) { | |
b0d623f7 | 189 | zfree(semaphore_zone, s); |
1c79356b A |
190 | return KERN_RESOURCE_SHORTAGE; |
191 | } | |
192 | ||
193 | ipc_kobject_set (s->port, (ipc_kobject_t) s, IKOT_SEMAPHORE); | |
194 | ||
195 | /* | |
196 | * Associate the new semaphore with the task by adding | |
197 | * the new semaphore to the task's semaphore list. | |
198 | * | |
199 | * Associate the task with the new semaphore by having the | |
200 | * semaphores task pointer point to the owning task's structure. | |
201 | */ | |
202 | task_lock(task); | |
203 | enqueue_head(&task->semaphore_list, (queue_entry_t) s); | |
204 | task->semaphores_owned++; | |
205 | s->owner = task; | |
206 | s->active = TRUE; | |
207 | task_unlock(task); | |
208 | ||
209 | *new_semaphore = s; | |
210 | ||
211 | return KERN_SUCCESS; | |
212 | } | |
213 | ||
214 | /* | |
215 | * Routine: semaphore_destroy | |
216 | * | |
217 | * Destroys a semaphore. This call will only succeed if the | |
218 | * specified task is the SAME task name specified at the semaphore's | |
219 | * creation. | |
220 | * | |
221 | * All threads currently blocked on the semaphore are awoken. These | |
222 | * threads will return with the KERN_TERMINATED error. | |
223 | */ | |
224 | kern_return_t | |
225 | semaphore_destroy( | |
226 | task_t task, | |
227 | semaphore_t semaphore) | |
228 | { | |
229 | int old_count; | |
1c79356b A |
230 | spl_t spl_level; |
231 | ||
232 | ||
233 | if (task == TASK_NULL || semaphore == SEMAPHORE_NULL) | |
234 | return KERN_INVALID_ARGUMENT; | |
235 | ||
236 | /* | |
237 | * Disown semaphore | |
238 | */ | |
239 | task_lock(task); | |
240 | if (semaphore->owner != task) { | |
241 | task_unlock(task); | |
242 | return KERN_INVALID_ARGUMENT; | |
243 | } | |
6d2010ae | 244 | remqueue((queue_entry_t) semaphore); |
1c79356b A |
245 | semaphore->owner = TASK_NULL; |
246 | task->semaphores_owned--; | |
247 | task_unlock(task); | |
248 | ||
249 | spl_level = splsched(); | |
250 | semaphore_lock(semaphore); | |
251 | ||
252 | /* | |
253 | * Deactivate semaphore | |
254 | */ | |
255 | assert(semaphore->active); | |
256 | semaphore->active = FALSE; | |
257 | ||
258 | /* | |
259 | * Wakeup blocked threads | |
260 | */ | |
261 | old_count = semaphore->count; | |
262 | semaphore->count = 0; | |
263 | ||
264 | if (old_count < 0) { | |
9bccf70c | 265 | wait_queue_wakeup64_all_locked(&semaphore->wait_queue, |
1c79356b A |
266 | SEMAPHORE_EVENT, |
267 | THREAD_RESTART, | |
268 | TRUE); /* unlock? */ | |
269 | } else { | |
270 | semaphore_unlock(semaphore); | |
271 | } | |
272 | splx(spl_level); | |
273 | ||
274 | /* | |
275 | * Deallocate | |
276 | * | |
b0d623f7 A |
277 | * Drop the task's semaphore reference, which in turn deallocates |
278 | * the semaphore structure if the reference count goes to zero. | |
1c79356b | 279 | */ |
1c79356b A |
280 | semaphore_dereference(semaphore); |
281 | return KERN_SUCCESS; | |
282 | } | |
283 | ||
284 | /* | |
285 | * Routine: semaphore_signal_internal | |
286 | * | |
287 | * Signals the semaphore as direct. | |
288 | * Assumptions: | |
289 | * Semaphore is locked. | |
290 | */ | |
291 | kern_return_t | |
292 | semaphore_signal_internal( | |
293 | semaphore_t semaphore, | |
91447636 A |
294 | thread_t thread, |
295 | int options) | |
1c79356b A |
296 | { |
297 | kern_return_t kr; | |
298 | spl_t spl_level; | |
299 | ||
300 | spl_level = splsched(); | |
301 | semaphore_lock(semaphore); | |
302 | ||
303 | if (!semaphore->active) { | |
304 | semaphore_unlock(semaphore); | |
305 | splx(spl_level); | |
306 | return KERN_TERMINATED; | |
307 | } | |
308 | ||
91447636 | 309 | if (thread != THREAD_NULL) { |
1c79356b | 310 | if (semaphore->count < 0) { |
9bccf70c | 311 | kr = wait_queue_wakeup64_thread_locked( |
1c79356b A |
312 | &semaphore->wait_queue, |
313 | SEMAPHORE_EVENT, | |
91447636 | 314 | thread, |
1c79356b A |
315 | THREAD_AWAKENED, |
316 | TRUE); /* unlock? */ | |
317 | } else { | |
318 | semaphore_unlock(semaphore); | |
319 | kr = KERN_NOT_WAITING; | |
320 | } | |
321 | splx(spl_level); | |
322 | return kr; | |
323 | } | |
324 | ||
325 | if (options & SEMAPHORE_SIGNAL_ALL) { | |
326 | int old_count = semaphore->count; | |
327 | ||
328 | if (old_count < 0) { | |
329 | semaphore->count = 0; /* always reset */ | |
9bccf70c | 330 | kr = wait_queue_wakeup64_all_locked( |
1c79356b A |
331 | &semaphore->wait_queue, |
332 | SEMAPHORE_EVENT, | |
333 | THREAD_AWAKENED, | |
334 | TRUE); /* unlock? */ | |
335 | } else { | |
336 | if (options & SEMAPHORE_SIGNAL_PREPOST) | |
337 | semaphore->count++; | |
338 | semaphore_unlock(semaphore); | |
339 | kr = KERN_SUCCESS; | |
340 | } | |
341 | splx(spl_level); | |
342 | return kr; | |
343 | } | |
344 | ||
345 | if (semaphore->count < 0) { | |
9bccf70c | 346 | if (wait_queue_wakeup64_one_locked( |
1c79356b A |
347 | &semaphore->wait_queue, |
348 | SEMAPHORE_EVENT, | |
349 | THREAD_AWAKENED, | |
350 | FALSE) == KERN_SUCCESS) { | |
351 | semaphore_unlock(semaphore); | |
352 | splx(spl_level); | |
353 | return KERN_SUCCESS; | |
354 | } else | |
355 | semaphore->count = 0; /* all waiters gone */ | |
356 | } | |
357 | ||
358 | if (options & SEMAPHORE_SIGNAL_PREPOST) { | |
359 | semaphore->count++; | |
360 | } | |
361 | ||
362 | semaphore_unlock(semaphore); | |
363 | splx(spl_level); | |
364 | return KERN_NOT_WAITING; | |
365 | } | |
366 | ||
367 | /* | |
368 | * Routine: semaphore_signal_thread | |
369 | * | |
91447636 A |
370 | * If the specified thread is blocked on the semaphore, it is |
371 | * woken up. If a NULL thread was supplied, then any one | |
1c79356b A |
372 | * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING |
373 | * and the semaphore is unchanged. | |
374 | */ | |
375 | kern_return_t | |
376 | semaphore_signal_thread( | |
377 | semaphore_t semaphore, | |
91447636 | 378 | thread_t thread) |
1c79356b A |
379 | { |
380 | kern_return_t ret; | |
381 | ||
382 | if (semaphore == SEMAPHORE_NULL) | |
383 | return KERN_INVALID_ARGUMENT; | |
384 | ||
385 | ret = semaphore_signal_internal(semaphore, | |
91447636 | 386 | thread, |
1c79356b A |
387 | SEMAPHORE_OPTION_NONE); |
388 | return ret; | |
389 | } | |
390 | ||
391 | /* | |
392 | * Routine: semaphore_signal_thread_trap | |
393 | * | |
394 | * Trap interface to the semaphore_signal_thread function. | |
395 | */ | |
396 | kern_return_t | |
397 | semaphore_signal_thread_trap( | |
91447636 | 398 | struct semaphore_signal_thread_trap_args *args) |
1c79356b | 399 | { |
91447636 A |
400 | mach_port_name_t sema_name = args->signal_name; |
401 | mach_port_name_t thread_name = args->thread_name; | |
1c79356b | 402 | semaphore_t semaphore; |
91447636 | 403 | thread_t thread; |
1c79356b A |
404 | kern_return_t kr; |
405 | ||
406 | /* | |
407 | * MACH_PORT_NULL is not an error. It means that we want to | |
408 | * select any one thread that is already waiting, but not to | |
409 | * pre-post the semaphore. | |
410 | */ | |
411 | if (thread_name != MACH_PORT_NULL) { | |
91447636 A |
412 | thread = port_name_to_thread(thread_name); |
413 | if (thread == THREAD_NULL) | |
1c79356b A |
414 | return KERN_INVALID_ARGUMENT; |
415 | } else | |
91447636 | 416 | thread = THREAD_NULL; |
1c79356b A |
417 | |
418 | kr = port_name_to_semaphore(sema_name, &semaphore); | |
91447636 A |
419 | if (kr == KERN_SUCCESS) { |
420 | kr = semaphore_signal_internal(semaphore, | |
421 | thread, | |
422 | SEMAPHORE_OPTION_NONE); | |
423 | semaphore_dereference(semaphore); | |
424 | } | |
425 | if (thread != THREAD_NULL) { | |
426 | thread_deallocate(thread); | |
1c79356b | 427 | } |
1c79356b A |
428 | return kr; |
429 | } | |
430 | ||
431 | ||
432 | ||
433 | /* | |
434 | * Routine: semaphore_signal | |
435 | * | |
436 | * Traditional (in-kernel client and MIG interface) semaphore | |
437 | * signal routine. Most users will access the trap version. | |
438 | * | |
439 | * This interface in not defined to return info about whether | |
440 | * this call found a thread waiting or not. The internal | |
441 | * routines (and future external routines) do. We have to | |
442 | * convert those into plain KERN_SUCCESS returns. | |
443 | */ | |
444 | kern_return_t | |
445 | semaphore_signal( | |
446 | semaphore_t semaphore) | |
447 | { | |
448 | kern_return_t kr; | |
449 | ||
450 | if (semaphore == SEMAPHORE_NULL) | |
451 | return KERN_INVALID_ARGUMENT; | |
452 | ||
453 | kr = semaphore_signal_internal(semaphore, | |
91447636 | 454 | THREAD_NULL, |
1c79356b A |
455 | SEMAPHORE_SIGNAL_PREPOST); |
456 | if (kr == KERN_NOT_WAITING) | |
457 | return KERN_SUCCESS; | |
458 | return kr; | |
459 | } | |
460 | ||
461 | /* | |
462 | * Routine: semaphore_signal_trap | |
463 | * | |
464 | * Trap interface to the semaphore_signal function. | |
465 | */ | |
466 | kern_return_t | |
467 | semaphore_signal_trap( | |
91447636 | 468 | struct semaphore_signal_trap_args *args) |
1c79356b | 469 | { |
91447636 | 470 | mach_port_name_t sema_name = args->signal_name; |
2d21ac55 A |
471 | |
472 | return (semaphore_signal_internal_trap(sema_name)); | |
473 | } | |
474 | ||
475 | kern_return_t | |
476 | semaphore_signal_internal_trap(mach_port_name_t sema_name) | |
477 | { | |
1c79356b A |
478 | semaphore_t semaphore; |
479 | kern_return_t kr; | |
480 | ||
481 | kr = port_name_to_semaphore(sema_name, &semaphore); | |
91447636 A |
482 | if (kr == KERN_SUCCESS) { |
483 | kr = semaphore_signal_internal(semaphore, | |
484 | THREAD_NULL, | |
485 | SEMAPHORE_SIGNAL_PREPOST); | |
486 | semaphore_dereference(semaphore); | |
487 | if (kr == KERN_NOT_WAITING) | |
488 | kr = KERN_SUCCESS; | |
1c79356b | 489 | } |
1c79356b A |
490 | return kr; |
491 | } | |
492 | ||
493 | /* | |
494 | * Routine: semaphore_signal_all | |
495 | * | |
496 | * Awakens ALL threads currently blocked on the semaphore. | |
497 | * The semaphore count returns to zero. | |
498 | */ | |
499 | kern_return_t | |
500 | semaphore_signal_all( | |
501 | semaphore_t semaphore) | |
502 | { | |
503 | kern_return_t kr; | |
504 | ||
505 | if (semaphore == SEMAPHORE_NULL) | |
506 | return KERN_INVALID_ARGUMENT; | |
507 | ||
508 | kr = semaphore_signal_internal(semaphore, | |
91447636 | 509 | THREAD_NULL, |
1c79356b A |
510 | SEMAPHORE_SIGNAL_ALL); |
511 | if (kr == KERN_NOT_WAITING) | |
512 | return KERN_SUCCESS; | |
513 | return kr; | |
514 | } | |
515 | ||
516 | /* | |
517 | * Routine: semaphore_signal_all_trap | |
518 | * | |
519 | * Trap interface to the semaphore_signal_all function. | |
520 | */ | |
521 | kern_return_t | |
522 | semaphore_signal_all_trap( | |
91447636 | 523 | struct semaphore_signal_all_trap_args *args) |
1c79356b | 524 | { |
91447636 | 525 | mach_port_name_t sema_name = args->signal_name; |
1c79356b A |
526 | semaphore_t semaphore; |
527 | kern_return_t kr; | |
528 | ||
529 | kr = port_name_to_semaphore(sema_name, &semaphore); | |
91447636 A |
530 | if (kr == KERN_SUCCESS) { |
531 | kr = semaphore_signal_internal(semaphore, | |
532 | THREAD_NULL, | |
533 | SEMAPHORE_SIGNAL_ALL); | |
534 | semaphore_dereference(semaphore); | |
535 | if (kr == KERN_NOT_WAITING) | |
536 | kr = KERN_SUCCESS; | |
1c79356b | 537 | } |
1c79356b A |
538 | return kr; |
539 | } | |
540 | ||
541 | /* | |
542 | * Routine: semaphore_convert_wait_result | |
543 | * | |
544 | * Generate the return code after a semaphore wait/block. It | |
545 | * takes the wait result as an input and coverts that to an | |
546 | * appropriate result. | |
547 | */ | |
548 | kern_return_t | |
549 | semaphore_convert_wait_result(int wait_result) | |
550 | { | |
551 | switch (wait_result) { | |
552 | case THREAD_AWAKENED: | |
553 | return KERN_SUCCESS; | |
554 | ||
555 | case THREAD_TIMED_OUT: | |
556 | return KERN_OPERATION_TIMED_OUT; | |
557 | ||
558 | case THREAD_INTERRUPTED: | |
559 | return KERN_ABORTED; | |
560 | ||
561 | case THREAD_RESTART: | |
562 | return KERN_TERMINATED; | |
563 | ||
564 | default: | |
565 | panic("semaphore_block\n"); | |
566 | return KERN_FAILURE; | |
567 | } | |
568 | } | |
569 | ||
570 | /* | |
571 | * Routine: semaphore_wait_continue | |
572 | * | |
573 | * Common continuation routine after waiting on a semphore. | |
574 | * It returns directly to user space. | |
575 | */ | |
576 | void | |
577 | semaphore_wait_continue(void) | |
578 | { | |
579 | thread_t self = current_thread(); | |
580 | int wait_result = self->wait_result; | |
581 | void (*caller_cont)(kern_return_t) = self->sth_continuation; | |
582 | ||
583 | assert(self->sth_waitsemaphore != SEMAPHORE_NULL); | |
584 | semaphore_dereference(self->sth_waitsemaphore); | |
585 | if (self->sth_signalsemaphore != SEMAPHORE_NULL) | |
586 | semaphore_dereference(self->sth_signalsemaphore); | |
587 | ||
588 | assert(caller_cont != (void (*)(kern_return_t))0); | |
589 | (*caller_cont)(semaphore_convert_wait_result(wait_result)); | |
590 | } | |
591 | ||
1c79356b A |
592 | /* |
593 | * Routine: semaphore_wait_internal | |
594 | * | |
595 | * Decrements the semaphore count by one. If the count is | |
596 | * negative after the decrement, the calling thread blocks | |
597 | * (possibly at a continuation and/or with a timeout). | |
598 | * | |
599 | * Assumptions: | |
600 | * The reference | |
601 | * A reference is held on the signal semaphore. | |
602 | */ | |
b0d623f7 | 603 | static kern_return_t |
1c79356b A |
604 | semaphore_wait_internal( |
605 | semaphore_t wait_semaphore, | |
606 | semaphore_t signal_semaphore, | |
b0d623f7 A |
607 | uint64_t deadline, |
608 | int option, | |
1c79356b A |
609 | void (*caller_cont)(kern_return_t)) |
610 | { | |
91447636 A |
611 | int wait_result; |
612 | spl_t spl_level; | |
1c79356b A |
613 | kern_return_t kr = KERN_ALREADY_WAITING; |
614 | ||
615 | spl_level = splsched(); | |
616 | semaphore_lock(wait_semaphore); | |
617 | ||
1c79356b A |
618 | if (!wait_semaphore->active) { |
619 | kr = KERN_TERMINATED; | |
620 | } else if (wait_semaphore->count > 0) { | |
621 | wait_semaphore->count--; | |
622 | kr = KERN_SUCCESS; | |
b0d623f7 | 623 | } else if (option & SEMAPHORE_TIMEOUT_NOBLOCK) { |
1c79356b | 624 | kr = KERN_OPERATION_TIMED_OUT; |
55e303ae | 625 | } else { |
91447636 | 626 | thread_t self = current_thread(); |
55e303ae | 627 | |
1c79356b | 628 | wait_semaphore->count = -1; /* we don't keep an actual count */ |
55e303ae | 629 | thread_lock(self); |
9bccf70c A |
630 | (void)wait_queue_assert_wait64_locked( |
631 | &wait_semaphore->wait_queue, | |
632 | SEMAPHORE_EVENT, | |
b0d623f7 | 633 | THREAD_ABORTSAFE, deadline, |
55e303ae A |
634 | self); |
635 | thread_unlock(self); | |
1c79356b A |
636 | } |
637 | semaphore_unlock(wait_semaphore); | |
638 | splx(spl_level); | |
639 | ||
640 | /* | |
641 | * wait_semaphore is unlocked so we are free to go ahead and | |
642 | * signal the signal_semaphore (if one was provided). | |
643 | */ | |
644 | if (signal_semaphore != SEMAPHORE_NULL) { | |
645 | kern_return_t signal_kr; | |
646 | ||
647 | /* | |
648 | * lock the signal semaphore reference we got and signal it. | |
649 | * This will NOT block (we cannot block after having asserted | |
650 | * our intention to wait above). | |
651 | */ | |
652 | signal_kr = semaphore_signal_internal(signal_semaphore, | |
91447636 | 653 | THREAD_NULL, |
1c79356b A |
654 | SEMAPHORE_SIGNAL_PREPOST); |
655 | ||
656 | if (signal_kr == KERN_NOT_WAITING) | |
657 | signal_kr = KERN_SUCCESS; | |
658 | else if (signal_kr == KERN_TERMINATED) { | |
659 | /* | |
660 | * Uh!Oh! The semaphore we were to signal died. | |
661 | * We have to get ourselves out of the wait in | |
662 | * case we get stuck here forever (it is assumed | |
663 | * that the semaphore we were posting is gating | |
664 | * the decision by someone else to post the | |
665 | * semaphore we are waiting on). People will | |
666 | * discover the other dead semaphore soon enough. | |
667 | * If we got out of the wait cleanly (someone | |
668 | * already posted a wakeup to us) then return that | |
669 | * (most important) result. Otherwise, | |
670 | * return the KERN_TERMINATED status. | |
671 | */ | |
672 | thread_t self = current_thread(); | |
673 | ||
674 | clear_wait(self, THREAD_INTERRUPTED); | |
675 | kr = semaphore_convert_wait_result(self->wait_result); | |
676 | if (kr == KERN_ABORTED) | |
677 | kr = KERN_TERMINATED; | |
678 | } | |
679 | } | |
680 | ||
681 | /* | |
682 | * If we had an error, or we didn't really need to wait we can | |
683 | * return now that we have signalled the signal semaphore. | |
684 | */ | |
685 | if (kr != KERN_ALREADY_WAITING) | |
686 | return kr; | |
1c79356b A |
687 | |
688 | /* | |
689 | * Now, we can block. If the caller supplied a continuation | |
690 | * pointer of his own for after the block, block with the | |
691 | * appropriate semaphore continuation. Thiswill gather the | |
692 | * semaphore results, release references on the semaphore(s), | |
693 | * and then call the caller's continuation. | |
694 | */ | |
695 | if (caller_cont) { | |
696 | thread_t self = current_thread(); | |
697 | ||
698 | self->sth_continuation = caller_cont; | |
699 | self->sth_waitsemaphore = wait_semaphore; | |
700 | self->sth_signalsemaphore = signal_semaphore; | |
91447636 A |
701 | wait_result = thread_block((thread_continue_t)semaphore_wait_continue); |
702 | } | |
703 | else { | |
9bccf70c | 704 | wait_result = thread_block(THREAD_CONTINUE_NULL); |
1c79356b A |
705 | } |
706 | ||
1c79356b A |
707 | return (semaphore_convert_wait_result(wait_result)); |
708 | } | |
709 | ||
710 | ||
711 | /* | |
712 | * Routine: semaphore_wait | |
713 | * | |
714 | * Traditional (non-continuation) interface presented to | |
715 | * in-kernel clients to wait on a semaphore. | |
716 | */ | |
717 | kern_return_t | |
718 | semaphore_wait( | |
719 | semaphore_t semaphore) | |
720 | { | |
721 | ||
722 | if (semaphore == SEMAPHORE_NULL) | |
723 | return KERN_INVALID_ARGUMENT; | |
724 | ||
725 | return(semaphore_wait_internal(semaphore, | |
b0d623f7 A |
726 | SEMAPHORE_NULL, |
727 | 0ULL, SEMAPHORE_OPTION_NONE, | |
728 | (void (*)(kern_return_t))0)); | |
729 | } | |
730 | ||
731 | kern_return_t | |
732 | semaphore_wait_noblock( | |
733 | semaphore_t semaphore) | |
734 | { | |
735 | ||
736 | if (semaphore == SEMAPHORE_NULL) | |
737 | return KERN_INVALID_ARGUMENT; | |
738 | ||
739 | return(semaphore_wait_internal(semaphore, | |
740 | SEMAPHORE_NULL, | |
741 | 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK, | |
742 | (void (*)(kern_return_t))0)); | |
743 | } | |
744 | ||
745 | kern_return_t | |
746 | semaphore_wait_deadline( | |
747 | semaphore_t semaphore, | |
748 | uint64_t deadline) | |
749 | { | |
750 | ||
751 | if (semaphore == SEMAPHORE_NULL) | |
752 | return KERN_INVALID_ARGUMENT; | |
753 | ||
754 | return(semaphore_wait_internal(semaphore, | |
755 | SEMAPHORE_NULL, | |
756 | deadline, SEMAPHORE_OPTION_NONE, | |
1c79356b A |
757 | (void (*)(kern_return_t))0)); |
758 | } | |
759 | ||
760 | /* | |
761 | * Trap: semaphore_wait_trap | |
762 | * | |
763 | * Trap version of semaphore wait. Called on behalf of user-level | |
764 | * clients. | |
765 | */ | |
91447636 | 766 | |
1c79356b A |
767 | kern_return_t |
768 | semaphore_wait_trap( | |
91447636 A |
769 | struct semaphore_wait_trap_args *args) |
770 | { | |
771 | return(semaphore_wait_trap_internal(args->wait_name, thread_syscall_return)); | |
772 | } | |
773 | ||
774 | ||
775 | ||
776 | kern_return_t | |
777 | semaphore_wait_trap_internal( | |
778 | mach_port_name_t name, | |
779 | void (*caller_cont)(kern_return_t)) | |
1c79356b A |
780 | { |
781 | semaphore_t semaphore; | |
782 | kern_return_t kr; | |
783 | ||
784 | kr = port_name_to_semaphore(name, &semaphore); | |
91447636 A |
785 | if (kr == KERN_SUCCESS) { |
786 | kr = semaphore_wait_internal(semaphore, | |
787 | SEMAPHORE_NULL, | |
b0d623f7 | 788 | 0ULL, SEMAPHORE_OPTION_NONE, |
91447636 A |
789 | caller_cont); |
790 | semaphore_dereference(semaphore); | |
791 | } | |
1c79356b A |
792 | return kr; |
793 | } | |
794 | ||
795 | /* | |
796 | * Routine: semaphore_timedwait | |
797 | * | |
798 | * Traditional (non-continuation) interface presented to | |
799 | * in-kernel clients to wait on a semaphore with a timeout. | |
800 | * | |
801 | * A timeout of {0,0} is considered non-blocking. | |
802 | */ | |
803 | kern_return_t | |
804 | semaphore_timedwait( | |
805 | semaphore_t semaphore, | |
806 | mach_timespec_t wait_time) | |
b0d623f7 A |
807 | { |
808 | int option = SEMAPHORE_OPTION_NONE; | |
809 | uint64_t deadline = 0; | |
810 | ||
1c79356b A |
811 | if (semaphore == SEMAPHORE_NULL) |
812 | return KERN_INVALID_ARGUMENT; | |
813 | ||
814 | if(BAD_MACH_TIMESPEC(&wait_time)) | |
815 | return KERN_INVALID_VALUE; | |
b0d623f7 A |
816 | |
817 | if (wait_time.tv_sec == 0 && wait_time.tv_nsec == 0) | |
818 | option = SEMAPHORE_TIMEOUT_NOBLOCK; | |
819 | else | |
820 | deadline = semaphore_deadline(wait_time.tv_sec, wait_time.tv_nsec); | |
1c79356b A |
821 | |
822 | return (semaphore_wait_internal(semaphore, | |
823 | SEMAPHORE_NULL, | |
b0d623f7 | 824 | deadline, option, |
1c79356b A |
825 | (void(*)(kern_return_t))0)); |
826 | ||
827 | } | |
828 | ||
829 | /* | |
830 | * Trap: semaphore_timedwait_trap | |
831 | * | |
832 | * Trap version of a semaphore_timedwait. The timeout parameter | |
833 | * is passed in two distinct parts and re-assembled on this side | |
834 | * of the trap interface (to accomodate calling conventions that | |
835 | * pass structures as pointers instead of inline in registers without | |
836 | * having to add a copyin). | |
837 | * | |
838 | * A timeout of {0,0} is considered non-blocking. | |
839 | */ | |
840 | kern_return_t | |
841 | semaphore_timedwait_trap( | |
91447636 | 842 | struct semaphore_timedwait_trap_args *args) |
1c79356b | 843 | { |
91447636 A |
844 | |
845 | return(semaphore_timedwait_trap_internal(args->wait_name, args->sec, args->nsec, thread_syscall_return)); | |
846 | } | |
847 | ||
848 | ||
849 | kern_return_t | |
850 | semaphore_timedwait_trap_internal( | |
851 | mach_port_name_t name, | |
852 | unsigned int sec, | |
853 | clock_res_t nsec, | |
854 | void (*caller_cont)(kern_return_t)) | |
855 | { | |
1c79356b A |
856 | semaphore_t semaphore; |
857 | mach_timespec_t wait_time; | |
858 | kern_return_t kr; | |
859 | ||
860 | wait_time.tv_sec = sec; | |
861 | wait_time.tv_nsec = nsec; | |
862 | if(BAD_MACH_TIMESPEC(&wait_time)) | |
863 | return KERN_INVALID_VALUE; | |
864 | ||
865 | kr = port_name_to_semaphore(name, &semaphore); | |
91447636 | 866 | if (kr == KERN_SUCCESS) { |
b0d623f7 A |
867 | int option = SEMAPHORE_OPTION_NONE; |
868 | uint64_t deadline = 0; | |
869 | ||
870 | if (sec == 0 && nsec == 0) | |
871 | option = SEMAPHORE_TIMEOUT_NOBLOCK; | |
872 | else | |
873 | deadline = semaphore_deadline(sec, nsec); | |
874 | ||
91447636 A |
875 | kr = semaphore_wait_internal(semaphore, |
876 | SEMAPHORE_NULL, | |
b0d623f7 | 877 | deadline, option, |
91447636 A |
878 | caller_cont); |
879 | semaphore_dereference(semaphore); | |
880 | } | |
1c79356b A |
881 | return kr; |
882 | } | |
883 | ||
884 | /* | |
885 | * Routine: semaphore_wait_signal | |
886 | * | |
887 | * Atomically register a wait on a semaphore and THEN signal | |
888 | * another. This is the in-kernel entry point that does not | |
889 | * block at a continuation and does not free a signal_semaphore | |
890 | * reference. | |
891 | */ | |
892 | kern_return_t | |
893 | semaphore_wait_signal( | |
894 | semaphore_t wait_semaphore, | |
895 | semaphore_t signal_semaphore) | |
896 | { | |
897 | if (wait_semaphore == SEMAPHORE_NULL) | |
898 | return KERN_INVALID_ARGUMENT; | |
899 | ||
900 | return(semaphore_wait_internal(wait_semaphore, | |
901 | signal_semaphore, | |
b0d623f7 | 902 | 0ULL, SEMAPHORE_OPTION_NONE, |
1c79356b A |
903 | (void(*)(kern_return_t))0)); |
904 | } | |
905 | ||
906 | /* | |
907 | * Trap: semaphore_wait_signal_trap | |
908 | * | |
909 | * Atomically register a wait on a semaphore and THEN signal | |
910 | * another. This is the trap version from user space. | |
911 | */ | |
912 | kern_return_t | |
913 | semaphore_wait_signal_trap( | |
91447636 A |
914 | struct semaphore_wait_signal_trap_args *args) |
915 | { | |
916 | return(semaphore_wait_signal_trap_internal(args->wait_name, args->signal_name, thread_syscall_return)); | |
917 | } | |
918 | ||
919 | kern_return_t | |
920 | semaphore_wait_signal_trap_internal( | |
921 | mach_port_name_t wait_name, | |
922 | mach_port_name_t signal_name, | |
923 | void (*caller_cont)(kern_return_t)) | |
1c79356b A |
924 | { |
925 | semaphore_t wait_semaphore; | |
926 | semaphore_t signal_semaphore; | |
927 | kern_return_t kr; | |
928 | ||
929 | kr = port_name_to_semaphore(signal_name, &signal_semaphore); | |
91447636 A |
930 | if (kr == KERN_SUCCESS) { |
931 | kr = port_name_to_semaphore(wait_name, &wait_semaphore); | |
932 | if (kr == KERN_SUCCESS) { | |
933 | kr = semaphore_wait_internal(wait_semaphore, | |
934 | signal_semaphore, | |
b0d623f7 | 935 | 0ULL, SEMAPHORE_OPTION_NONE, |
91447636 A |
936 | caller_cont); |
937 | semaphore_dereference(wait_semaphore); | |
938 | } | |
1c79356b | 939 | semaphore_dereference(signal_semaphore); |
1c79356b | 940 | } |
1c79356b A |
941 | return kr; |
942 | } | |
943 | ||
944 | ||
945 | /* | |
946 | * Routine: semaphore_timedwait_signal | |
947 | * | |
948 | * Atomically register a wait on a semaphore and THEN signal | |
949 | * another. This is the in-kernel entry point that does not | |
950 | * block at a continuation. | |
951 | * | |
952 | * A timeout of {0,0} is considered non-blocking. | |
953 | */ | |
954 | kern_return_t | |
955 | semaphore_timedwait_signal( | |
956 | semaphore_t wait_semaphore, | |
957 | semaphore_t signal_semaphore, | |
958 | mach_timespec_t wait_time) | |
959 | { | |
b0d623f7 A |
960 | int option = SEMAPHORE_OPTION_NONE; |
961 | uint64_t deadline = 0; | |
962 | ||
1c79356b A |
963 | if (wait_semaphore == SEMAPHORE_NULL) |
964 | return KERN_INVALID_ARGUMENT; | |
965 | ||
966 | if(BAD_MACH_TIMESPEC(&wait_time)) | |
967 | return KERN_INVALID_VALUE; | |
b0d623f7 A |
968 | |
969 | if (wait_time.tv_sec == 0 && wait_time.tv_nsec == 0) | |
970 | option = SEMAPHORE_TIMEOUT_NOBLOCK; | |
971 | else | |
972 | deadline = semaphore_deadline(wait_time.tv_sec, wait_time.tv_nsec); | |
1c79356b A |
973 | |
974 | return(semaphore_wait_internal(wait_semaphore, | |
975 | signal_semaphore, | |
b0d623f7 | 976 | deadline, option, |
1c79356b A |
977 | (void(*)(kern_return_t))0)); |
978 | } | |
979 | ||
980 | /* | |
981 | * Trap: semaphore_timedwait_signal_trap | |
982 | * | |
983 | * Atomically register a timed wait on a semaphore and THEN signal | |
984 | * another. This is the trap version from user space. | |
985 | */ | |
986 | kern_return_t | |
987 | semaphore_timedwait_signal_trap( | |
91447636 A |
988 | struct semaphore_timedwait_signal_trap_args *args) |
989 | { | |
990 | return(semaphore_timedwait_signal_trap_internal(args->wait_name, args->signal_name, args->sec, args->nsec, thread_syscall_return)); | |
991 | } | |
992 | ||
993 | kern_return_t | |
994 | semaphore_timedwait_signal_trap_internal( | |
995 | mach_port_name_t wait_name, | |
996 | mach_port_name_t signal_name, | |
997 | unsigned int sec, | |
998 | clock_res_t nsec, | |
999 | void (*caller_cont)(kern_return_t)) | |
1c79356b A |
1000 | { |
1001 | semaphore_t wait_semaphore; | |
1002 | semaphore_t signal_semaphore; | |
1003 | mach_timespec_t wait_time; | |
1004 | kern_return_t kr; | |
1005 | ||
1006 | wait_time.tv_sec = sec; | |
1007 | wait_time.tv_nsec = nsec; | |
1008 | if(BAD_MACH_TIMESPEC(&wait_time)) | |
1009 | return KERN_INVALID_VALUE; | |
1010 | ||
1011 | kr = port_name_to_semaphore(signal_name, &signal_semaphore); | |
91447636 A |
1012 | if (kr == KERN_SUCCESS) { |
1013 | kr = port_name_to_semaphore(wait_name, &wait_semaphore); | |
1014 | if (kr == KERN_SUCCESS) { | |
b0d623f7 A |
1015 | int option = SEMAPHORE_OPTION_NONE; |
1016 | uint64_t deadline = 0; | |
1017 | ||
1018 | if (sec == 0 && nsec == 0) | |
1019 | option = SEMAPHORE_TIMEOUT_NOBLOCK; | |
1020 | else | |
1021 | deadline = semaphore_deadline(sec, nsec); | |
1022 | ||
91447636 A |
1023 | kr = semaphore_wait_internal(wait_semaphore, |
1024 | signal_semaphore, | |
b0d623f7 | 1025 | deadline, option, |
91447636 A |
1026 | caller_cont); |
1027 | semaphore_dereference(wait_semaphore); | |
1028 | } | |
1c79356b | 1029 | semaphore_dereference(signal_semaphore); |
1c79356b | 1030 | } |
1c79356b A |
1031 | return kr; |
1032 | } | |
1033 | ||
1034 | ||
1035 | /* | |
1036 | * Routine: semaphore_reference | |
1037 | * | |
1038 | * Take out a reference on a semaphore. This keeps the data structure | |
1039 | * in existence (but the semaphore may be deactivated). | |
1040 | */ | |
1041 | void | |
1042 | semaphore_reference( | |
1043 | semaphore_t semaphore) | |
1044 | { | |
b0d623f7 | 1045 | (void)hw_atomic_add(&semaphore->ref_count, 1); |
1c79356b A |
1046 | } |
1047 | ||
1048 | /* | |
1049 | * Routine: semaphore_dereference | |
1050 | * | |
1051 | * Release a reference on a semaphore. If this is the last reference, | |
1052 | * the semaphore data structure is deallocated. | |
1053 | */ | |
1054 | void | |
1055 | semaphore_dereference( | |
1056 | semaphore_t semaphore) | |
1057 | { | |
1058 | int ref_count; | |
1c79356b A |
1059 | |
1060 | if (semaphore != NULL) { | |
b0d623f7 | 1061 | ref_count = hw_atomic_sub(&semaphore->ref_count, 1); |
1c79356b | 1062 | |
b0d623f7 | 1063 | if (ref_count == 0) { |
1c79356b | 1064 | assert(wait_queue_empty(&semaphore->wait_queue)); |
b0d623f7 | 1065 | ipc_port_dealloc_kernel(semaphore->port); |
91447636 | 1066 | zfree(semaphore_zone, semaphore); |
b0d623f7 | 1067 | } |
1c79356b A |
1068 | } |
1069 | } |