Zero  0.1.0
Classes | Macros | Functions
critical_section.h File Reference

Go to the source code of this file.

Classes

struct  critical_section< Lock >
 
struct  critical_section< Lock *& >
 Helper class for CRITICAL_SECTION idiom (macro). More...
 

Macros

#define CRITICAL_SECTION(name, lock)   critical_section<__typeof__(lock)&> name(lock)
 
#define SPECIALIZE_CS(Lock, Extra, ExtraInit, Acquire, Release)
 Macro that enables use of CRITICAL_SECTION(name,lock) More...
 

Functions

 SPECIALIZE_CS (pthread_mutex_t, int _dummy,(_dummy=0), pthread_mutex_lock(_mutex), pthread_mutex_unlock(_mutex))
 

Macro Definition Documentation

§ CRITICAL_SECTION

#define CRITICAL_SECTION (   name,
  lock 
)    critical_section<__typeof__(lock)&> name(lock)

This macro starts a critical section protected by the given lock (2nd argument). The critical_section structure it creates is named by the 1st argument. The rest of the scope (in which this macro is used) becomes the scope of the critical section, since it is the destruction of this critical_section structure that releases the lock.

The programmer can release the lock early by calling <name>.pause() or <name>.exit(). The programmer can reacquire the lock by calling <name>.resume() if <name>.pause() was called, but not after <name>.exit().

See also
critical_section

§ SPECIALIZE_CS

#define SPECIALIZE_CS (   Lock,
  Extra,
  ExtraInit,
  Acquire,
  Release 
)
Value:
template<> struct critical_section<Lock&> { \
critical_section(Lock &mutex) \
: _mutex(&mutex) \
{ ExtraInit; Acquire; } \
~critical_section() { \
if(_mutex) \
Release; \
_mutex = nullptr; \
} \
void pause() { Release; } \
void resume() { Acquire; }\
void exit() { Release; _mutex = nullptr; } \
Lock &hand_off() { \
Lock* rval = _mutex; \
_mutex = nullptr; \
return *rval; \
} \
private: \
Lock* _mutex; \
Extra; \
void operator=(critical_section const &); \
critical_section(critical_section const &); \
}
Definition: critical_section.h:78

Macro that enables use of CRITICAL_SECTION(name,lock)

Create a templated class that holds

  • a reference to the given lock and
  • the Extra (2nd macro argument)

and it

  • applies the ExtraInit and Acquire commands upon construction,
  • applies the Release command upon destruction.

Function Documentation

§ SPECIALIZE_CS()

SPECIALIZE_CS ( pthread_mutex_t  ,
int  _dummy,
(_dummy=0)  ,
pthread_mutex_lock(_mutex)  ,
pthread_mutex_unlock(_mutex)   
)