~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/scheduler.cc

  • Committer: Brian Aker
  • Date: 2008-07-13 22:45:08 UTC
  • Revision ID: brian@tangent.org-20080713224508-hb20z4okblotb39a
longlong replacement

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
  Implementation for the thread scheduler
18
18
*/
19
19
 
20
 
#include <drizzled/server_includes.h>
21
 
#include <libdrizzle/libdrizzle.h>
 
20
#ifdef USE_PRAGMA_INTERFACE
 
21
#pragma implementation
 
22
#endif
 
23
 
 
24
#include <mysql_priv.h>
22
25
#include "event.h"
23
26
 
24
27
 
28
31
 */
29
32
 
30
33
static bool init_dummy(void) {return 0;}
31
 
static void post_kill_dummy(THD *thd __attribute__((unused))) {}
 
34
static void post_kill_dummy(THD *thd __attribute__((__unused__))) {}
32
35
static void end_dummy(void) {}
33
 
static bool end_thread_dummy(THD *thd __attribute__((unused)),
34
 
                             bool cache_thread __attribute__((unused)))
 
36
static bool end_thread_dummy(THD *thd __attribute__((__unused__)),
 
37
                             bool cache_thread __attribute__((__unused__)))
35
38
{ return 0; }
36
39
 
37
40
/*
47
50
   end_thread(end_thread_dummy), end(end_dummy)
48
51
{}
49
52
 
50
 
static uint32_t created_threads, killed_threads;
 
53
static uint created_threads, killed_threads;
51
54
static bool kill_pool_threads;
52
55
 
53
56
static struct event thd_add_event;
108
111
 
109
112
thd_scheduler::~thd_scheduler()
110
113
{
111
 
  free(io_event);
 
114
  my_free(io_event, MYF(MY_ALLOW_ZERO_PTR));
112
115
}
113
116
 
114
117
 
119
122
    
120
123
  if (!io_event)
121
124
  {
122
 
    sql_print_error(_("Memory allocation error in thd_scheduler::init\n"));
 
125
    sql_print_error("Memory allocation error in thd_scheduler::init\n");
123
126
    return true;
124
127
  }
125
128
  
126
 
  event_set(io_event, net_get_sd(&(parent_thd->net)), EV_READ, 
 
129
  event_set(io_event, parent_thd->net.vio->sd, EV_READ, 
127
130
            libevent_io_callback, (void*)parent_thd);
128
131
    
129
132
  list.data= parent_thd;
196
199
 
197
200
static bool libevent_init(void)
198
201
{
199
 
  uint32_t i;
 
202
  uint i;
200
203
 
201
204
  event_init();
202
205
  
210
213
  /* set up the pipe used to add new thds to the event pool */
211
214
  if (init_pipe(thd_add_pipe))
212
215
  {
213
 
    sql_print_error(_("init_pipe(thd_add_pipe) error in libevent_init\n"));
 
216
    sql_print_error("init_pipe(thd_add_pipe) error in libevent_init\n");
214
217
    return(1);
215
218
  }
216
219
  /* set up the pipe used to kill thds in the event queue */
217
220
  if (init_pipe(thd_kill_pipe))
218
221
  {
219
 
    sql_print_error(_("init_pipe(thd_kill_pipe) error in libevent_init\n"));
 
222
    sql_print_error("init_pipe(thd_kill_pipe) error in libevent_init\n");
220
223
    close(thd_add_pipe[0]);
221
224
    close(thd_add_pipe[1]);
222
225
    return(1);
228
231
 
229
232
 if (event_add(&thd_add_event, NULL) || event_add(&thd_kill_event, NULL))
230
233
 {
231
 
   sql_print_error(_("thd_add_event event_add error in libevent_init\n"));
 
234
   sql_print_error("thd_add_event event_add error in libevent_init\n");
232
235
   libevent_end();
233
236
   return(1);
234
237
   
244
247
    if ((error= pthread_create(&thread, &connection_attrib,
245
248
                               libevent_thread_proc, 0)))
246
249
    {
247
 
      sql_print_error(_("Can't create completion port thread (error %d)"),
 
250
      sql_print_error("Can't create completion port thread (error %d)",
248
251
                      error);
249
252
      pthread_mutex_unlock(&LOCK_thread_count);
250
253
      libevent_end();                      // Cleanup
357
360
      /* Add to libevent */
358
361
      if (event_add(thd->scheduler.io_event, NULL))
359
362
      {
360
 
        sql_print_error(_("event_add error in libevent_add_thd_callback\n"));
 
363
        sql_print_error("event_add error in libevent_add_thd_callback\n");
361
364
        libevent_connection_close(thd);
362
365
      } 
363
366
      else
383
386
{
384
387
  if (thd->scheduler.init(thd))
385
388
  {
386
 
    sql_print_error(_("Scheduler init error in libevent_add_new_connection\n"));
 
389
    sql_print_error("Scheduler init error in libevent_add_new_connection\n");
387
390
    pthread_mutex_unlock(&LOCK_thread_count);
388
391
    libevent_connection_close(thd);
389
392
    return;
430
433
{
431
434
  thd->killed= THD::KILL_CONNECTION;          // Avoid error messages
432
435
 
433
 
  if (net_get_sd(&(thd->net)) >= 0)                  // not already closed
 
436
  if (thd->net.vio->sd >= 0)                  // not already closed
434
437
  {
435
438
    end_connection(thd);
436
439
    close_connection(thd, 0, 1);
449
452
 
450
453
static bool libevent_should_close_connection(THD* thd)
451
454
{
452
 
  return net_should_close(&(thd->net)) ||
 
455
  return thd->net.error ||
 
456
         thd->net.vio == 0 ||
453
457
         thd->killed == THD::KILL_CONNECTION;
454
458
}
455
459
 
459
463
  These procs only return/terminate on shutdown (kill_pool_threads == true).
460
464
*/
461
465
 
462
 
pthread_handler_t libevent_thread_proc(void *arg __attribute__((unused)))
 
466
pthread_handler_t libevent_thread_proc(void *arg __attribute__((__unused__)))
463
467
{
464
468
  if (init_new_connection_handler_thread())
465
469
  {
466
470
    my_thread_global_end();
467
 
    sql_print_error(_("libevent_thread_proc: my_thread_init() failed\n"));
 
471
    sql_print_error("libevent_thread_proc: my_thread_init() failed\n");
468
472
    exit(1);
469
473
  }
470
474
 
572
576
    Note: we cannot add for event processing because the whole request might
573
577
    already be buffered and we wouldn't receive an event.
574
578
  */
575
 
  if (net_more_data(&(thd->net)))
 
579
  if (thd->net.vio == 0 || thd->net.vio->read_pos < thd->net.vio->read_end)
576
580
    return true;
577
581
  
578
582
  thd->scheduler.thread_detach();