]> git.tdb.fi Git - libs/core.git/blob - source/core/unix/mutex.cpp
Avoid leaking external references through default c'tors and d'tors
[libs/core.git] / source / core / unix / mutex.cpp
1 #include <cerrno>
2 #include <pthread.h>
3 #include "mutex.h"
4 #include "mutex_private.h"
5 #include "systemerror.h"
6
7 namespace Msp {
8
9 Mutex::Mutex():
10         priv(new Private)
11 {
12         pthread_mutex_init(&priv->mutex, 0);
13 }
14
15 Mutex::~Mutex()
16 {
17         pthread_mutex_destroy(&priv->mutex);
18         delete priv;
19 }
20
21 void Mutex::lock()
22 {
23         if(int err = pthread_mutex_lock(&priv->mutex))
24                 throw system_error("pthread_mutex_lock", err);
25 }
26
27 bool Mutex::trylock()
28 {
29         int err = pthread_mutex_trylock(&priv->mutex);
30         if(err && err!=EBUSY)
31                 throw system_error("pthread_mutex_trylock", err);
32         return !err;
33 }
34
35 void Mutex::unlock()
36 {
37         if(int err = pthread_mutex_unlock(&priv->mutex))
38                 throw system_error("pthread_mutex_unlock", err);
39 }
40
41 } // namespace Msp