Path: blob/master/3rdparty/openexr/IlmThread/IlmThreadSemaphoreWin32.cpp
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2005, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////3334//-----------------------------------------------------------------------------35//36// class Semaphore -- implementation for Windows37//38//-----------------------------------------------------------------------------3940#include "IlmThreadSemaphore.h"41#include "Iex.h"42#include <string>43#include <assert.h>44#include <iostream>4546namespace IlmThread {4748using namespace Iex;4950namespace {5152std::string53errorString ()54{55LPSTR messageBuffer;56DWORD bufferLength;57std::string message;5859//60// Call FormatMessage() to allow for message61// text to be acquired from the system.62//6364if (bufferLength = FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |65FORMAT_MESSAGE_IGNORE_INSERTS |66FORMAT_MESSAGE_FROM_SYSTEM,670,68GetLastError (),69MAKELANGID (LANG_NEUTRAL,70SUBLANG_DEFAULT),71(LPSTR) &messageBuffer,720,73NULL))74{75message = messageBuffer;76LocalFree (messageBuffer);77}7879return message;80}8182} // namespace838485Semaphore::Semaphore (unsigned int value)86{87if ((_semaphore = ::CreateSemaphore (0, value, 0x7fffffff, 0)) == 0)88{89THROW (LogicExc, "Could not create semaphore "90"(" << errorString() << ").");91}92}939495Semaphore::~Semaphore()96{97bool ok = ::CloseHandle (_semaphore) != FALSE;98assert (ok);99}100101102void103Semaphore::wait()104{105if (::WaitForSingleObject (_semaphore, INFINITE) != WAIT_OBJECT_0)106{107THROW (LogicExc, "Could not wait on semaphore "108"(" << errorString() << ").");109}110}111112113bool114Semaphore::tryWait()115{116return ::WaitForSingleObject (_semaphore, 0) == WAIT_OBJECT_0;117}118119120void121Semaphore::post()122{123if (!::ReleaseSemaphore (_semaphore, 1, 0))124{125THROW (LogicExc, "Could not post on semaphore "126"(" << errorString() << ").");127}128}129130131int132Semaphore::value() const133{134LONG v = -1;135136if (!::ReleaseSemaphore (_semaphore, 0, &v) || v < 0)137{138THROW (LogicExc, "Could not get value of semaphore "139"(" << errorString () << ").");140}141142return v;143}144145} // namespace IlmThread146147148