IceUtil::CountDownLatch::CountDownLatch(int count) : _count(count) { if(count < 0) { throw Exception(__FILE__, __LINE__); } #ifdef _WIN32 _event = CreateEvent(0, TRUE, FALSE, 0); if(_event == 0) { throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } #else int rc = pthread_mutex_init(&_mutex, 0); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } rc = pthread_cond_init(&_cond, 0); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } #endif }
Cond::Cond() { pthread_condattr_t attr; int rt = pthread_condattr_init(&attr); #ifdef _NO_EXCEPTION assert( 0 == rt ); #else if( 0 != rt ) { throw ThreadSyscallException(__FILE__, __LINE__, rt); } #endif rt = pthread_cond_init(&_cond, &attr); #ifdef _NO_EXCEPTION assert( 0 == rt ); #else if( 0 != rt ) { throw ThreadSyscallException(__FILE__, __LINE__, rt); } #endif rt = pthread_condattr_destroy(&attr); #ifdef _NO_EXCEPTION assert( 0 == rt ); #else if( 0 != rt ) { throw ThreadSyscallException(__FILE__, __LINE__, rt); } #endif }
IceUtil::Cond::Cond() { pthread_condattr_t attr; int rc = pthread_condattr_init(&attr); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } #if !defined(__hpux) && !defined(__APPLE__) rc = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } #endif rc = pthread_cond_init(&_cond, &attr); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } rc = pthread_condattr_destroy(&attr); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } }
void IceUtil::CountDownLatch::await() const { #ifdef _WIN32 while(InterlockedExchangeAdd(&_count, 0) > 0) { DWORD rc = WaitForSingleObject(_event, INFINITE); assert(rc == WAIT_OBJECT_0 || rc == WAIT_FAILED); if(rc == WAIT_FAILED) { throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } } #else lock(); while(_count > 0) { int rc = pthread_cond_wait(&_cond, &_mutex); if(rc != 0) { pthread_mutex_unlock(&_mutex); throw ThreadSyscallException(__FILE__, __LINE__, rc); } } unlock(); #endif }
Threading::Cond::Cond(void) { pthread_condattr_t condattr; int returnVal = pthread_condattr_init(&condattr); if (0 != returnVal) { throw ThreadSyscallException(__FILE__, __LINE__, returnVal); } #if ! defined(__hpux) && ! defined(__APPLE__) returnVal = pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC); if (0 != returnVal) { throw ThreadSyscallException(__FILE__, __LINE__, returnVal); } #endif returnVal = pthread_cond_init(&m_cond, &condattr); if (0 != returnVal) { throw ThreadSyscallException(__FILE__, __LINE__, returnVal); } returnVal = pthread_condattr_destroy(&condattr); if (0 != returnVal) { throw ThreadSyscallException(__FILE__, __LINE__, returnVal); } }
void IceUtil::CountDownLatch::countDown() { #ifdef _WIN32 if(InterlockedDecrement(&_count) == 0) { if(SetEvent(_event) == 0) { throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } } #else bool broadcast = false; lock(); if(_count > 0 && --_count == 0) { broadcast = true; } #if defined(__APPLE__) // // On MacOS X we do the broadcast with the mutex held. This seems to be necessary to prevent the // broadcast call to hang (spinning in an infinite loop). // if(broadcast) { int rc = pthread_cond_broadcast(&_cond); if(rc != 0) { pthread_mutex_unlock(&_mutex); throw ThreadSyscallException(__FILE__, __LINE__, rc); } } unlock(); #else unlock(); if(broadcast) { int rc = pthread_cond_broadcast(&_cond); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } } #endif #endif }
int Thread::detach() { if(!_detachable) { #ifdef _NO_EXCEPTION TBSYS_LOG(ERROR,"%s","BadThreadControlException"); JUST_RETURN( _detachable==false, -1 ); #else throw BadThreadControlException(__FILE__, __LINE__); #endif } const int rt = pthread_detach(_thread); #ifdef _NO_EXCEPTION if ( 0 != rt ) { TBSYS_LOG(ERROR,"%s","ThreadSyscallException"); return -1; } #else if( 0 != rt ) { throw ThreadSyscallException(__FILE__, __LINE__, rt); } #endif return 0; }
IceUtil::Semaphore::Semaphore(long initial) { _sem = CreateSemaphore(0, initial, 0x7fffffff, 0); if(_sem == INVALID_HANDLE_VALUE) { throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } }
void Threading::Cond::Broadcast() { int returnVal = pthread_cond_broadcast(&m_cond); if (0 != returnVal) { throw ThreadSyscallException(__FILE__, __LINE__, returnVal); } }
void Threading::Cond::Signal() { int returnVal = pthread_cond_signal(&m_cond); if (0 != returnVal) { throw ThreadSyscallException(__FILE__, __LINE__, returnVal); } }
void IceUtil::Semaphore::wait() const { int rc = WaitForSingleObject(_sem, INFINITE); if(rc != WAIT_OBJECT_0) { throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } }
void IceUtil::Cond::broadcast() { int rc = pthread_cond_broadcast(&_cond); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } }
void IceUtil::Cond::signal() { int rc = pthread_cond_signal(&_cond); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } }
void IceUtil::Semaphore::post(int count) const { int rc = ReleaseSemaphore(_sem, count, 0); if(rc == 0) { throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } }
void IceUtil::CountDownLatch::unlock() const { int rc = pthread_mutex_unlock(&_mutex); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } }
bool IceUtil::Semaphore::timedWait(const Time& timeout) const { int rc = WaitForSingleObject(_sem, static_cast<long>(timeout.toMilliSeconds())); if(rc != WAIT_TIMEOUT && rc != WAIT_OBJECT_0) { throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); } return rc != WAIT_TIMEOUT; }
void Cond::broadcast() { const int rt = pthread_cond_broadcast(&_cond); #ifdef _NO_EXCEPTION assert( 0 == rt ); #else if( 0 != rt ) { throw ThreadSyscallException(__FILE__, __LINE__, rt); } #endif }
Threading::Cond::Cond(void) try : # if defined(USING_COND_ANY) m_cond(std::condition_variable_any()) # else m_cond(std::condition_variable()); # endif { } catch(const std::system_error& ex) { throw ThreadSyscallException(__FILE__, __LINE__, ex.code().value()); }
IceUtil::Cond::Cond() { int rc; pthread_condattr_t attr; rc = pthread_condattr_init(&attr); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } rc = pthread_cond_init(&_cond, &attr); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } rc = pthread_condattr_destroy(&attr); if(rc != 0) { throw ThreadSyscallException(__FILE__, __LINE__, rc); } }
int Thread::start(size_t stackSize) { ThreadPtr keepMe = this; Mutex::Lock sync(_mutex); if(_started) { #ifdef _NO_EXCEPTION JUST_RETURN( _started == true , -1 ); TBSYS_LOG(ERROR,"%s","ThreadStartedException"); #else throw ThreadStartedException(__FILE__, __LINE__); #endif } __incRef(); if(stackSize > 0) { pthread_attr_t attr; int rt = pthread_attr_init(&attr); #ifdef _NO_EXCEPTION if ( 0 != rt ) { __decRef(); TBSYS_LOG(ERROR,"%s","ThreadSyscallException"); return -1; } #else if ( 0 != rt ) { __decRef(); throw ThreadSyscallException(__FILE__, __LINE__, rt); } #endif if(stackSize < PTHREAD_STACK_MIN) { stackSize = PTHREAD_STACK_MIN; } rt = pthread_attr_setstacksize(&attr, stackSize); #ifdef _NO_EXCEPTION if ( 0 != rt ) { __decRef(); TBSYS_LOG(ERROR,"%s","ThreadSyscallException"); return -1; } #else if( 0 != rt ) { __decRef(); throw ThreadSyscallException(__FILE__, __LINE__, rt); } #endif rt = pthread_create(&_thread, &attr, startHook, this); #ifdef _NO_EXCEPTION if ( 0 != rt ) { __decRef(); TBSYS_LOG(ERROR,"%s","ThreadSyscallException"); return -1; } #else if( 0 != rt ) { __decRef(); throw ThreadSyscallException(__FILE__, __LINE__, rt); } #endif } else { const int rt = pthread_create(&_thread, 0, startHook, this); #ifdef _NO_EXCEPTION if ( 0 != rt ) { __decRef(); TBSYS_LOG(ERROR,"%s","ThreadSyscallException"); return -1; } #else if( 0 != rt ) { __decRef(); throw ThreadSyscallException(__FILE__, __LINE__, rt); } #endif } if ( _detachable ) { detach(); } _started = true; _running = true; return 0; }