4. Counter

Start cpp section to pthread/pthread_counter.hpp[1 /1 ]
     1: #line 882 "./lpsrc/flx_pthread.pak"
     2: #ifndef __PTHREAD_COUNTER__
     3: #define __PTHREAD_COUNTER__
     4: #include <flx_pthread_config.hpp>
     5: #include "pthread_mutex.hpp"
     6: #include "pthread_condv.hpp"
     7: 
     8: namespace flx { namespace pthread {
     9: 
    10: // ********************************************************
    11: /// Counter with zero signal
    12: // ********************************************************
    13: class PTHREAD_EXTERN flx_ts_counter_t {
    14:   flx_mutex_t m;
    15:   flx_condv_t c;
    16:   long x;
    17:   void operator=(flx_ts_counter_t const &);
    18:   flx_ts_counter_t(flx_ts_counter_t const &);
    19: public:
    20:   flx_ts_counter_t();
    21:   ~flx_ts_counter_t();
    22:   long pre_incr(); // value AFTER increment
    23:   long pre_decr(); // value AFTER decrement
    24:   long post_incr(); // value BEFORE increment
    25:   long post_decr(); // value BEFORE decrement
    26: 
    27:   long get();
    28:   long set(long);   // returns argument
    29:   long swap(long);  // returns old value
    30:   long decr_pos(); // decrement if >0
    31:   void wait_zero(); // wait for zero
    32:   long operator++() { return pre_incr(); }
    33:   long operator--() { return pre_decr(); }
    34:   long operator++(int) { return post_incr(); }
    35:   long operator--(int) { return post_decr(); }
    36:   long operator*() { return get(); }
    37:   long operator=(long a) { return set(a); }
    38: };
    39: }}
    40: 
    41: #endif
    42: 
End cpp section to pthread/pthread_counter.hpp[1]
Start cpp section to pthread/pthread_counter.cpp[1 /1 ]
     1: #line 925 "./lpsrc/flx_pthread.pak"
     2: #include "pthread_counter.hpp"
     3: #include <stdio.h>
     4: 
     5: namespace flx { namespace pthread {
     6: 
     7: 
     8: flx_ts_counter_t::flx_ts_counter_t() : x(0) {}
     9: 
    10: flx_ts_counter_t::~flx_ts_counter_t() {
    11:   wait_zero();
    12: }
    13: 
    14: long flx_ts_counter_t::pre_incr() {
    15:   flx_mutex_locker_t l(m);
    16:   ++x;
    17:   return x;
    18: }
    19: 
    20: long flx_ts_counter_t::pre_decr() {
    21:   flx_mutex_locker_t l(m);
    22:   --x;
    23:   if(x==0) c.signal();
    24:   return x;
    25: }
    26: 
    27: long flx_ts_counter_t::post_incr() {
    28:   flx_mutex_locker_t l(m);
    29:   ++x;
    30:   return x+1;
    31: }
    32: 
    33: long flx_ts_counter_t::post_decr() {
    34:   flx_mutex_locker_t l(m);
    35:   --x;
    36:   if(x==0) c.signal();
    37:   return x+1;
    38: }
    39: 
    40: long flx_ts_counter_t::decr_pos() {
    41:   flx_mutex_locker_t l(m);
    42:   if(x>0)--x;
    43:   if(x==0) c.signal();
    44:   return x;
    45: }
    46: 
    47: long flx_ts_counter_t::get() {
    48:   flx_mutex_locker_t l(m);
    49:   return x;
    50: }
    51: 
    52: long flx_ts_counter_t::set(long a) {
    53:   flx_mutex_locker_t l(m);
    54:   x = a;
    55:   return x;
    56: }
    57: 
    58: long flx_ts_counter_t::swap(long a) {
    59:   flx_mutex_locker_t l(m);
    60:   long tmp = x;
    61:   x = a;
    62:   if(x==0) c.signal();
    63:   return tmp;
    64: }
    65: 
    66: void flx_ts_counter_t::wait_zero() {
    67:   flx_mutex_locker_t l(m);
    68:   while(1){
    69:     if(x==0)return;
    70:     c.wait(&m);
    71:   }
    72: }
    73: 
    74: }}
    75: 
End cpp section to pthread/pthread_counter.cpp[1]