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