~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/sleep/sleep.cc

  • Committer: Monty Taylor
  • Date: 2010-02-05 08:11:15 UTC
  • mfrom: (1283 build)
  • mto: (1273.13.43 fix_is)
  • mto: This revision was merged to the branch mainline in revision 1300.
  • Revision ID: mordred@inaugust.com-20100205081115-dr82nvrwv4lvw7sd
Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
69
69
 
70
70
int64_t Item_func_sleep::val_int()
71
71
{
 
72
  int error= 0;
 
73
 
72
74
  /* int time in seconds, decimal allowed */
73
75
  double dtime;
74
76
 
75
 
  Session &session(getSession());
 
77
  struct timespec abstime;
 
78
 
 
79
  pthread_cond_t cond;
 
80
 
 
81
  Session *session= current_session;
76
82
 
77
83
  if ((arg_count != 1) || ! (dtime= args[0]->val_real()))
78
84
  {
92
98
  if (dtime < 0.00001)
93
99
    return 0;
94
100
 
 
101
  /* need to obtain time value for passing to cond_timedwait */
 
102
  set_timespec_nsec(abstime, (uint64_t)(dtime * 1000000000ULL));
 
103
 
 
104
  pthread_mutex_init(&LOCK_sleep, MY_MUTEX_INIT_FAST);
 
105
  pthread_cond_init(&cond, NULL);
 
106
 
 
107
  /* don't run if not killed */
 
108
  pthread_mutex_lock(&LOCK_sleep);
 
109
  while (! session->killed)
95
110
  {
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)
 
111
    error= pthread_cond_timedwait(&cond, &LOCK_sleep, &abstime);
 
112
    if (error == ETIMEDOUT || error == ETIME)
105
113
    {
106
 
      my_error(drizzled::ER_QUERY_INTERRUPTED, MYF(0));
107
 
      null_value= true;
108
 
 
109
 
      return 0;
 
114
      break;
110
115
    }
 
116
    error= 0;
111
117
  }
 
118
  pthread_mutex_unlock(&LOCK_sleep);
112
119
 
 
120
  /* relenquish pthread cond */
 
121
  pthread_cond_destroy(&cond);
 
122
  pthread_mutex_destroy(&LOCK_sleep);
113
123
 
114
124
  null_value= false;
115
125
 
116
126
  return (int64_t) 0;
117
127
}
118
128
 
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);
 
129
plugin::Create_function<Item_func_sleep> *sleep_udf= NULL;
 
130
 
 
131
static int sleep_plugin_init(drizzled::plugin::Registry &registry)
 
132
{
 
133
  sleep_udf= new plugin::Create_function<Item_func_sleep>("sleep");
 
134
  registry.add(sleep_udf);
 
135
 
 
136
  return 0;
 
137
}
 
138
 
 
139
static int sleep_plugin_deinit(drizzled::plugin::Registry &registry)
 
140
{
 
141
  registry.remove(sleep_udf);
 
142
  delete sleep_udf;
 
143
 
 
144
  return 0;
 
145
}
 
146
 
 
147
 
 
148
DRIZZLE_PLUGIN(sleep_plugin_init, sleep_plugin_deinit, NULL, NULL);