summaryrefslogtreecommitdiff
path: root/src/libmowgli/thread/mutex.c
blob: 1d5d4804e0bc7706e726bc9ef12456128b29a0f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * libmowgli: A collection of useful routines for programming.
 * mutex.c: Cross-platform mutexes.
 *
 * Copyright (c) 2011 William Pitcock <nenolod@dereferenced.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "mowgli.h"

#if defined(_WIN32)
extern const mowgli_mutex_ops_t _mowgli_win32_mutex_ops;
#elif defined(_sun) || defined(_sco)
extern const mowgli_mutex_ops_t _mowgli_sun_mutex_ops;
#else
extern const mowgli_mutex_ops_t _mowgli_posix_mutex_ops;
#endif

extern const mowgli_mutex_ops_t _mowgli_null_mutex_ops;

static const mowgli_mutex_ops_t *_mowgli_mutex_ops = NULL;

static inline const mowgli_mutex_ops_t *get_mutex_platform(void)
{
	/* allow for threading policy to set custom mutex ops */
	if (_mowgli_mutex_ops != NULL)
		return _mowgli_mutex_ops;

#if defined(_WIN32)
	return &_mowgli_win32_mutex_ops;
#endif

#if defined(_sun) || defined(_sco)
	return &_mowgli_sun_mutex_ops;
#endif

#if !defined(MOWGLI_FEATURE_HAVE_NATIVE_MUTEXES)
	return &_mowgli_posix_mutex_ops;
#endif

	return &_mowgli_null_mutex_ops;
}

mowgli_mutex_t *mowgli_mutex_create(void)
{
	mowgli_mutex_t *mutex = mowgli_alloc(sizeof(mowgli_mutex_t));

	return_val_if_fail(mutex != NULL, NULL);

	if (mowgli_mutex_init(mutex))
		return mutex;
	else
	{
		mowgli_free(mutex);
		return NULL;
	}
}

int mowgli_mutex_init(mowgli_mutex_t *mutex)
{
	return_val_if_fail(mutex != NULL, -1);

	mutex->ops = get_mutex_platform();

	return mutex->ops->mutex_create(mutex);
}

int mowgli_mutex_lock(mowgli_mutex_t *mutex)
{
	return_val_if_fail(mutex != NULL, -1);
	return_val_if_fail(mutex->ops != NULL, -1);

	return mutex->ops->mutex_lock(mutex);
}

int mowgli_mutex_trylock(mowgli_mutex_t *mutex)
{
	return_val_if_fail(mutex != NULL, -1);
	return_val_if_fail(mutex->ops != NULL, -1);

	return mutex->ops->mutex_trylock(mutex);
}

int mowgli_mutex_unlock(mowgli_mutex_t *mutex)
{
	return_val_if_fail(mutex != NULL, -1);
	return_val_if_fail(mutex->ops != NULL, -1);

	return mutex->ops->mutex_unlock(mutex);
}

int mowgli_mutex_uninit(mowgli_mutex_t *mutex)
{
	return_val_if_fail(mutex != NULL, -1);
	return_val_if_fail(mutex->ops != NULL, -1);

	return mutex->ops->mutex_destroy(mutex);
}

void mowgli_mutex_destroy(mowgli_mutex_t *mutex)
{
	return_if_fail(mutex != NULL);

	mowgli_mutex_uninit(mutex);
	mowgli_free(mutex);
}

void mowgli_mutex_set_policy(mowgli_thread_policy_t policy)
{
	switch (policy)
	{
	case MOWGLI_THREAD_POLICY_DISABLED:
		_mowgli_mutex_ops = &_mowgli_null_mutex_ops;
		break;
	case MOWGLI_THREAD_POLICY_DEFAULT:
	default:
		_mowgli_mutex_ops = NULL;
	}
}