~skinny.moey/drizzle/innodb-replication

« back to all changes in this revision

Viewing changes to plugin/user_locks/locks.cc

  • Committer: Brian Aker
  • Date: 2010-11-08 22:35:57 UTC
  • mfrom: (1802.1.114 trunk)
  • Revision ID: brian@tangent.org-20101108223557-w3xzwp9hjjtjhtc1
MergeĀ inĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <boost/thread/locks.hpp>
 
25
 
 
26
#include <string>
 
27
 
 
28
namespace user_locks {
 
29
 
 
30
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Key &arg, int64_t wait_for)
 
31
{
 
32
  boost::system_time timeout= boost::get_system_time() + boost::posix_time::seconds(wait_for);
 
33
  boost::unique_lock<boost::mutex> scope(mutex);
 
34
  
 
35
  LockMap::iterator iter;
 
36
  while ((iter= lock_map.find(arg)) != lock_map.end())
 
37
  {
 
38
    if (id_arg == (*iter).second->id)
 
39
    {
 
40
      // We own the lock, so we just exit.
 
41
      return true;
 
42
    }
 
43
    bool success= cond.timed_wait(scope, timeout);
 
44
 
 
45
    if (not success)
 
46
      return false;
 
47
  }
 
48
 
 
49
  if (iter == lock_map.end())
 
50
  {
 
51
    return lock_map.insert(std::make_pair(arg, new lock_st(id_arg))).second;
 
52
  }
 
53
 
 
54
  return false;
 
55
}
 
56
 
 
57
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Key &arg)
 
58
{
 
59
  boost::unique_lock<boost::mutex> scope(mutex);
 
60
  return lock_map.insert(std::make_pair(arg, new lock_st(id_arg))).second;
 
61
}
 
62
 
 
63
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Keys &arg)
 
64
{
 
65
  boost::unique_lock<boost::mutex> scope(mutex);
 
66
 
 
67
  for (user_locks::Keys::const_iterator iter= arg.begin(); iter != arg.end(); iter++)
 
68
  {
 
69
    LockMap::iterator record= lock_map.find(*iter);
 
70
 
 
71
    if (record != lock_map.end())
 
72
    {
 
73
      if (id_arg != (*record).second->id)
 
74
        return false;
 
75
    }
 
76
  }
 
77
 
 
78
  for (Keys::iterator iter= arg.begin(); iter != arg.end(); iter++)
 
79
  {
 
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)));
 
82
  }
 
83
 
 
84
  return true;
 
85
}
 
86
 
 
87
bool Locks::isUsed(const user_locks::Key &arg, drizzled::session_id_t &id_arg)
 
88
{
 
89
  boost::unique_lock<boost::mutex> scope(mutex);
 
90
  
 
91
  LockMap::iterator iter= lock_map.find(arg);
 
92
  
 
93
  if ( iter == lock_map.end())
 
94
    return false;
 
95
 
 
96
  id_arg= (*iter).second->id;
 
97
 
 
98
  return true;
 
99
}
 
100
 
 
101
bool Locks::isFree(const user_locks::Key &arg)
 
102
{
 
103
  boost::unique_lock<boost::mutex> scope(mutex);
 
104
 
 
105
  LockMap::iterator iter= lock_map.find(arg);
 
106
  
 
107
  return iter != lock_map.end();
 
108
}
 
109
 
 
110
void Locks::Copy(LockMap &lock_map_arg)
 
111
{
 
112
  lock_map_arg= lock_map;
 
113
}
 
114
 
 
115
boost::tribool Locks::release(const user_locks::Key &arg, drizzled::session_id_t &id_arg)
 
116
{
 
117
  size_t elements= 0;
 
118
  boost::unique_lock<boost::mutex> scope(mutex);
 
119
  LockMap::iterator iter= lock_map.find(arg);
 
120
 
 
121
  // Nothing is found
 
122
  if ( iter == lock_map.end())
 
123
    return boost::indeterminate;
 
124
 
 
125
  if ((*iter).second->id == id_arg)
 
126
    elements= lock_map.erase(arg);
 
127
 
 
128
  if (elements)
 
129
  {
 
130
    cond.notify_one();
 
131
    return true;
 
132
  }
 
133
 
 
134
  return false;
 
135
}
 
136
 
 
137
} /* namespace user_locks */