~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/sleep/sleep.cc

  • Committer: Brian Aker
  • Date: 2010-11-11 18:59:06 UTC
  • mto: This revision was merged to the branch mainline in revision 1927.
  • Revision ID: brian@tangent.org-20101111185906-at4lyjpv9aewg3fz
Encapsulate temporary tables.

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
 *
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 (not session->getKilled())
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
 
 
129
plugin::Create_function<Item_func_sleep> *sleep_udf= NULL;
 
130
 
119
131
static int sleep_plugin_init(drizzled::module::Context &context)
120
132
{
121
 
  context.add(new plugin::Create_function<Item_func_sleep>("sleep"));
 
133
  sleep_udf= new plugin::Create_function<Item_func_sleep>("sleep");
 
134
  context.add(sleep_udf);
122
135
 
123
136
  return 0;
124
137
}