summaryrefslogtreecommitdiff
path: root/boost/thread/lockable_concepts.hpp
blob: bedd962e3be7614c0398f36f001df4ec16351d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// (C) Copyright 2012 Vicente Botet
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_THREAD_LOCKABLE_CONCEPTS_HPP
#define BOOST_THREAD_LOCKABLE_CONCEPTS_HPP

#include <boost/chrono/chrono.hpp>
#include <boost/concept_check.hpp>

namespace boost
{

  /**
   * BasicLockable object supports the basic features
   * required to delimit a critical region
   * Supports the basic lock and unlock functions.
   */

  //[BasicLockable
  template <typename Mutex>
  struct BasicLockable
  {

    BOOST_CONCEPT_USAGE(BasicLockable)
    {
      l.lock();
      l.unlock();
    }
    BasicLockable() : l(*static_cast<Mutex*>(0)) {}
  private:
    BasicLockable operator=(BasicLockable const&);

    Mutex& l;
  }
  ;
  //]
  /**
   * Lockable extends BasicLockable
   * with try_lock functions.
   */

  //[Lockable
  template <typename Mutex>
  struct Lockable
  {
    BOOST_CONCEPT_ASSERT(( BasicLockable<Mutex> ));

    BOOST_CONCEPT_USAGE(Lockable)
    {
      if (l.try_lock()) return;
    }
    Lockable() : l(*static_cast<Mutex*>(0)) {}
  private:
    Lockable operator=(Lockable const&);
    Mutex& l;
  };
  //]

  /**
   * TimedLockable object extends Lockable
   * with timed lock functions: try_lock_until and try_lock_for and the exception based lock_until and lock_for
   */

  //[TimedLockable
  template <typename Mutex>
  struct TimedLockable
  {
    BOOST_CONCEPT_ASSERT(( Lockable<Mutex> ));

    BOOST_CONCEPT_USAGE(TimedLockable)
    {
      if (l.try_lock_until(t)) return;
      if (l.try_lock_for(d)) return;
    }
    TimedLockable() : l(*static_cast<Mutex*>(0)) {}
  private:
    TimedLockable operator=(TimedLockable const&);
    Mutex& l;
    chrono::system_clock::time_point t;
    chrono::system_clock::duration d;
  };
  //]

  /**
   * SharedLockable object extends TimedLockable
   * with the lock_shared, lock_shared_until, lock_shared_for, try_lock_shared_until, try_lock_shared
   * and unlock_shared functions
   */
  //[SharedLockable
  template <typename Mutex>
  struct SharedLockable
  {
    BOOST_CONCEPT_ASSERT(( TimedLockable<Mutex> ));

    BOOST_CONCEPT_USAGE(SharedLockable)
    {
      l.lock_shared();
      l.unlock_shared();
      if (l.try_lock_shared()) return;
      if (l.try_lock_shared_until(t)) return;
      if (l.try_lock_shared_for(d)) return;
    }
    SharedLockable() : l(*static_cast<Mutex*>(0)) {}
  private:
    SharedLockable operator=(SharedLockable const&);
    Mutex& l;
    chrono::system_clock::time_point t;
    chrono::system_clock::duration d;
  };
  //]

  /**
   * UpgradeLockable object extends SharedLockable
   * with the lock_upgrade, lock_upgrade_until, unlock_upgrade_and_lock,
   * unlock_and_lock_shared and unlock_upgrade_and_lock_shared functions
   */

  //[UpgradeLockable
  template <typename Mutex>
  struct UpgradeLockable
  {
    BOOST_CONCEPT_ASSERT(( SharedLockable<Mutex> ));

    BOOST_CONCEPT_USAGE(UpgradeLockable)
    {
      l.lock_upgrade();
      l.unlock_upgrade();
      if (l.try_lock_upgrade()) return;
      if (l.try_lock_upgrade_until(t)) return;
      if (l.try_lock_upgrade_for(d)) return;
      if (l.try_unlock_shared_and_lock()) return;
      if (l.try_unlock_shared_and_lock_until(t)) return;
      if (l.try_unlock_shared_and_lock_for(d)) return;
      l.unlock_and_lock_shared();
      if (l.try_unlock_shared_and_lock_upgrade()) return;
      if (l.try_unlock_shared_and_lock_upgrade_until(t)) return;
      if (l.try_unlock_shared_and_lock_upgrade_for(d)) return;
      l.unlock_and_lock_upgrade();
      l.unlock_upgrade_and_lock();
      if (l.try_unlock_upgrade_and_lock()) return;
      if (l.try_unlock_upgrade_and_lock_until(t)) return;
      if (l.try_unlock_upgrade_and_lock_for(d)) return;
      l.unlock_upgrade_and_lock_shared();
    }
    UpgradeLockable() : l(*static_cast<Mutex*>(0)) {}
  private:
    UpgradeLockable operator=(UpgradeLockable const&);
    Mutex& l;
    chrono::system_clock::time_point t;
    chrono::system_clock::duration d;
  };
  //]

}
#endif