~ubuntu-branches/debian/sid/boost1.49/sid

« back to all changes in this revision

Viewing changes to boost/interprocess/detail/os_thread_functions.hpp

  • Committer: Package Import Robot
  • Author(s): Steve M. Robbins
  • Date: 2012-02-26 00:31:44 UTC
  • Revision ID: package-import@ubuntu.com-20120226003144-eaytp12cbf6ubpms
Tags: upstream-1.49.0
ImportĀ upstreamĀ versionĀ 1.49.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////////
 
2
//
 
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)
 
6
//
 
7
// See http://www.boost.org/libs/interprocess for documentation.
 
8
//
 
9
//////////////////////////////////////////////////////////////////////////////
 
10
 
 
11
#ifndef BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP
 
12
#define BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP
 
13
 
 
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>
 
18
 
 
19
#if (defined BOOST_INTERPROCESS_WINDOWS)
 
20
#  include <boost/interprocess/detail/win32_api.hpp>
 
21
#else
 
22
#  ifdef BOOST_HAS_UNISTD_H
 
23
#     include <pthread.h>
 
24
#     include <unistd.h>
 
25
#     include <sched.h>
 
26
#     include <time.h>
 
27
#  else
 
28
#     error Unknown platform
 
29
#  endif
 
30
#endif
 
