brickOS C++ v0.9.0
CriticalSectionBlock.H
Go to the documentation of this file.
1// The contents of this file are subject to the Mozilla Public License
2// Version 1.0 (the "License"); you may not use this file except in
3// compliance with the License. You may obtain a copy of the License
4// at http://www.mozilla.org/MPL/
5//
6// Software distributed under the License is distributed on an "AS IS"
7// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
8// the License for the specific language governing rights and
9// limitations under the License.
10//
11// Contributor Henner Zeller <H.Zeller@acm.org>
12
13#ifndef __CRITICALSECTIONBLOCK_H
14#define __CRITICALSECTIONBLOCK_H
15
16#include <critsec.h>
17
71private:
72 critsec_t *_critsec;
77 char _checkonceHackCounter;
78
79public:
84 : _critsec(critsec),
85 _checkonceHackCounter(1)
86 {
87 enter_critical_section(_critsec);
88 }
89
93 inline bool checkonce() { return _checkonceHackCounter-- > 0; }
94
101};
102
131#define synchronized(cs) for(CriticalSectionBlock __currentlocked(cs);__currentlocked.checkonce();/* */)
132
133/*
134 * Local variables:
135 * c-basic-offset: 8
136 * End:
137 */
138
139#endif /* __CRITICALSECTIONBLOCK_H */
A CriticalSectionBlock locks a critical section for the lifetime of the CriticalSectionBlock.
CriticalSectionBlock(critsec_t *critsec)
Constructor locks the critical section.
~CriticalSectionBlock()
destructor automatically unlocks the critical section.
Interface: critical section management.
int enter_critical_section(critsec_t *cs)
enter critical section
struct critsec critsec_t
critical section type definition
Definition critsec.h:55
#define leave_critical_section(cs)
leave critical section
Definition critsec.h:101