File indexing completed on 2025-07-15 08:36:45
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #ifndef BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP
0023 #define BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP
0024
0025 #ifndef BOOST_CONFIG_HPP
0026 # include <boost/config.hpp>
0027 #endif
0028 #
0029 #if defined(BOOST_HAS_PRAGMA_ONCE)
0030 # pragma once
0031 #endif
0032
0033 #include <boost/interprocess/detail/config_begin.hpp>
0034 #include <boost/interprocess/detail/workaround.hpp>
0035 #include <boost/interprocess/streams/bufferstream.hpp>
0036 #include <cstddef>
0037 #include <cassert>
0038
0039 #if defined(BOOST_INTERPROCESS_WINDOWS)
0040 # include <boost/interprocess/detail/win32_api.hpp>
0041 # include <boost/winapi/thread.hpp>
0042 #else
0043 # include <pthread.h>
0044 # include <unistd.h>
0045 # include <sched.h>
0046 # include <time.h>
0047 # include <errno.h>
0048 # ifdef BOOST_INTERPROCESS_BSD_DERIVATIVE
0049
0050
0051 # include <sys/types.h>
0052 # include <sys/param.h>
0053 # include <sys/sysctl.h>
0054 # endif
0055 #if defined(__VXWORKS__)
0056 #include <vxCpuLib.h>
0057 #endif
0058
0059
0060
0061 # if (defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__))
0062 # include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
0063 # define BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME
0064 # elif defined(CLOCK_MONOTONIC_PRECISE)
0065 # define BOOST_INTERPROCESS_CLOCK_MONOTONIC CLOCK_MONOTONIC_PRECISE
0066 # elif defined(CLOCK_MONOTONIC_RAW)
0067 # define BOOST_INTERPROCESS_CLOCK_MONOTONIC CLOCK_MONOTONIC_RAW
0068 # elif defined(CLOCK_HIGHRES)
0069 # define BOOST_INTERPROCESS_CLOCK_MONOTONIC CLOCK_HIGHRES
0070 # elif defined(CLOCK_MONOTONIC)
0071 # define BOOST_INTERPROCESS_CLOCK_MONOTONIC CLOCK_MONOTONIC
0072 # else
0073 # error "No high resolution steady clock in your system, please provide a patch"
0074 # endif
0075 #endif
0076
0077 namespace boost {
0078 namespace interprocess {
0079 namespace ipcdetail{
0080
0081 #if defined (BOOST_INTERPROCESS_WINDOWS)
0082
0083 typedef unsigned long OS_process_id_t;
0084 typedef unsigned long OS_thread_id_t;
0085 struct OS_thread_t
0086 {
0087 OS_thread_t()
0088 : m_handle()
0089 {}
0090
0091
0092 void* handle() const
0093 { return m_handle; }
0094
0095 void* m_handle;
0096 };
0097
0098 typedef OS_thread_id_t OS_systemwide_thread_id_t;
0099
0100
0101 inline OS_process_id_t get_current_process_id()
0102 { return winapi::get_current_process_id(); }
0103
0104 inline OS_process_id_t get_invalid_process_id()
0105 { return OS_process_id_t(0); }
0106
0107
0108 inline OS_thread_id_t get_current_thread_id()
0109 { return winapi::get_current_thread_id(); }
0110
0111 inline OS_thread_id_t get_invalid_thread_id()
0112 { return OS_thread_id_t(0xffffffff); }
0113
0114 inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
0115 { return id1 == id2; }
0116
0117
0118 inline unsigned long get_system_tick_ns()
0119 {
0120 unsigned long curres, ignore1, ignore2;
0121 winapi::query_timer_resolution(&ignore1, &ignore2, &curres);
0122
0123 return (curres - 1ul)*100ul;
0124 }
0125
0126
0127 inline unsigned long get_system_tick_us()
0128 {
0129 unsigned long curres, ignore1, ignore2;
0130 winapi::query_timer_resolution(&ignore1, &ignore2, &curres);
0131
0132 return (curres - 1ul)/10ul + 1ul;
0133 }
0134
0135 typedef unsigned __int64 OS_highres_count_t;
0136
0137 inline unsigned long get_system_tick_in_highres_counts()
0138 {
0139 __int64 freq;
0140 unsigned long curres, ignore1, ignore2;
0141 winapi::query_timer_resolution(&ignore1, &ignore2, &curres);
0142
0143 if(!winapi::query_performance_frequency(&freq)){
0144
0145 return (curres-1ul)/10000ul + 1ul;
0146 }
0147 else{
0148
0149 __int64 count_fs = (1000000000000000LL - 1LL)/freq + 1LL;
0150 __int64 tick_counts = (static_cast<__int64>(curres)*100000000LL - 1LL)/count_fs + 1LL;
0151 return static_cast<unsigned long>(tick_counts);
0152 }
0153 }
0154
0155 inline OS_highres_count_t get_current_system_highres_count()
0156 {
0157 __int64 count;
0158 if(!winapi::query_performance_counter(&count)){
0159 count = winapi::get_tick_count();
0160 }
0161 return (OS_highres_count_t)count;
0162 }
0163
0164 inline unsigned get_current_system_highres_rand()
0165 {
0166 unsigned __int64 count = (unsigned __int64) get_current_system_highres_count();
0167 return static_cast<unsigned>(count + (count >> 32u));
0168 }
0169
0170 inline void zero_highres_count(OS_highres_count_t &count)
0171 { count = 0; }
0172
0173 inline bool is_highres_count_zero(const OS_highres_count_t &count)
0174 { return count == 0; }
0175
0176 template <class Ostream>
0177 inline Ostream &ostream_highres_count(Ostream &ostream, const OS_highres_count_t &count)
0178 {
0179 ostream << count;
0180 return ostream;
0181 }
0182
0183 inline OS_highres_count_t system_highres_count_subtract(const OS_highres_count_t &l, const OS_highres_count_t &r)
0184 { return l - r; }
0185
0186 inline bool system_highres_count_less(const OS_highres_count_t &l, const OS_highres_count_t &r)
0187 { return l < r; }
0188
0189 inline bool system_highres_count_less_ul(const OS_highres_count_t &l, unsigned long r)
0190 { return l < static_cast<OS_highres_count_t>(r); }
0191
0192 inline void thread_sleep_tick()
0193 { winapi::sleep_tick(); }
0194
0195 inline void thread_yield()
0196 { winapi::sched_yield(); }
0197
0198 inline void thread_sleep_ms(unsigned int ms)
0199 { winapi::sleep(ms); }
0200
0201
0202 inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
0203 {
0204 return get_current_thread_id();
0205 }
0206
0207 inline void systemwide_thread_id_copy
0208 (const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
0209 {
0210 to = from;
0211 }
0212
0213 inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
0214 {
0215 return equal_thread_id(id1, id2);
0216 }
0217
0218 inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
0219 {
0220 return get_invalid_thread_id();
0221 }
0222
0223 inline long double get_current_process_creation_time()
0224 {
0225 winapi::interprocess_filetime CreationTime, ExitTime, KernelTime, UserTime;
0226
0227 winapi::get_process_times
0228 ( winapi::get_current_process(), &CreationTime, &ExitTime, &KernelTime, &UserTime);
0229
0230 typedef long double ldouble_t;
0231 const ldouble_t resolution = (100.0l/1000000000.0l);
0232 return CreationTime.dwHighDateTime*(ldouble_t(1u<<31u)*2.0l*resolution) +
0233 CreationTime.dwLowDateTime*resolution;
0234 }
0235
0236 inline unsigned int get_num_cores()
0237 {
0238 winapi::interprocess_system_info sysinfo;
0239 winapi::get_system_info( &sysinfo );
0240
0241 return static_cast<unsigned>(sysinfo.dwNumberOfProcessors);
0242 }
0243
0244 #else
0245
0246 typedef pthread_t OS_thread_t;
0247 typedef pthread_t OS_thread_id_t;
0248 typedef pid_t OS_process_id_t;
0249
0250 struct OS_systemwide_thread_id_t
0251 {
0252 OS_systemwide_thread_id_t()
0253 : pid(), tid()
0254 {}
0255
0256 OS_systemwide_thread_id_t(pid_t p, pthread_t t)
0257 : pid(p), tid(t)
0258 {}
0259
0260 OS_systemwide_thread_id_t(const OS_systemwide_thread_id_t &x)
0261 : pid(x.pid), tid(x.tid)
0262 {}
0263
0264 OS_systemwide_thread_id_t(const volatile OS_systemwide_thread_id_t &x)
0265 : pid(x.pid), tid(x.tid)
0266 {}
0267
0268 OS_systemwide_thread_id_t & operator=(const OS_systemwide_thread_id_t &x)
0269 { pid = x.pid; tid = x.tid; return *this; }
0270
0271 OS_systemwide_thread_id_t & operator=(const volatile OS_systemwide_thread_id_t &x)
0272 { pid = x.pid; tid = x.tid; return *this; }
0273
0274 void operator=(const OS_systemwide_thread_id_t &x) volatile
0275 { pid = x.pid; tid = x.tid; }
0276
0277 pid_t pid;
0278 pthread_t tid;
0279 };
0280
0281 inline void systemwide_thread_id_copy
0282 (const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
0283 {
0284 to.pid = from.pid;
0285 to.tid = from.tid;
0286 }
0287
0288
0289 inline OS_process_id_t get_current_process_id()
0290 { return ::getpid(); }
0291
0292 inline OS_process_id_t get_invalid_process_id()
0293 { return pid_t(0); }
0294
0295
0296 inline OS_thread_id_t get_current_thread_id()
0297 { return ::pthread_self(); }
0298
0299 inline OS_thread_id_t get_invalid_thread_id()
0300 {
0301 static pthread_t invalid_id;
0302 return invalid_id;
0303 }
0304
0305 inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
0306 { return 0 != pthread_equal(id1, id2); }
0307
0308 inline void thread_yield()
0309 { ::sched_yield(); }
0310
0311 #ifndef BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME
0312 typedef struct timespec OS_highres_count_t;
0313 #else
0314 typedef unsigned long long OS_highres_count_t;
0315 #endif
0316
0317 inline unsigned long get_system_tick_ns()
0318 {
0319 #ifdef _SC_CLK_TCK
0320 long ticks_per_second =::sysconf(_SC_CLK_TCK);
0321 if(ticks_per_second <= 0){
0322 ticks_per_second = 100;
0323 }
0324 return 999999999ul/static_cast<unsigned long>(ticks_per_second)+1ul;
0325 #else
0326 #error "Can't obtain system tick value for your system, please provide a patch"
0327 #endif
0328 }
0329
0330 inline unsigned long get_system_tick_in_highres_counts()
0331 {
0332 #ifndef BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME
0333 return get_system_tick_ns();
0334 #else
0335 mach_timebase_info_data_t info;
0336 mach_timebase_info(&info);
0337
0338 return static_cast<unsigned long>
0339 (
0340 static_cast<double>(get_system_tick_ns())
0341 / (static_cast<double>(info.numer) / info.denom)
0342 );
0343 #endif
0344 }
0345
0346
0347 inline unsigned long get_system_tick_us()
0348 {
0349 return (get_system_tick_ns()-1)/1000ul + 1ul;
0350 }
0351
0352 inline OS_highres_count_t get_current_system_highres_count()
0353 {
0354 #if defined(BOOST_INTERPROCESS_CLOCK_MONOTONIC)
0355 struct timespec count;
0356 ::clock_gettime(BOOST_INTERPROCESS_CLOCK_MONOTONIC, &count);
0357 return count;
0358 #elif defined(BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME)
0359 return ::mach_absolute_time();
0360 #endif
0361 }
0362
0363 inline unsigned get_current_system_highres_rand()
0364 {
0365 OS_highres_count_t count = get_current_system_highres_count();
0366 #if defined(BOOST_INTERPROCESS_CLOCK_MONOTONIC)
0367 return static_cast<unsigned>(count.tv_sec + count.tv_nsec);
0368 #elif defined(BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME)
0369 return static_cast<unsigned>(count);
0370 #endif
0371 }
0372
0373 #ifndef BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME
0374
0375 inline void zero_highres_count(OS_highres_count_t &count)
0376 { count.tv_sec = 0; count.tv_nsec = 0; }
0377
0378 inline bool is_highres_count_zero(const OS_highres_count_t &count)
0379 { return count.tv_sec == 0 && count.tv_nsec == 0; }
0380
0381 template <class Ostream>
0382 inline Ostream &ostream_highres_count(Ostream &ostream, const OS_highres_count_t &count)
0383 {
0384 ostream << count.tv_sec << "s:" << count.tv_nsec << "ns";
0385 return ostream;
0386 }
0387
0388 inline OS_highres_count_t system_highres_count_subtract(const OS_highres_count_t &l, const OS_highres_count_t &r)
0389 {
0390 OS_highres_count_t res;
0391
0392 if (l.tv_nsec < r.tv_nsec){
0393 res.tv_nsec = 1000000000 + l.tv_nsec - r.tv_nsec;
0394 res.tv_sec = l.tv_sec - 1 - r.tv_sec;
0395 }
0396 else{
0397 res.tv_nsec = l.tv_nsec - r.tv_nsec;
0398 res.tv_sec = l.tv_sec - r.tv_sec;
0399 }
0400
0401 return res;
0402 }
0403
0404 inline bool system_highres_count_less(const OS_highres_count_t &l, const OS_highres_count_t &r)
0405 { return l.tv_sec < r.tv_sec || (l.tv_sec == r.tv_sec && l.tv_nsec < r.tv_nsec); }
0406
0407 inline bool system_highres_count_less_ul(const OS_highres_count_t &l, unsigned long r)
0408 { return !l.tv_sec && (static_cast<unsigned long>(l.tv_nsec) < r); }
0409
0410 #else
0411
0412 inline void zero_highres_count(OS_highres_count_t &count)
0413 { count = 0; }
0414
0415 inline bool is_highres_count_zero(const OS_highres_count_t &count)
0416 { return count == 0; }
0417
0418 template <class Ostream>
0419 inline Ostream &ostream_highres_count(Ostream &ostream, const OS_highres_count_t &count)
0420 {
0421 ostream << count ;
0422 return ostream;
0423 }
0424
0425 inline OS_highres_count_t system_highres_count_subtract(const OS_highres_count_t &l, const OS_highres_count_t &r)
0426 { return l - r; }
0427
0428 inline bool system_highres_count_less(const OS_highres_count_t &l, const OS_highres_count_t &r)
0429 { return l < r; }
0430
0431 inline bool system_highres_count_less_ul(const OS_highres_count_t &l, unsigned long r)
0432 { return l < static_cast<OS_highres_count_t>(r); }
0433
0434 #endif
0435
0436 inline void thread_sleep_tick()
0437 {
0438 struct timespec rqt;
0439
0440 rqt.tv_sec = 0;
0441 rqt.tv_nsec = (long)get_system_tick_ns()/2;
0442
0443 struct timespec rmn;
0444 while (0 != ::nanosleep(&rqt, &rmn) && errno == EINTR) {
0445 rqt.tv_sec = rmn.tv_sec;
0446 rqt.tv_nsec = rmn.tv_nsec;
0447 }
0448 }
0449
0450 inline void thread_sleep_ms(unsigned int ms)
0451 {
0452 struct timespec rqt;
0453 rqt.tv_sec = ms/1000u;
0454 rqt.tv_nsec = (ms%1000u)*1000000u;
0455
0456 struct timespec rmn;
0457 while (0 != ::nanosleep(&rqt, &rmn) && errno == EINTR) {
0458 rqt.tv_sec = rmn.tv_sec;
0459 rqt.tv_nsec = rmn.tv_nsec;
0460 }
0461 }
0462
0463
0464 inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
0465 {
0466 return OS_systemwide_thread_id_t(::getpid(), ::pthread_self());
0467 }
0468
0469 inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
0470 {
0471 return (0 != pthread_equal(id1.tid, id2.tid)) && (id1.pid == id2.pid);
0472 }
0473
0474 inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
0475 {
0476 return OS_systemwide_thread_id_t(get_invalid_process_id(), get_invalid_thread_id());
0477 }
0478
0479 inline long double get_current_process_creation_time()
0480 { return 0.0L; }
0481
0482 inline unsigned int get_num_cores()
0483 {
0484 #ifdef _SC_NPROCESSORS_ONLN
0485 long cores = ::sysconf(_SC_NPROCESSORS_ONLN);
0486
0487
0488
0489
0490 if(cores <= 0){
0491 return 1;
0492 }
0493
0494 else if(static_cast<unsigned long>(cores) >=
0495 static_cast<unsigned long>(static_cast<unsigned int>(-1))){
0496 return static_cast<unsigned int>(-1);
0497 }
0498 else{
0499 return static_cast<unsigned int>(cores);
0500 }
0501 #elif defined(BOOST_INTERPROCESS_BSD_DERIVATIVE) && defined(HW_NCPU)
0502 int request[2] = { CTL_HW, HW_NCPU };
0503 int num_cores;
0504 std::size_t result_len = sizeof(num_cores);
0505 if ( (::sysctl (request, 2, &num_cores, &result_len, 0, 0) < 0) || (num_cores <= 0) ){
0506
0507 return 1;
0508 }
0509 else{
0510 return static_cast<unsigned int>(num_cores);
0511 }
0512 #elif defined(__VXWORKS__)
0513 cpuset_t set = ::vxCpuEnabledGet();
0514 #ifdef __DCC__
0515 int i;
0516 for( i = 0; set; ++i)
0517 {
0518 set &= set -1;
0519 }
0520 return(i);
0521 #else
0522 return (__builtin_popcount(set) );
0523 #endif
0524 #endif
0525 }
0526
0527 inline int thread_create(OS_thread_t * thread, void *(*start_routine)(void*), void* arg)
0528 { return pthread_create(thread, 0, start_routine, arg); }
0529
0530 inline void thread_join(OS_thread_t thread)
0531 {
0532 int ret = pthread_join(thread, 0);
0533 (void)ret;
0534 assert(0 == ret);
0535 }
0536
0537 #endif
0538
0539 typedef char pid_str_t[sizeof(OS_process_id_t)*3+1];
0540
0541 inline void get_pid_str(pid_str_t &pid_str, OS_process_id_t pid)
0542 {
0543 bufferstream bstream(pid_str, sizeof(pid_str));
0544 bstream << pid << std::ends;
0545 }
0546
0547 inline void get_pid_str(pid_str_t &pid_str)
0548 { get_pid_str(pid_str, get_current_process_id()); }
0549
0550 #if defined(BOOST_INTERPROCESS_WINDOWS)
0551
0552 inline int thread_create( OS_thread_t * thread, boost::ipwinapiext::LPTHREAD_START_ROUTINE_ start_routine, void* arg )
0553 {
0554 void* h = boost::ipwinapiext::CreateThread(0, 0, start_routine, arg, 0, 0);
0555
0556 if( h != 0 ){
0557 thread->m_handle = h;
0558 return 0;
0559 }
0560 else{
0561 return 1;
0562 }
0563 }
0564
0565 inline void thread_join( OS_thread_t thread)
0566 {
0567 {
0568 unsigned long ret = winapi::wait_for_single_object( thread.handle(), winapi::infinite_time );
0569 assert(0 == ret);
0570 (void)ret;
0571 }
0572 {
0573 bool ret = winapi::close_handle(thread.handle());
0574 assert(true == ret);
0575 (void)ret;
0576 }
0577 }
0578
0579 #endif
0580
0581 class abstract_thread
0582 {
0583 public:
0584 virtual ~abstract_thread() {}
0585 virtual void run() = 0;
0586 };
0587
0588 template<class T>
0589 class os_thread_func_ptr_deleter
0590 {
0591 public:
0592 explicit os_thread_func_ptr_deleter(T* p)
0593 : m_p(p)
0594 {}
0595
0596 T *release()
0597 { T *p = m_p; m_p = 0; return p; }
0598
0599 T *get() const
0600 { return m_p; }
0601
0602 T *operator ->() const
0603 { return m_p; }
0604
0605 ~os_thread_func_ptr_deleter()
0606 { delete m_p; }
0607
0608 private:
0609 T *m_p;
0610 };
0611
0612 #if defined(BOOST_INTERPROCESS_WINDOWS)
0613
0614 inline boost::winapi::DWORD_ __stdcall launch_thread_routine(boost::winapi::LPVOID_ pv)
0615 {
0616 os_thread_func_ptr_deleter<abstract_thread> pt( static_cast<abstract_thread *>( pv ) );
0617 pt->run();
0618 return 0;
0619 }
0620
0621 #else
0622
0623 extern "C" void * launch_thread_routine( void * pv );
0624
0625 inline void * launch_thread_routine( void * pv )
0626 {
0627 os_thread_func_ptr_deleter<abstract_thread> pt( static_cast<abstract_thread *>( pv ) );
0628 pt->run();
0629 return 0;
0630 }
0631
0632 #endif
0633
0634 template<class F>
0635 class launch_thread_impl
0636 : public abstract_thread
0637 {
0638 public:
0639 explicit launch_thread_impl( F f )
0640 : f_( f )
0641 {}
0642
0643 virtual void run() BOOST_OVERRIDE
0644 { f_(); }
0645
0646 private:
0647 F f_;
0648 };
0649
0650 template<class F>
0651 inline int thread_launch( OS_thread_t & pt, F f )
0652 {
0653 os_thread_func_ptr_deleter<abstract_thread> p( new launch_thread_impl<F>( f ) );
0654
0655 int r = thread_create(&pt, launch_thread_routine, p.get());
0656 if( r == 0 ){
0657 p.release();
0658 }
0659
0660 return r;
0661 }
0662
0663 }
0664 }
0665 }
0666
0667 #include <boost/interprocess/detail/config_end.hpp>
0668
0669 #endif