Debian Codegraph @

libmowgli-2/src/libmowgli/thread/mutex.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * mutex.c: Cross-platform mutexes.
4 *
5 * Copyright (c) 2011 William Pitcock <[email protected]>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #include "mowgli.h"
25 #include "thread/mutex_internal.h"
26
27 static const mowgli_mutex_ops_t *_mowgli_mutex_ops = NULL;
28
29 static inline const mowgli_mutex_ops_t *
30 get_mutex_platform(void)
31 {
32 /* allow for threading policy to set custom mutex ops */
33 if (_mowgli_mutex_ops != NULL)
34 return _mowgli_mutex_ops;
35
36 #if defined(_WIN32)
37 return &_mowgli_win32_mutex_ops;
38 #endif
39
40 #if !defined(MOWGLI_FEATURE_HAVE_NATIVE_MUTEXES)
41 return &_mowgli_posix_mutex_ops;
42 #endif
43
44 return &_mowgli_null_mutex_ops;
45 }
46
47 mowgli_mutex_t *
48 mowgli_mutex_create(void)
49 {
50 mowgli_mutex_t *mutex = mowgli_alloc(sizeof *mutex);
51
52 return_val_if_fail(mutex != NULL, NULL);
53
54 if (mowgli_mutex_init(mutex))
55 {
56 return mutex;
57 }
58 else
59 {
60 mowgli_free(mutex);
61 return NULL;
62 }
63 }
64
65 int
66 mowgli_mutex_init(mowgli_mutex_t *mutex)
67 {
68 return_val_if_fail(mutex != NULL, -1);
69
70 mutex->ops = get_mutex_platform();
71
72 return mutex->ops->mutex_create(mutex);
73 }
74
75 int
76 mowgli_mutex_lock(mowgli_mutex_t *mutex)
77 {
78 return_val_if_fail(mutex != NULL, -1);
79 return_val_if_fail(mutex->ops != NULL, -1);
80
81 return mutex->ops->mutex_lock(mutex);
82 }
83
84 int
85 mowgli_mutex_trylock(mowgli_mutex_t *mutex)
86 {
87 return_val_if_fail(mutex != NULL, -1);
88 return_val_if_fail(mutex->ops != NULL, -1);
89
90 return mutex->ops->mutex_trylock(mutex);
91 }
92
93 int
94 mowgli_mutex_unlock(mowgli_mutex_t *mutex)
95 {
96 return_val_if_fail(mutex != NULL, -1);
97 return_val_if_fail(mutex->ops != NULL, -1);
98
99 return mutex->ops->mutex_unlock(mutex);
100 }
101
102 int
103 mowgli_mutex_uninit(mowgli_mutex_t *mutex)
104 {
105 return_val_if_fail(mutex != NULL, -1);
106 return_val_if_fail(mutex->ops != NULL, -1);
107
108 return mutex->ops->mutex_destroy(mutex);
109 }
110
111 void
112 mowgli_mutex_destroy(mowgli_mutex_t *mutex)
113 {
114 return_if_fail(mutex != NULL);
115
116 mowgli_mutex_uninit(mutex);
117 mowgli_free(mutex);
118 }
119
120 void
121 mowgli_mutex_set_policy(mowgli_thread_policy_t policy)
122 {
123 switch (policy)
124 {
125 case MOWGLI_THREAD_POLICY_DISABLED:
126 _mowgli_mutex_ops = &_mowgli_null_mutex_ops;
127 break;
128 case MOWGLI_THREAD_POLICY_DEFAULT:
129 default:
130 _mowgli_mutex_ops = NULL;
131 }
132 }