Files
rocm-systems/rocclr/runtime/thread/semaphore.cpp
T
foreman aaf533287b P4 to Git Change 1082950 by lmoriche@lmoriche_opencl_dev on 2014/09/30 16:50:37
ECR #304775 - Implement the changes recommended in review#5943

	Pre-checkin: http://ocltc.amd.com:8111/viewModification.html?modId=40717&personal=true&buildTypeId=&tab=vcsModificationBuilds&show_all_builds=true

Affected files ...

... //depot/stg/opencl/drivers/opencl/runtime/platform/command.cpp#65 edit
... //depot/stg/opencl/drivers/opencl/runtime/platform/memory.cpp#109 edit
... //depot/stg/opencl/drivers/opencl/runtime/thread/semaphore.cpp#7 edit
... //depot/stg/opencl/drivers/opencl/runtime/utils/concurrent.hpp#6 edit
2014-09-30 17:21:19 -04:00

94 строки
1.9 KiB
C++

//
// Copyright (c) 2008,2010 Advanced Micro Devices, Inc. All rights reserved.
//
#include "thread/semaphore.hpp"
#include "thread/thread.hpp"
#if defined(_WIN32) || defined(__CYGWIN__)
# include <windows.h>
#else // !_WIN32
# include <semaphore.h>
# include <errno.h>
#endif // !_WIN32
namespace amd {
Semaphore::Semaphore()
{
std::atomic_init(&state_, 0);
#ifdef _WIN32
handle_ = static_cast<void*>(CreateSemaphore(NULL, 0, LONG_MAX, NULL));
assert(handle_ != NULL && "CreateSemaphore failed");
#else // !_WIN32
if (sem_init(&sem_, 0, 0) != 0) {
fatal("sem_init() failed");
}
#endif // !_WIN32
}
Semaphore::~Semaphore()
{
#ifdef _WIN32
if (!CloseHandle(static_cast<HANDLE>(handle_))) {
fatal("CloseHandle() failed");
}
#else // !_WIN32
if (sem_destroy(&sem_) != 0) {
fatal("sem_destroy() failed");
}
#endif // !WIN32
}
void
Semaphore::post()
{
int state = state_;
for (;;) {
if (state > 0) {
if (state == state_.load(std::memory_order_acquire)) {
return;
}
continue;
}
if (state_.compare_exchange_weak(state, state+1,
std::memory_order_acq_rel, std::memory_order_acquire)) {
break;
}
}
if (state < 0) {
// We have threads waiting on this event.
#ifdef _WIN32
ReleaseSemaphore(static_cast<HANDLE>(handle_), 1, NULL);
#else // !_WIN32
if (0 != sem_post(&sem_)) {
fatal("sem_post() failed");
}
#endif // !_WIN32
}
}
void
Semaphore::wait()
{
if (state_-- > 0) {
return;
}
#ifdef _WIN32
if (WAIT_OBJECT_0 != WaitForSingleObject(
static_cast<HANDLE>(handle_), INFINITE)) {
fatal("WaitForSingleObject failed");
}
#else // !_WIN32
while (0 != sem_wait(&sem_)) {
if (EINTR != errno) {
fatal("sem_wait() failed");
}
}
#endif // !_WIN32
}
} // namespace amd