µ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++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2016-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#include <cmsis-plus/estd/mutex>
13
14// ----------------------------------------------------------------------------
15
16#if defined(__clang__)
17#pragma clang diagnostic ignored "-Wc++98-compat"
18#endif
19
20// ----------------------------------------------------------------------------
21
22namespace os
23{
24 namespace estd
25 {
26 // ========================================================================
27
28 void
30 {
32 res = nm_.lock ();
33 if (res != os::rtos::result::ok)
34 {
35 os::estd::__throw_cmsis_error (static_cast<int> (res),
36 "mutex lock failed");
37 }
38 }
39
40 bool
42 {
44 res = nm_.try_lock ();
45 if (res == os::rtos::result::ok)
46 {
47 return true;
48 }
49 else if (res == EWOULDBLOCK)
50 {
51 return false;
52 }
53
54 os::estd::__throw_cmsis_error (static_cast<int> (res),
55 "mutex try_lock failed");
56 // return false;
57 }
58
59 void
61 {
63 res = nm_.unlock ();
64 if (res != os::rtos::result::ok)
65 {
66 os::estd::__throw_cmsis_error (static_cast<int> (res),
67 "mutex unlock failed");
68 }
69 }
70
71 // ========================================================================
72
73 void
75 {
77 res = nm_.lock ();
78 if (res != os::rtos::result::ok)
79 {
80 os::estd::__throw_cmsis_error (static_cast<int> (res),
81 "recursive_mutex lock failed");
82 }
83 }
84
85 bool
87 {
89 res = nm_.try_lock ();
90 if (res == os::rtos::result::ok)
91 {
92 return true;
93 }
94 else if (res == EWOULDBLOCK)
95 {
96 return false;
97 }
98
99 os::estd::__throw_cmsis_error (static_cast<int> (res),
100 "recursive_mutex try_lock failed");
101 // return false;
102 }
103
104 void
106 {
108 res = nm_.unlock ();
109 if (res != os::rtos::result::ok)
110 {
111 os::estd::__throw_cmsis_error (static_cast<int> (res),
112 "recursive_mutex unlock failed");
113 }
114 }
115
116 // ========================================================================
117
118 } /* namespace estd */
119} /* namespace os */
120
121// ----------------------------------------------------------------------------
native_type nm_
Definition mutex:89
void lock()
Definition mutex.cpp:29
bool try_lock()
Definition mutex.cpp:41
void unlock()
Definition mutex.cpp:60
native_type nm_
Definition mutex:124
bool try_lock() noexcept
Definition mutex.cpp:86
result_t lock(void)
Lock/acquire the mutex.
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:179
uint32_t result_t
Type of values returned by RTOS functions.
Definition os-decls.h:95
System namespace.