/usr/include/raul/Semaphore.hpp is in libraul-dev 0.8.0+dfsg0-0.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | /* This file is part of Raul.
* Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
*
* Raul is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* Raul is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef RAUL_SEMAPHORE_HPP
#define RAUL_SEMAPHORE_HPP
#ifdef __APPLE__
#include <limits.h>
#include <CoreServices/CoreServices.h>
#else
#include <semaphore.h>
#endif
#include <boost/utility.hpp>
namespace Raul {
/** Counting semaphore.
*
* \ingroup raul
*/
class Semaphore : boost::noncopyable {
public:
inline Semaphore(unsigned int initial) {
#ifdef __APPLE__
MPCreateSemaphore(UINT_MAX, initial, &_sem);
#else
sem_init(&_sem, 0, initial);
#endif
}
inline ~Semaphore() {
#ifdef __APPLE__
MPDeleteSemaphore(_sem);
#else
sem_destroy(&_sem);
#endif
}
/** Destroy and reset the semaphore to an initial value.
*
* This must not be called while there are any waiters.
*/
inline void reset(unsigned int initial) {
#ifdef __APPLE__
MPDeleteSemaphore(_sem);
MPCreateSemaphore(UINT_MAX, initial, &_sem);
#else
sem_destroy(&_sem);
sem_init(&_sem, 0, initial);
#endif
}
/** Increment (and signal any waiters).
*
* Realtime safe.
*/
inline void post() {
#ifdef __APPLE__
MPSignalSemaphore(_sem);
#else
sem_post(&_sem);
#endif
}
/** Wait until count is > 0, then decrement.
*
* Obviously not realtime safe.
*/
inline void wait() {
#ifdef __APPLE__
MPWaitOnSemaphore(_sem, kDurationForever);
#else
/* Note that sem_wait always returns 0 in practice, except in
gdb (at least), where it returns nonzero, so the while is
necessary (and is the correct/safe solution in any case).
*/
while (sem_wait(&_sem) != 0) {}
#endif
}
/** Non-blocking version of wait().
*
* \return true if decrement was successful (lock was acquired).
*
* Realtime safe?
*/
inline bool try_wait() {
#ifdef __APPLE__
return MPWaitOnSemaphore(_sem, kDurationImmediate) == noErr;
#else
return (sem_trywait(&_sem) == 0);
#endif
}
private:
#ifdef __APPLE__
MPSemaphoreID _sem;
#else
sem_t _sem;
#endif
};
} // namespace Raul
#endif // RAUL_SEMAPHORE_HPP
|