/Src/Dependencies/Boost/boost/interprocess/detail/os_thread_functions.hpp

http://hadesmem.googlecode.com/ · C++ Header · 201 lines · 138 code · 48 blank · 15 comment · 5 complexity · d3d4a4bc58b36539182d2cdf7948dbb9 MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2009. 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. #ifndef BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP
  12. #include <boost/interprocess/detail/config_begin.hpp>
  13. #include <boost/interprocess/detail/workaround.hpp>
  14. #include <boost/interprocess/streams/bufferstream.hpp>
  15. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  16. #if (defined BOOST_INTERPROCESS_WINDOWS)
  17. # include <boost/interprocess/detail/win32_api.hpp>
  18. #else
  19. # ifdef BOOST_HAS_UNISTD_H
  20. # include <pthread.h>
  21. # include <unistd.h>
  22. # include <sched.h>
  23. # else
  24. # error Unknown platform
  25. # endif
  26. #endif
  27. namespace boost {
  28. namespace interprocess {
  29. namespace detail{
  30. #if (defined BOOST_INTERPROCESS_WINDOWS)
  31. typedef unsigned long OS_process_id_t;
  32. typedef unsigned long OS_thread_id_t;
  33. typedef OS_thread_id_t OS_systemwide_thread_id_t;
  34. //process
  35. inline OS_process_id_t get_current_process_id()
  36. { return winapi::get_current_process_id(); }
  37. inline OS_process_id_t get_invalid_process_id()
  38. { return OS_process_id_t(0); }
  39. //thread
  40. inline OS_thread_id_t get_current_thread_id()
  41. { return winapi::get_current_thread_id(); }
  42. inline OS_thread_id_t get_invalid_thread_id()
  43. { return OS_thread_id_t(0xffffffff); }
  44. inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
  45. { return id1 == id2; }
  46. inline void thread_yield()
  47. { winapi::sched_yield(); }
  48. //systemwide thread
  49. inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
  50. {
  51. return get_current_thread_id();
  52. }
  53. inline void systemwide_thread_id_copy
  54. (const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
  55. {
  56. to = from;
  57. }
  58. inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
  59. {
  60. return equal_thread_id(id1, id2);
  61. }
  62. inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
  63. {
  64. return get_invalid_thread_id();
  65. }
  66. inline long double get_current_process_creation_time()
  67. {
  68. winapi::interprocess_filetime CreationTime, ExitTime, KernelTime, UserTime;
  69. get_process_times
  70. ( winapi::get_current_process(), &CreationTime, &ExitTime, &KernelTime, &UserTime);
  71. typedef long double ldouble_t;
  72. const ldouble_t resolution = (100.0l/1000000000.0l);
  73. return CreationTime.dwHighDateTime*(ldouble_t(1u<<31u)*2.0l*resolution) +
  74. CreationTime.dwLowDateTime*resolution;
  75. }
  76. #else //#if (defined BOOST_INTERPROCESS_WINDOWS)
  77. typedef pthread_t OS_thread_id_t;
  78. typedef pid_t OS_process_id_t;
  79. struct OS_systemwide_thread_id_t
  80. {
  81. OS_systemwide_thread_id_t()
  82. : pid(), tid()
  83. {}
  84. OS_systemwide_thread_id_t(pid_t p, pthread_t t)
  85. : pid(p), tid(t)
  86. {}
  87. OS_systemwide_thread_id_t(const OS_systemwide_thread_id_t &x)
  88. : pid(x.pid), tid(x.tid)
  89. {}
  90. OS_systemwide_thread_id_t(const volatile OS_systemwide_thread_id_t &x)
  91. : pid(x.pid), tid(x.tid)
  92. {}
  93. OS_systemwide_thread_id_t & operator=(const OS_systemwide_thread_id_t &x)
  94. { pid = x.pid; tid = x.tid; return *this; }
  95. OS_systemwide_thread_id_t & operator=(const volatile OS_systemwide_thread_id_t &x)
  96. { pid = x.pid; tid = x.tid; return *this; }
  97. void operator=(const OS_systemwide_thread_id_t &x) volatile
  98. { pid = x.pid; tid = x.tid; }
  99. pid_t pid;
  100. pthread_t tid;
  101. };
  102. inline void systemwide_thread_id_copy
  103. (const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
  104. {
  105. to.pid = from.pid;
  106. to.tid = from.tid;
  107. }
  108. //process
  109. inline OS_process_id_t get_current_process_id()
  110. { return ::getpid(); }
  111. inline OS_process_id_t get_invalid_process_id()
  112. { return pid_t(0); }
  113. //thread
  114. inline OS_thread_id_t get_current_thread_id()
  115. { return ::pthread_self(); }
  116. inline OS_thread_id_t get_invalid_thread_id()
  117. {
  118. static pthread_t invalid_id;
  119. return invalid_id;
  120. }
  121. inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
  122. { return 0 != pthread_equal(id1, id2); }
  123. inline void thread_yield()
  124. { ::sched_yield(); }
  125. //systemwide thread
  126. inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
  127. {
  128. return OS_systemwide_thread_id_t(::getpid(), ::pthread_self());
  129. }
  130. inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
  131. {
  132. return (0 != pthread_equal(id1.tid, id2.tid)) && (id1.pid == id2.pid);
  133. }
  134. inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
  135. {
  136. return OS_systemwide_thread_id_t(get_invalid_process_id(), get_invalid_thread_id());
  137. }
  138. inline long double get_current_process_creation_time()
  139. { return 0.0L; }
  140. #endif //#if (defined BOOST_INTERPROCESS_WINDOWS)
  141. typedef char pid_str_t[sizeof(OS_process_id_t)*3+1];
  142. inline void get_pid_str(pid_str_t &pid_str, OS_process_id_t pid)
  143. {
  144. bufferstream bstream(pid_str, sizeof(pid_str));
  145. bstream << pid << std::ends;
  146. }
  147. inline void get_pid_str(pid_str_t &pid_str)
  148. { get_pid_str(pid_str, get_current_process_id()); }
  149. } //namespace detail{
  150. } //namespace interprocess {
  151. } //namespace boost {
  152. #include <boost/interprocess/detail/config_end.hpp>
  153. #endif //BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP