1
//////////////////////////////////////////////////////////////////////////////
3
// (C) Copyright Ion Gaztanaga 2005-2011. 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)
7
// See http://www.boost.org/libs/interprocess for documentation.
9
//////////////////////////////////////////////////////////////////////////////
11
#ifndef BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP
12
#define BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP
14
#include <boost/interprocess/detail/config_begin.hpp>
15
#include <boost/interprocess/detail/workaround.hpp>
16
#include <boost/interprocess/streams/bufferstream.hpp>
17
#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
19
#if (defined BOOST_INTERPROCESS_WINDOWS)
20
# include <boost/interprocess/detail/win32_api.hpp>
22
# ifdef BOOST_HAS_UNISTD_H
28
# error Unknown platform
33
namespace interprocess {
36
#if (defined BOOST_INTERPROCESS_WINDOWS)
38
typedef unsigned long OS_process_id_t;
39
typedef unsigned long OS_thread_id_t;
40
typedef OS_thread_id_t OS_systemwide_thread_id_t;
43
inline OS_process_id_t get_current_process_id()
44
{ return winapi::get_current_process_id(); }
46
inline OS_process_id_t get_invalid_process_id()
47
{ return OS_process_id_t(0); }
50
inline OS_thread_id_t get_current_thread_id()
51
{ return winapi::get_current_thread_id(); }
53
inline OS_thread_id_t get_invalid_thread_id()
54
{ return OS_thread_id_t(0xffffffff); }
56
inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
57
{ return id1 == id2; }
59
inline void thread_yield()
60
{ winapi::sched_yield(); }
62
inline void thread_sleep(unsigned int ms)
63
{ winapi::Sleep(ms); }
66
inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
68
return get_current_thread_id();
71
inline void systemwide_thread_id_copy
72
(const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
77
inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
79
return equal_thread_id(id1, id2);
82
inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
84
return get_invalid_thread_id();
87
inline long double get_current_process_creation_time()
89
winapi::interprocess_filetime CreationTime, ExitTime, KernelTime, UserTime;
92
( winapi::get_current_process(), &CreationTime, &ExitTime, &KernelTime, &UserTime);
94
typedef long double ldouble_t;
95
const ldouble_t resolution = (100.0l/1000000000.0l);
96
return CreationTime.dwHighDateTime*(ldouble_t(1u<<31u)*2.0l*resolution) +
97
CreationTime.dwLowDateTime*resolution;
101
#else //#if (defined BOOST_INTERPROCESS_WINDOWS)
103
typedef pthread_t OS_thread_id_t;
104
typedef pid_t OS_process_id_t;
106
struct OS_systemwide_thread_id_t
108
OS_systemwide_thread_id_t()
112
OS_systemwide_thread_id_t(pid_t p, pthread_t t)
116
OS_systemwide_thread_id_t(const OS_systemwide_thread_id_t &x)
117
: pid(x.pid), tid(x.tid)
120
OS_systemwide_thread_id_t(const volatile OS_systemwide_thread_id_t &x)
121
: pid(x.pid), tid(x.tid)
124
OS_systemwide_thread_id_t & operator=(const OS_systemwide_thread_id_t &x)
125
{ pid = x.pid; tid = x.tid; return *this; }
127
OS_systemwide_thread_id_t & operator=(const volatile OS_systemwide_thread_id_t &x)
128
{ pid = x.pid; tid = x.tid; return *this; }
130
void operator=(const OS_systemwide_thread_id_t &x) volatile
131
{ pid = x.pid; tid = x.tid; }
137
inline void systemwide_thread_id_copy
138
(const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
145
inline OS_process_id_t get_current_process_id()
146
{ return ::getpid(); }
148
inline OS_process_id_t get_invalid_process_id()
152
inline OS_thread_id_t get_current_thread_id()
153
{ return ::pthread_self(); }
155
inline OS_thread_id_t get_invalid_thread_id()
157
static pthread_t invalid_id;
161
inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
162
{ return 0 != pthread_equal(id1, id2); }
164
inline void thread_yield()
167
inline void thread_sleep(unsigned int ms)
169
const struct timespec rqt = { ms/1000u, (ms%1000u)*1000000u };
170
::nanosleep(&rqt, 0);
174
inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
176
return OS_systemwide_thread_id_t(::getpid(), ::pthread_self());
179
inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
181
return (0 != pthread_equal(id1.tid, id2.tid)) && (id1.pid == id2.pid);
184
inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
186
return OS_systemwide_thread_id_t(get_invalid_process_id(), get_invalid_thread_id());
189
inline long double get_current_process_creation_time()
192
#endif //#if (defined BOOST_INTERPROCESS_WINDOWS)
194
typedef char pid_str_t[sizeof(OS_process_id_t)*3+1];
196
inline void get_pid_str(pid_str_t &pid_str, OS_process_id_t pid)
198
bufferstream bstream(pid_str, sizeof(pid_str));
199
bstream << pid << std::ends;
202
inline void get_pid_str(pid_str_t &pid_str)
203
{ get_pid_str(pid_str, get_current_process_id()); }
205
} //namespace ipcdetail{
206
} //namespace interprocess {
207
} //namespace boost {
209
#include <boost/interprocess/detail/config_end.hpp>
211
#endif //BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP