2014-07-04 16:17:05 -04:00
|
|
|
//
|
|
|
|
|
// Copyright (c) 2008,2010 Advanced Micro Devices, Inc. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#ifndef SEMAPHORE_HPP_
|
|
|
|
|
#define SEMAPHORE_HPP_
|
|
|
|
|
|
|
|
|
|
#include "top.hpp"
|
|
|
|
|
#include "utils/util.hpp"
|
|
|
|
|
|
2014-09-29 17:38:55 -04:00
|
|
|
#include <atomic>
|
2014-07-04 16:17:05 -04:00
|
|
|
#if defined(__linux__)
|
2017-04-13 13:56:38 -04:00
|
|
|
#include <semaphore.h>
|
2014-07-04 16:17:05 -04:00
|
|
|
#endif /*linux*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace amd {
|
|
|
|
|
|
|
|
|
|
/*! \addtogroup Threads
|
|
|
|
|
* @{
|
|
|
|
|
*
|
|
|
|
|
* \addtogroup Synchronization
|
|
|
|
|
* @{
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class Thread;
|
|
|
|
|
|
|
|
|
|
//! \brief Counting semaphore
|
2017-04-13 13:56:38 -04:00
|
|
|
class Semaphore : public HeapObject {
|
|
|
|
|
private:
|
|
|
|
|
std::atomic_int state_; //!< This semaphore's value.
|
2014-07-04 16:17:05 -04:00
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2017-04-13 13:56:38 -04:00
|
|
|
void* handle_; //!< The semaphore object's handle.
|
|
|
|
|
char padding_[64 - sizeof(void*) - sizeof(std::atomic_int)];
|
|
|
|
|
#else // !_WIN32
|
|
|
|
|
sem_t sem_; //!< The semaphore object's identifier.
|
|
|
|
|
char padding_[64 - sizeof(sem_t) - sizeof(std::atomic_int)];
|
2014-07-04 16:17:05 -04:00
|
|
|
#endif /*!_WIN32*/
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
public:
|
|
|
|
|
Semaphore();
|
|
|
|
|
~Semaphore();
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
//! \brief Decrement this semaphore
|
|
|
|
|
void wait();
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
//! \brief Increment this semaphore
|
|
|
|
|
void post();
|
2014-07-04 16:17:05 -04:00
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
//! \brief Reset this semaphore.
|
|
|
|
|
void reset() { state_.store(0, std::memory_order_release); }
|
2014-07-04 16:17:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*! @}
|
|
|
|
|
* @}
|
|
|
|
|
*/
|
|
|
|
|
|
2017-04-13 13:56:38 -04:00
|
|
|
} // namespace amd
|
2014-07-04 16:17:05 -04:00
|
|
|
|
|
|
|
|
#endif /*SEMAPHORE_HPP_*/
|