~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/sleep/sleep.cc

Merge Stewart's dead code removal

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 * Copyright (C) 2009 Sun Microsystems, Inc.
 
4
 * Copyright 2009 Sun Microsystems
5
5
 *
6
6
 * Authors:
7
7
 *
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
23
23
 */
24
 
 
25
 
#include "config.h"
26
24
 
27
25
#include <unistd.h>
28
26
#include <time.h>
29
27
 
 
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>
34
33
 
35
34
#include <string>
69
68
 
70
69
int64_t Item_func_sleep::val_int()
71
70
{
 
71
  int error= 0;
 
72
 
72
73
  /* int time in seconds, decimal allowed */
73
74
  double dtime;
74
75
 
75
 
  Session &session(getSession());
 
76
  struct timespec abstime;
 
77
 
 
78
  pthread_cond_t cond;
 
79
 
 
80
  Session *session= current_session;
76
81
 
77
82
  if ((arg_count != 1) || ! (dtime= args[0]->val_real()))
78
83
  {
92
97
  if (dtime < 0.00001)
93
98
    return 0;
94
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)
95
109
  {
96
 
    boost::this_thread::restore_interruption dl(session.getThreadInterupt());
97
 
 
98
 
    try {
99
 
      boost::xtime xt; 
100
 
      xtime_get(&xt, boost::TIME_UTC); 
101
 
      xt.nsec += (uint64_t)(dtime * 1000000000ULL); 
102
 
      session.getThread()->sleep(xt);
103
 
    }
104
 
    catch(boost::thread_interrupted const& error)
 
110
    error= pthread_cond_timedwait(&cond, &LOCK_sleep, &abstime);
 
111
    if (error == ETIMEDOUT || error == ETIME)
105
112
    {
106
 
      my_error(drizzled::ER_QUERY_INTERRUPTED, MYF(0));
107
 
      null_value= true;
108
 
 
109
 
      return 0;
 
113
      break;
110
114
    }
 
115
    error= 0;
111
116
  }
 
117
  pthread_mutex_unlock(&LOCK_sleep);
112
118
 
 
119
  /* relenquish pthread cond */
 
120
  pthread_cond_destroy(&cond);
 
121
  pthread_mutex_destroy(&LOCK_sleep);
113
122
 
114
123
  null_value= false;
115
124
 
116
125
  return (int64_t) 0;
117
126
}
118
127
 
119
 
static int sleep_plugin_init(drizzled::module::Context &context)
120
 
{
121
 
  context.add(new plugin::Create_function<Item_func_sleep>("sleep"));
122
 
 
123
 
  return 0;
124
 
}
125
 
 
126
 
DRIZZLE_PLUGIN(sleep_plugin_init, NULL, NULL);
 
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;