21
21
* along with this program; if not, write to the Free Software
22
22
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
25
#include <unistd.h>
28
#include <drizzled/server_includes.h>
30
29
#include <drizzled/session.h>
31
30
#include <drizzled/item/func.h>
32
#include "drizzled/internal/my_pthread.h"
31
#include <mysys/my_pthread.h>
33
32
#include <drizzled/function/str/strfunc.h>
70
69
int64_t Item_func_sleep::val_int()
72
73
/* int time in seconds, decimal allowed */
75
Session &session(getSession());
76
struct timespec abstime;
80
Session *session= current_session;
77
82
if ((arg_count != 1) || ! (dtime= args[0]->val_real()))
92
97
if (dtime < 0.00001)
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)
96
boost::this_thread::restore_interruption dl(session.getThreadInterupt());
100
xtime_get(&xt, boost::TIME_UTC);
101
xt.nsec += (uint64_t)(dtime * 1000000000ULL);
102
session.getThread()->sleep(xt);
104
catch(boost::thread_interrupted const& error)
110
error= pthread_cond_timedwait(&cond, &LOCK_sleep, &abstime);
111
if (error == ETIMEDOUT || error == ETIME)
106
my_error(drizzled::ER_QUERY_INTERRUPTED, MYF(0));
117
pthread_mutex_unlock(&LOCK_sleep);
119
/* relenquish pthread cond */
120
pthread_cond_destroy(&cond);
121
pthread_mutex_destroy(&LOCK_sleep);
114
123
null_value= false;
116
125
return (int64_t) 0;
119
static int sleep_plugin_init(drizzled::module::Context &context)
121
context.add(new plugin::Create_function<Item_func_sleep>("sleep"));
126
DRIZZLE_PLUGIN(sleep_plugin_init, NULL, NULL);
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;