PageRenderTime 54ms CodeModel.GetById 15ms app.highlight 30ms RepoModel.GetById 5ms app.codeStats 1ms

/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
 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#  else
 27#     error Unknown platform
 28#  endif
 29#endif
 30
 31namespace boost {
 32namespace interprocess {
 33namespace detail{
 34
 35#if (defined BOOST_INTERPROCESS_WINDOWS)
 36
 37typedef unsigned long OS_process_id_t;
 38typedef unsigned long OS_thread_id_t;
 39typedef OS_thread_id_t OS_systemwide_thread_id_t;
 40
 41//process
 42inline OS_process_id_t get_current_process_id()
 43{  return winapi::get_current_process_id();  }
 44
 45inline OS_process_id_t get_invalid_process_id()
 46{  return OS_process_id_t(0);  }
 47
 48//thread
 49inline OS_thread_id_t get_current_thread_id()
 50{  return winapi::get_current_thread_id();  }
 51
 52inline OS_thread_id_t get_invalid_thread_id()
 53{  return OS_thread_id_t(0xffffffff);  }
 54
 55inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
 56{  return id1 == id2;  }
 57
 58inline void thread_yield()
 59{  winapi::sched_yield();  }
 60
 61//systemwide thread
 62inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
 63{
 64   return get_current_thread_id();
 65}
 66
 67inline void systemwide_thread_id_copy
 68   (const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
 69{
 70   to = from;
 71}
 72
 73inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
 74{
 75   return equal_thread_id(id1, id2);
 76}
 77
 78inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
 79{
 80   return get_invalid_thread_id();
 81}
 82
 83inline long double get_current_process_creation_time()
 84{
 85   winapi::interprocess_filetime CreationTime, ExitTime, KernelTime, UserTime;
 86
 87   get_process_times
 88      ( winapi::get_current_process(), &CreationTime, &ExitTime, &KernelTime, &UserTime);
 89
 90   typedef long double ldouble_t;
 91   const ldouble_t resolution = (100.0l/1000000000.0l);
 92   return CreationTime.dwHighDateTime*(ldouble_t(1u<<31u)*2.0l*resolution) +
 93              CreationTime.dwLowDateTime*resolution;
 94}
 95
 96
 97#else    //#if (defined BOOST_INTERPROCESS_WINDOWS)
 98
 99typedef pthread_t OS_thread_id_t;
100typedef pid_t     OS_process_id_t;
101
102struct OS_systemwide_thread_id_t
103{  
104   OS_systemwide_thread_id_t()
105      :  pid(), tid()
106   {}
107
108   OS_systemwide_thread_id_t(pid_t p, pthread_t t)
109      :  pid(p), tid(t)
110   {}
111
112   OS_systemwide_thread_id_t(const OS_systemwide_thread_id_t &x)
113      :  pid(x.pid), tid(x.tid)
114   {}
115
116   OS_systemwide_thread_id_t(const volatile OS_systemwide_thread_id_t &x)
117      :  pid(x.pid), tid(x.tid)
118   {}
119
120   OS_systemwide_thread_id_t & operator=(const OS_systemwide_thread_id_t &x)
121   {  pid = x.pid;   tid = x.tid;   return *this;   }
122
123   OS_systemwide_thread_id_t & operator=(const volatile OS_systemwide_thread_id_t &x)
124   {  pid = x.pid;   tid = x.tid;   return *this;  }
125
126   void operator=(const OS_systemwide_thread_id_t &x) volatile
127   {  pid = x.pid;   tid = x.tid;   }
128
129   pid_t       pid;
130   pthread_t   tid;
131};
132
133inline void systemwide_thread_id_copy
134   (const volatile OS_systemwide_thread_id_t &from, volatile OS_systemwide_thread_id_t &to)
135{
136   to.pid = from.pid;
137   to.tid = from.tid;
138}
139
140//process
141inline OS_process_id_t get_current_process_id()
142{  return ::getpid();  }
143
144inline OS_process_id_t get_invalid_process_id()
145{  return pid_t(0);  }
146
147//thread
148inline OS_thread_id_t get_current_thread_id()
149{  return ::pthread_self();  }
150
151inline OS_thread_id_t get_invalid_thread_id()
152{ 
153   static pthread_t invalid_id;
154   return invalid_id;
155}
156
157inline bool equal_thread_id(OS_thread_id_t id1, OS_thread_id_t id2)
158{  return 0 != pthread_equal(id1, id2);  }
159
160inline void thread_yield()
161{  ::sched_yield();  }
162
163//systemwide thread
164inline OS_systemwide_thread_id_t get_current_systemwide_thread_id()
165{
166   return OS_systemwide_thread_id_t(::getpid(), ::pthread_self());
167}
168
169inline bool equal_systemwide_thread_id(const OS_systemwide_thread_id_t &id1, const OS_systemwide_thread_id_t &id2)
170{
171   return (0 != pthread_equal(id1.tid, id2.tid)) && (id1.pid == id2.pid);
172}
173
174inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
175{
176   return OS_systemwide_thread_id_t(get_invalid_process_id(), get_invalid_thread_id());
177}
178
179inline long double get_current_process_creation_time()
180{ return 0.0L; }
181
182#endif   //#if (defined BOOST_INTERPROCESS_WINDOWS)
183
184typedef char pid_str_t[sizeof(OS_process_id_t)*3+1];
185
186inline void get_pid_str(pid_str_t &pid_str, OS_process_id_t pid)
187{
188   bufferstream bstream(pid_str, sizeof(pid_str));
189   bstream << pid << std::ends;
190}
191
192inline void get_pid_str(pid_str_t &pid_str)
193{  get_pid_str(pid_str, get_current_process_id());  }
194
195}  //namespace detail{
196}  //namespace interprocess {
197}  //namespace boost {
198
199#include <boost/interprocess/detail/config_end.hpp>
200
201#endif   //BOOST_INTERPROCESS_DETAIL_OS_THREAD_FUNCTIONS_HPP