]>
git.saurik.com Git - apple/libplatform.git/blob - src/os/semaphore.c
2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
21 #include "os/internal.h"
22 #include <mach/mach_init.h>
23 #include <mach/semaphore.h>
24 #include <mach/task.h>
25 #include <mach/thread_switch.h>
27 #define OS_VERIFY_MIG(x, msg) do { \
28 if (unlikely((x) == MIG_REPLY_MISMATCH)) { \
29 __LIBPLATFORM_CLIENT_CRASH__(x, msg); \
33 #define OS_SEMAPHORE_VERIFY_KR(x, msg) do { \
35 __LIBPLATFORM_CLIENT_CRASH__(x, msg); \
40 _os_semaphore_create(void)
44 kr
= semaphore_create(mach_task_self(), &s4
, SYNC_POLICY_FIFO
, 0);
45 OS_VERIFY_MIG(kr
, "Allocating semaphore failed with MIG_REPLY_MISMATCH");
46 OS_SEMAPHORE_VERIFY_KR(kr
, "Creating semaphore failed, possible port leak");
47 return (os_semaphore_t
)s4
;
51 _os_semaphore_dispose(os_semaphore_t sema
)
53 semaphore_t s4
= (semaphore_t
)sema
;
54 kern_return_t kr
= semaphore_destroy(mach_task_self(), s4
);
55 OS_SEMAPHORE_VERIFY_KR(kr
, "Destroying semaphore failed");
59 _os_semaphore_signal(os_semaphore_t sema
)
61 semaphore_t s4
= (semaphore_t
)sema
;
62 kern_return_t kr
= semaphore_signal(s4
);
63 OS_SEMAPHORE_VERIFY_KR(kr
, "Signaling semaphore failed");
67 _os_semaphore_wait(os_semaphore_t sema
)
69 semaphore_t s4
= (semaphore_t
)sema
;
72 kr
= semaphore_wait(s4
);
73 } while (unlikely(kr
== KERN_ABORTED
));
74 OS_SEMAPHORE_VERIFY_KR(kr
, "Waiting on semaphore failed");