1*9203SMark.Logan@Sun.COM /*******************************************************************************
2*9203SMark.Logan@Sun.COM * Copyright (C) 2004-2008 Intel Corp. All rights reserved.
3*9203SMark.Logan@Sun.COM *
4*9203SMark.Logan@Sun.COM * Redistribution and use in source and binary forms, with or without
5*9203SMark.Logan@Sun.COM * modification, are permitted provided that the following conditions are met:
6*9203SMark.Logan@Sun.COM *
7*9203SMark.Logan@Sun.COM * - Redistributions of source code must retain the above copyright notice,
8*9203SMark.Logan@Sun.COM * this list of conditions and the following disclaimer.
9*9203SMark.Logan@Sun.COM *
10*9203SMark.Logan@Sun.COM * - Redistributions in binary form must reproduce the above copyright notice,
11*9203SMark.Logan@Sun.COM * this list of conditions and the following disclaimer in the documentation
12*9203SMark.Logan@Sun.COM * and/or other materials provided with the distribution.
13*9203SMark.Logan@Sun.COM *
14*9203SMark.Logan@Sun.COM * - Neither the name of Intel Corp. nor the names of its
15*9203SMark.Logan@Sun.COM * contributors may be used to endorse or promote products derived from this
16*9203SMark.Logan@Sun.COM * software without specific prior written permission.
17*9203SMark.Logan@Sun.COM *
18*9203SMark.Logan@Sun.COM * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
19*9203SMark.Logan@Sun.COM * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*9203SMark.Logan@Sun.COM * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*9203SMark.Logan@Sun.COM * ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS
22*9203SMark.Logan@Sun.COM * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*9203SMark.Logan@Sun.COM * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*9203SMark.Logan@Sun.COM * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*9203SMark.Logan@Sun.COM * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*9203SMark.Logan@Sun.COM * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*9203SMark.Logan@Sun.COM * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*9203SMark.Logan@Sun.COM * POSSIBILITY OF SUCH DAMAGE.
29*9203SMark.Logan@Sun.COM *******************************************************************************/
30*9203SMark.Logan@Sun.COM
31*9203SMark.Logan@Sun.COM //////////////////////////////////////////////////////////////////////////
32*9203SMark.Logan@Sun.COM // SemaphoreLinux.cpp
33*9203SMark.Logan@Sun.COM //
34*9203SMark.Logan@Sun.COM // This file contains the linux implementation of the Semaphore class
35*9203SMark.Logan@Sun.COM //////////////////////////////////////////////////////////////////////////
36*9203SMark.Logan@Sun.COM #ifdef HAVE_CONFIG_H
37*9203SMark.Logan@Sun.COM #include "config.h"
38*9203SMark.Logan@Sun.COM #endif
39*9203SMark.Logan@Sun.COM #include "Semaphore.h"
40*9203SMark.Logan@Sun.COM #include <semaphore.h>
41*9203SMark.Logan@Sun.COM
42*9203SMark.Logan@Sun.COM class OSSemaphore {
43*9203SMark.Logan@Sun.COM public:
44*9203SMark.Logan@Sun.COM sem_t _sem;
45*9203SMark.Logan@Sun.COM };
46*9203SMark.Logan@Sun.COM
Semaphore(int maxval)47*9203SMark.Logan@Sun.COM Semaphore::Semaphore(int maxval)
48*9203SMark.Logan@Sun.COM {
49*9203SMark.Logan@Sun.COM _osSemaphore = new OSSemaphore;
50*9203SMark.Logan@Sun.COM sem_init(&_osSemaphore->_sem, 0, maxval);
51*9203SMark.Logan@Sun.COM }
52*9203SMark.Logan@Sun.COM
~Semaphore()53*9203SMark.Logan@Sun.COM Semaphore::~Semaphore()
54*9203SMark.Logan@Sun.COM {
55*9203SMark.Logan@Sun.COM sem_destroy(&_osSemaphore->_sem);
56*9203SMark.Logan@Sun.COM delete _osSemaphore;
57*9203SMark.Logan@Sun.COM }
58*9203SMark.Logan@Sun.COM
acquire()59*9203SMark.Logan@Sun.COM void Semaphore::acquire()
60*9203SMark.Logan@Sun.COM {
61*9203SMark.Logan@Sun.COM // blocks until value !=0, and decrements the value
62*9203SMark.Logan@Sun.COM sem_wait(&_osSemaphore->_sem);
63*9203SMark.Logan@Sun.COM }
64*9203SMark.Logan@Sun.COM
release()65*9203SMark.Logan@Sun.COM void Semaphore::release()
66*9203SMark.Logan@Sun.COM {
67*9203SMark.Logan@Sun.COM // increments the value by 1
68*9203SMark.Logan@Sun.COM sem_post(&_osSemaphore->_sem);
69*9203SMark.Logan@Sun.COM }
70*9203SMark.Logan@Sun.COM
acquireTry()71*9203SMark.Logan@Sun.COM bool Semaphore::acquireTry()
72*9203SMark.Logan@Sun.COM {
73*9203SMark.Logan@Sun.COM // try to acquire the semaphore: return 'true' if succeded
74*9203SMark.Logan@Sun.COM return (sem_trywait(&_osSemaphore->_sem) == 0);
75*9203SMark.Logan@Sun.COM }
76*9203SMark.Logan@Sun.COM
77