ders::barrier Class Reference

Represents implementation independent thread barrier. More...

#include <thread.hpp>

List of all members.

Public Member Functions

 barrier (mem_pool &mp, int cnt)
 ~barrier ()
int inc_count ()
int dec_count ()
int get_count ()
bool wait ()
void release_one ()
void release_all ()
barrier_listenerset_listener (barrier_listener *bl)


Detailed Description

Represents implementation independent thread barrier.

Definition at line 127 of file thread.hpp.


Constructor & Destructor Documentation

ders::barrier::barrier ( mem_pool mp,
int  cnt 
)

Definition at line 75 of file mt_thread.cpp.

00075                                       :
00076   mtx(new_mutex(mp)), cv1(new_cond_var(mp, *mtx)), cv2(new_cond_var(mp, *mtx)),
00077   maxCnt(cnt), wtCnt(0), relCnt(0), ret(false), blnr(0)
00078 {
00079  assert(maxCnt>=0);
00080 }

ders::barrier::~barrier (  ) 

Definition at line 82 of file mt_thread.cpp.

00083 {
00084  assert(wtCnt==0);
00085 }


Member Function Documentation

int ders::barrier::inc_count (  ) 

Definition at line 87 of file mt_thread.cpp.

00088 {
00089  auto_mutex amt(*mtx);
00090  return ++maxCnt;
00091 }

int ders::barrier::dec_count (  ) 

Definition at line 93 of file mt_thread.cpp.

00094 {
00095  auto_mutex amt(*mtx);
00096  assert(wtCnt<maxCnt);
00097 
00098  relCnt=--maxCnt;
00099  cv1->notify_all();
00100 
00101  return maxCnt;
00102 }

int ders::barrier::get_count (  ) 

Definition at line 104 of file mt_thread.cpp.

00105 {
00106  auto_mutex amt(*mtx);
00107  assert(maxCnt>=0);
00108  return maxCnt;
00109 }

bool ders::barrier::wait (  ) 

Definition at line 111 of file mt_thread.cpp.

00112 {
00113  auto_mutex amt(*mtx);
00114  assert(maxCnt>0);
00115 
00116  while (wtCnt>0 && relCnt>0)
00117        cv2->wait();
00118 
00119  assert(wtCnt<maxCnt);
00120  if (++wtCnt==maxCnt) {
00121     barrier_listener::action act=barrier_listener::release_all_true;
00122     if (blnr) {
00123        try { act=blnr->on_release(*this); }
00124        catch (...) { hard_assert(false); }
00125     }
00126 
00127     switch (act) {
00128            case barrier_listener::release_one_false: {
00129                 if (relCnt<maxCnt) relCnt++;
00130 
00131                 break;
00132            }
00133            case barrier_listener::release_all_false: {
00134                 relCnt=maxCnt;
00135                 cv1->notify_all();
00136 
00137                 break;
00138            }
00139            case barrier_listener::release_all_true: {
00140                 relCnt=maxCnt;
00141                 ret=true;
00142                 cv1->notify_all();
00143 
00144                 break;
00145            }
00146     }
00147  }
00148 
00149  for (;;) {
00150      if (relCnt>0) {
00151         relCnt--;
00152         break;
00153      }
00154 
00155      cv1->wait();
00156  }
00157 
00158  bool sav=ret;
00159  assert(wtCnt>0);
00160  if (--wtCnt==0) ret=false;
00161 
00162  if (wtCnt==0 || relCnt==0) cv2->notify_all();
00163 
00164  return sav;
00165 }

void ders::barrier::release_one (  ) 

Definition at line 167 of file mt_thread.cpp.

00168 {
00169  auto_mutex amt(*mtx);
00170 
00171  if (relCnt<maxCnt) relCnt++;
00172  cv1->notify();
00173 }

void ders::barrier::release_all (  ) 

Definition at line 175 of file mt_thread.cpp.

00176 {
00177  auto_mutex amt(*mtx);
00178 
00179  relCnt=maxCnt;
00180  cv1->notify_all();
00181 }

barrier_listener * ders::barrier::set_listener ( barrier_listener bl  ) 

Definition at line 183 of file mt_thread.cpp.

00184 {
00185  barrier_listener* ret=blnr;
00186  blnr=bl;
00187 
00188  return ret;
00189 }


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

Generated on Tue Dec 8 11:35:33 2009 for derslib by  doxygen 1.5.5