ResSem

Files CCore/inc/task/ResSem.h CCore/src/task/ResSem.cpp

ResSem is used to control a resource usage. This class is a hybrid of the AntiSem and Sem. It has an internal counter, which remains in the range [0,max_count], where max_count is a ResSem counter limit. Initially the counter equals max_count. Like a usual semaphore, ResSem has take() and give() operations, but it has the additional "gateway" operation wait(), which blocks the calling thread until the counter gets back to its maximum value.

Return back to the basket with apples. Initially basket contains a number of apples. When somebody takes an apple, it must give it back after time. I.e. an apple is not consumed, but is borrowed. Basket host is waiting until all apples are returned.


class ResSem : public Funchor_nocopy
 {
   ....

  public:
   
   // constructors
  
   explicit ResSem(ulen max_count);
   
   ResSem(TextLabel name,ulen max_count);
   
   ~ResSem();
   
   // give
   
   void give();
   
   // take
  
   bool try_take();
  
   void take();
  
   bool take(MSec timeout);
  
   bool take(TimeScope time_scope);
   
   // wait
   
   bool try_wait();
   
   void wait();
  
   bool wait(MSec timeout);
   
   bool wait(TimeScope time_scope);
   
   // functions
   
   Function<void (void)> function_give() { return FunctionOf(this,&ResSem::give); }
 };

The constructor argument max_count is the maximum ResSem counter. It cannot be zero. Initially the counter is at that maximum.

give() increments the counter. If the counter is at maximum, the Abort() is called. This method may release threads, blocked by the method wait(). It also may release a thread, blocked by the method take(). These two situations cannot happens simultaneously.

try_take() tries to decrement the counter. This method never blocks. The return value is true, if the method was successful.

take() decrements the counter. It may block the calling thread indefinitely, until the operation becomes possible.

take(MSec) and take(TimeScope) are timed variants of the take(). These methods wait up to the specified timeout is expired, then operation failed. The return value is true, if the method was successful.

try_wait() checks, if the the counter is at maximum. This method never blocks.

wait() blocks the calling thread indefinitely until the the counter gets at maximum.

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.

HCore ResSem

HCore ResSem has the common interface.


class ResSem : public Funchor_nocopy
 {
   ....

  public:
   
   // constructors
  
   explicit ResSem(ulen max_count);
   
   ResSem(TextLabel name,ulen max_count);
   
   ~ResSem();
   
   // give
   
   void give();
   
   // take
  
   bool try_take();
  
   void take();
  
   bool take(MSec timeout);
  
   bool take(TimeScope time_scope);
   
   // wait
   
   bool try_wait();
   
   void wait();
  
   bool wait(MSec timeout);
   
   bool wait(TimeScope time_scope);
   
   // functions
   
   Function<void (void)> function_give() { return FunctionOf(this,&ResSem::give); }
 };

XCore ResSem

XCore ResSem is essentially the same, but has give() method variants for different execution contexts.


class ResSem : public Funchor_nocopy
 {
   ....

  public: 
   
   // constructors
  
   explicit ResSem(ulen max_count);
   
   ResSem(TextLabel name,ulen max_count);
   
   ~ResSem();
   
   TextLabel getName() const { return name; }
   
   // give
   
   void give();
   
   void give_int();
   
   void give_any();
   
   // take
  
   bool try_take();
  
   void take();
  
   bool take(MSec timeout);
  
   bool take(TimeScope time_scope);
   
   // wait
   
   bool try_wait();
   
   void wait();
  
   bool wait(MSec timeout);
   
   bool wait(TimeScope time_scope);
   
   // functions
   
   Function<void (void)> function_give() { return FunctionOf(this,&ResSem::give); }
   
   Function<void (void)> function_give_int() { return FunctionOf(this,&ResSem::give_int); }
   
   Function<void (void)> function_give_any() { return FunctionOf(this,&ResSem::give_any); }
 };