~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to portable/libtorrent/include/libtorrent/asio/detail/scoped_lock.hpp

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

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