1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright 2009 Sun Microsystems
8
* Patrick Galbraith <pat@patg.net>
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.
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.
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
28
#include <drizzled/server_includes.h>
29
#include <drizzled/session.h>
30
#include <drizzled/item/func.h>
31
#include <mysys/my_pthread.h>
32
#include <drizzled/function/str/strfunc.h>
37
using namespace drizzled;
40
class Item_func_sleep : public Item_int_func
42
/* for thread-safe sleep() */
43
pthread_mutex_t LOCK_sleep;
47
Item_func_sleep() : Item_int_func()
52
const char *func_name() const
57
void fix_length_and_dec()
62
bool check_argument_count(int n)
69
int64_t Item_func_sleep::val_int()
73
/* int time in seconds, decimal allowed */
76
struct timespec abstime;
80
Session *session= current_session;
82
if ((arg_count != 1) || ! (dtime= args[0]->val_real()))
89
On 64-bit OSX pthread_cond_timedwait() waits forever
90
if passed abstime time has already been exceeded by
92
When given a very short timeout (< 10 mcs) just return
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.
100
/* need to obtain time value for passing to cond_timedwait */
101
set_timespec_nsec(abstime, (uint64_t)(dtime * 1000000000ULL));
103
pthread_mutex_init(&LOCK_sleep, MY_MUTEX_INIT_FAST);
104
pthread_cond_init(&cond, NULL);
106
/* don't run if not killed */
107
pthread_mutex_lock(&LOCK_sleep);
108
while (! session->killed)
110
error= pthread_cond_timedwait(&cond, &LOCK_sleep, &abstime);
111
if (error == ETIMEDOUT || error == ETIME)
117
pthread_mutex_unlock(&LOCK_sleep);
119
/* relenquish pthread cond */
120
pthread_cond_destroy(&cond);
121
pthread_mutex_destroy(&LOCK_sleep);
128
plugin::Create_function<Item_func_sleep> *sleep_udf= NULL;
130
static int sleep_plugin_init(drizzled::plugin::Registry ®istry)
132
sleep_udf= new plugin::Create_function<Item_func_sleep>("sleep");
133
registry.add(sleep_udf);
138
static int sleep_plugin_deinit(drizzled::plugin::Registry ®istry)
140
registry.remove(sleep_udf);
147
drizzle_declare_plugin(sleep)
154
sleep_plugin_init, /* Plugin Init */
155
sleep_plugin_deinit, /* Plugin Deinit */
156
NULL, /* status variables */
157
NULL, /* system variables */
158
NULL /* config options */
160
drizzle_declare_plugin_end;