13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef _DESEMAPHORE_HPP
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define _DESEMAPHORE_HPP
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements C++ Base Library
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * -----------------------------
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief deSemaphore C++ wrapper.
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deDefs.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deSemaphore.h"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace de
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*!
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Semaphore
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Semaphore provides standard semaphore functionality.
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Semaphore is thread-safe counter that can be used to control access
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * to a set of resources. The count can be incremented and decremented.
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * When decrementing causes counter to reach negative value, it will
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * block until increment has been called from an another thread.
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass Semaphore
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					Semaphore		(int initialValue, deUint32 flags = 0);
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					~Semaphore		(void);
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void			increment		(void) throw();
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void			decrement		(void) throw();
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool			tryDecrement	(void) throw();
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					Semaphore		(const Semaphore& other); // Not allowed!
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Semaphore&		operator=		(const Semaphore& other); // Not allowed!
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deSemaphore		m_semaphore;
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Inline implementations.
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*!
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Increment semaphore count.
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Incremeting increases semaphore value by 1. If a value is currently
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * negative (there is a thread waiting in decrement) the waiting thread
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * will be resumed.
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Incrementing semaphore will never block.
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline void Semaphore::increment (void) throw()
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deSemaphore_increment(m_semaphore);
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*!
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Decrement semaphore count.
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Decrementing decreases semaphore value by 1. If resulting value is negative
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * (only -1 is possible) decrement() will block until the value is again 0
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * (increment() has been called).
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * If there is an another thread waiting in decrement(), the current thread
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * will block until other thread(s) have been resumed.
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline void Semaphore::decrement (void) throw()
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deSemaphore_decrement(m_semaphore);
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*!
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Try to decrement semaphore value.
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \return true if decrementing was successful without blocking, false
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *		   otherwise
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * This function will never block, i.e. it will return false if decrementing
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * semaphore counter would result in negative value or there is already
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * one or more threads waiting for this semaphore.
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyryinline bool Semaphore::tryDecrement (void) throw()
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return deSemaphore_tryDecrement(m_semaphore) == DE_TRUE;
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // de
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif // _DESEMAPHORE_HPP
107