Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/3rdparty/openexr/IlmThread/IlmThreadSemaphoreWin32.cpp
16337 views
1
///////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (c) 2005, Industrial Light & Magic, a division of Lucas
4
// Digital Ltd. LLC
5
//
6
// All rights reserved.
7
//
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
10
// met:
11
// * Redistributions of source code must retain the above copyright
12
// notice, this list of conditions and the following disclaimer.
13
// * Redistributions in binary form must reproduce the above
14
// copyright notice, this list of conditions and the following disclaimer
15
// in the documentation and/or other materials provided with the
16
// distribution.
17
// * Neither the name of Industrial Light & Magic nor the names of
18
// its contributors may be used to endorse or promote products derived
19
// from this software without specific prior written permission.
20
//
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
//
33
///////////////////////////////////////////////////////////////////////////
34
35
//-----------------------------------------------------------------------------
36
//
37
// class Semaphore -- implementation for Windows
38
//
39
//-----------------------------------------------------------------------------
40
41
#include "IlmThreadSemaphore.h"
42
#include "Iex.h"
43
#include <string>
44
#include <assert.h>
45
#include <iostream>
46
47
namespace IlmThread {
48
49
using namespace Iex;
50
51
namespace {
52
53
std::string
54
errorString ()
55
{
56
LPSTR messageBuffer;
57
DWORD bufferLength;
58
std::string message;
59
60
//
61
// Call FormatMessage() to allow for message
62
// text to be acquired from the system.
63
//
64
65
if (bufferLength = FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
66
FORMAT_MESSAGE_IGNORE_INSERTS |
67
FORMAT_MESSAGE_FROM_SYSTEM,
68
0,
69
GetLastError (),
70
MAKELANGID (LANG_NEUTRAL,
71
SUBLANG_DEFAULT),
72
(LPSTR) &messageBuffer,
73
0,
74
NULL))
75
{
76
message = messageBuffer;
77
LocalFree (messageBuffer);
78
}
79
80
return message;
81
}
82
83
} // namespace
84
85
86
Semaphore::Semaphore (unsigned int value)
87
{
88
if ((_semaphore = ::CreateSemaphore (0, value, 0x7fffffff, 0)) == 0)
89
{
90
THROW (LogicExc, "Could not create semaphore "
91
"(" << errorString() << ").");
92
}
93
}
94
95
96
Semaphore::~Semaphore()
97
{
98
bool ok = ::CloseHandle (_semaphore) != FALSE;
99
assert (ok);
100
}
101
102
103
void
104
Semaphore::wait()
105
{
106
if (::WaitForSingleObject (_semaphore, INFINITE) != WAIT_OBJECT_0)
107
{
108
THROW (LogicExc, "Could not wait on semaphore "
109
"(" << errorString() << ").");
110
}
111
}
112
113
114
bool
115
Semaphore::tryWait()
116
{
117
return ::WaitForSingleObject (_semaphore, 0) == WAIT_OBJECT_0;
118
}
119
120
121
void
122
Semaphore::post()
123
{
124
if (!::ReleaseSemaphore (_semaphore, 1, 0))
125
{
126
THROW (LogicExc, "Could not post on semaphore "
127
"(" << errorString() << ").");
128
}
129
}
130
131
132
int
133
Semaphore::value() const
134
{
135
LONG v = -1;
136
137
if (!::ReleaseSemaphore (_semaphore, 0, &v) || v < 0)
138
{
139
THROW (LogicExc, "Could not get value of semaphore "
140
"(" << errorString () << ").");
141
}
142
143
return v;
144
}
145
146
} // namespace IlmThread
147
148