~drizzle-trunk/drizzle/development

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
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2010 Brian Aker
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "plugin/user_locks/module.h"

#include <boost/thread/locks.hpp>

#include <string>

namespace user_locks {

bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Key &arg, int64_t wait_for)
{
  boost::system_time timeout= boost::get_system_time() + boost::posix_time::seconds(wait_for);
  boost::unique_lock<boost::mutex> scope(mutex);
  
  LockMap::iterator iter;
  while ((iter= lock_map.find(arg)) != lock_map.end())
  {
    if (id_arg == (*iter).second->id)
    {
      // We own the lock, so we just exit.
      return true;
    }
    bool success= cond.timed_wait(scope, timeout);

    if (not success)
      return false;
  }

  if (iter == lock_map.end())
  {
    return lock_map.insert(std::make_pair(arg, new lock_st(id_arg))).second;
  }

  return false;
}

bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Key &arg)
{
  boost::unique_lock<boost::mutex> scope(mutex);
  return lock_map.insert(std::make_pair(arg, new lock_st(id_arg))).second;
}

bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Keys &arg)
{
  boost::unique_lock<boost::mutex> scope(mutex);

  for (user_locks::Keys::const_iterator iter= arg.begin(); iter != arg.end(); iter++)
  {
    LockMap::iterator record= lock_map.find(*iter);

    if (record != lock_map.end())
    {
      if (id_arg != (*record).second->id)
        return false;
    }
  }

  for (Keys::iterator iter= arg.begin(); iter != arg.end(); iter++)
  {
    //is_locked can fail in cases where we already own the lock.
    lock_map.insert(std::make_pair(*iter, new lock_st(id_arg)));
  }

  return true;
}

bool Locks::isUsed(const user_locks::Key &arg, drizzled::session_id_t &id_arg)
{
  boost::unique_lock<boost::mutex> scope(mutex);
  
  LockMap::iterator iter= lock_map.find(arg);
  
  if ( iter == lock_map.end())
    return false;

  id_arg= (*iter).second->id;

  return true;
}

bool Locks::isFree(const user_locks::Key &arg)
{
  boost::unique_lock<boost::mutex> scope(mutex);

  LockMap::iterator iter= lock_map.find(arg);
  
  return iter != lock_map.end();
}

void Locks::Copy(LockMap &lock_map_arg)
{
  //@todo add lock(?)
  lock_map_arg= lock_map;
}

boost::tribool Locks::release(const user_locks::Key &arg, drizzled::session_id_t &id_arg)
{
  size_t elements= 0;
  boost::unique_lock<boost::mutex> scope(mutex);
  LockMap::iterator iter= lock_map.find(arg);

  // Nothing is found
  if ( iter == lock_map.end())
    return boost::indeterminate;

  if ((*iter).second->id == id_arg)
    elements= lock_map.erase(arg);

  if (elements)
  {
    cond.notify_one();
    return true;
  }

  return false;
}

} /* namespace user_locks */