inline handle duplicate_handle(handle source) { handle const current_process=GetCurrentProcess(); long const same_access_flag=2; handle new_handle=0; bool const success=DuplicateHandle(current_process,source,current_process,&new_handle,0,false,same_access_flag)!=0; if(!success) { boost::throw_exception(thread_resource_error()); } return new_handle; }
void add_thread(thread* thrd) { if(thrd) { BOOST_THREAD_ASSERT_PRECONDITION( ! is_thread_in(thrd) , thread_resource_error(system::errc::resource_deadlock_would_occur, "lslboost::thread_group: trying to add a duplicated thread") ); lslboost::lock_guard<shared_mutex> guard(m); threads.push_back(thrd); } }
recursive_mutex() { #ifdef BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE pthread_mutexattr_t attr; int const init_attr_res=pthread_mutexattr_init(&attr); if(init_attr_res) { boost::throw_exception(thread_resource_error()); } int const set_attr_res=pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE); if(set_attr_res) { BOOST_VERIFY(!pthread_mutexattr_destroy(&attr)); boost::throw_exception(thread_resource_error()); } int const res=pthread_mutex_init(&m,&attr); if(res) { BOOST_VERIFY(!pthread_mutexattr_destroy(&attr)); boost::throw_exception(thread_resource_error()); } BOOST_VERIFY(!pthread_mutexattr_destroy(&attr)); #else int const res=pthread_mutex_init(&m,NULL); if(res) { boost::throw_exception(thread_resource_error()); } int const res2=pthread_cond_init(&cond,NULL); if(res2) { BOOST_VERIFY(!pthread_mutex_destroy(&m)); boost::throw_exception(thread_resource_error()); } is_locked=false; count=0; #endif }
inline handle create_anonymous_semaphore(long initial_count,long max_count) { #if !defined(BOOST_NO_ANSI_APIS) handle const res=CreateSemaphoreA(0,initial_count,max_count,0); #else handle const res=CreateSemaphoreW(0,initial_count,max_count,0); #endif if(!res) { boost::throw_exception(thread_resource_error()); } return res; }
inline handle create_anonymous_event(event_type type,initial_event_state state) { #if !defined(BOOST_NO_ANSI_APIS) handle const res=win32::CreateEventA(0,type,state,0); #else handle const res=win32::CreateEventW(0,type,state,0); #endif if(!res) { boost::throw_exception(thread_resource_error()); } return res; }
recursive_mutex() { pthread_mutexattr_t attr; int const init_attr_res=pthread_mutexattr_init(&attr); if(init_attr_res) { throw thread_resource_error(); } int const set_attr_res=pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE); if(set_attr_res) { throw thread_resource_error(); } int const res=pthread_mutex_init(&m,&attr); if(res) { throw thread_resource_error(); } BOOST_VERIFY(!pthread_mutexattr_destroy(&attr)); }
recursive_timed_mutex() { #ifdef BOOST_PTHREAD_HAS_TIMEDLOCK pthread_mutexattr_t attr; int const init_attr_res=pthread_mutexattr_init(&attr); if(init_attr_res) { throw thread_resource_error(); } int const set_attr_res=pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE); if(set_attr_res) { throw thread_resource_error(); } int const res=pthread_mutex_init(&m,&attr); if(res) { BOOST_VERIFY(!pthread_mutexattr_destroy(&attr)); throw thread_resource_error(); } BOOST_VERIFY(!pthread_mutexattr_destroy(&attr)); #else int const res=pthread_mutex_init(&m,NULL); if(res) { throw thread_resource_error(); } int const res2=pthread_cond_init(&cond,NULL); if(res2) { BOOST_VERIFY(!pthread_mutex_destroy(&m)); throw thread_resource_error(); } is_locked=false; count=0; #endif }
void join_all() { BOOST_THREAD_ASSERT_PRECONDITION( ! is_this_thread_in() , thread_resource_error(static_cast<int>(system::errc::resource_deadlock_would_occur), "boost::thread_group: trying joining itself") ); boost::shared_lock<shared_mutex> guard(m); for(std::list<thread*>::iterator it=threads.begin(),end=threads.end(); it!=end; ++it) { if ((*it)->joinable()) (*it)->join(); } }
thread::thread(const function0<void>& threadfunc) : m_joinable(true) { thread_param param(threadfunc); #if defined(BOOST_HAS_WINTHREADS) m_thread = reinterpret_cast<void*>(_beginthreadex(0, 0, &thread_proxy, ¶m, 0, &m_id)); if (!m_thread) throw thread_resource_error(); #elif defined(BOOST_HAS_PTHREADS) int res = 0; res = pthread_create(&m_thread, 0, &thread_proxy, ¶m); if (res != 0) throw thread_resource_error(); #elif defined(BOOST_HAS_MPTASKS) threads::mac::detail::thread_init(); threads::mac::detail::create_singletons(); OSStatus lStatus = noErr; m_pJoinQueueID = kInvalidID; m_pTaskID = kInvalidID; lStatus = MPCreateQueue(&m_pJoinQueueID); if (lStatus != noErr) throw thread_resource_error(); lStatus = MPCreateTask(&thread_proxy, ¶m, 0UL, m_pJoinQueueID, NULL, NULL, 0UL, &m_pTaskID); if (lStatus != noErr) { lStatus = MPDeleteQueue(m_pJoinQueueID); assert(lStatus == noErr); throw thread_resource_error(); } #endif param.wait(); }