]> git.tdb.fi Git - libs/core.git/blob - source/core/mutex.cpp
Use native CreateDirectory/RemoveDirectory calls on Windows
[libs/core.git] / source / core / mutex.cpp
1 #ifndef WIN32
2 #include <cerrno>
3 #endif
4 #include "mutex.h"
5 #include "mutex_private.h"
6 #include "systemerror.h"
7
8 namespace Msp {
9
10 Mutex::Mutex():
11         priv(new Private)
12 {
13 #ifdef WIN32
14         InitializeCriticalSection(&priv->crit);
15 #else
16         pthread_mutex_init(&priv->mutex, 0);
17 #endif
18 }
19
20 Mutex::~Mutex()
21 {
22 #ifdef WIN32
23         DeleteCriticalSection(&priv->crit);
24 #else
25         pthread_mutex_destroy(&priv->mutex);
26 #endif
27         delete priv;
28 }
29
30 void Mutex::lock()
31 {
32 #ifdef WIN32
33         EnterCriticalSection(&priv->crit);
34 #else
35         if(int err = pthread_mutex_lock(&priv->mutex))
36                 throw system_error("Mutex::lock", err);
37 #endif
38 }
39
40 bool Mutex::trylock()
41 {
42 #ifdef WIN32
43         return TryEnterCriticalSection(&priv->crit);
44 #else
45         int err = pthread_mutex_trylock(&priv->mutex);
46         if(err && err!=EBUSY)
47                 throw system_error("Mutex::trylock", err);
48         return !err;
49 #endif
50 }
51
52 void Mutex::unlock()
53 {
54 #ifdef WIN32
55         LeaveCriticalSection(&priv->crit);
56 #else
57         if(int err = pthread_mutex_unlock(&priv->mutex))
58                 throw system_error("Mutex::unlock", err);
59 #endif
60 }
61
62 } // namespace Msp