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