IntrOS
Loading...
Searching...
No Matches
oscriticalsection.h
1/******************************************************************************
2
3 @file IntrOS: oscriticalsection.h
4 @author Rajmund Szymanski
5 @date 17.11.2025
6 @brief This file contains definitions for IntrOS.
7
8 ******************************************************************************
9
10 Copyright (c) 2018-2022 Rajmund Szymanski. All rights reserved.
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to
14 deal in the Software without restriction, including without limitation the
15 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 sell copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 IN THE SOFTWARE.
29
30 ******************************************************************************/
31
32#ifndef __INTROS_CRI_H
33#define __INTROS_CRI_H
34
35#include "oskernel.h"
36
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47/******************************************************************************
48 *
49 * Name : core_set_lock
50 *
51 * Description : disable interrupts
52 *
53 * Parameters : none
54 *
55 * Return : previous interrupts state
56 *
57 * Note : for internal use
58 *
59 ******************************************************************************/
60
61__STATIC_INLINE
62lck_t core_set_lock( void )
63{
64 lck_t prv = port_get_lock();
65 port_set_lock();
66 __COMPILER_BARRIER();
67 return prv;
68}
69
70/******************************************************************************
71 *
72 * Name : core_clr_lock
73 *
74 * Description : enable interrupts
75 *
76 * Parameters : none
77 *
78 * Return : previous interrupts state
79 *
80 * Note : for internal use
81 *
82 ******************************************************************************/
83
84__STATIC_INLINE
85lck_t core_clr_lock( void )
86{
87 lck_t prv = port_get_lock();
88 port_clr_lock();
89 __COMPILER_BARRIER();
90 return prv;
91}
92
93/******************************************************************************
94 *
95 * Name : core_put_lock
96 *
97 * Description : restore interrupts state
98 *
99 * Parameters
100 * lck : interrupts state
101 *
102 * Return : none
103 *
104 * Note : for internal use
105 *
106 ******************************************************************************/
107
108__STATIC_INLINE
109void core_put_lock( lck_t lck )
110{
111 __COMPILER_BARRIER();
112 port_put_lock(lck);
113}
114
115/******************************************************************************
116 *
117 * Name : core_get_lock
118 *
119 * Description : return current interrupts state
120 *
121 * Parameters : none
122 *
123 * Return : current interrupts state
124 *
125 * Note : for internal use
126 *
127 ******************************************************************************/
128
129__STATIC_INLINE
130lck_t core_get_lock( void )
131{
132 return port_get_lock();
133}
134
139
140#define sys_lock() \
141 do { lck_t __LOCK = core_set_lock()
142
147
148#define sys_unlock() \
149 core_put_lock(__LOCK); } while (0)
150
151#ifdef __cplusplus
152}
153#endif
154
155/* -------------------------------------------------------------------------- */
156
157#if defined(__cplusplus)
158namespace intros {
159
163
165{
167 CriticalSection() { lck_ = core_set_lock(); }
168
169 ~CriticalSection() { core_put_lock(lck_); }
170
171 CriticalSection( CriticalSection&& ) = delete;
172 CriticalSection( const CriticalSection& ) = delete;
173 CriticalSection& operator=( CriticalSection&& ) = delete;
174 CriticalSection& operator=( const CriticalSection& ) = delete;
175
176 private:
177 lck_t lck_;
178};
179
180} // namespace
181#endif//__cplusplus
182
183/* -------------------------------------------------------------------------- */
184
186
187#endif//__INTROS_CRI_H
create and initialize a critical section guard object
Definition oscriticalsection.h:165
CriticalSection()
constructor, see the constructor parameters of the class
Definition oscriticalsection.h:167