31
 
 
32
namespace boost {
 
33
namespace interprocess {
 
34
namespace ipcdetail{
 
35
 
 
36
#if (defined BOOST_INTERPROCESS_WINDOWS)
 
37
 
 
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;
 
41
 
 
42
//process
 
43
inline OS_process_id_t get_current_process_id()
 
44
{  return winapi::get_current_process_id();  }
 
45
 
 
46
inline OS_process_id_t get_invalid_process_id()
 
47
{  return OS_process_id_t(0);  }
 
48
 
 
49
//thread
 
50
inline OS_thread_id_t get_current_thread_id()
 
51
{  return winapi::get_current_thread_id();  }
 
52
 
 
53
inline OS_thread_id_t get_invalid_thread_id()
 
54
{  return OS_thread_id_t(0xffffffff);  }
 
55
 
 
56
inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
 
57
{  return id1 == id2;  }
 
58
 
 
59
inline void thread_yield()
 
60
{  winapi::sched_yield();  }
 
61
 
 
62
inline void thread_sleep(unsigned int ms)
 
63
{  winapi::Sleep(ms);  }
 
64
 
 
65
//systemwide thread
 
66
inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
 
67
{
 
68
   return get_current_thread_id();
 
69
}
 
70
 
 
71
inline void systemwide_thread_id_copy
 
72
   (const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
 
73
{
 
74
   to = from;
 
75
}
 
76
 
 
77
inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
 
78
{
 
79
   return equal_thread_id(id1, id2);
 
80
}
 
81
 
 
82
inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
 
83
{
 
84
   return get_invalid_thread_id();
 
85
}
 
86
 
 
87
inline long double get_current_process_creation_time()
 
88
{
 
89
   winapi::interprocess_filetime CreationTime, ExitTime, KernelTime, UserTime;
 
90
 
 
91
   get_process_times
 
92
      ( winapi::get_current_process(), &CreationTime, &ExitTime, &KernelTime, &UserTime);
 
93
 
 
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;
 
98
}
 
99
 
 
100
 
 
101
#else    //#if (defined BOOST_INTERPROCESS_WINDOWS)
 
102
 
 
103
typedef pthread_t OS_thread_id_t;
 
104
typedef pid_t     OS_process_id_t;
 
105
 
 
106
struct OS_systemwide_thread_id_t
 
107
{  
 
108
   OS_systemwide_thread_id_t()
 
109
      :  pid(), tid()
 
110
   {}
 
111
 
 
112
   OS_systemwide_thread_id_t(pid_t p, pthread_t t)
 
113
      :  pid(p), tid(t)
 
114
   {}
 
115
 
 
116
   OS_systemwide_thread_id_t(const OS_systemwide_thread_id_t &x)
 
117
      :  pid(x.pid), tid(x.tid)
 
118
   {}
 
119
 
 
120
   OS_systemwide_thread_id_t(const volatile OS_systemwide_thread_id_t &x)
 
121
      :  pid(x.pid), tid(x.tid)
 
122
   {}
 
123
 
 
124
   OS_systemwide_thread_id_t & operator=(const OS_systemwide_thread_id_t &x)
 
125
   {  pid = x.pid;   tid = x.tid;   return *this;   }
 
126
 
 
127
   OS_systemwide_thread_id_t & operator=(const volatile OS_systemwide_thread_id_t &x)
 
128
   {  pid = x.pid;   tid = x.tid;   return *this;  }
 
129
 
 
130
   void operator=(const OS_systemwide_thread_id_t &x) volatile
 
131
   {  pid = x.pid;   tid = x.tid;   }
 
132
 
 
133
   pid_t       pid;
 
134
   pthread_t   tid;
 
135
};
 
136
 
 
137
inline void systemwide_thread_id_copy
 
138
   (const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
 
139
{
 
140
   to.pid = from.pid;
 
141
   to.tid = from.tid;
 
142
}
 
143
 
 
144
//process
 
145
inline OS_process_id_t get_current_process_id()
 
146
{  return ::getpid();  }
 
147
 
 
148
inline OS_process_id_t get_invalid_process_id()
 
149
{  return pid_t(0);  }
 
150
 
 
151
//thread
 
152
inline OS_thread_id_t get_current_thread_id()
 
153
{  return ::pthread_self();  }
 
154
 
 
155
inline OS_thread_id_t get_invalid_thread_id()
 
156
 
157
   static pthread_t invalid_id;
 
158
   return invalid_id;
 
159
}
 
160
 
 
161
inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
 
162
{  return 0 != pthread_equal(id1, id2);  }
 
163
 
 
164
inline void thread_yield()
 
165
{  ::sched_yield();  }
 
166
 
 
167
inline void thread_sleep(unsigned int ms)
 
168
{
 
169
   const struct timespec rqt = { ms/1000u, (ms%1000u)*1000000u  };
 
170
   ::nanosleep(&rqt, 0);
 
171
}
 
172
 
 
173
//systemwide thread
 
174
inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
 
175
{
 
176
   return OS_systemwide_thread_id_t(::getpid(), ::pthread_self());
 
177
}
 
178
 
 
179
inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
 
180
{
 
181
   return (0 != pthread_equal(id1.tid, id2.tid)) && (id1.pid == id2.pid);
 
182
}
 
183
 
 
184
inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
 
185
{
 
186
   return OS_systemwide_thread_id_t(get_invalid_process_id(), get_invalid_thread_id());
 
187
}
 
188
 
 
189
inline long double get_current_process_creation_time()
 
190
{ return 0.0L; }
 
191
 
 
192
#endif   //#if (defined BOOST_INTERPROCESS_WINDOWS)
 
193
 
 
194
typedef char pid_str_t[sizeof(OS_process_id_t)*3+1];
 
195
 
 
196
inline void get_pid_str(pid_str_t &pid_str, OS_process_id_t pid)
 
197
{
 
198
   bufferstream bstream(pid_str, sizeof(pid_str));
 
199
   bstream << pid << std::ends;
 
200
}
 
201
 
 
202
inline void get_pid_str(pid_str_t &pid_str)
 
203
{  get_pid_str(pid_str, get_current_process_id());  }
 
204
 
 
205
}  //namespace ipcdetail{
 
206
}  //namespace interprocess {
 
207
}  //namespace boost {
 
208
 
 
209
#include <boost/interprocess/detail/config_end.hpp>
 
210
 
 
211
#endif   //BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP