1e5c31af7Sopenharmony_ci#ifndef _DESEMAPHORE_HPP
2e5c31af7Sopenharmony_ci#define _DESEMAPHORE_HPP
3e5c31af7Sopenharmony_ci/*-------------------------------------------------------------------------
4e5c31af7Sopenharmony_ci * drawElements C++ Base Library
5e5c31af7Sopenharmony_ci * -----------------------------
6e5c31af7Sopenharmony_ci *
7e5c31af7Sopenharmony_ci * Copyright 2014 The Android Open Source Project
8e5c31af7Sopenharmony_ci *
9e5c31af7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
10e5c31af7Sopenharmony_ci * you may not use this file except in compliance with the License.
11e5c31af7Sopenharmony_ci * You may obtain a copy of the License at
12e5c31af7Sopenharmony_ci *
13e5c31af7Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
14e5c31af7Sopenharmony_ci *
15e5c31af7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
16e5c31af7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
17e5c31af7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18e5c31af7Sopenharmony_ci * See the License for the specific language governing permissions and
19e5c31af7Sopenharmony_ci * limitations under the License.
20e5c31af7Sopenharmony_ci *
21e5c31af7Sopenharmony_ci *//*!
22e5c31af7Sopenharmony_ci * \file
23e5c31af7Sopenharmony_ci * \brief deSemaphore C++ wrapper.
24e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
25e5c31af7Sopenharmony_ci
26e5c31af7Sopenharmony_ci#include "deDefs.hpp"
27e5c31af7Sopenharmony_ci#include "deSemaphore.h"
28e5c31af7Sopenharmony_ci
29e5c31af7Sopenharmony_cinamespace de
30e5c31af7Sopenharmony_ci{
31e5c31af7Sopenharmony_ci
32e5c31af7Sopenharmony_ci/*--------------------------------------------------------------------*//*!
33e5c31af7Sopenharmony_ci * \brief Semaphore
34e5c31af7Sopenharmony_ci *
35e5c31af7Sopenharmony_ci * Semaphore provides standard semaphore functionality.
36e5c31af7Sopenharmony_ci *
37e5c31af7Sopenharmony_ci * Semaphore is thread-safe counter that can be used to control access
38e5c31af7Sopenharmony_ci * to a set of resources. The count can be incremented and decremented.
39e5c31af7Sopenharmony_ci * When decrementing causes counter to reach negative value, it will
40e5c31af7Sopenharmony_ci * block until increment has been called from an another thread.
41e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
42e5c31af7Sopenharmony_ciclass Semaphore
43e5c31af7Sopenharmony_ci{
44e5c31af7Sopenharmony_cipublic:
45e5c31af7Sopenharmony_ci					Semaphore		(int initialValue, deUint32 flags = 0);
46e5c31af7Sopenharmony_ci					~Semaphore		(void);
47e5c31af7Sopenharmony_ci
48e5c31af7Sopenharmony_ci	void			increment		(void) throw();
49e5c31af7Sopenharmony_ci	void			decrement		(void) throw();
50e5c31af7Sopenharmony_ci	bool			tryDecrement	(void) throw();
51e5c31af7Sopenharmony_ci
52e5c31af7Sopenharmony_ciprivate:
53e5c31af7Sopenharmony_ci					Semaphore		(const Semaphore& other); // Not allowed!
54e5c31af7Sopenharmony_ci	Semaphore&		operator=		(const Semaphore& other); // Not allowed!
55e5c31af7Sopenharmony_ci
56e5c31af7Sopenharmony_ci	deSemaphore		m_semaphore;
57e5c31af7Sopenharmony_ci};
58e5c31af7Sopenharmony_ci
59e5c31af7Sopenharmony_ci// Inline implementations.
60e5c31af7Sopenharmony_ci
61e5c31af7Sopenharmony_ci/*--------------------------------------------------------------------*//*!
62e5c31af7Sopenharmony_ci * \brief Increment semaphore count.
63e5c31af7Sopenharmony_ci *
64e5c31af7Sopenharmony_ci * Incremeting increases semaphore value by 1. If a value is currently
65e5c31af7Sopenharmony_ci * negative (there is a thread waiting in decrement) the waiting thread
66e5c31af7Sopenharmony_ci * will be resumed.
67e5c31af7Sopenharmony_ci *
68e5c31af7Sopenharmony_ci * Incrementing semaphore will never block.
69e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
70e5c31af7Sopenharmony_ciinline void Semaphore::increment (void) throw()
71e5c31af7Sopenharmony_ci{
72e5c31af7Sopenharmony_ci	deSemaphore_increment(m_semaphore);
73e5c31af7Sopenharmony_ci}
74e5c31af7Sopenharmony_ci
75e5c31af7Sopenharmony_ci/*--------------------------------------------------------------------*//*!
76e5c31af7Sopenharmony_ci * \brief Decrement semaphore count.
77e5c31af7Sopenharmony_ci *
78e5c31af7Sopenharmony_ci * Decrementing decreases semaphore value by 1. If resulting value is negative
79e5c31af7Sopenharmony_ci * (only -1 is possible) decrement() will block until the value is again 0
80e5c31af7Sopenharmony_ci * (increment() has been called).
81e5c31af7Sopenharmony_ci *
82e5c31af7Sopenharmony_ci * If there is an another thread waiting in decrement(), the current thread
83e5c31af7Sopenharmony_ci * will block until other thread(s) have been resumed.
84e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
85e5c31af7Sopenharmony_ciinline void Semaphore::decrement (void) throw()
86e5c31af7Sopenharmony_ci{
87e5c31af7Sopenharmony_ci	deSemaphore_decrement(m_semaphore);
88e5c31af7Sopenharmony_ci}
89e5c31af7Sopenharmony_ci
90e5c31af7Sopenharmony_ci/*--------------------------------------------------------------------*//*!
91e5c31af7Sopenharmony_ci * \brief Try to decrement semaphore value.
92e5c31af7Sopenharmony_ci * \return true if decrementing was successful without blocking, false
93e5c31af7Sopenharmony_ci *		   otherwise
94e5c31af7Sopenharmony_ci *
95e5c31af7Sopenharmony_ci * This function will never block, i.e. it will return false if decrementing
96e5c31af7Sopenharmony_ci * semaphore counter would result in negative value or there is already
97e5c31af7Sopenharmony_ci * one or more threads waiting for this semaphore.
98e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
99e5c31af7Sopenharmony_ciinline bool Semaphore::tryDecrement (void) throw()
100e5c31af7Sopenharmony_ci{
101e5c31af7Sopenharmony_ci	return deSemaphore_tryDecrement(m_semaphore) == DE_TRUE;
102e5c31af7Sopenharmony_ci}
103e5c31af7Sopenharmony_ci
104e5c31af7Sopenharmony_ci} // de
105e5c31af7Sopenharmony_ci
106e5c31af7Sopenharmony_ci#endif // _DESEMAPHORE_HPP
107