~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/user_locks/locks.cc

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:30:27 UTC
  • mto: (1902.1.1 build) (1910.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1903.
  • Revision ID: stewart@flamingspork.com-20101103033027-lskb6gxwwforfz71
fix docs warning: underline/overline too short for replace.rst

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <config.h>
22
 
#include <plugin/user_locks/module.h>
 
21
#include "config.h"
 
22
#include "plugin/user_locks/module.h"
23
23
 
24
24
#include <boost/thread/locks.hpp>
25
25
 
29
29
 
30
30
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Key &arg, int64_t wait_for)
31
31
{
 
32
  boost::system_time timeout= boost::get_system_time() + boost::posix_time::seconds(wait_for);
32
33
  boost::unique_lock<boost::mutex> scope(mutex);
33
 
  boost::system_time timeout= boost::get_system_time() + boost::posix_time::seconds(wait_for);
34
34
  
35
35
  LockMap::iterator iter;
36
36
  while ((iter= lock_map.find(arg)) != lock_map.end())
40
40
      // We own the lock, so we just exit.
41
41
      return true;
42
42
    }
43
 
    try {
44
 
      if (wait_for)
45
 
      {
46
 
        bool success= release_cond.timed_wait(scope, timeout);
 
43
    bool success= cond.timed_wait(scope, timeout);
47
44
 
48
 
        if (not success)
49
 
          return false;
50
 
      }
51
 
      else
52
 
      {
53
 
        release_cond.wait(scope);
54
 
      }
55
 
    }
56
 
    catch(boost::thread_interrupted const& error)
57
 
    {
58
 
      // Currently nothing is done here.
59
 
      throw error;
60
 
    }
 
45
    if (not success)
 
46
      return false;
61
47
  }
62
48
 
63
49
  if (iter == lock_map.end())
64
50
  {
65
 
    create_cond.notify_all();
66
 
    return lock_map.insert(std::make_pair(arg, new Lock(id_arg))).second;
 
51
    return lock_map.insert(std::make_pair(arg, new lock_st(id_arg))).second;
67
52
  }
68
53
 
69
54
  return false;
70
55
}
71
56
 
72
 
// Currently we just let timeouts occur, and the caller will need to know
73
 
// what it is looking for/whether to go back into this.
74
 
void Locks::waitCreate(int64_t wait_for)
 
57
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Key &arg)
75
58
{
76
59
  boost::unique_lock<boost::mutex> scope(mutex);
77
 
  boost::system_time timeout= boost::get_system_time() + boost::posix_time::seconds(wait_for);
78
 
  bool timed_out;
79
 
 
80
 
  try {
81
 
    timed_out= 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
 
  }
 
60
  return lock_map.insert(std::make_pair(arg, new lock_st(id_arg))).second;
88
61
}
89
62
 
90
63
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Keys &arg)
91
64
{
92
65
  boost::unique_lock<boost::mutex> scope(mutex);
93
 
  user_locks::Keys created;
94
 
  bool error= false;
95
66
 
96
67
  for (user_locks::Keys::const_iterator iter= arg.begin(); iter != arg.end(); iter++)
97
68
  {
98
69
    LockMap::iterator record= lock_map.find(*iter);
99
70
 
100
 
    if (record != lock_map.end()) // Found, so check ownership of the lock
 
71
    if (record != lock_map.end())
101
72
    {
102
73
      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, new Lock(id_arg)));
112
 
      created.insert(*iter);
 
74
        return false;
113
75
    }
114
76
  }
115
77
 
116
 
  if (error)
 
78
  for (Keys::iterator iter= arg.begin(); iter != arg.end(); iter++)
117
79
  {
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;
 
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)));
124
82
  }
125
83
 
126
 
  create_cond.notify_all();
127
 
 
128
84
  return true;
129
85
}
130
86
 
153
109
 
154
110
void Locks::Copy(LockMap &lock_map_arg)
155
111
{
156
 
  boost::unique_lock<boost::mutex> scope(mutex);
157
112
  lock_map_arg= lock_map;
158
113
}
159
114
 
160
 
locks::return_t Locks::release(const user_locks::Key &arg, drizzled::session_id_t &id_arg, bool and_wait)
 
115
boost::tribool Locks::release(const user_locks::Key &arg, drizzled::session_id_t &id_arg)
161
116
{
162
117
  size_t elements= 0;
163
118
  boost::unique_lock<boost::mutex> scope(mutex);
165
120
 
166
121
  // Nothing is found
167
122
  if ( iter == lock_map.end())
168
 
    return locks::NOT_FOUND;
 
123
    return boost::indeterminate;
169
124
 
170
125
  if ((*iter).second->id == id_arg)
171
 
  {
172
126
    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
 
    }
 
127
 
 
128
  if (elements)
 
129
  {
 
130
    cond.notify_one();
 
131
    return true;
202
132
  }
203
133
 
204
 
  return locks::NOT_OWNED_BY;
 
134
  return false;
205
135
}
206
136
 
207
137
} /* namespace user_locks */