summaryrefslogtreecommitdiff
path: root/3rdparty/plibsys/src/pmutex.h
blob: 449a6528fbc4ece93b75d13825de1a29fc044347 (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
135
136
/*
 * The MIT License
 *
 * Copyright (C) 2010-2017 Alexander Saprykin <saprykin.spb@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * @file pmutex.h
 * @brief Mutex routines
 * @author Alexander Saprykin
 *
 * A mutex is a mutual exclusive (hence mutex) synchronization primitive which
 * allows access to a critical section only to one of the concurrently running
 * threads. It is used to protected shared data structures from concurrent
 * modifications which could lead to unpredictable behavior.
 *
 * When entering a critical section a thread must call p_mutex_lock() to get a
 * lock. If another thread is already holding the lock all other threads will
 * be suspended until the lock is released with p_mutex_unlock(). After
 * releasing the lock one of the waiting threads is resumed to continue
 * execution. On most systems it is not specified whether a mutex waiting queue
 * is fair (FIFO) or not.
 *
 * The typical mutex usage:
 * @code
 * p_mutex_lock (mutex);
 *
 * ... code in critical section ...
 *
 * p_mutex_unlock (mutex);
 * @endcode
 * You can also think of the mutex as a binary semaphore.
 *
 * It is implementation dependent whether recursive locking or non-locked mutex
 * unlocking is allowed, but such actions can lead to unpredictable behavior.
 * Do not rely on such behavior in cross-platform applications.
 *
 * This is the thread scoped mutex implementation. You could not share this
 * mutex outside the process adress space, but you can share it between the
 * threads of the same process.
 */

#if !defined (PLIBSYS_H_INSIDE) && !defined (PLIBSYS_COMPILATION)
#  error "Header files shouldn't be included directly, consider using <plibsys.h> instead."
#endif

#ifndef PLIBSYS_HEADER_PMUTEX_H
#define PLIBSYS_HEADER_PMUTEX_H

#include <pmacros.h>
#include <ptypes.h>

P_BEGIN_DECLS

/** Mutex opaque data structure. */
typedef struct PMutex_ PMutex;

/**
 * @brief Creates a new #PMutex object.
 * @return Pointer to a newly created #PMutex object.
 * @since 0.0.1
 */
P_LIB_API PMutex *	p_mutex_new	(void);

/**
 * @brief Locks a mutex.
 * @param mutex #PMutex to lock.
 * @return TRUE in case of success, FALSE otherwise.
 * @since 0.0.1
 * @warning Do not lock the mutex recursively - it may lead to an application
 * deadlock (implementation dependent).
 *
 * Forces the calling thread to sleep until @a mutex becomes available for
 * locking.
 */
P_LIB_API pboolean	p_mutex_lock	(PMutex *mutex);

/**
 * @brief Tries to lock a mutex immediately.
 * @param mutex #PMutex to lock.
 * @return TRUE in case of success, FALSE otherwise.
 * @since 0.0.1
 * @warning Do not lock the mutex recursively - it may lead to an application
 * deadlock (implementation dependent).
 *
 * Tries to lock @a mutex and returns immediately if it is not available for
 * locking.
 */
P_LIB_API pboolean	p_mutex_trylock	(PMutex *mutex);

/**
 * @brief Releases a locked mutex.
 * @param mutex #PMutex to release.
 * @return TRUE in case of success, FALSE otherwise.
 * @since 0.0.1
 * @warning Do not use this function on non-locked mutexes - behavior may be
 * unpredictable.
 *
 * If @a mutex was previously locked then it becomes unlocked.
 *
 * It's implementation dependent whether only the same thread can lock and
 * unlock the same mutex.
 */
P_LIB_API pboolean	p_mutex_unlock	(PMutex *mutex);

/**
 * @brief Frees #PMutex object.
 * @param mutex #PMutex to free.
 * @since 0.0.1
 * @warning It doesn't unlock @a mutex before freeing memory, so you should do
 * it manually.
 */
P_LIB_API void		p_mutex_free	(PMutex *mutex);

P_END_DECLS

#endif /* PLIBSYS_HEADER_PMUTEX_H */