Files
rocm-systems/rocclr/compiler/lib/promotions/oclutils/thread/semaphore.cpp
T
foreman 3607d18592 P4 to Git Change 1179091 by nhaustov@nhaustov_hsa on 2015/08/11 05:48:01
ECR #333756 - Merge oclutils from runtime.

	Also updates temp file handling code to use pid/atomic which should fix Bug 10793.
	Note, changed the functions to use different prefix to avoid clash with runtime code.

	Reviewed by: Laurent Morichetti
	Testing: smoke, pre-checkin

Affected files ...

... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/os/alloc.cpp#2 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/os/os.cpp#5 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/os/os.hpp#6 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/os/os_posix.cpp#10 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/os/os_win32.cpp#5 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/thread/atomic.hpp#4 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/thread/monitor.cpp#2 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/thread/monitor.hpp#2 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/thread/semaphore.cpp#2 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/thread/semaphore.hpp#3 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/thread/thread.cpp#3 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/thread/thread.hpp#3 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/utils/debug.hpp#2 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/utils/macros.hpp#3 integrate
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/utils/traits.hpp#2 delete
... //depot/stg/opencl/drivers/opencl/compiler/lib/promotions/oclutils/utils/util.hpp#2 integrate
2015-08-11 06:09:15 -04:00

96 řádky
2.0 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_.load(std::memory_order_relaxed);
for (;;) {
if (state > 0) {
int newstate = state_.load(std::memory_order_acquire);
if (state == newstate) {
return;
}
state = newstate;
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