µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
mutex.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2016 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <cmsis-plus/estd/mutex>
29
30// ----------------------------------------------------------------------------
31
32#if defined(__clang__)
33#pragma clang diagnostic ignored "-Wc++98-compat"
34#endif
35
36// ----------------------------------------------------------------------------
37
38namespace os
39{
40 namespace estd
41 {
42 // ========================================================================
43
44 void
46 {
48 res = nm_.lock ();
49 if (res != os::rtos::result::ok)
50 {
51 os::estd::__throw_cmsis_error (static_cast<int> (res),
52 "mutex lock failed");
53 }
54 }
55
56 bool
58 {
60 res = nm_.try_lock ();
61 if (res == os::rtos::result::ok)
62 {
63 return true;
64 }
65 else if (res == EWOULDBLOCK)
66 {
67 return false;
68 }
69
70 os::estd::__throw_cmsis_error (static_cast<int> (res),
71 "mutex try_lock failed");
72 // return false;
73 }
74
75 void
77 {
79 res = nm_.unlock ();
80 if (res != os::rtos::result::ok)
81 {
82 os::estd::__throw_cmsis_error (static_cast<int> (res),
83 "mutex unlock failed");
84 }
85 }
86
87 // ========================================================================
88
89 void
91 {
93 res = nm_.lock ();
94 if (res != os::rtos::result::ok)
95 {
96 os::estd::__throw_cmsis_error (static_cast<int> (res),
97 "recursive_mutex lock failed");
98 }
99 }
100
101 bool
103 {
105 res = nm_.try_lock ();
106 if (res == os::rtos::result::ok)
107 {
108 return true;
109 }
110 else if (res == EWOULDBLOCK)
111 {
112 return false;
113 }
114
115 os::estd::__throw_cmsis_error (static_cast<int> (res),
116 "recursive_mutex try_lock failed");
117 //return false;
118 }
119
120 void
122 {
124 res = nm_.unlock ();
125 if (res != os::rtos::result::ok)
126 {
127 os::estd::__throw_cmsis_error (static_cast<int> (res),
128 "recursive_mutex unlock failed");
129 }
130 }
131
132 // ==========================================================================
133
134 } /* namespace estd */
135} /* namespace os */
136
137// ----------------------------------------------------------------------------
native_type nm_
Definition mutex:108
void lock()
Definition mutex.cpp:45
bool try_lock()
Definition mutex.cpp:57
void unlock()
Definition mutex.cpp:76
native_type nm_
Definition mutex:145
bool try_lock() noexcept
Definition mutex.cpp:102
result_t lock(void)
Lock/acquire the mutex.
Definition os-mutex.cpp:953
result_t try_lock(void)
Try to lock/acquire the mutex.
result_t unlock(void)
Unlock/release the mutex.
void __throw_cmsis_error(int ev, const char *what_arg)
@ ok
Function completed; no errors or events occurred.
Definition os-decls.h:195
uint32_t result_t
Type of values returned by RTOS functions.
Definition os-decls.h:110
System namespace.