xref: /aosp_15_r20/external/deqp/framework/delibs/dethread/unix/deSemaphoreUnix.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 Unix implementation of semaphore.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "deSemaphore.h"
25 
26 #if (DE_OS == DE_OS_UNIX || DE_OS == DE_OS_ANDROID || DE_OS == DE_OS_SYMBIAN || DE_OS == DE_OS_QNX) || \
27     (DE_OS == DE_OS_FUCHSIA)
28 
29 #include "deMemory.h"
30 
31 #include <semaphore.h>
32 
33 DE_STATIC_ASSERT(sizeof(deSemaphore) >= sizeof(sem_t *));
34 
deSemaphore_create(int initialValue,const deSemaphoreAttributes * attributes)35 deSemaphore deSemaphore_create(int initialValue, const deSemaphoreAttributes *attributes)
36 {
37     sem_t *sem = (sem_t *)deMalloc(sizeof(sem_t));
38 
39     DE_UNREF(attributes);
40     DE_ASSERT(initialValue >= 0);
41 
42     if (!sem)
43         return 0;
44 
45     if (sem_init(sem, 0, (unsigned int)initialValue) != 0)
46     {
47         deFree(sem);
48         return 0;
49     }
50 
51     return (deSemaphore)sem;
52 }
53 
deSemaphore_destroy(deSemaphore semaphore)54 void deSemaphore_destroy(deSemaphore semaphore)
55 {
56     sem_t *sem = (sem_t *)semaphore;
57     DE_ASSERT(sem);
58     sem_destroy(sem);
59     deFree(sem);
60 }
61 
deSemaphore_increment(deSemaphore semaphore)62 void deSemaphore_increment(deSemaphore semaphore)
63 {
64     sem_t *sem = (sem_t *)semaphore;
65     int ret    = sem_post(sem);
66     DE_ASSERT(ret == 0);
67     DE_UNREF(ret);
68 }
69 
deSemaphore_decrement(deSemaphore semaphore)70 void deSemaphore_decrement(deSemaphore semaphore)
71 {
72     sem_t *sem = (sem_t *)semaphore;
73     int ret    = sem_wait(sem);
74     DE_ASSERT(ret == 0);
75     DE_UNREF(ret);
76 }
77 
deSemaphore_tryDecrement(deSemaphore semaphore)78 bool deSemaphore_tryDecrement(deSemaphore semaphore)
79 {
80     sem_t *sem = (sem_t *)semaphore;
81     DE_ASSERT(sem);
82     return (sem_trywait(sem) == 0);
83 }
84 
85 #endif /* DE_OS */
86