1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
1 |
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
1103.7.11
by Patrick Galbraith
Ok, sorry for confusion on license issues. sleep is derived, so needs |
4 |
* Copyright 2009 Sun Microsystems
|
5 |
*
|
|
6 |
* Authors:
|
|
7 |
*
|
|
8 |
* Patrick Galbraith <pat@patg.net>
|
|
9 |
*
|
|
10 |
*
|
|
11 |
* This program is free software; you can redistribute it and/or modify
|
|
12 |
* it under the terms of the GNU General Public License as published by
|
|
13 |
* the Free Software Foundation; version 2 of the License.
|
|
14 |
*
|
|
15 |
* This program is distributed in the hope that it will be useful,
|
|
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 |
* GNU General Public License for more details.
|
|
19 |
*
|
|
20 |
* You should have received a copy of the GNU General Public License
|
|
21 |
* along with this program; if not, write to the Free Software
|
|
22 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
23 |
*/
|
24 |
||
25 |
#include <unistd.h> |
|
1103.7.3
by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;) |
26 |
#include <time.h> |
27 |
||
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
28 |
#include <drizzled/server_includes.h> |
1103.7.3
by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;) |
29 |
#include <drizzled/session.h> |
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
30 |
#include <drizzled/item/func.h> |
1103.7.3
by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;) |
31 |
#include <mysys/my_pthread.h> |
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
32 |
#include <drizzled/function/str/strfunc.h> |
33 |
||
34 |
#include <string> |
|
35 |
||
36 |
using namespace std; |
|
1103.7.6
by Patrick Galbraith
Merge with main branch, modification to sleep plugin |
37 |
using namespace drizzled; |
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
38 |
|
1103.7.3
by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;) |
39 |
|
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
40 |
class Item_func_sleep : public Item_int_func |
41 |
{
|
|
1103.7.12
by Patrick Galbraith
Changed LOCK_sleep to be a private, class member, making |
42 |
/* for thread-safe sleep() */
|
43 |
pthread_mutex_t LOCK_sleep; |
|
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
44 |
|
45 |
public: |
|
46 |
int64_t val_int(); |
|
47 |
Item_func_sleep() : Item_int_func() |
|
48 |
{
|
|
49 |
unsigned_flag= true; |
|
50 |
}
|
|
51 |
||
52 |
const char *func_name() const |
|
53 |
{
|
|
54 |
return "sleep"; |
|
55 |
}
|
|
56 |
||
1103.7.12
by Patrick Galbraith
Changed LOCK_sleep to be a private, class member, making |
57 |
void fix_length_and_dec() |
58 |
{
|
|
1103.7.8
by Patrick Galbraith
Some changes per review |
59 |
max_length= 1; |
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
60 |
}
|
61 |
||
62 |
bool check_argument_count(int n) |
|
63 |
{
|
|
64 |
return (n == 1); |
|
65 |
}
|
|
66 |
||
67 |
};
|
|
68 |
||
69 |
int64_t Item_func_sleep::val_int() |
|
70 |
{
|
|
1103.7.3
by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;) |
71 |
int error= 0; |
72 |
||
73 |
/* int time in seconds, decimal allowed */
|
|
74 |
double dtime; |
|
75 |
||
76 |
struct timespec abstime; |
|
77 |
||
78 |
pthread_cond_t cond; |
|
79 |
||
80 |
Session *session= current_session; |
|
81 |
||
82 |
if ((arg_count != 1) || ! (dtime= args[0]->val_real())) |
|
83 |
{
|
|
84 |
null_value= true; |
|
85 |
return 0; |
|
86 |
}
|
|
87 |
||
88 |
/*
|
|
89 |
On 64-bit OSX pthread_cond_timedwait() waits forever
|
|
90 |
if passed abstime time has already been exceeded by
|
|
91 |
the system time.
|
|
92 |
When given a very short timeout (< 10 mcs) just return
|
|
93 |
immediately.
|
|
94 |
We assume that the lines between this test and the call
|
|
95 |
to pthread_cond_timedwait() will be executed in less than 0.00001 sec.
|
|
96 |
*/
|
|
97 |
if (dtime < 0.00001) |
|
98 |
return 0; |
|
99 |
||
100 |
/* need to obtain time value for passing to cond_timedwait */
|
|
101 |
set_timespec_nsec(abstime, (uint64_t)(dtime * 1000000000ULL)); |
|
102 |
||
1154.1.2
by Brian Aker
Patrick + leak fix +test fix by Brian |
103 |
pthread_mutex_init(&LOCK_sleep, MY_MUTEX_INIT_FAST); |
1103.7.3
by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;) |
104 |
pthread_cond_init(&cond, NULL); |
1154.1.2
by Brian Aker
Patrick + leak fix +test fix by Brian |
105 |
|
106 |
/* don't run if not killed */
|
|
1103.7.3
by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;) |
107 |
pthread_mutex_lock(&LOCK_sleep); |
1103.7.5
by Patrick Galbraith
Style fixes, removed non-needed stuffs |
108 |
while (! session->killed) |
1103.7.3
by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;) |
109 |
{
|
110 |
error= pthread_cond_timedwait(&cond, &LOCK_sleep, &abstime); |
|
111 |
if (error == ETIMEDOUT || error == ETIME) |
|
1103.7.5
by Patrick Galbraith
Style fixes, removed non-needed stuffs |
112 |
{
|
1103.7.3
by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;) |
113 |
break; |
1103.7.5
by Patrick Galbraith
Style fixes, removed non-needed stuffs |
114 |
}
|
1103.7.3
by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;) |
115 |
error= 0; |
116 |
}
|
|
117 |
pthread_mutex_unlock(&LOCK_sleep); |
|
118 |
||
119 |
/* relenquish pthread cond */
|
|
120 |
pthread_cond_destroy(&cond); |
|
1154.1.2
by Brian Aker
Patrick + leak fix +test fix by Brian |
121 |
pthread_mutex_destroy(&LOCK_sleep); |
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
122 |
|
1103.7.6
by Patrick Galbraith
Merge with main branch, modification to sleep plugin |
123 |
null_value= false; |
1154.1.2
by Brian Aker
Patrick + leak fix +test fix by Brian |
124 |
|
1103.7.6
by Patrick Galbraith
Merge with main branch, modification to sleep plugin |
125 |
return (int64_t) 0; |
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
126 |
}
|
127 |
||
1103.7.6
by Patrick Galbraith
Merge with main branch, modification to sleep plugin |
128 |
plugin::Create_function<Item_func_sleep> *sleep_udf= NULL; |
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
129 |
|
1103.7.4
by Patrick Galbraith
Merged sleep() branch with main. |
130 |
static int sleep_plugin_init(drizzled::plugin::Registry ®istry) |
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
131 |
{
|
1103.7.6
by Patrick Galbraith
Merge with main branch, modification to sleep plugin |
132 |
sleep_udf= new plugin::Create_function<Item_func_sleep>("sleep"); |
1154.1.2
by Brian Aker
Patrick + leak fix +test fix by Brian |
133 |
registry.add(sleep_udf); |
134 |
||
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
135 |
return 0; |
136 |
}
|
|
137 |
||
1103.7.4
by Patrick Galbraith
Merged sleep() branch with main. |
138 |
static int sleep_plugin_deinit(drizzled::plugin::Registry ®istry) |
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
139 |
{
|
1154.1.2
by Brian Aker
Patrick + leak fix +test fix by Brian |
140 |
registry.remove(sleep_udf); |
1103.7.6
by Patrick Galbraith
Merge with main branch, modification to sleep plugin |
141 |
delete sleep_udf; |
1154.1.2
by Brian Aker
Patrick + leak fix +test fix by Brian |
142 |
|
1103.7.1
by Patrick Galbraith
Drizzle can now sleep! |
143 |
return 0; |
144 |
}
|
|
145 |
||
146 |
||
147 |
drizzle_declare_plugin(sleep) |
|
148 |
{
|
|
149 |
"sleep", |
|
150 |
"1.0", |
|
151 |
"Patrick Galbraith", |
|
152 |
"sleep()", |
|
153 |
PLUGIN_LICENSE_GPL, |
|
154 |
sleep_plugin_init, /* Plugin Init */ |
|
155 |
sleep_plugin_deinit, /* Plugin Deinit */ |
|
156 |
NULL, /* status variables */ |
|
157 |
NULL, /* system variables */ |
|
158 |
NULL /* config options */ |
|
159 |
}
|
|
160 |
drizzle_declare_plugin_end; |