1// LazyInitializable.cpp 2 3#include "LazyInitializable.h" 4 5// constructor 6LazyInitializable::LazyInitializable() 7 : fInitStatus(B_NO_INIT), 8 fInitSemaphore(-1) 9{ 10 fInitSemaphore = create_sem(1, "init semaphore"); 11 if (fInitSemaphore < 0) 12 fInitStatus = fInitSemaphore; 13} 14 15// constructor 16LazyInitializable::LazyInitializable(bool init) 17 : fInitStatus(B_NO_INIT), 18 fInitSemaphore(-1) 19{ 20 if (init) { 21 fInitSemaphore = create_sem(1, "init semaphore"); 22 if (fInitSemaphore < 0) 23 fInitStatus = fInitSemaphore; 24 } else 25 fInitStatus = B_OK; 26} 27 28// destructor 29LazyInitializable::~LazyInitializable() 30{ 31 if (fInitSemaphore >= 0) 32 delete_sem(fInitSemaphore); 33} 34 35// Access 36status_t 37LazyInitializable::Access() 38{ 39 if (fInitSemaphore >= 0) { 40 status_t error = B_OK; 41 do { 42 error = acquire_sem(fInitSemaphore); 43 } while (error == B_INTERRUPTED); 44 if (error == B_OK) { 45 // we are the first: initialize 46 fInitStatus = FirstTimeInit(); 47 delete_sem(fInitSemaphore); 48 fInitSemaphore = -1; 49 } 50 } 51 return fInitStatus; 52} 53 54// InitCheck 55status_t 56LazyInitializable::InitCheck() const 57{ 58 return fInitStatus; 59} 60 61