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
return lock_map.insert(std::make_pair(arg, new lock_st(id_arg))).second;
57
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Key &arg)
59
boost::unique_lock<boost::mutex> scope(mutex);
60
return lock_map.insert(std::make_pair(arg, new lock_st(id_arg))).second;
63
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Keys &arg)
65
boost::unique_lock<boost::mutex> scope(mutex);
67
for (user_locks::Keys::const_iterator iter= arg.begin(); iter != arg.end(); iter++)
69
LockMap::iterator record= lock_map.find(*iter);
71
if (record != lock_map.end())
73
if (id_arg != (*record).second->id)
78
for (Keys::iterator iter= arg.begin(); iter != arg.end(); iter++)
80
//is_locked can fail in cases where we already own the lock.
81
lock_map.insert(std::make_pair(*iter, new lock_st(id_arg)));
87
bool Locks::isUsed(const user_locks::Key &arg, drizzled::session_id_t &id_arg)
89
boost::unique_lock<boost::mutex> scope(mutex);
91
LockMap::iterator iter= lock_map.find(arg);
93
if ( iter == lock_map.end())
96
id_arg= (*iter).second->id;
101
bool Locks::isFree(const user_locks::Key &arg)
103
boost::unique_lock<boost::mutex> scope(mutex);
105
LockMap::iterator iter= lock_map.find(arg);
107
return iter != lock_map.end();
110
void Locks::Copy(LockMap &lock_map_arg)
112
lock_map_arg= lock_map;
115
boost::tribool Locks::release(const user_locks::Key &arg, drizzled::session_id_t &id_arg)
118
boost::unique_lock<boost::mutex> scope(mutex);
119
LockMap::iterator iter= lock_map.find(arg);
122
if ( iter == lock_map.end())
123
return boost::indeterminate;
125
if ((*iter).second->id == id_arg)
126
elements= lock_map.erase(arg);
137
} /* namespace user_locks */