#include <CriticalSectionBlock.H>
Collaboration diagram for CriticalSectionBlock:
Public Member Functions | |
CriticalSectionBlock (critsec_t *critsec) | |
Constructor locks the critical section. | |
bool | checkonce () |
don't use. | |
~CriticalSectionBlock () | |
destructor automatically unlocks the critical section. | |
Private Attributes | |
critsec_t * | _critsec |
char | _checkonceHackCounter |
little hack, to make it possible to implement the synchronized() macro with the for(;;) loop below. |
The CriticalSectionBlock gets a critical section and initially enters it, i.e. the constructor blocks until the section is free. The critical section is unlocked in the destructor.
The actual trick is, that if the CriticalSectionBlock is created as automatic variable on the stack (complicated expression for local variable), C++ implicitly calls the destructor, whenever we leave the block it is created in.
Leaving the block may be due to a regular return, or an Exception or just falling off the edge, thus this automatically takes care of cleaning up the section which is especially handy if we expect Exceptions. Never forgotten-to-unlock critical sections again .. (isn't this cool ?)
Usage like:
-------------------------------------------------------------------- critsec_t someCriticalSection; void foo () { CriticalSectionBlock criticalSection(&someCriticalSection); //.. critical section protected ... myCriticalDataStructure = 42; // criticalSection is automatically released } ---------------------------------------------------------------------There is as well a macro available which utilizes this class to make it really easy to use and to read. People who have programmed Java reckognize this; use this in favour of the plain CriticalSectionBlock because it improves readability a lot:
--------------------------------------------------------------------- critsec_t someCriticalSection; void foo () { // ... synchronized (&someCriticalSection) { //.. critical section protected... myCriticalDataStructure = 42; } // ... } ---------------------------------------------------------------------
Definition at line 70 of file CriticalSectionBlock.H.
|
Constructor locks the critical section.
Definition at line 83 of file CriticalSectionBlock.H. References _checkonceHackCounter, _critsec, critsec_t, and enter_critical_section(). |
|
destructor automatically unlocks the critical section.
Definition at line 98 of file CriticalSectionBlock.H. References _critsec, and leave_critical_section. |
|
don't use. Used internally for the synchronized() { } macro. Definition at line 93 of file CriticalSectionBlock.H. References _checkonceHackCounter. |
|
little hack, to make it possible to implement the synchronized() macro with the for(;;) loop below.
Definition at line 77 of file CriticalSectionBlock.H. Referenced by checkonce(), and CriticalSectionBlock(). |
|
Definition at line 72 of file CriticalSectionBlock.H. Referenced by CriticalSectionBlock(), and ~CriticalSectionBlock(). |
brickOS is released under the
Mozilla Public License.
Original code copyright 1998-2002 by the authors. |