~drizzle-trunk/drizzle/development

1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 * Copyright (C) 2009 Sun Microsystems, Inc.
1103.7.11 by Patrick Galbraith
Ok, sorry for confusion on license issues. sleep is derived, so needs
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
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
23
 */
1241.13.6 by Monty Taylor
Fixed solaris build.
24
 
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
25
#include <config.h>
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
26
27
#include <unistd.h>
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
28
#include <time.h>
29
30
#include <drizzled/session.h>
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
31
#include <drizzled/item/func.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
32
#include <drizzled/internal/my_pthread.h>
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
33
#include <drizzled/function/str/strfunc.h>
2198.1.2 by Olaf van der Spek
Refactor includes
34
#include <drizzled/plugin/function.h>
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
35
36
#include <string>
37
38
using namespace std;
1103.7.6 by Patrick Galbraith
Merge with main branch, modification to sleep plugin
39
using namespace drizzled;
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
40
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
41
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
42
class Item_func_sleep : public Item_int_func
43
{
1103.7.12 by Patrick Galbraith
Changed LOCK_sleep to be a private, class member, making
44
  /* for thread-safe sleep() */
45
  pthread_mutex_t LOCK_sleep;
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
46
47
public:
48
  int64_t val_int();
49
  Item_func_sleep() : Item_int_func()
50
  {
51
    unsigned_flag= true;
52
  }
53
54
  const char *func_name() const
55
  {
56
    return "sleep";
57
  }
58
1103.7.12 by Patrick Galbraith
Changed LOCK_sleep to be a private, class member, making
59
  void fix_length_and_dec()
60
  {
1103.7.8 by Patrick Galbraith
Some changes per review
61
    max_length= 1;
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
62
  }
63
64
  bool check_argument_count(int n)
65
  {
66
    return (n == 1);
67
  }
68
69
};
70
71
int64_t Item_func_sleep::val_int()
72
{
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
73
  /* int time in seconds, decimal allowed */
74
  double dtime;
75
1963.2.2 by Brian Aker
Fix sleep() so that a kill command will kill it.
76
  Session &session(getSession());
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
77
78
  if ((arg_count != 1) || ! (dtime= args[0]->val_real()))
79
  {
80
    null_value= true;
81
    return 0;
82
  }
83
84
  /*
85
    On 64-bit OSX pthread_cond_timedwait() waits forever
86
    if passed abstime time has already been exceeded by 
87
    the system time.
88
    When given a very short timeout (< 10 mcs) just return 
89
    immediately.
90
    We assume that the lines between this test and the call 
91
    to pthread_cond_timedwait() will be executed in less than 0.00001 sec.
92
  */
93
  if (dtime < 0.00001)
94
    return 0;
95
96
  {
1963.2.2 by Brian Aker
Fix sleep() so that a kill command will kill it.
97
    boost::this_thread::restore_interruption dl(session.getThreadInterupt());
98
99
    try {
100
      boost::xtime xt; 
101
      xtime_get(&xt, boost::TIME_UTC); 
102
      xt.nsec += (uint64_t)(dtime * 1000000000ULL); 
103
      session.getThread()->sleep(xt);
104
    }
105
    catch(boost::thread_interrupted const& error)
1103.7.5 by Patrick Galbraith
Style fixes, removed non-needed stuffs
106
    {
1963.2.2 by Brian Aker
Fix sleep() so that a kill command will kill it.
107
      my_error(drizzled::ER_QUERY_INTERRUPTED, MYF(0));
108
      null_value= true;
109
110
      return 0;
1103.7.5 by Patrick Galbraith
Style fixes, removed non-needed stuffs
111
    }
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
112
  }
113
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
114
1103.7.6 by Patrick Galbraith
Merge with main branch, modification to sleep plugin
115
  null_value= false;
1154.1.2 by Brian Aker
Patrick + leak fix +test fix by Brian
116
1103.7.6 by Patrick Galbraith
Merge with main branch, modification to sleep plugin
117
  return (int64_t) 0;
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
118
}
119
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
120
static int sleep_plugin_init(drizzled::module::Context &context)
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
121
{
1963.2.2 by Brian Aker
Fix sleep() so that a kill command will kill it.
122
  context.add(new plugin::Create_function<Item_func_sleep>("sleep"));
1154.1.2 by Brian Aker
Patrick + leak fix +test fix by Brian
123
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
124
  return 0;
125
}
126
1633.6.2 by Vijay Samuel
Reverted changes.
127
DRIZZLE_PLUGIN(sleep_plugin_init, NULL, NULL);