Home | History | Annotate | Download | only in decpp
      1 #ifndef _DESEMAPHORE_HPP
      2 #define _DESEMAPHORE_HPP
      3 /*-------------------------------------------------------------------------
      4  * drawElements C++ Base Library
      5  * -----------------------------
      6  *
      7  * Copyright 2014 The Android Open Source Project
      8  *
      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
     12  *
     13  *      http://www.apache.org/licenses/LICENSE-2.0
     14  *
     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.
     20  *
     21  *//*!
     22  * \file
     23  * \brief deSemaphore C++ wrapper.
     24  *//*--------------------------------------------------------------------*/
     25 
     26 #include "deDefs.hpp"
     27 #include "deSemaphore.h"
     28 
     29 namespace de
     30 {
     31 
     32 /*--------------------------------------------------------------------*//*!
     33  * \brief Semaphore
     34  *
     35  * Semaphore provides standard semaphore functionality.
     36  *
     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  *//*--------------------------------------------------------------------*/
     42 class Semaphore
     43 {
     44 public:
     45 					Semaphore		(int initialValue, deUint32 flags = 0);
     46 					~Semaphore		(void);
     47 
     48 	void			increment		(void) throw();
     49 	void			decrement		(void) throw();
     50 	bool			tryDecrement	(void) throw();
     51 
     52 private:
     53 					Semaphore		(const Semaphore& other); // Not allowed!
     54 	Semaphore&		operator=		(const Semaphore& other); // Not allowed!
     55 
     56 	deSemaphore		m_semaphore;
     57 };
     58 
     59 // Inline implementations.
     60 
     61 /*--------------------------------------------------------------------*//*!
     62  * \brief Increment semaphore count.
     63  *
     64  * Incremeting increases semaphore value by 1. If a value is currently
     65  * negative (there is a thread waiting in decrement) the waiting thread
     66  * will be resumed.
     67  *
     68  * Incrementing semaphore will never block.
     69  *//*--------------------------------------------------------------------*/
     70 inline void Semaphore::increment (void) throw()
     71 {
     72 	deSemaphore_increment(m_semaphore);
     73 }
     74 
     75 /*--------------------------------------------------------------------*//*!
     76  * \brief Decrement semaphore count.
     77  *
     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).
     81  *
     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()
     86 {
     87 	deSemaphore_decrement(m_semaphore);
     88 }
     89 
     90 /*--------------------------------------------------------------------*//*!
     91  * \brief Try to decrement semaphore value.
     92  * \return true if decrementing was successful without blocking, false
     93  *		   otherwise
     94  *
     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()
    100 {
    101 	return deSemaphore_tryDecrement(m_semaphore) == DE_TRUE;
    102 }
    103 
    104 } // de
    105 
    106 #endif // _DESEMAPHORE_HPP
    107