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 #ifndef __OS_SEMAPHORE_PRIVATE__
22 #define __OS_SEMAPHORE_PRIVATE__
24 #include <Availability.h>
26 #include <os/base_private.h>
29 OS_ASSUME_NONNULL_BEGIN
33 #define OS_SEMAPHORE_SPI_VERSION 20130313
35 typedef uintptr_t os_semaphore_t
;
37 __OSX_AVAILABLE_STARTING(__MAC_10_9
,__IPHONE_7_0
)
38 OS_EXPORT OS_WARN_RESULT OS_NOTHROW
39 os_semaphore_t
_os_semaphore_create(void);
41 __OSX_AVAILABLE_STARTING(__MAC_10_9
,__IPHONE_7_0
)
43 void _os_semaphore_dispose(os_semaphore_t
);
45 __OSX_AVAILABLE_STARTING(__MAC_10_9
,__IPHONE_7_0
)
47 void _os_semaphore_wait(os_semaphore_t
);
49 __OSX_AVAILABLE_STARTING(__MAC_10_9
,__IPHONE_7_0
)
51 void _os_semaphore_signal(os_semaphore_t
);
53 OS_WARN_RESULT OS_NOTHROW
54 __header_always_inline os_semaphore_t
55 os_get_cached_semaphore(void)
58 sema
= (os_semaphore_t
)_os_tsd_get_direct(__TSD_SEMAPHORE_CACHE
);
59 if (os_unlikely(!sema
)) {
60 return _os_semaphore_create();
62 _os_tsd_set_direct(__TSD_SEMAPHORE_CACHE
, 0);
67 __header_always_inline
void
68 os_put_cached_semaphore(os_semaphore_t sema
)
70 os_semaphore_t old_sema
;
71 old_sema
= (os_semaphore_t
)_os_tsd_get_direct(__TSD_SEMAPHORE_CACHE
);
72 _os_tsd_set_direct(__TSD_SEMAPHORE_CACHE
, (void*)sema
);
73 if (os_unlikely(old_sema
)) {
74 return _os_semaphore_dispose(old_sema
);
79 __header_always_inline
void
80 os_semaphore_wait(os_semaphore_t sema
)
82 return _os_semaphore_wait(sema
);
86 __header_always_inline
void
87 os_semaphore_signal(os_semaphore_t sema
)
89 return _os_semaphore_signal(sema
);
96 #endif // __OS_SEMAPHORE_PRIVATE__