drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
locks.cc
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2010 Brian Aker
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <config.h>
22#include <plugin/user_locks/module.h>
23
24#include <memory>
25#include <boost/thread/locks.hpp>
26
27#include <string>
28
29namespace user_locks {
30
31bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Key &arg, int64_t wait_for)
32{
33 boost::unique_lock<boost::mutex> scope(mutex);
34 boost::system_time timeout= boost::get_system_time() + boost::posix_time::seconds(wait_for);
35
36 LockMap::iterator iter;
37 while ((iter= lock_map.find(arg)) != lock_map.end())
38 {
39 if (id_arg == iter->second->id)
40 {
41 // We own the lock, so we just exit.
42 return true;
43 }
44 try {
45 if (wait_for)
46 {
47 bool success= release_cond.timed_wait(scope, timeout);
48
49 if (not success)
50 return false;
51 }
52 else
53 {
54 release_cond.wait(scope);
55 }
56 }
57 catch(boost::thread_interrupted const& error)
58 {
59 // Currently nothing is done here.
60 throw error;
61 }
62 }
63
64 if (iter == lock_map.end())
65 {
66 create_cond.notify_all();
67 return lock_map.insert(std::make_pair(arg, std::make_shared<Lock>(id_arg))).second;
68 }
69
70 return false;
71}
72
73// Currently we just let timeouts occur, and the caller will need to know
74// what it is looking for/whether to go back into this.
75void Locks::waitCreate(int64_t wait_for)
76{
77 boost::unique_lock<boost::mutex> scope(mutex);
78 boost::system_time timeout= boost::get_system_time() + boost::posix_time::seconds(wait_for);
79
80 try {
81 create_cond.timed_wait(scope, timeout);
82 }
83 catch(boost::thread_interrupted const& error)
84 {
85 // Currently nothing is done here.
86 throw error;
87 }
88}
89
90bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Keys &arg)
91{
92 boost::unique_lock<boost::mutex> scope(mutex);
93 user_locks::Keys created;
94 bool error= false;
95
96 for (user_locks::Keys::const_iterator iter= arg.begin(); iter != arg.end(); iter++)
97 {
98 LockMap::iterator record= lock_map.find(*iter);
99
100 if (record != lock_map.end()) // Found, so check ownership of the lock
101 {
102 if (id_arg != (*record).second->id)
103 {
104 // So it turns out the locks exist, and we can't grab them all
105 error= true;
106 break;
107 }
108 }
109 else
110 {
111 lock_map.insert(std::make_pair(*iter, std::make_shared<Lock>(id_arg)));
112 created.insert(*iter);
113 }
114 }
115
116 if (error)
117 {
118 for (user_locks::Keys::const_iterator iter= created.begin(); iter != created.end(); iter++)
119 {
120 lock_map.erase(*iter);
121 }
122
123 return false;
124 }
125
126 create_cond.notify_all();
127
128 return true;
129}
130
131bool Locks::isUsed(const user_locks::Key &arg, drizzled::session_id_t &id_arg)
132{
133 boost::unique_lock<boost::mutex> scope(mutex);
134
135 LockMap::iterator iter= lock_map.find(arg);
136
137 if ( iter == lock_map.end())
138 return false;
139
140 id_arg= iter->second->id;
141
142 return true;
143}
144
145bool Locks::isFree(const user_locks::Key &arg)
146{
147 boost::unique_lock<boost::mutex> scope(mutex);
148
149 LockMap::iterator iter= lock_map.find(arg);
150
151 return iter != lock_map.end();
152}
153
154void Locks::Copy(LockMap &lock_map_arg)
155{
156 boost::unique_lock<boost::mutex> scope(mutex);
157 lock_map_arg= lock_map;
158}
159
160locks::return_t Locks::release(const user_locks::Key &arg, drizzled::session_id_t &id_arg, bool and_wait)
161{
162 size_t elements= 0;
163 boost::unique_lock<boost::mutex> scope(mutex);
164 LockMap::iterator iter= lock_map.find(arg);
165
166 // Nothing is found
167 if ( iter == lock_map.end())
168 return locks::NOT_FOUND;
169
170 if (iter->second->id == id_arg)
171 {
172 elements= lock_map.erase(arg);
173 assert(elements); // If we can't find what we just found, then we are broken
174
175 if (elements)
176 {
177 release_cond.notify_one();
178
179 if (and_wait)
180 {
181 bool found= false;
182 while (not found)
183 {
184 assert(boost::this_thread::interruption_enabled());
185 try {
186 create_cond.wait(scope);
187 }
188 catch(boost::thread_interrupted const& error)
189 {
190 // Currently nothing is done here.
191 throw error;
192 }
193 iter= lock_map.find(arg);
194
195 if (iter != lock_map.end())
196 found= true;
197 }
198 }
199
200 return locks::SUCCESS;
201 }
202 }
203
204 return locks::NOT_OWNED_BY;
205}
206
207} /* namespace user_locks */