summaryrefslogtreecommitdiff
path: root/src/semaphore.hpp
diff options
context:
space:
mode:
authorMartin Sustrik <sustrik@250bpm.com>2010-08-30 17:04:51 +0200
committerMartin Sustrik <sustrik@250bpm.com>2010-08-30 17:04:51 +0200
commite45583c0f2b8fb11f119317e7c94052c07c24ea8 (patch)
tree4265176256e3f91da84f06c7baf76c72a235c60a /src/semaphore.hpp
parentf0a36f9994e0a4e6e509bdf4b6dd75af27f984bb (diff)
OSX build fixed -- semaphore replaced by mutex
Diffstat (limited to 'src/semaphore.hpp')
-rw-r--r--src/semaphore.hpp55
1 files changed, 53 insertions, 2 deletions
diff --git a/src/semaphore.hpp b/src/semaphore.hpp
index 1c4d2a0..453c1b0 100644
--- a/src/semaphore.hpp
+++ b/src/semaphore.hpp
@@ -76,7 +76,59 @@ namespace zmq
HANDLE ev;
- // Disable copying of the object.
+ semaphore_t (const semaphore_t&);
+ void operator = (const semaphore_t&);
+ };
+
+#elif defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_OPENVMS
+
+ // On platforms that allow for double locking of a mutex from the same
+ // thread, simple semaphore is implemented using mutex, as it is more
+ // efficient than full-blown semaphore.
+
+ // Note that OS-level semaphore is not implemented on OSX, so the below
+ // code is not only optimisation, it's necessary to make 0MQ work on OSX.
+
+ class semaphore_t
+ {
+ public:
+
+ // Initialise the semaphore.
+ inline semaphore_t ()
+ {
+ int rc = pthread_mutex_init (&mutex, NULL);
+ posix_assert (rc);
+ rc = pthread_mutex_lock (&mutex);
+ posix_assert (rc);
+ }
+
+ // Destroy the semaphore.
+ inline ~semaphore_t ()
+ {
+ int rc = pthread_mutex_unlock (&mutex);
+ posix_assert (rc);
+ rc = pthread_mutex_destroy (&mutex);
+ posix_assert (rc);
+ }
+
+ // Wait for the semaphore.
+ inline void wait ()
+ {
+ int rc = pthread_mutex_lock (&mutex);
+ posix_assert (rc);
+ }
+
+ // Post the semaphore.
+ inline void post ()
+ {
+ int rc = pthread_mutex_unlock (&mutex);
+ posix_assert (rc);
+ }
+
+ private:
+
+ pthread_mutex_t mutex;
+
semaphore_t (const semaphore_t&);
void operator = (const semaphore_t&);
};
@@ -122,7 +174,6 @@ namespace zmq
// Underlying system semaphore object.
sem_t sem;
- // Disable copying of the object.
semaphore_t (const semaphore_t&);
void operator = (const semaphore_t&);
};