Drizzled Public API Documentation

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 <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
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 
36 #include <string>
37 
38 using namespace std;
39 using namespace drizzled;
40 
41 
43 {
44  /* for thread-safe sleep() */
45  pthread_mutex_t LOCK_sleep;
46 
47 public:
48  int64_t val_int();
50  {
51  unsigned_flag= true;
52  }
53 
54  const char *func_name() const
55  {
56  return "sleep";
57  }
58 
59  void fix_length_and_dec()
60  {
61  max_length= 1;
62  }
63 
65  {
66  return (n == 1);
67  }
68 
69 };
70 
72 {
73  /* int time in seconds, decimal allowed */
74  double dtime;
75 
76  Session &session(getSession());
77 
78  if ((arg_count != 1) || ! (dtime= args[0]->val_real()))
79  {
80  null_value= true;
81  return 0;
82  }
83 
84  /*
85  On 64-bit OSX pthread_cond_timedwait() waits forever
86  if passed abstime time has already been exceeded by
87  the system time.
88  When given a very short timeout (< 10 mcs) just return
89  immediately.
90  We assume that the lines between this test and the call
91  to pthread_cond_timedwait() will be executed in less than 0.00001 sec.
92  */
93  if (dtime < 0.00001)
94  return 0;
95 
96  {
97  boost::this_thread::restore_interruption dl(session.getThreadInterupt());
98 
99  try {
100  boost::xtime xt;
101  xtime_get(&xt, boost::TIME_UTC_);
102  xt.nsec += (uint64_t)(dtime * 1000000000ULL);
103  session.getThread()->sleep(xt);
104  }
105  catch(boost::thread_interrupted const& error)
106  {
107  my_error(drizzled::ER_QUERY_INTERRUPTED, MYF(0));
108  null_value= true;
109 
110  return 0;
111  }
112  }
113 
114 
115  null_value= false;
116 
117  return (int64_t) 0;
118 }
119 
120 static int sleep_plugin_init(drizzled::module::Context &context)
121 {
122  context.add(new plugin::Create_function<Item_func_sleep>("sleep"));
123 
124  return 0;
125 }
126 
127 DRIZZLE_DECLARE_PLUGIN
128 {
129  DRIZZLE_VERSION_ID,
130  "sleep",
131  "1.0",
132  "Patrick Galbraith",
133  N_("SLEEP function"),
134  PLUGIN_LICENSE_GPL,
135  sleep_plugin_init,
136  NULL,
137  NULL,
138 }
139 DRIZZLE_DECLARE_PLUGIN_END;
TODO: Rename this file - func.h is stupid.
bool check_argument_count(int n)
Definition: sleep.cc:64
int64_t val_int()
Definition: sleep.cc:71