• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 platforms
38 //	that support Posix threads and Posix semaphores
39 //
40 //-----------------------------------------------------------------------------
41 
42 #include "IlmBaseConfig.h"
43 
44 #if HAVE_PTHREAD && HAVE_POSIX_SEMAPHORES
45 
46 #include "IlmThreadSemaphore.h"
47 #include "Iex.h"
48 #include <assert.h>
49 
50 namespace IlmThread {
51 
52 
Semaphore(unsigned int value)53 Semaphore::Semaphore (unsigned int value)
54 {
55     if (::sem_init (&_semaphore, 0, value))
56     Iex::throwErrnoExc ("Cannot initialize semaphore (%T).");
57 }
58 
59 
~Semaphore()60 Semaphore::~Semaphore ()
61 {
62     int error = ::sem_destroy (&_semaphore);
63     assert (error == 0);
64 }
65 
66 
67 void
wait()68 Semaphore::wait ()
69 {
70     ::sem_wait (&_semaphore);
71 }
72 
73 
74 bool
tryWait()75 Semaphore::tryWait ()
76 {
77     return sem_trywait (&_semaphore) == 0;
78 }
79 
80 
81 void
post()82 Semaphore::post ()
83 {
84     if (::sem_post (&_semaphore))
85         Iex::throwErrnoExc ("Post operation on semaphore failed (%T).");
86 }
87 
88 
89 int
value() const90 Semaphore::value () const
91 {
92     int value;
93 
94     if (::sem_getvalue (&_semaphore, &value))
95         Iex::throwErrnoExc ("Cannot read semaphore value (%T).");
96 
97     return value;
98 }
99 
100 
101 } // namespace IlmThread
102 
103 #endif
104