// This Barrier can be used only once and cannot be reused. // Header file sbarrier.h for class Barrier. (SingleBarrier use) // Includes all functions associated with the Barrier // To use a Barrier, first share a pointer variable of size // (Barrier*) and use Barrierinit function on that pointer // passing the numtoblock. // For example, if 3 processes are to be blocked, // Barrier *B; // B = (Barrier*)shareint(sizeof(Barrier*), shareid); // B->Barrierinit(3); #ifndef BARRIER_H #define BARRIER_H #include using namespace std; #include "utility.h" class Barrier { public: Barrier(); //default constructor ~Barrier() {} //destructor void Barrierinit(int numtoblock); // initialize barrier void WaitAtBarrier(); //Blocks processes void SemRelease(); //Releases Semaphores private: int toblock; int gateopen; int blocked; int gate; }; Barrier::Barrier() { toblock = blocked = gateopen = gate = 0; } void Barrier::Barrierinit(int numtoblock) { toblock = numtoblock; //Number of processes to block gateopen = 0; //status of gate blocked = 0; //No process have called yet spin_lock_init(gate, 0); //Initialize spinlock controlling barrier } void Barrier::WaitAtBarrier() { spin_lock(gate); blocked++; // Increment number of processes at gate if (blocked == toblock) // All processes at the gate { gateopen = 1; // Open the gate } spin_unlock(gate); while (!gateopen); // Wait till gate opens } void Barrier::SemRelease() // a public function to release semaphore { clean_up_sem(gate); } #endif