~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/sleep/sleep.cc

  • Committer: Brian Aker
  • Date: 2009-10-02 19:39:20 UTC
  • mfrom: (1154.1.2 staging)
  • Revision ID: brian@gaz-20091002193920-6qeftbkoqyg0hmku
  Brian Aker 2009-10-02 [merge] Patrick + leak fix +test fix by Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
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
 
23
 */
 
24
 
 
25
#include <unistd.h>
 
26
#include <time.h>
 
27
 
 
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>
 
33
 
 
34
#include <string>
 
35
 
 
36
using namespace std;
 
37
using namespace drizzled;
 
38
 
 
39
 
 
40
class Item_func_sleep : public Item_int_func
 
41
{
 
42
  /* for thread-safe sleep() */
 
43
  pthread_mutex_t LOCK_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
 
 
57
  void fix_length_and_dec()
 
58
  {
 
59
    max_length= 1;
 
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
{
 
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
 
 
103
  pthread_mutex_init(&LOCK_sleep, MY_MUTEX_INIT_FAST);
 
104
  pthread_cond_init(&cond, NULL);
 
105
 
 
106
  /* don't run if not killed */
 
107
  pthread_mutex_lock(&LOCK_sleep);
 
108
  while (! session->killed)
 
109
  {
 
110
    error= pthread_cond_timedwait(&cond, &LOCK_sleep, &abstime);
 
111
    if (error == ETIMEDOUT || error == ETIME)
 
112
    {
 
113
      break;
 
114
    }
 
115
    error= 0;
 
116
  }
 
117
  pthread_mutex_unlock(&LOCK_sleep);
 
118
 
 
119
  /* relenquish pthread cond */
 
120
  pthread_cond_destroy(&cond);
 
121
  pthread_mutex_destroy(&LOCK_sleep);
 
122
 
 
123
  null_value= false;
 
124
 
 
125
  return (int64_t) 0;
 
126
}
 
127
 
 
128
plugin::Create_function<Item_func_sleep> *sleep_udf= NULL;
 
129
 
 
130
static int sleep_plugin_init(drizzled::plugin::Registry &registry)
 
131
{
 
132
  sleep_udf= new plugin::Create_function<Item_func_sleep>("sleep");
 
133
  registry.add(sleep_udf);
 
134
 
 
135
  return 0;
 
136
}
 
137
 
 
138
static int sleep_plugin_deinit(drizzled::plugin::Registry &registry)
 
139
{
 
140
  registry.remove(sleep_udf);
 
141
  delete sleep_udf;
 
142
 
 
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;