summaryrefslogtreecommitdiffstats
path: root/sql/scheduler.cc
blob: 7261c5f39d79e3e8f9cd508b672f411b2e6d58a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* Copyright (c) 2007, 2013, Oracle and/or its affiliates.
   Copyright (c) 2012, 2014, SkySQL Ab.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */

/*
  Implementation for the thread scheduler
*/

#ifdef USE_PRAGMA_INTERFACE
#pragma implementation
#endif

#include "mariadb.h"
#include "mysqld.h"
#include "scheduler.h"
#include "sql_class.h"
#include "sql_callback.h"
#include <violite.h>

/** @internal
  Helper functions to allow mysys to call the thread scheduler when
  waiting for locks.
*/

/**@{*/
extern "C"
{
static void scheduler_wait_lock_begin(void) { 
  thd_wait_begin(NULL, THD_WAIT_TABLE_LOCK);
}

static void scheduler_wait_lock_end(void) {
  thd_wait_end(NULL);
}

static void scheduler_wait_sync_begin(void) {
  thd_wait_begin(NULL, THD_WAIT_SYNC);
}

static void scheduler_wait_sync_end(void) {
  thd_wait_end(NULL);
}

static void scheduler_wait_net_begin(void) {
   thd_wait_begin(NULL, THD_WAIT_NET);
}

static void scheduler_wait_net_end(void) {
   thd_wait_end(NULL);
}

};
/**@}*/

/**
  Common scheduler init function.

  The scheduler is either initialized by calling
  one_thread_scheduler() or one_thread_per_connection_scheduler() in
  mysqld.cc, so this init function will always be called.
 */

void scheduler_init()
{
  thr_set_lock_wait_callback(scheduler_wait_lock_begin,
                             scheduler_wait_lock_end);
  thr_set_sync_wait_callback(scheduler_wait_sync_begin,
                             scheduler_wait_sync_end);

  vio_set_wait_callback(scheduler_wait_net_begin,
    scheduler_wait_net_end);
}


/**
  Kill notification callback,  used by  one-thread-per-connection
  and threadpool scheduler.

  Wakes up a thread that is stuck in read/poll/epoll/event-poll 
  routines used by threadpool, such that subsequent attempt to 
  read from  client connection will result in IO error.
*/

void post_kill_notification(THD *thd)
{
  DBUG_ENTER("post_kill_notification");
  if (current_thd == thd || thd->system_thread)
    DBUG_VOID_RETURN;

  if (thd->net.vio)
    vio_shutdown(thd->net.vio, SHUT_RD);
  DBUG_VOID_RETURN;
}

/*
  Initialize scheduler for --thread-handling=one-thread-per-connection
*/

#ifndef EMBEDDED_LIBRARY

void one_thread_per_connection_scheduler(scheduler_functions *func,
    ulong *arg_max_connections,
    Atomic_counter<uint> *arg_connection_count)
{
  scheduler_init();
  func->max_threads= *arg_max_connections + 1;
  func->max_connections= arg_max_connections;
  func->connection_count= arg_connection_count;
  func->add_connection= create_thread_to_handle_connection;
  func->post_kill_notification= post_kill_notification;
}
#else
void handle_connection_in_main_thread(CONNECT *connect)
{
}
#endif

/*
  Initialize scheduler for --thread-handling=no-threads
*/

void one_thread_scheduler(scheduler_functions *func,
    Atomic_counter<uint> *arg_connection_count)
{
  scheduler_init();
  func->max_threads= 1;
  func->max_connections= &max_connections;
  func->connection_count= arg_connection_count;
  func->add_connection= handle_connection_in_main_thread;
}