1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Brian Aker
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.
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.
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
22
#include "plugin/user_locks/module.h"
24
#include <boost/thread/locks.hpp>
28
namespace user_locks {
30
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Key &arg, int64_t wait_for)
32
boost::system_time timeout= boost::get_system_time() + boost::posix_time::seconds(wait_for);
33
boost::unique_lock<boost::mutex> scope(mutex);
35
LockMap::iterator iter;
36
while ((iter= lock_map.find(arg)) != lock_map.end())
38
if (id_arg == (*iter).second->id)
40
// We own the lock, so we just exit.
43
bool success= cond.timed_wait(scope, timeout);
49
if (iter == lock_map.end())
51
std::pair<LockMap::iterator, bool> is_locked;
53
is_locked= lock_map.insert(std::make_pair(arg, new lock_st(id_arg)));
54
return is_locked.second;
60
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Key &arg)
62
std::pair<LockMap::iterator, bool> is_locked;
63
boost::unique_lock<boost::mutex> scope(mutex);
64
is_locked= lock_map.insert(std::make_pair(arg, new lock_st(id_arg)));
66
return is_locked.second;
69
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Keys &arg)
71
boost::unique_lock<boost::mutex> scope(mutex);
73
for (user_locks::Keys::const_iterator iter= arg.begin(); iter != arg.end(); iter++)
75
LockMap::iterator record= lock_map.find(*iter);
77
if (record != lock_map.end())
79
if (id_arg != (*record).second->id)
84
for (Keys::iterator iter= arg.begin(); iter != arg.end(); iter++)
86
std::pair<LockMap::iterator, bool> is_locked;
87
//is_locked can fail in cases where we already own the lock.
88
is_locked= lock_map.insert(std::make_pair(*iter, new lock_st(id_arg)));
94
bool Locks::isUsed(const user_locks::Key &arg, drizzled::session_id_t &id_arg)
96
boost::unique_lock<boost::mutex> scope(mutex);
98
LockMap::iterator iter= lock_map.find(arg);
100
if ( iter == lock_map.end())
103
id_arg= (*iter).second->id;
108
bool Locks::isFree(const user_locks::Key &arg)
110
boost::unique_lock<boost::mutex> scope(mutex);
112
LockMap::iterator iter= lock_map.find(arg);
114
return iter != lock_map.end();
117
void Locks::Copy(LockMap &lock_map_arg)
119
lock_map_arg= lock_map;
122
boost::tribool Locks::release(const user_locks::Key &arg, drizzled::session_id_t &id_arg)
125
boost::unique_lock<boost::mutex> scope(mutex);
126
LockMap::iterator iter= lock_map.find(arg);
129
if ( iter == lock_map.end())
130
return boost::indeterminate;
132
if ((*iter).second->id == id_arg)
133
elements= lock_map.erase(arg);
144
} /* namespace user_locks */