~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/sleep/sleep.cc

Reverted 1103

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 (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
 
 
35
 
#include <string>
36
 
 
37
 
using namespace std;
38
 
using namespace drizzled;
39
 
 
40
 
 
41
 
class Item_func_sleep : public Item_int_func
42
 
{
43
 
  /* for thread-safe sleep() */
44
 
  pthread_mutex_t LOCK_sleep;
45
 
 
46
 
public:
47
 
  int64_t val_int();
48
 
  Item_func_sleep() : Item_int_func()
49
 
  {
50
 
    unsigned_flag= true;
51
 
  }
52
 
 
53
 
  const char *func_name() const
54
 
  {
55
 
    return "sleep";
56
 
  }
57
 
 
58
 
  void fix_length_and_dec()
59
 
  {
60
 
    max_length= 1;
61
 
  }
62
 
 
63
 
  bool check_argument_count(int n)
64
 
  {
65
 
    return (n == 1);
66
 
  }
67
 
 
68
 
};
69
 
 
70
 
int64_t Item_func_sleep::val_int()
71
 
{
72
 
  /* int time in seconds, decimal allowed */
73
 
  double dtime;
74
 
 
75
 
  Session &session(getSession());
76
 
 
77
 
  if ((arg_count != 1) || ! (dtime= args[0]->val_real()))
78
 
  {
79
 
    null_value= true;
80
 
    return 0;
81
 
  }
82
 
 
83
 
  /*
84
 
    On 64-bit OSX pthread_cond_timedwait() waits forever
85
 
    if passed abstime time has already been exceeded by 
86
 
    the system time.
87
 
    When given a very short timeout (< 10 mcs) just return 
88
 
    immediately.
89
 
    We assume that the lines between this test and the call 
90
 
    to pthread_cond_timedwait() will be executed in less than 0.00001 sec.
91
 
  */
92
 
  if (dtime < 0.00001)
93
 
    return 0;
94
 
 
95
 
  {
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)
105
 
    {
106
 
      my_error(drizzled::ER_QUERY_INTERRUPTED, MYF(0));
107
 
      null_value= true;
108
 
 
109
 
      return 0;
110
 
    }
111
 
  }
112
 
 
113
 
 
114
 
  null_value= false;
115
 
 
116
 
  return (int64_t) 0;
117
 
}
118
 
 
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);