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@
22 * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch
23 * which are subject to change in future releases of Mac OS X. Any applications
24 * relying on these interfaces WILL break.
27 #ifndef __DISPATCH_SEMAPHORE_INTERNAL__
28 #define __DISPATCH_SEMAPHORE_INTERNAL__
30 struct dispatch_queue_s
;
32 DISPATCH_CLASS_DECL(semaphore
);
33 struct dispatch_semaphore_s
{
34 DISPATCH_STRUCT_HEADER(semaphore
);
36 semaphore_t dsema_port
;
42 #error "No supported semaphore type"
45 long volatile dsema_value
;
47 long volatile dsema_sent_ksignals
;
48 long volatile dsema_group_waiters
;
50 struct dispatch_continuation_s
*volatile dsema_notify_head
;
51 struct dispatch_continuation_s
*volatile dsema_notify_tail
;
54 DISPATCH_CLASS_DECL(group
);
56 void _dispatch_semaphore_dispose(dispatch_object_t dou
);
57 size_t _dispatch_semaphore_debug(dispatch_object_t dou
, char *buf
,
60 typedef uintptr_t _dispatch_thread_semaphore_t
;
62 _dispatch_thread_semaphore_t
_dispatch_thread_semaphore_create(void);
63 void _dispatch_thread_semaphore_dispose(_dispatch_thread_semaphore_t
);
64 void _dispatch_thread_semaphore_wait(_dispatch_thread_semaphore_t
);
65 void _dispatch_thread_semaphore_signal(_dispatch_thread_semaphore_t
);
67 DISPATCH_ALWAYS_INLINE
68 static inline _dispatch_thread_semaphore_t
69 _dispatch_get_thread_semaphore(void)
71 _dispatch_thread_semaphore_t sema
= (_dispatch_thread_semaphore_t
)
72 _dispatch_thread_getspecific(dispatch_sema4_key
);
73 if (slowpath(!sema
)) {
74 return _dispatch_thread_semaphore_create();
76 _dispatch_thread_setspecific(dispatch_sema4_key
, NULL
);
80 DISPATCH_ALWAYS_INLINE
82 _dispatch_put_thread_semaphore(_dispatch_thread_semaphore_t sema
)
84 _dispatch_thread_semaphore_t old_sema
= (_dispatch_thread_semaphore_t
)
85 _dispatch_thread_getspecific(dispatch_sema4_key
);
86 _dispatch_thread_setspecific(dispatch_sema4_key
, (void*)sema
);
87 if (slowpath(old_sema
)) {
88 return _dispatch_thread_semaphore_dispose(old_sema
);