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