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
139
140
141
142
143
144
145
146
147
148
149
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2008 Sun Microsystems
*
* 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; version 2 of the License.
*
* 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 <drizzled/server_includes.h>
#include CSTDINT_H
#include <drizzled/functions/lock.h>
#include <mysys/hash.h>
#include <drizzled/session.h>
/*
** User level locks
*/
pthread_mutex_t LOCK_user_locks;
static HASH hash_user_locks;
class User_level_lock
{
unsigned char *key;
size_t key_length;
public:
int count;
bool locked;
pthread_cond_t cond;
my_thread_id thread_id;
void set_thread(Session *session) { thread_id= session->thread_id; }
User_level_lock(const unsigned char *key_arg,uint32_t length, ulong id)
:key_length(length),count(1),locked(1), thread_id(id)
{
key= (unsigned char*) my_memdup(key_arg,length,MYF(0));
pthread_cond_init(&cond,NULL);
if (key)
{
if (my_hash_insert(&hash_user_locks,(unsigned char*) this))
{
free(key);
key=0;
}
}
}
~User_level_lock()
{
if (key)
{
hash_delete(&hash_user_locks,(unsigned char*) this);
free(key);
}
pthread_cond_destroy(&cond);
}
inline bool initialized() { return key != 0; }
friend void item_user_lock_release(User_level_lock *ull);
friend unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
bool not_used);
};
unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
bool not_used __attribute__((unused)))
{
*length= ull->key_length;
return ull->key;
}
void item_user_lock_release(User_level_lock *ull)
{
ull->locked=0;
ull->thread_id= 0;
if (--ull->count)
pthread_cond_signal(&ull->cond);
else
delete ull;
}
/**
Check a user level lock.
Sets null_value=true on error.
@retval
1 Available
@retval
0 Already taken, or error
*/
int64_t Item_func_is_free_lock::val_int()
{
assert(fixed == 1);
String *res=args[0]->val_str(&value);
User_level_lock *ull;
null_value=0;
if (!res || !res->length())
{
null_value=1;
return 0;
}
pthread_mutex_lock(&LOCK_user_locks);
ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
(size_t) res->length());
pthread_mutex_unlock(&LOCK_user_locks);
if (!ull || !ull->locked)
return 1;
return 0;
}
int64_t Item_func_is_used_lock::val_int()
{
assert(fixed == 1);
String *res=args[0]->val_str(&value);
User_level_lock *ull;
null_value=1;
if (!res || !res->length())
return 0;
pthread_mutex_lock(&LOCK_user_locks);
ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
(size_t) res->length());
pthread_mutex_unlock(&LOCK_user_locks);
if (!ull || !ull->locked)
return 0;
null_value=0;
return ull->thread_id;
}
|