1 | |
---|---|
2 | // vim:sw=2:ai |
3 | |
4 | /* |
5 | * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. |
6 | * See COPYRIGHT.txt for details. |
7 | */ |
8 | |
9 | #ifndef DENA_MUTEX_HPP |
10 | #define DENA_MUTEX_HPP |
11 | |
12 | #include <pthread.h> |
13 | #include <stdlib.h> |
14 | |
15 | #include "fatal.hpp" |
16 | #include "util.hpp" |
17 | |
18 | namespace dena { |
19 | |
20 | struct condition; |
21 | |
22 | struct mutex : private noncopyable { |
23 | friend struct condition; |
24 | mutex() { |
25 | if (pthread_mutex_init(&mtx, 0) != 0) { |
26 | fatal_abort("pthread_mutex_init"); |
27 | } |
28 | } |
29 | ~mutex() { |
30 | if (pthread_mutex_destroy(&mtx) != 0) { |
31 | fatal_abort("pthread_mutex_destroy"); |
32 | } |
33 | } |
34 | void lock() const { |
35 | if (pthread_mutex_lock(&mtx) != 0) { |
36 | fatal_abort("pthread_mutex_lock"); |
37 | } |
38 | } |
39 | void unlock() const { |
40 | if (pthread_mutex_unlock(&mtx) != 0) { |
41 | fatal_abort("pthread_mutex_unlock"); |
42 | } |
43 | } |
44 | private: |
45 | mutable pthread_mutex_t mtx; |
46 | }; |
47 | |
48 | }; |
49 | |
50 | #endif |
51 | |
52 |