aboutsummaryrefslogtreecommitdiff
path: root/src/mutex.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mutex.h')
-rw-r--r--src/mutex.h68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/mutex.h b/src/mutex.h
new file mode 100644
index 0000000..b5c8b7a
--- /dev/null
+++ b/src/mutex.h
@@ -0,0 +1,68 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_MUTEX_H
9#define BU_MUTEX_H
10
11#include <pthread.h>
12
13namespace Bu
14{
15 /**
16 * Simple mutex wrapper. Currently this doesn't do anything extra for you
17 * except keep all of the functionality together in an OO sorta' way and
18 * keep you from having to worry about cleaning up your mutexes properly,
19 * or initing them.
20 *@ingroup Threading
21 */
22 class Mutex
23 {
24 public:
25 /**
26 * Create an unlocked mutex.
27 */
28 Mutex();
29
30 /**
31 * Destroy a mutex. This can only be done when a mutex is unlocked.
32 * Failure to unlock before destroying a mutex object could cause it to
33 * wait for the mutex to unlock, the odds of which are usually farily
34 * low at deconstruction time.
35 */
36 ~Mutex();
37
38 /**
39 * Lock the mutex. This causes all future calls to lock on this
40 * instance of mutex to block until the first thread that called mutex
41 * unlocks it. At that point the next thread that called lock will get
42 * a chance to go to work. Because of the nature of a mutex lock it is
43 * a very bad idea to do any kind of serious or rather time consuming
44 * computation within a locked section. This can cause thread-deadlock
45 * and your program may hang.
46 */
47 int lock();
48
49 /**
50 * Unlock the mutex. This allows the next thread that asked for a lock
51 * to lock the mutex and continue with execution.
52 */
53 int unlock();
54
55 /**
56 * Try to lock the mutex. This is the option to go with if you cannot
57 * avoid putting lengthy operations within a locked section. trylock
58 * will attempt to lock the mutex, if the mutex is already locked this
59 * function returns immediately with an error code.
60 */
61 int trylock();
62
63 protected:
64 pthread_mutex_t mutex; /**< The internal mutex reference. */
65 };
66}
67
68#endif