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