1
///////////////////////////////////////////////////////////////////////////
3
// Copyright (c) 2005, Industrial Light & Magic, a division of Lucas
6
// All rights reserved.
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
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
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.
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.
33
///////////////////////////////////////////////////////////////////////////
35
//-----------------------------------------------------------------------------
37
// class Semaphore -- implementation for for platforms that do
38
// support Posix threads but do not support Posix semaphores,
41
//-----------------------------------------------------------------------------
43
#include "IlmBaseConfig.h"
45
#if HAVE_PTHREAD && !HAVE_POSIX_SEMAPHORES
47
#include "IlmThreadSemaphore.h"
54
Semaphore::Semaphore (unsigned int value)
56
if (int error = ::pthread_mutex_init (&_semaphore.mutex, 0))
57
Iex::throwErrnoExc ("Cannot initialize mutex (%T).", error);
59
if (int error = ::pthread_cond_init (&_semaphore.nonZero, 0))
60
Iex::throwErrnoExc ("Cannot initialize condition variable (%T).",
63
_semaphore.count = value;
64
_semaphore.numWaiting = 0;
68
Semaphore::~Semaphore ()
70
int error = ::pthread_cond_destroy (&_semaphore.nonZero);
72
error = ::pthread_mutex_destroy (&_semaphore.mutex);
80
::pthread_mutex_lock (&_semaphore.mutex);
82
_semaphore.numWaiting++;
84
while (_semaphore.count == 0)
86
if (int error = ::pthread_cond_wait (&_semaphore.nonZero,
89
::pthread_mutex_unlock (&_semaphore.mutex);
91
Iex::throwErrnoExc ("Cannot wait on condition variable (%T).",
96
_semaphore.numWaiting--;
99
::pthread_mutex_unlock (&_semaphore.mutex);
104
Semaphore::tryWait ()
106
::pthread_mutex_lock (&_semaphore.mutex);
108
if (_semaphore.count == 0)
110
::pthread_mutex_unlock (&_semaphore.mutex);
116
::pthread_mutex_unlock (&_semaphore.mutex);
125
::pthread_mutex_lock (&_semaphore.mutex);
127
if (_semaphore.numWaiting > 0)
129
if (int error = ::pthread_cond_signal (&_semaphore.nonZero))
131
::pthread_mutex_unlock (&_semaphore.mutex);
133
Iex::throwErrnoExc ("Cannot signal condition variable (%T).",
139
::pthread_mutex_unlock (&_semaphore.mutex);
144
Semaphore::value () const
146
::pthread_mutex_lock (&_semaphore.mutex);
147
int value = _semaphore.count;
148
::pthread_mutex_unlock (&_semaphore.mutex);
153
} // namespace IlmThread
1
///////////////////////////////////////////////////////////////////////////
3
// Copyright (c) 2005, Industrial Light & Magic, a division of Lucas
6
// All rights reserved.
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
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
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.
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.
33
///////////////////////////////////////////////////////////////////////////
35
//-----------------------------------------------------------------------------
37
// class Semaphore -- implementation for for platforms that do
38
// support Posix threads but do not support Posix semaphores,
41
//-----------------------------------------------------------------------------
43
#include "IlmBaseConfig.h"
45
#if HAVE_PTHREAD && !HAVE_POSIX_SEMAPHORES
47
#include "IlmThreadSemaphore.h"
54
Semaphore::Semaphore (unsigned int value)
56
if (int error = ::pthread_mutex_init (&_semaphore.mutex, 0))
57
Iex::throwErrnoExc ("Cannot initialize mutex (%T).", error);
59
if (int error = ::pthread_cond_init (&_semaphore.nonZero, 0))
60
Iex::throwErrnoExc ("Cannot initialize condition variable (%T).",
63
_semaphore.count = value;
64
_semaphore.numWaiting = 0;
68
Semaphore::~Semaphore ()
70
int error = ::pthread_cond_destroy (&_semaphore.nonZero);
72
error = ::pthread_mutex_destroy (&_semaphore.mutex);
80
::pthread_mutex_lock (&_semaphore.mutex);
82
_semaphore.numWaiting++;
84
while (_semaphore.count == 0)
86
if (int error = ::pthread_cond_wait (&_semaphore.nonZero,
89
::pthread_mutex_unlock (&_semaphore.mutex);
91
Iex::throwErrnoExc ("Cannot wait on condition variable (%T).",
96
_semaphore.numWaiting--;
99
::pthread_mutex_unlock (&_semaphore.mutex);
104
Semaphore::tryWait ()
106
::pthread_mutex_lock (&_semaphore.mutex);
108
if (_semaphore.count == 0)
110
::pthread_mutex_unlock (&_semaphore.mutex);
116
::pthread_mutex_unlock (&_semaphore.mutex);
125
::pthread_mutex_lock (&_semaphore.mutex);
127
if (_semaphore.numWaiting > 0)
129
if (int error = ::pthread_cond_signal (&_semaphore.nonZero))
131
::pthread_mutex_unlock (&_semaphore.mutex);
133
Iex::throwErrnoExc ("Cannot signal condition variable (%T).",
139
::pthread_mutex_unlock (&_semaphore.mutex);
144
Semaphore::value () const
146
::pthread_mutex_lock (&_semaphore.mutex);
147
int value = _semaphore.count;
148
::pthread_mutex_unlock (&_semaphore.mutex);
153
} // namespace IlmThread