drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
barriers.cc
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 <memory>
25#include <boost/thread/locks.hpp>
26
27#include <string>
28
29namespace user_locks {
30namespace barriers {
31
32bool Barriers::create(const user_locks::Key &arg, drizzled::session_id_t owner)
33{
34 boost::unique_lock<boost::mutex> scope(mutex);
35 return barrier_map.insert(std::make_pair(arg, std::make_shared<Barrier>(owner))).second;
36}
37
38bool Barriers::create(const user_locks::Key &arg, drizzled::session_id_t owner, int64_t wait_count)
39{
40 boost::unique_lock<boost::mutex> scope(mutex);
41 return barrier_map.insert(std::make_pair(arg, std::make_shared<Barrier>(owner, wait_count))).second;
42}
43
44/*
45 @note return
46
47 true -> release happened.
48 false -> no release, we were not the owner
49 indeterminate -> barrier was not found.
50
51*/
52return_t Barriers::release(const user_locks::Key &arg, drizzled::session_id_t owner)
53{
54 boost::unique_lock<boost::mutex> scope(mutex);
55 Barrier::shared_ptr iter= find_ptr2(barrier_map, arg);
56
57 // Nothing is found
58 if (not iter)
59 return NOT_FOUND;
60
61 if (iter->getOwner() != owner)
62 return NOT_OWNED_BY;
63
64 iter->signal(); // We tell anyone left to start running
65 (void)barrier_map.erase(arg);
66
67 return SUCCESS;
68}
69
70Barrier::shared_ptr Barriers::find(const user_locks::Key &arg)
71{
72 boost::unique_lock<boost::mutex> scope(mutex);
73 return find_ptr2(barrier_map, arg);
74}
75
76void Barriers::Copy(Map &arg)
77{
78 boost::unique_lock<boost::mutex> scope(mutex);
79 arg= barrier_map;
80}
81
82} /* namespace barriers */
83} /* namespace user_locks */