summaryrefslogtreecommitdiff
path: root/libs/interprocess/test/upgradable_mutex_test.cpp
blob: 61379ddd87e81fe3d4a0da0aea42ebd271a4ba59 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2004-2011. 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)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////

#include <boost/interprocess/detail/config_begin.hpp>
#include "mutex_test_template.hpp"
#include "sharable_mutex_test_template.hpp"
#include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/sharable_lock.hpp>
#include <boost/interprocess/sync/upgradable_lock.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "util.hpp"

int main ()
{
   using namespace boost::interprocess;

   test::test_all_lock<interprocess_upgradable_mutex>();
   test::test_all_mutex<true, interprocess_upgradable_mutex>();
   test::test_all_sharable_mutex<true, interprocess_upgradable_mutex>();

   //Test lock transition
   {
      typedef interprocess_upgradable_mutex Mutex;
      Mutex mut;
      Mutex mut2;

      //Conversions to scoped_lock
      {
         scoped_lock<Mutex>      lock(mut);
         scoped_lock<Mutex>      e_lock(boost::move(lock));
         lock.swap(e_lock);
      }
      {
         scoped_lock<Mutex>      lock(mut);
         scoped_lock<Mutex>      e_lock(mut2);
         e_lock = boost::move(lock);
      }
      {
         upgradable_lock<Mutex>  u_lock(mut);
         //This calls unlock_upgradable_and_lock()
         scoped_lock<Mutex>      e_lock(boost::move(u_lock));
      }
      {
         upgradable_lock<Mutex>  u_lock(mut);
         //This calls unlock_upgradable_and_lock()
         scoped_lock<Mutex>      e_lock(mut2);
         scoped_lock<Mutex>      moved(boost::move(u_lock));
         e_lock = boost::move(moved);
      }
      {
         upgradable_lock<Mutex>  u_lock(mut);
         //This calls try_unlock_upgradable_and_lock()
         scoped_lock<Mutex>      e_lock(boost::move(u_lock), try_to_lock);
      }
      {
         upgradable_lock<Mutex>  u_lock(mut);
         //This calls try_unlock_upgradable_and_lock()
         scoped_lock<Mutex>      e_lock(mut2);
         scoped_lock<Mutex>      moved(boost::move(u_lock), try_to_lock);
         e_lock = boost::move(moved);
      }
      {
         boost::posix_time::ptime t = test::delay(100);
         upgradable_lock<Mutex>  u_lock(mut);
         //This calls timed_unlock_upgradable_and_lock()
         scoped_lock<Mutex>      e_lock(boost::move(u_lock), t);
      }
      {
         boost::posix_time::ptime t = test::delay(100);
         upgradable_lock<Mutex>  u_lock(mut);
         //This calls timed_unlock_upgradable_and_lock()
         scoped_lock<Mutex>      e_lock(mut2);
         scoped_lock<Mutex>      moved(boost::move(u_lock), t);
         e_lock = boost::move(moved);
      }
      {
         sharable_lock<Mutex>    s_lock(mut);
         //This calls try_unlock_sharable_and_lock()
         scoped_lock<Mutex>      e_lock(boost::move(s_lock), try_to_lock);
      }
      {
         sharable_lock<Mutex>    s_lock(mut);
         //This calls try_unlock_sharable_and_lock()
         scoped_lock<Mutex>      e_lock(mut2);
         scoped_lock<Mutex>      moved(boost::move(s_lock), try_to_lock);
         e_lock = boost::move(moved);
      }
      //Conversions to upgradable_lock
      {
         upgradable_lock<Mutex>  lock(mut);
         upgradable_lock<Mutex>  u_lock(boost::move(lock));
         lock.swap(u_lock);
      }
      {
         upgradable_lock<Mutex>  lock(mut);
         upgradable_lock<Mutex>  u_lock(mut2);
         upgradable_lock<Mutex>  moved(boost::move(lock));
         u_lock = boost::move(moved);
      }
      {
         sharable_lock<Mutex>    s_lock(mut);
         //This calls unlock_sharable_and_lock_upgradable()
         upgradable_lock<Mutex>  u_lock(boost::move(s_lock), try_to_lock);
      }
      {
         sharable_lock<Mutex>    s_lock(mut);
         //This calls unlock_sharable_and_lock_upgradable()
         upgradable_lock<Mutex>  u_lock(mut2);
         upgradable_lock<Mutex>  moved(boost::move(s_lock), try_to_lock);
         u_lock = boost::move(moved);
      }
      {
         scoped_lock<Mutex>      e_lock(mut);
         //This calls unlock_and_lock_upgradable()
         upgradable_lock<Mutex>  u_lock(boost::move(e_lock));
      }
      {
         scoped_lock<Mutex>      e_lock(mut);
         //This calls unlock_and_lock_upgradable()
         upgradable_lock<Mutex>  u_lock(mut2);
         upgradable_lock<Mutex>  moved(boost::move(e_lock));
         u_lock = boost::move(moved);
      }
      //Conversions to sharable_lock
      {
         sharable_lock<Mutex>    lock(mut);
         sharable_lock<Mutex>    s_lock(boost::move(lock));
         lock.swap(s_lock);
      }
      {
         sharable_lock<Mutex>    lock(mut);
         sharable_lock<Mutex>    s_lock(mut2);
         sharable_lock<Mutex>    moved(boost::move(lock));
         s_lock = boost::move(moved);
      }
      {
         upgradable_lock<Mutex>  u_lock(mut);
         //This calls unlock_upgradable_and_lock_sharable()
         sharable_lock<Mutex>    s_lock(boost::move(u_lock));
      }
      {
         upgradable_lock<Mutex>  u_lock(mut);
         //This calls unlock_upgradable_and_lock_sharable()
         sharable_lock<Mutex>    s_lock(mut2);
         sharable_lock<Mutex>    moved(boost::move(u_lock));
         s_lock = boost::move(moved);
      }
      {
         scoped_lock<Mutex>      e_lock(mut);
         //This calls unlock_and_lock_sharable()
         sharable_lock<Mutex>    s_lock(boost::move(e_lock));
      }
      {
         scoped_lock<Mutex>      e_lock(mut);
         //This calls unlock_and_lock_sharable()
         sharable_lock<Mutex>    s_lock(mut2);
         sharable_lock<Mutex>    moved(boost::move(e_lock));
         s_lock = boost::move(moved);
      }
   }

   return 0;
}

#include <boost/interprocess/detail/config_end.hpp>