Statistics
| Revision:

root / tmp / org.txm.statsengine.r.core.win32 / res / win32 / library / BH / include / boost / asio / detail / scoped_lock.hpp @ 2486

History | View | Annotate | Download (1.9 kB)

1 2486 sjacqu01
//
2 2486 sjacqu01
// detail/scoped_lock.hpp
3 2486 sjacqu01
// ~~~~~~~~~~~~~~~~~~~~~~
4 2486 sjacqu01
//
5 2486 sjacqu01
// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 2486 sjacqu01
//
7 2486 sjacqu01
// Distributed under the Boost Software License, Version 1.0. (See accompanying
8 2486 sjacqu01
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 2486 sjacqu01
//
10 2486 sjacqu01
11 2486 sjacqu01
#ifndef BOOST_ASIO_DETAIL_SCOPED_LOCK_HPP
12 2486 sjacqu01
#define BOOST_ASIO_DETAIL_SCOPED_LOCK_HPP
13 2486 sjacqu01
14 2486 sjacqu01
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 2486 sjacqu01
# pragma once
16 2486 sjacqu01
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 2486 sjacqu01
18 2486 sjacqu01
#include <boost/asio/detail/noncopyable.hpp>
19 2486 sjacqu01
20 2486 sjacqu01
#include <boost/asio/detail/push_options.hpp>
21 2486 sjacqu01
22 2486 sjacqu01
namespace boost {
23 2486 sjacqu01
namespace asio {
24 2486 sjacqu01
namespace detail {
25 2486 sjacqu01
26 2486 sjacqu01
// Helper class to lock and unlock a mutex automatically.
27 2486 sjacqu01
template <typename Mutex>
28 2486 sjacqu01
class scoped_lock
29 2486 sjacqu01
  : private noncopyable
30 2486 sjacqu01
{
31 2486 sjacqu01
public:
32 2486 sjacqu01
  // Tag type used to distinguish constructors.
33 2486 sjacqu01
  enum adopt_lock_t { adopt_lock };
34 2486 sjacqu01
35 2486 sjacqu01
  // Constructor adopts a lock that is already held.
36 2486 sjacqu01
  scoped_lock(Mutex& m, adopt_lock_t)
37 2486 sjacqu01
    : mutex_(m),
38 2486 sjacqu01
      locked_(true)
39 2486 sjacqu01
  {
40 2486 sjacqu01
  }
41 2486 sjacqu01
42 2486 sjacqu01
  // Constructor acquires the lock.
43 2486 sjacqu01
  explicit scoped_lock(Mutex& m)
44 2486 sjacqu01
    : mutex_(m)
45 2486 sjacqu01
  {
46 2486 sjacqu01
    mutex_.lock();
47 2486 sjacqu01
    locked_ = true;
48 2486 sjacqu01
  }
49 2486 sjacqu01
50 2486 sjacqu01
  // Destructor releases the lock.
51 2486 sjacqu01
  ~scoped_lock()
52 2486 sjacqu01
  {
53 2486 sjacqu01
    if (locked_)
54 2486 sjacqu01
      mutex_.unlock();
55 2486 sjacqu01
  }
56 2486 sjacqu01
57 2486 sjacqu01
  // Explicitly acquire the lock.
58 2486 sjacqu01
  void lock()
59 2486 sjacqu01
  {
60 2486 sjacqu01
    if (!locked_)
61 2486 sjacqu01
    {
62 2486 sjacqu01
      mutex_.lock();
63 2486 sjacqu01
      locked_ = true;
64 2486 sjacqu01
    }
65 2486 sjacqu01
  }
66 2486 sjacqu01
67 2486 sjacqu01
  // Explicitly release the lock.
68 2486 sjacqu01
  void unlock()
69 2486 sjacqu01
  {
70 2486 sjacqu01
    if (locked_)
71 2486 sjacqu01
    {
72 2486 sjacqu01
      mutex_.unlock();
73 2486 sjacqu01
      locked_ = false;
74 2486 sjacqu01
    }
75 2486 sjacqu01
  }
76 2486 sjacqu01
77 2486 sjacqu01
  // Test whether the lock is held.
78 2486 sjacqu01
  bool locked() const
79 2486 sjacqu01
  {
80 2486 sjacqu01
    return locked_;
81 2486 sjacqu01
  }
82 2486 sjacqu01
83 2486 sjacqu01
  // Get the underlying mutex.
84 2486 sjacqu01
  Mutex& mutex()
85 2486 sjacqu01
  {
86 2486 sjacqu01
    return mutex_;
87 2486 sjacqu01
  }
88 2486 sjacqu01
89 2486 sjacqu01
private:
90 2486 sjacqu01
  // The underlying mutex.
91 2486 sjacqu01
  Mutex& mutex_;
92 2486 sjacqu01
93 2486 sjacqu01
  // Whether the mutex is currently locked or unlocked.
94 2486 sjacqu01
  bool locked_;
95 2486 sjacqu01
};
96 2486 sjacqu01
97 2486 sjacqu01
} // namespace detail
98 2486 sjacqu01
} // namespace asio
99 2486 sjacqu01
} // namespace boost
100 2486 sjacqu01
101 2486 sjacqu01
#include <boost/asio/detail/pop_options.hpp>
102 2486 sjacqu01
103 2486 sjacqu01
#endif // BOOST_ASIO_DETAIL_SCOPED_LOCK_HPP