~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
 *
1103.7.11 by Patrick Galbraith
Ok, sorry for confusion on license issues. sleep is derived, so needs
4
 * Copyright 2009 Sun Microsystems
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
 
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
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>
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
32
#include "drizzled/internal/my_pthread.h"
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
33
#include <drizzled/function/str/strfunc.h>
34
35
#include <string>
36
37
using namespace std;
1103.7.6 by Patrick Galbraith
Merge with main branch, modification to sleep plugin
38
using namespace drizzled;
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
39
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
40
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
41
class Item_func_sleep : public Item_int_func
42
{
1103.7.12 by Patrick Galbraith
Changed LOCK_sleep to be a private, class member, making
43
  /* for thread-safe sleep() */
44
  pthread_mutex_t LOCK_sleep;
1103.7.1 by Patrick Galbraith
Drizzle can now 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
1103.7.12 by Patrick Galbraith
Changed LOCK_sleep to be a private, class member, making
58
  void fix_length_and_dec()
59
  {
1103.7.8 by Patrick Galbraith
Some changes per review
60
    max_length= 1;
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
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
{
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
72
  int error= 0;
73
74
  /* int time in seconds, decimal allowed */
75
  double dtime;
76
77
  struct timespec abstime;
78
79
  pthread_cond_t cond;
80
81
  Session *session= current_session;
82
83
  if ((arg_count != 1) || ! (dtime= args[0]->val_real()))
84
  {
85
    null_value= true;
86
    return 0;
87
  }
88
89
  /*
90
    On 64-bit OSX pthread_cond_timedwait() waits forever
91
    if passed abstime time has already been exceeded by 
92
    the system time.
93
    When given a very short timeout (< 10 mcs) just return 
94
    immediately.
95
    We assume that the lines between this test and the call 
96
    to pthread_cond_timedwait() will be executed in less than 0.00001 sec.
97
  */
98
  if (dtime < 0.00001)
99
    return 0;
100
101
  /* need to obtain time value for passing to cond_timedwait */
102
  set_timespec_nsec(abstime, (uint64_t)(dtime * 1000000000ULL));
103
1154.1.2 by Brian Aker
Patrick + leak fix +test fix by Brian
104
  pthread_mutex_init(&LOCK_sleep, MY_MUTEX_INIT_FAST);
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
105
  pthread_cond_init(&cond, NULL);
1154.1.2 by Brian Aker
Patrick + leak fix +test fix by Brian
106
107
  /* don't run if not killed */
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
108
  pthread_mutex_lock(&LOCK_sleep);
1910.2.8 by Brian Aker
Enapsulate Kill.
109
  while (not session->getKilled())
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
110
  {
111
    error= pthread_cond_timedwait(&cond, &LOCK_sleep, &abstime);
112
    if (error == ETIMEDOUT || error == ETIME)
1103.7.5 by Patrick Galbraith
Style fixes, removed non-needed stuffs
113
    {
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
114
      break;
1103.7.5 by Patrick Galbraith
Style fixes, removed non-needed stuffs
115
    }
1103.7.3 by Patrick Galbraith
So, I did say I would get to sleep() on the the plane to Seattle ;)
116
    error= 0;
117
  }
118
  pthread_mutex_unlock(&LOCK_sleep);
119
120
  /* relenquish pthread cond */
121
  pthread_cond_destroy(&cond);
1154.1.2 by Brian Aker
Patrick + leak fix +test fix by Brian
122
  pthread_mutex_destroy(&LOCK_sleep);
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
123
1103.7.6 by Patrick Galbraith
Merge with main branch, modification to sleep plugin
124
  null_value= false;
1154.1.2 by Brian Aker
Patrick + leak fix +test fix by Brian
125
1103.7.6 by Patrick Galbraith
Merge with main branch, modification to sleep plugin
126
  return (int64_t) 0;
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
127
}
128
1103.7.6 by Patrick Galbraith
Merge with main branch, modification to sleep plugin
129
plugin::Create_function<Item_func_sleep> *sleep_udf= NULL;
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
130
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
131
static int sleep_plugin_init(drizzled::module::Context &context)
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
132
{
1103.7.6 by Patrick Galbraith
Merge with main branch, modification to sleep plugin
133
  sleep_udf= new plugin::Create_function<Item_func_sleep>("sleep");
1324.2.2 by Monty Taylor
Use the plugin::Context everywhere.
134
  context.add(sleep_udf);
1154.1.2 by Brian Aker
Patrick + leak fix +test fix by Brian
135
1103.7.1 by Patrick Galbraith
Drizzle can now sleep!
136
  return 0;
137
}
138
1633.6.2 by Vijay Samuel
Reverted changes.
139
DRIZZLE_PLUGIN(sleep_plugin_init, NULL, NULL);