Files CCore/inc/AntiSem.h CCore/src/AntiSem.cpp
AntiSem is a "gateway" synchronization class. I.e. it may allow or block a thread execution, if some condition is met. The difference with a "consuming" synchronization class is the blocking calls does not change the state.
AntiSem has an internal counter. You may increase or decrease this counter. If the counter is below the given level, AntiSem is opened, otherwise it is closed. When it is closed, the method wait() blocks the calling thread while the AntiSem is closed.
class AntiSem : public Funchor_nocopy
{
....
public:
// constructors
explicit AntiSem(ulen level=0);
explicit AntiSem(TextLabel name,ulen level=0);
~AntiSem();
// add/sub
void add(ulen dcount);
void sub(ulen dcount);
// inc/dec
void inc() { add(1); }
void dec() { sub(1); }
// wait
bool try_wait();
void wait();
bool wait(MSec timeout);
bool wait(TimeScope time_scope);
// functions
Function<void (void)> function_inc() { return FunctionOf(this,&AntiSem::inc); }
Function<void (void)> function_dec() { return FunctionOf(this,&AntiSem::dec); }
};
The constructor argument level is a sensitive level of the AntiSem. The object is opened if counter <= level. It has zero value by default.
add() increases the counter by the given value. If the overflow happens, the Abort() is called.
sub() decreases the counter by the given value. If the underflow happens, the Abort() is called. If the counter value becomes <= level, all blocked on the AntiSem threads are released.
inc() is the counter increment, this method is equivalent add(1).
dec() is the counter decrement, this method is equivalent dec(1). It is also a releasing method.
try_wait() checks, if the object is opened. This method never blocks.
wait() blocks the calling thread indefinitely until the object becomes opened.
wait(MSec) and wait(TimeScope) are timed variants of the wait(). These methods wait up to the specified timeout is expired, then operation failed. The return value is true, if the method was successful.
The most common usage of the AntiSem is the implementation of the atomic reference counter with waiting.
HCore AntiSem has the common interface.
class AntiSem : public Funchor_nocopy
{
....
public:
// constructors
explicit AntiSem(ulen level=0);
explicit AntiSem(TextLabel name,ulen level=0);
~AntiSem();
// add/sub
void add(ulen dcount);
void sub(ulen dcount);
// inc/dec
void inc() { add(1); }
void dec() { sub(1); }
// wait
bool try_wait();
void wait();
bool wait(MSec timeout);
bool wait(TimeScope time_scope);
// functions
Function<void (void)> function_inc() { return FunctionOf(this,&AntiSem::inc); }
Function<void (void)> function_dec() { return FunctionOf(this,&AntiSem::dec); }
};
XCore AntiSem is essentially the same, but has add(), sub(), inc(), dec() methods variants for different execution contexts.
class AntiSem : public Funchor_nocopy
{
....
public:
// constructors
explicit AntiSem(ulen level=0);
explicit AntiSem(TextLabel name,ulen level=0);
~AntiSem();
TextLabel getName() const { return name; }
// add/sub
void add(ulen dcount);
void sub(ulen dcount);
void add_int(ulen dcount);
void sub_int(ulen dcount);
void add_any(ulen dcount);
void sub_any(ulen dcount);
// inc/dec
void inc() { add(1); }
void dec() { sub(1); }
void inc_int() { add_int(1); }
void dec_int() { sub_int(1); }
void inc_any() { add_any(1); }
void dec_any() { sub_any(1); }
// wait
bool try_wait();
void wait();
bool wait(MSec timeout);
bool wait(TimeScope time_scope);
// functions
Function<void (void)> function_inc() { return FunctionOf(this,&AntiSem::inc); }
Function<void (void)> function_dec() { return FunctionOf(this,&AntiSem::dec); }
Function<void (void)> function_inc_int() { return FunctionOf(this,&AntiSem::inc_int); }
Function<void (void)> function_dec_int() { return FunctionOf(this,&AntiSem::dec_int); }
Function<void (void)> function_inc_any() { return FunctionOf(this,&AntiSem::inc_any); }
Function<void (void)> function_dec_any() { return FunctionOf(this,&AntiSem::dec_any); }
};