xref: /aosp_15_r20/external/deqp/framework/delibs/dethread/unix/deSemaphoreMach.c (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 /*-------------------------------------------------------------------------
2  * drawElements Thread Library
3  * ---------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Mach implementation of semaphore.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "deSemaphore.h"
25 
26 #if (DE_OS == DE_OS_IOS || DE_OS == DE_OS_OSX)
27 
28 #include "deMemory.h"
29 
30 #include <mach/semaphore.h>
31 #include <mach/task.h>
32 #include <mach/mach_init.h>
33 
34 DE_STATIC_ASSERT(sizeof(deSemaphore) >= sizeof(semaphore_t));
35 
deSemaphore_create(int initialValue,const deSemaphoreAttributes * attributes)36 deSemaphore deSemaphore_create(int initialValue, const deSemaphoreAttributes *attributes)
37 {
38     semaphore_t sem;
39 
40     DE_UNREF(attributes);
41     DE_ASSERT(initialValue >= 0);
42 
43     if (semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, initialValue) != KERN_SUCCESS)
44         return 0;
45 
46     return (deSemaphore)sem;
47 }
48 
deSemaphore_destroy(deSemaphore semaphore)49 void deSemaphore_destroy(deSemaphore semaphore)
50 {
51     semaphore_t sem   = (semaphore_t)semaphore;
52     kern_return_t res = semaphore_destroy(mach_task_self(), sem);
53     DE_ASSERT(res == 0);
54     DE_UNREF(res);
55 }
56 
deSemaphore_increment(deSemaphore semaphore)57 void deSemaphore_increment(deSemaphore semaphore)
58 {
59     semaphore_t sem   = (semaphore_t)semaphore;
60     kern_return_t res = semaphore_signal(sem);
61     DE_ASSERT(res == 0);
62     DE_UNREF(res);
63 }
64 
deSemaphore_decrement(deSemaphore semaphore)65 void deSemaphore_decrement(deSemaphore semaphore)
66 {
67     semaphore_t sem   = (semaphore_t)semaphore;
68     kern_return_t res = semaphore_wait(sem);
69     DE_ASSERT(res == 0);
70     DE_UNREF(res);
71 }
72 
deSemaphore_tryDecrement(deSemaphore semaphore)73 bool deSemaphore_tryDecrement(deSemaphore semaphore)
74 {
75     semaphore_t sem    = (semaphore_t)semaphore;
76     mach_timespec_t ts = {0, 1}; /* one nanosecond */
77     return (semaphore_timedwait(sem, ts) == KERN_SUCCESS);
78 }
79 
80 #endif /* DE_OS */
81