summaryrefslogtreecommitdiff
path: root/include/wolf/mutex.h
blob: fd01b36e9c9b00a53e500f397e3feca75340ed2d (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
/*
    Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef WOLF_MUTEX_H
#define WOLF_MUTEX_H

/**
 * @addtogroup wolf_threading Threading support
 * @{
 */

/**
 * @file mutex.h
 * @brief Portable mutexes for thread synchronization
 * @author Andreas Baumann <abaumann@yahoo.com>
 */

#ifdef __cplusplus
extern "C" {
#endif

#include "port/sys.h"

#include "errors.h"

#ifdef HAVE_PTHREADS

#include <pthread.h>			/* for mutex functions */
#include <assert.h>			/* for assert */

typedef pthread_mutex_t wolf_mutex_t;

#define WOLF_MUTEX_DEFAULT_ATTR PTHREAD_MUTEX_NORMAL

wolf_error_t wolf_mutex_init( wolf_mutex_t *mutex );
wolf_error_t wolf_mutex_destroy( wolf_mutex_t *mutex );

static INLINE void wolf_mutex_lock( wolf_mutex_t *mutex ) {
	int res;
	res = pthread_mutex_lock( mutex );
	assert( res == 0 );
}

static INLINE void wolf_mutex_unlock( wolf_mutex_t *mutex ) {
	int res;
	res = pthread_mutex_unlock( mutex );
	assert( res == 0 );
}

#endif /* HAVE_PTHREADS */

#ifdef _WIN32

#include <windows.h>

typedef CRITICAL_SECTION wolf_mutex_t;

#define WOLF_MUTEX_DEFAULT_ATTR

wolf_error_t wolf_mutex_init( wolf_mutex_t *mutex );
wolf_error_t wolf_mutex_destroy( wolf_mutex_t *mutex );

static INLINE void wolf_mutex_lock( wolf_mutex_t *mutex ) {
	EnterCriticalSection( mutex );
}

static INLINE void wolf_mutex_unlock(  wolf_mutex_t *mutex ) {
	LeaveCriticalSection( mutex );
}


#endif /* _WIN32 */

#ifdef __cplusplus
}
#endif

/** @} */ /* @addtogroup wolf_threading */

#endif /* ifndef WOLF_MUTEX_H */