PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/interprocess/sync/emulation/interprocess_semaphore.hpp

http://hadesmem.googlecode.com/
C++ Header | 75 lines | 47 code | 12 blank | 16 comment | 6 complexity | dfe966da121e7b16599d58ef9f0693cf 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#include<boost/interprocess/exceptions.hpp>
12#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
13
14namespace boost {
15namespace interprocess {
16
17
18inline interprocess_semaphore::~interprocess_semaphore()
19{}
20
21inline interprocess_semaphore::interprocess_semaphore(unsigned int initialCount)
22{  detail::atomic_write32(&this->m_count, boost::uint32_t(initialCount));  }
23
24inline void interprocess_semaphore::post()
25{
26   detail::atomic_inc32(&m_count);
27}
28
29inline void interprocess_semaphore::wait()
30{
31   while(!detail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0))){
32      while(detail::atomic_read32(&m_count) == 0){
33         detail::thread_yield();
34      }
35   }
36}
37
38inline bool interprocess_semaphore::try_wait()
39{
40   return detail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0));
41}
42
43inline bool interprocess_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
44{
45   if(abs_time == boost::posix_time::pos_infin){
46      this->wait();
47      return true;
48   }
49   //Obtain current count and target time
50   boost::posix_time::ptime now(microsec_clock::universal_time());
51   if(now >= abs_time)
52      return false;
53
54   do{
55      if(this->try_wait()){
56         break;
57      }
58      now = microsec_clock::universal_time();
59
60      if(now >= abs_time){
61         return this->try_wait();
62      }
63      // relinquish current time slice
64      detail::thread_yield();
65   }while (true);
66   return true;
67}
68/*
69inline int interprocess_semaphore::get_count() const
70{
71   return (int)detail::atomic_read32(&m_count);
72}*/
73
74}  //namespace interprocess {
75}  //namespace boost {