drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
sleep.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2009 Sun Microsystems, Inc.
5 *
6 * Authors:
7 *
8 * Patrick Galbraith <[email protected]>
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
23 */
24
25#include <config.h>
26
27#include <unistd.h>
28#include <time.h>
29
30#include <drizzled/session.h>
31#include <drizzled/item/func.h>
32#include <drizzled/internal/my_pthread.h>
33#include <drizzled/function/str/strfunc.h>
34#include <drizzled/plugin/function.h>
35#include <drizzled/item/cmpfunc.h>
36
37#include <string>
38
39using namespace std;
40using namespace drizzled;
41
42
43class Item_func_sleep : public Item_int_func
44{
45 /* for thread-safe sleep() */
46 pthread_mutex_t LOCK_sleep;
47
48public:
49 int64_t val_int();
50 Item_func_sleep() : Item_int_func()
51 {
52 unsigned_flag= true;
53 }
54
55 const char *func_name() const
56 {
57 return "sleep";
58 }
59
60 void fix_length_and_dec()
61 {
62 max_length= 1;
63 }
64
66 {
67 return (n == 1);
68 }
69
70};
71
73{
74 /* int time in seconds, decimal allowed */
75 double dtime= args[0]->val_real();
76
77 Session &session(getSession());
78
79 if ((arg_count != 1) || compare_double(0, dtime))
80 {
81 null_value= true;
82 return 0;
83 }
84
85 /*
86 On 64-bit OSX pthread_cond_timedwait() waits forever
87 if passed abstime time has already been exceeded by
88 the system time.
89 When given a very short timeout (< 10 mcs) just return
90 immediately.
91 We assume that the lines between this test and the call
92 to pthread_cond_timedwait() will be executed in less than 0.00001 sec.
93 */
94 if (dtime < 0.00001)
95 return 0;
96
97 {
98 boost::this_thread::restore_interruption dl(session.getThreadInterupt());
99
100 try {
101 boost::xtime xt;
102#if BOOST_VERSION >= 105000
103 xtime_get(&xt, boost::TIME_UTC_);
104#else
105 xtime_get(&xt, boost::TIME_UTC);
106#endif
107 xt.nsec += (uint64_t)(dtime * 1000000000ULL);
108 session.getThread()->sleep(xt);
109 }
110 catch(boost::thread_interrupted const& error)
111 {
112 my_error(drizzled::ER_QUERY_INTERRUPTED, MYF(0));
113 null_value= true;
114
115 return 0;
116 }
117 }
118
119
120 null_value= false;
121
122 return (int64_t) 0;
123}
124
125static int sleep_plugin_init(drizzled::module::Context &context)
126{
127 context.add(new plugin::Create_function<Item_func_sleep>("sleep"));
128
129 return 0;
130}
131
132DRIZZLE_DECLARE_PLUGIN
133{
134 DRIZZLE_VERSION_ID,
135 "sleep",
136 "1.0",
137 "Patrick Galbraith",
138 N_("SLEEP function"),
139 PLUGIN_LICENSE_GPL,
140 sleep_plugin_init,
141 NULL,
142 NULL,
143}
144DRIZZLE_DECLARE_PLUGIN_END;
bool check_argument_count(int n)
Definition sleep.cc:65
int64_t val_int()
Definition sleep.cc:72
bool null_value
Definition item.h:124
TODO: Rename this file - func.h is stupid.
STL namespace.