CriticalSectionBlock Class Reference

A CriticalSectionBlock locks a critical section for the lifetime of the CriticalSectionBlock. More...

#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.
 

Detailed Description

A CriticalSectionBlock locks a critical section for the lifetime of the CriticalSectionBlock.

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;
    }
    // ...
}
--------------------------------------------------------------------- 
Author
Henner Zeller H.Zel.nosp@m.ler@.nosp@m.acm.o.nosp@m.rg

Definition at line 70 of file CriticalSectionBlock.H.

Constructor & Destructor Documentation

◆ CriticalSectionBlock()

CriticalSectionBlock::CriticalSectionBlock ( critsec_t critsec)
inline

Constructor locks the critical section.

Definition at line 83 of file CriticalSectionBlock.H.

References _critsec, and enter_critical_section().

◆ ~CriticalSectionBlock()

CriticalSectionBlock::~CriticalSectionBlock ( )
inline

destructor automatically unlocks the critical section.

Definition at line 98 of file CriticalSectionBlock.H.

References _critsec, and leave_critical_section.

Member Function Documentation

◆ checkonce()

bool CriticalSectionBlock::checkonce ( )
inline

don't use.

Used internally for the synchronized() { } macro.

Definition at line 93 of file CriticalSectionBlock.H.

References _checkonceHackCounter.

Member Data Documentation

◆ _checkonceHackCounter

char CriticalSectionBlock::_checkonceHackCounter
private

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().

◆ _critsec

critsec_t* CriticalSectionBlock::_critsec
private

Definition at line 72 of file CriticalSectionBlock.H.

Referenced by CriticalSectionBlock(), and ~CriticalSectionBlock().


The documentation for this class was generated from the following file:

brickOS is released under the Mozilla Public License.
Original code copyright 1998-2005 by the authors.

Generated for brickOS Kernel Developer by doxygen 1.9.8