~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/user_locks/locks.cc

  • Committer: lbieber at stabletransit
  • Date: 2010-10-19 14:03:27 UTC
  • mfrom: (1861.1.2 build)
  • Revision ID: lbieber@drizzle-build-n02.wc1.dfw1.stabletransit.com-20101019140327-2jvt5j2wi4pzhm1z
Merge Brian - Small collection of cleanups/refactor'ing around locks
Merge Monty - fix a few things in the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
    std::pair<LockMap::iterator, bool> is_locked;
 
52
 
 
53
    is_locked= lock_map.insert(std::make_pair(arg, new lock_st(id_arg)));
 
54
    return is_locked.second;
67
55
  }
68
56
 
69
57
  return false;
70
58
}
71
59
 
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)
 
60
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Key &arg)
75
61
{
 
62
  std::pair<LockMap::iterator, bool> is_locked;
76
63
  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;
 
64
  is_locked= lock_map.insert(std::make_pair(arg, new lock_st(id_arg)));
79
65
 
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
 
  }
 
66
  return is_locked.second;
88
67
}
89
68
 
90
69
bool Locks::lock(drizzled::session_id_t id_arg, const user_locks::Keys &arg)
91
70
{
92
71
  boost::unique_lock<boost::mutex> scope(mutex);
93
 
  user_locks::Keys created;
94
 
  bool error= false;
95
72
 
96
73
  for (user_locks::Keys::const_iterator iter= arg.begin(); iter != arg.end(); iter++)
97
74
  {
98
75
    LockMap::iterator record= lock_map.find(*iter);
99
76
 
100
 
    if (record != lock_map.end()) // Found, so check ownership of the lock
 
77
    if (record != lock_map.end())
101
78
    {
102
79
      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);
 
80
        return false;
113
81
    }
114
82
  }
115
83
 
116
 
  if (error)
 
84
  for (Keys::iterator iter= arg.begin(); iter != arg.end(); iter++)
117
85
  {
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;
 
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)));
124
89
  }
125
90
 
126
 
  create_cond.notify_all();
127
 
 
128
91
  return true;
129
92
}
130
93
 
153
116
 
154
117
void Locks::Copy(LockMap &lock_map_arg)
155
118
{
156
 
  boost::unique_lock<boost::mutex> scope(mutex);
157
119
  lock_map_arg= lock_map;
158
120
}
159
121
 
160
 
locks::return_t Locks::release(const user_locks::Key &arg, drizzled::session_id_t &id_arg, bool and_wait)
 
122
boost::tribool Locks::release(const user_locks::Key &arg, drizzled::session_id_t &id_arg)
161
123
{
162
124
  size_t elements= 0;
163
125
  boost::unique_lock<boost::mutex> scope(mutex);
165
127
 
166
128
  // Nothing is found
167
129
  if ( iter == lock_map.end())
168
 
    return locks::NOT_FOUND;
 
130
    return boost::indeterminate;
169
131
 
170
132
  if ((*iter).second->id == id_arg)
171
 
  {
172
133
    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
 
    }
 
134
 
 
135
  if (elements)
 
136
  {
 
137
    cond.notify_one();
 
138
    return true;
202
139
  }
203
140
 
204
 
  return locks::NOT_OWNED_BY;
 
141
  return false;
205
142
}
206
143
 
207
144
} /* namespace user_locks */