os_thread_functions.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. //Thread launching functions are adapted from boost/detail/lightweight_thread.hpp
  11. //
  12. // boost/detail/lightweight_thread.hpp
  13. //
  14. // Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
  15. // Copyright (c) 2008 Peter Dimov
  16. //
  17. // Distributed under the Boost Software License, Version 1.0.
  18. // See accompanying file LICENSE_1_0.txt or copy at
  19. // http://www.boost.org/LICENSE_1_0.txt
  20. #ifndef BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP
  21. #define BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP
  22. #ifndef BOOST_CONFIG_HPP
  23. # include <boost/config.hpp>
  24. #endif
  25. #
  26. #if defined(BOOST_HAS_PRAGMA_ONCE)
  27. # pragma once
  28. #endif
  29. #include <boost/interprocess/detail/config_begin.hpp>
  30. #include <boost/interprocess/detail/workaround.hpp>
  31. #include <boost/interprocess/streams/bufferstream.hpp>
  32. #include <cstddef>
  33. #include <cassert>
  34. #if defined(BOOST_INTERPROCESS_WINDOWS)
  35. # include <boost/interprocess/detail/win32_api.hpp>
  36. # include <boost/winapi/thread.hpp>
  37. #else
  38. # include <pthread.h>
  39. # include <unistd.h>
  40. # include <sched.h>
  41. # include <time.h>
  42. # include <errno.h>
  43. # ifdef BOOST_INTERPROCESS_BSD_DERIVATIVE
  44. //Some *BSD systems (OpenBSD & NetBSD) need sys/param.h before sys/sysctl.h, whereas
  45. //others (FreeBSD & Darwin) need sys/types.h
  46. # include <sys/types.h>
  47. # include <sys/param.h>
  48. # include <sys/sysctl.h>
  49. # endif
  50. #if defined(__VXWORKS__)
  51. #include <vxCpuLib.h>
  52. #endif
  53. //According to the article "C/C++ tip: How to measure elapsed real time for benchmarking"
  54. //Check MacOs first as macOS 10.12 SDK defines both CLOCK_MONOTONIC and
  55. //CLOCK_MONOTONIC_RAW and no clock_gettime.
  56. # if (defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__))
  57. # include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
  58. # define BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME
  59. # elif defined(CLOCK_MONOTONIC_PRECISE) //BSD
  60. # define BOOST_INTERPROCESS_CLOCK_MONOTONIC CLOCK_MONOTONIC_PRECISE
  61. # elif defined(CLOCK_MONOTONIC_RAW) //Linux
  62. # define BOOST_INTERPROCESS_CLOCK_MONOTONIC CLOCK_MONOTONIC_RAW
  63. # elif defined(CLOCK_HIGHRES) //Solaris
  64. # define BOOST_INTERPROCESS_CLOCK_MONOTONIC CLOCK_HIGHRES
  65. # elif defined(CLOCK_MONOTONIC) //POSIX (AIX, BSD, Linux, Solaris)
  66. # define BOOST_INTERPROCESS_CLOCK_MONOTONIC CLOCK_MONOTONIC
  67. # else
  68. # error "No high resolution steady clock in your system, please provide a patch"
  69. # endif
  70. #endif
  71. namespace boost {
  72. namespace interprocess {
  73. namespace ipcdetail{
  74. #if defined (BOOST_INTERPROCESS_WINDOWS)
  75. typedef unsigned long OS_process_id_t;
  76. typedef unsigned long OS_thread_id_t;
  77. struct OS_thread_t
  78. {
  79. OS_thread_t()
  80. : m_handle()
  81. {}
  82. void* handle() const
  83. { return m_handle; }
  84. void* m_handle;
  85. };
  86. typedef OS_thread_id_t OS_systemwide_thread_id_t;
  87. //process
  88. inline OS_process_id_t get_current_process_id()
  89. { return winapi::get_current_process_id(); }
  90. inline OS_process_id_t get_invalid_process_id()
  91. { return OS_process_id_t(0); }
  92. //thread
  93. inline OS_thread_id_t get_current_thread_id()
  94. { return winapi::get_current_thread_id(); }
  95. inline OS_thread_id_t get_invalid_thread_id()
  96. { return OS_thread_id_t(0xffffffff); }
  97. inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
  98. { return id1 == id2; }
  99. //return the system tick in ns
  100. inline unsigned long get_system_tick_ns()
  101. {
  102. unsigned long curres, ignore1, ignore2;
  103. winapi::query_timer_resolution(&ignore1, &ignore2, &curres);
  104. //Windows API returns the value in hundreds of ns
  105. return (curres - 1ul)*100ul;
  106. }
  107. //return the system tick in us
  108. inline unsigned long get_system_tick_us()
  109. {
  110. unsigned long curres, ignore1, ignore2;
  111. winapi::query_timer_resolution(&ignore1, &ignore2, &curres);
  112. //Windows API returns the value in hundreds of ns
  113. return (curres - 1ul)/10ul + 1ul;
  114. }
  115. typedef unsigned __int64 OS_highres_count_t;
  116. inline unsigned long get_system_tick_in_highres_counts()
  117. {
  118. __int64 freq;
  119. unsigned long curres, ignore1, ignore2;
  120. winapi::query_timer_resolution(&ignore1, &ignore2, &curres);
  121. //Frequency in counts per second
  122. if(!winapi::query_performance_frequency(&freq)){
  123. //Tick resolution in ms
  124. return (curres-1ul)/10000ul + 1ul;
  125. }
  126. else{
  127. //In femtoseconds
  128. __int64 count_fs = (1000000000000000LL - 1LL)/freq + 1LL;
  129. __int64 tick_counts = (static_cast<__int64>(curres)*100000000LL - 1LL)/count_fs + 1LL;
  130. return static_cast<unsigned long>(tick_counts);
  131. }
  132. }
  133. inline OS_highres_count_t get_current_system_highres_count()
  134. {
  135. __int64 count;
  136. if(!winapi::query_performance_counter(&count)){
  137. count = winapi::get_tick_count();
  138. }
  139. return (OS_highres_count_t)count;
  140. }
  141. inline unsigned get_current_system_highres_rand()
  142. {
  143. unsigned __int64 count = (unsigned __int64) get_current_system_highres_count();
  144. return static_cast<unsigned>(count + (count >> 32u));
  145. }
  146. inline void zero_highres_count(OS_highres_count_t &count)
  147. { count = 0; }
  148. inline bool is_highres_count_zero(const OS_highres_count_t &count)
  149. { return count == 0; }
  150. template <class Ostream>
  151. inline Ostream &ostream_highres_count(Ostream &ostream, const OS_highres_count_t &count)
  152. {
  153. ostream << count;
  154. return ostream;
  155. }
  156. inline OS_highres_count_t system_highres_count_subtract(const OS_highres_count_t &l, const OS_highres_count_t &r)
  157. { return l - r; }
  158. inline bool system_highres_count_less(const OS_highres_count_t &l, const OS_highres_count_t &r)
  159. { return l < r; }
  160. inline bool system_highres_count_less_ul(const OS_highres_count_t &l, unsigned long r)
  161. { return l < static_cast<OS_highres_count_t>(r); }
  162. inline void thread_sleep_tick()
  163. { winapi::sleep_tick(); }
  164. inline void thread_yield()
  165. { winapi::sched_yield(); }
  166. inline void thread_sleep_ms(unsigned int ms)
  167. { winapi::sleep(ms); }
  168. //systemwide thread
  169. inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
  170. {
  171. return get_current_thread_id();
  172. }
  173. inline void systemwide_thread_id_copy
  174. (const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
  175. {
  176. to = from;
  177. }
  178. inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
  179. {
  180. return equal_thread_id(id1, id2);
  181. }
  182. inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
  183. {
  184. return get_invalid_thread_id();
  185. }
  186. inline long double get_current_process_creation_time()
  187. {
  188. winapi::interprocess_filetime CreationTime, ExitTime, KernelTime, UserTime;
  189. winapi::get_process_times
  190. ( winapi::get_current_process(), &CreationTime, &ExitTime, &KernelTime, &UserTime);
  191. typedef long double ldouble_t;
  192. const ldouble_t resolution = (100.0l/1000000000.0l);
  193. return CreationTime.dwHighDateTime*(ldouble_t(1u<<31u)*2.0l*resolution) +
  194. CreationTime.dwLowDateTime*resolution;
  195. }
  196. inline unsigned int get_num_cores()
  197. {
  198. winapi::interprocess_system_info sysinfo;
  199. winapi::get_system_info( &sysinfo );
  200. //in Windows dw is long which is equal in bits to int
  201. return static_cast<unsigned>(sysinfo.dwNumberOfProcessors);
  202. }
  203. #else //#if defined (BOOST_INTERPROCESS_WINDOWS)
  204. typedef pthread_t OS_thread_t;
  205. typedef pthread_t OS_thread_id_t;
  206. typedef pid_t OS_process_id_t;
  207. struct OS_systemwide_thread_id_t
  208. {
  209. OS_systemwide_thread_id_t()
  210. : pid(), tid()
  211. {}
  212. OS_systemwide_thread_id_t(pid_t p, pthread_t t)
  213. : pid(p), tid(t)
  214. {}
  215. OS_systemwide_thread_id_t(const OS_systemwide_thread_id_t &x)
  216. : pid(x.pid), tid(x.tid)
  217. {}
  218. OS_systemwide_thread_id_t(const volatile OS_systemwide_thread_id_t &x)
  219. : pid(x.pid), tid(x.tid)
  220. {}
  221. OS_systemwide_thread_id_t & operator=(const OS_systemwide_thread_id_t &x)
  222. { pid = x.pid; tid = x.tid; return *this; }
  223. OS_systemwide_thread_id_t & operator=(const volatile OS_systemwide_thread_id_t &x)
  224. { pid = x.pid; tid = x.tid; return *this; }
  225. void operator=(const OS_systemwide_thread_id_t &x) volatile
  226. { pid = x.pid; tid = x.tid; }
  227. pid_t pid;
  228. pthread_t tid;
  229. };
  230. inline void systemwide_thread_id_copy
  231. (const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
  232. {
  233. to.pid = from.pid;
  234. to.tid = from.tid;
  235. }
  236. //process
  237. inline OS_process_id_t get_current_process_id()
  238. { return ::getpid(); }
  239. inline OS_process_id_t get_invalid_process_id()
  240. { return pid_t(0); }
  241. //thread
  242. inline OS_thread_id_t get_current_thread_id()
  243. { return ::pthread_self(); }
  244. inline OS_thread_id_t get_invalid_thread_id()
  245. {
  246. static pthread_t invalid_id;
  247. return invalid_id;
  248. }
  249. inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
  250. { return 0 != pthread_equal(id1, id2); }
  251. inline void thread_yield()
  252. { ::sched_yield(); }
  253. #ifndef BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME
  254. typedef struct timespec OS_highres_count_t;
  255. #else
  256. typedef unsigned long long OS_highres_count_t;
  257. #endif
  258. inline unsigned long get_system_tick_ns()
  259. {
  260. #ifdef _SC_CLK_TCK
  261. long ticks_per_second =::sysconf(_SC_CLK_TCK); // ticks per sec
  262. if(ticks_per_second <= 0){ //Try a typical value on error
  263. ticks_per_second = 100;
  264. }
  265. return 999999999ul/static_cast<unsigned long>(ticks_per_second)+1ul;
  266. #else
  267. #error "Can't obtain system tick value for your system, please provide a patch"
  268. #endif
  269. }
  270. inline unsigned long get_system_tick_in_highres_counts()
  271. {
  272. #ifndef BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME
  273. return get_system_tick_ns();
  274. #else
  275. mach_timebase_info_data_t info;
  276. mach_timebase_info(&info);
  277. //ns
  278. return static_cast<unsigned long>
  279. (
  280. static_cast<double>(get_system_tick_ns())
  281. / (static_cast<double>(info.numer) / info.denom)
  282. );
  283. #endif
  284. }
  285. //return system ticks in us
  286. inline unsigned long get_system_tick_us()
  287. {
  288. return (get_system_tick_ns()-1)/1000ul + 1ul;
  289. }
  290. inline OS_highres_count_t get_current_system_highres_count()
  291. {
  292. #if defined(BOOST_INTERPROCESS_CLOCK_MONOTONIC)
  293. struct timespec count;
  294. ::clock_gettime(BOOST_INTERPROCESS_CLOCK_MONOTONIC, &count);
  295. return count;
  296. #elif defined(BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME)
  297. return ::mach_absolute_time();
  298. #endif
  299. }
  300. inline unsigned get_current_system_highres_rand()
  301. {
  302. OS_highres_count_t count = get_current_system_highres_count();
  303. #if defined(BOOST_INTERPROCESS_CLOCK_MONOTONIC)
  304. return static_cast<unsigned>(count.tv_sec + count.tv_nsec);
  305. #elif defined(BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME)
  306. return static_cast<unsigned>(count);
  307. #endif
  308. }
  309. #ifndef BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME
  310. inline void zero_highres_count(OS_highres_count_t &count)
  311. { count.tv_sec = 0; count.tv_nsec = 0; }
  312. inline bool is_highres_count_zero(const OS_highres_count_t &count)
  313. { return count.tv_sec == 0 && count.tv_nsec == 0; }
  314. template <class Ostream>
  315. inline Ostream &ostream_highres_count(Ostream &ostream, const OS_highres_count_t &count)
  316. {
  317. ostream << count.tv_sec << "s:" << count.tv_nsec << "ns";
  318. return ostream;
  319. }
  320. inline OS_highres_count_t system_highres_count_subtract(const OS_highres_count_t &l, const OS_highres_count_t &r)
  321. {
  322. OS_highres_count_t res;
  323. if (l.tv_nsec < r.tv_nsec){
  324. res.tv_nsec = 1000000000 + l.tv_nsec - r.tv_nsec;
  325. res.tv_sec = l.tv_sec - 1 - r.tv_sec;
  326. }
  327. else{
  328. res.tv_nsec = l.tv_nsec - r.tv_nsec;
  329. res.tv_sec = l.tv_sec - r.tv_sec;
  330. }
  331. return res;
  332. }
  333. inline bool system_highres_count_less(const OS_highres_count_t &l, const OS_highres_count_t &r)
  334. { return l.tv_sec < r.tv_sec || (l.tv_sec == r.tv_sec && l.tv_nsec < r.tv_nsec); }
  335. inline bool system_highres_count_less_ul(const OS_highres_count_t &l, unsigned long r)
  336. { return !l.tv_sec && (static_cast<unsigned long>(l.tv_nsec) < r); }
  337. #else
  338. inline void zero_highres_count(OS_highres_count_t &count)
  339. { count = 0; }
  340. inline bool is_highres_count_zero(const OS_highres_count_t &count)
  341. { return count == 0; }
  342. template <class Ostream>
  343. inline Ostream &ostream_highres_count(Ostream &ostream, const OS_highres_count_t &count)
  344. {
  345. ostream << count ;
  346. return ostream;
  347. }
  348. inline OS_highres_count_t system_highres_count_subtract(const OS_highres_count_t &l, const OS_highres_count_t &r)
  349. { return l - r; }
  350. inline bool system_highres_count_less(const OS_highres_count_t &l, const OS_highres_count_t &r)
  351. { return l < r; }
  352. inline bool system_highres_count_less_ul(const OS_highres_count_t &l, unsigned long r)
  353. { return l < static_cast<OS_highres_count_t>(r); }
  354. #endif
  355. inline void thread_sleep_tick()
  356. {
  357. struct timespec rqt;
  358. //Sleep for the half of the tick time
  359. rqt.tv_sec = 0;
  360. rqt.tv_nsec = (long)get_system_tick_ns()/2;
  361. struct timespec rmn;
  362. while (0 != ::nanosleep(&rqt, &rmn) && errno == EINTR) {
  363. rqt.tv_sec = rmn.tv_sec;
  364. rqt.tv_nsec = rmn.tv_nsec;
  365. }
  366. }
  367. inline void thread_sleep_ms(unsigned int ms)
  368. {
  369. struct timespec rqt;
  370. rqt.tv_sec = ms/1000u;
  371. rqt.tv_nsec = (ms%1000u)*1000000u;
  372. struct timespec rmn;
  373. while (0 != ::nanosleep(&rqt, &rmn) && errno == EINTR) {
  374. rqt.tv_sec = rmn.tv_sec;
  375. rqt.tv_nsec = rmn.tv_nsec;
  376. }
  377. }
  378. //systemwide thread
  379. inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
  380. {
  381. return OS_systemwide_thread_id_t(::getpid(), ::pthread_self());
  382. }
  383. inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
  384. {
  385. return (0 != pthread_equal(id1.tid, id2.tid)) && (id1.pid == id2.pid);
  386. }
  387. inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
  388. {
  389. return OS_systemwide_thread_id_t(get_invalid_process_id(), get_invalid_thread_id());
  390. }
  391. inline long double get_current_process_creation_time()
  392. { return 0.0L; }
  393. inline unsigned int get_num_cores()
  394. {
  395. #ifdef _SC_NPROCESSORS_ONLN
  396. long cores = ::sysconf(_SC_NPROCESSORS_ONLN);
  397. // sysconf returns -1 if the name is invalid, the option does not exist or
  398. // does not have a definite limit.
  399. // if sysconf returns some other negative number, we have no idea
  400. // what is going on. Default to something safe.
  401. if(cores <= 0){
  402. return 1;
  403. }
  404. //Check for overflow (unlikely)
  405. else if(static_cast<unsigned long>(cores) >=
  406. static_cast<unsigned long>(static_cast<unsigned int>(-1))){
  407. return static_cast<unsigned int>(-1);
  408. }
  409. else{
  410. return static_cast<unsigned int>(cores);
  411. }
  412. #elif defined(BOOST_INTERPROCESS_BSD_DERIVATIVE) && defined(HW_NCPU)
  413. int request[2] = { CTL_HW, HW_NCPU };
  414. int num_cores;
  415. std::size_t result_len = sizeof(num_cores);
  416. if ( (::sysctl (request, 2, &num_cores, &result_len, 0, 0) < 0) || (num_cores <= 0) ){
  417. //Return a safe value
  418. return 1;
  419. }
  420. else{
  421. return static_cast<unsigned int>(num_cores);
  422. }
  423. #elif defined(__VXWORKS__)
  424. cpuset_t set = ::vxCpuEnabledGet();
  425. #ifdef __DCC__
  426. int i;
  427. for( i = 0; set; ++i)
  428. {
  429. set &= set -1;
  430. }
  431. return(i);
  432. #else
  433. return (__builtin_popcount(set) );
  434. #endif
  435. #endif
  436. }
  437. inline int thread_create(OS_thread_t * thread, void *(*start_routine)(void*), void* arg)
  438. { return pthread_create(thread, 0, start_routine, arg); }
  439. inline void thread_join(OS_thread_t thread)
  440. {
  441. int ret = pthread_join(thread, 0);
  442. (void)ret;
  443. assert(0 == ret);
  444. }
  445. #endif //#if defined (BOOST_INTERPROCESS_WINDOWS)
  446. typedef char pid_str_t[sizeof(OS_process_id_t)*3+1];
  447. inline void get_pid_str(pid_str_t &pid_str, OS_process_id_t pid)
  448. {
  449. bufferstream bstream(pid_str, sizeof(pid_str));
  450. bstream << pid << std::ends;
  451. }
  452. inline void get_pid_str(pid_str_t &pid_str)
  453. { get_pid_str(pid_str, get_current_process_id()); }
  454. #if defined(BOOST_INTERPROCESS_WINDOWS)
  455. inline int thread_create( OS_thread_t * thread, boost::ipwinapiext::LPTHREAD_START_ROUTINE_ start_routine, void* arg )
  456. {
  457. void* h = boost::ipwinapiext::CreateThread(0, 0, start_routine, arg, 0, 0);
  458. if( h != 0 ){
  459. thread->m_handle = h;
  460. return 0;
  461. }
  462. else{
  463. return 1;
  464. }
  465. }
  466. inline void thread_join( OS_thread_t thread)
  467. {
  468. {
  469. unsigned long ret = winapi::wait_for_single_object( thread.handle(), winapi::infinite_time );
  470. assert(0 == ret);
  471. (void)ret;
  472. }
  473. {
  474. bool ret = winapi::close_handle(thread.handle());
  475. assert(true == ret);
  476. (void)ret;
  477. }
  478. }
  479. #endif
  480. class abstract_thread
  481. {
  482. public:
  483. virtual ~abstract_thread() {}
  484. virtual void run() = 0;
  485. };
  486. template<class T>
  487. class os_thread_func_ptr_deleter
  488. {
  489. public:
  490. explicit os_thread_func_ptr_deleter(T* p)
  491. : m_p(p)
  492. {}
  493. T *release()
  494. { T *p = m_p; m_p = 0; return p; }
  495. T *get() const
  496. { return m_p; }
  497. T *operator ->() const
  498. { return m_p; }
  499. ~os_thread_func_ptr_deleter()
  500. { delete m_p; }
  501. private:
  502. T *m_p;
  503. };
  504. #if defined(BOOST_INTERPROCESS_WINDOWS)
  505. inline boost::winapi::DWORD_ __stdcall launch_thread_routine(boost::winapi::LPVOID_ pv)
  506. {
  507. os_thread_func_ptr_deleter<abstract_thread> pt( static_cast<abstract_thread *>( pv ) );
  508. pt->run();
  509. return 0;
  510. }
  511. #else
  512. extern "C" void * launch_thread_routine( void * pv );
  513. inline void * launch_thread_routine( void * pv )
  514. {
  515. os_thread_func_ptr_deleter<abstract_thread> pt( static_cast<abstract_thread *>( pv ) );
  516. pt->run();
  517. return 0;
  518. }
  519. #endif
  520. template<class F>
  521. class launch_thread_impl
  522. : public abstract_thread
  523. {
  524. public:
  525. explicit launch_thread_impl( F f )
  526. : f_( f )
  527. {}
  528. virtual void run() BOOST_OVERRIDE
  529. { f_(); }
  530. private:
  531. F f_;
  532. };
  533. template<class F>
  534. inline int thread_launch( OS_thread_t & pt, F f )
  535. {
  536. os_thread_func_ptr_deleter<abstract_thread> p( new launch_thread_impl<F>( f ) );
  537. int r = thread_create(&pt, launch_thread_routine, p.get());
  538. if( r == 0 ){
  539. p.release();
  540. }
  541. return r;
  542. }
  543. } //namespace ipcdetail{
  544. } //namespace interprocess {
  545. } //namespace boost {
  546. #include <boost/interprocess/detail/config_end.hpp>
  547. #endif //BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP