que0que.c 34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*****************************************************************************

Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.

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., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

*****************************************************************************/

19 20
/**************************************************//**
@file que/que0que.c
osku's avatar
osku committed
21 22 23 24 25 26
Query graph

Created 5/27/1996 Heikki Tuuri
*******************************************************/

#include "que0que.h"
27

osku's avatar
osku committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#ifdef UNIV_NONINL
#include "que0que.ic"
#endif

#include "usr0sess.h"
#include "trx0trx.h"
#include "trx0roll.h"
#include "row0undo.h"
#include "row0ins.h"
#include "row0upd.h"
#include "row0sel.h"
#include "row0purge.h"
#include "dict0crea.h"
#include "log0log.h"
#include "eval0proc.h"
#include "eval0eval.h"
44
#include "pars0types.h"
osku's avatar
osku committed
45 46 47 48 49

#define QUE_PARALLELIZE_LIMIT	(64 * 256 * 256 * 256)
#define QUE_ROUND_ROBIN_LIMIT	(64 * 256 * 256 * 256)
#define QUE_MAX_LOOPS_WITHOUT_CHECK	16

50
#ifdef UNIV_DEBUG
osku's avatar
osku committed
51 52
/* If the following flag is set TRUE, the module will print trace info
of SQL execution in the UNIV_SQL_DEBUG version */
53 54
UNIV_INTERN ibool	que_trace_on		= FALSE;
#endif /* UNIV_DEBUG */
osku's avatar
osku committed
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
/* Short introduction to query graphs
   ==================================

A query graph consists of nodes linked to each other in various ways. The
execution starts at que_run_threads() which takes a que_thr_t parameter.
que_thr_t contains two fields that control query graph execution: run_node
and prev_node. run_node is the next node to execute and prev_node is the
last node executed.

Each node has a pointer to a 'next' statement, i.e., its brother, and a
pointer to its parent node. The next pointer is NULL in the last statement
of a block.

Loop nodes contain a link to the first statement of the enclosed statement
list. While the loop runs, que_thr_step() checks if execution to the loop
node came from its parent or from one of the statement nodes in the loop. If
it came from the parent of the loop node it starts executing the first
statement node in the loop. If it came from one of the statement nodes in
the loop, then it checks if the statement node has another statement node
following it, and runs it if so.

To signify loop ending, the loop statements (see e.g. while_step()) set
que_thr_t->run_node to the loop node's parent node. This is noticed on the
next call of que_thr_step() and execution proceeds to the node pointed to by
the loop node's 'next' pointer.

For example, the code:

X := 1;
WHILE X < 5 LOOP
 X := X + 1;
 X := X + 1;
X := 5

will result in the following node hierarchy, with the X-axis indicating
'next' links and the Y-axis indicating parent/child links:

A - W - A
    |
    |
    A - A

A = assign_node_t, W = while_node_t. */

osku's avatar
osku committed
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
/* How a stored procedure containing COMMIT or ROLLBACK commands
is executed?

The commit or rollback can be seen as a subprocedure call.
The problem is that if there are several query threads
currently running within the transaction, their action could
mess the commit or rollback operation. Or, at the least, the
operation would be difficult to visualize and keep in control.

Therefore the query thread requesting a commit or a rollback
sends to the transaction a signal, which moves the transaction
to TRX_QUE_SIGNALED state. All running query threads of the
transaction will eventually notice that the transaction is now in
this state and voluntarily suspend themselves. Only the last
query thread which suspends itself will trigger handling of
the signal.

When the transaction starts to handle a rollback or commit
signal, it builds a query graph which, when executed, will
roll back or commit the incomplete transaction. The transaction
is moved to the TRX_QUE_ROLLING_BACK or TRX_QUE_COMMITTING state.
If specified, the SQL cursors opened by the transaction are closed.
When the execution of the graph completes, it is like returning
from a subprocedure: the query thread which requested the operation
starts running again. */

126
/**********************************************************************//**
osku's avatar
osku committed
127 128 129 130 131 132 133 134
Moves a thread from another state to the QUE_THR_RUNNING state. Increments
the n_active_thrs counters of the query graph and transaction.
***NOTE***: This is the only function in which such a transition is allowed
to happen! */
static
void
que_thr_move_to_run_state(
/*======================*/
135
	que_thr_t*	thr);	/*!< in: an query thread */
osku's avatar
osku committed
136

137
/***********************************************************************//**
osku's avatar
osku committed
138
Adds a query graph to the session's list of graphs. */
139
UNIV_INTERN
osku's avatar
osku committed
140 141 142
void
que_graph_publish(
/*==============*/
143 144
	que_t*	graph,	/*!< in: graph */
	sess_t*	sess)	/*!< in: session */
osku's avatar
osku committed
145 146 147 148 149 150
{
	ut_ad(mutex_own(&kernel_mutex));

	UT_LIST_ADD_LAST(graphs, sess->graphs, graph);
}

151
/***********************************************************************//**
152 153
Creates a query graph fork node.
@return	own: fork node */
154
UNIV_INTERN
osku's avatar
osku committed
155 156 157
que_fork_t*
que_fork_create(
/*============*/
158
	que_t*		graph,		/*!< in: graph, if NULL then this
osku's avatar
osku committed
159 160
					fork node is assumed to be the
					graph root */
161 162 163
	que_node_t*	parent,		/*!< in: parent node */
	ulint		fork_type,	/*!< in: fork type */
	mem_heap_t*	heap)		/*!< in: memory heap where created */
osku's avatar
osku committed
164 165 166 167
{
	que_fork_t*	fork;

	ut_ad(heap);
168

osku's avatar
osku committed
169 170 171 172 173 174 175 176 177 178 179 180
	fork = mem_heap_alloc(heap, sizeof(que_fork_t));

	fork->common.type = QUE_NODE_FORK;
	fork->n_active_thrs = 0;

	fork->state = QUE_FORK_COMMAND_WAIT;

	if (graph != NULL) {
		fork->graph = graph;
	} else {
		fork->graph = fork;
	}
181

osku's avatar
osku committed
182 183 184 185 186 187 188 189
	fork->common.parent = parent;
	fork->fork_type = fork_type;

	fork->caller = NULL;

	UT_LIST_INIT(fork->thrs);

	fork->sym_tab = NULL;
190
	fork->info = NULL;
191

osku's avatar
osku committed
192
	fork->heap = heap;
193

osku's avatar
osku committed
194 195 196
	return(fork);
}

197
/***********************************************************************//**
198 199
Creates a query graph thread node.
@return	own: query thread node */
200
UNIV_INTERN
osku's avatar
osku committed
201 202 203
que_thr_t*
que_thr_create(
/*===========*/
204 205
	que_fork_t*	parent,	/*!< in: parent node, i.e., a fork node */
	mem_heap_t*	heap)	/*!< in: memory heap where created */
osku's avatar
osku committed
206 207
{
	que_thr_t*	thr;
208

osku's avatar
osku committed
209
	ut_ad(parent && heap);
210

osku's avatar
osku committed
211 212 213 214 215 216 217 218 219 220 221
	thr = mem_heap_alloc(heap, sizeof(que_thr_t));

	thr->common.type = QUE_NODE_THR;
	thr->common.parent = parent;

	thr->magic_n = QUE_THR_MAGIC_N;

	thr->graph = parent->graph;

	thr->state = QUE_THR_COMMAND_WAIT;

222
	thr->is_active = FALSE;
osku's avatar
osku committed
223 224 225

	thr->run_node = NULL;
	thr->resource = 0;
226
	thr->lock_state = QUE_THR_LOCK_NOLOCK;
osku's avatar
osku committed
227

228 229
	thr->slot = NULL;

osku's avatar
osku committed
230 231 232 233 234
	UT_LIST_ADD_LAST(thrs, parent->thrs, thr);

	return(thr);
}

235
/**********************************************************************//**
osku's avatar
osku committed
236 237 238 239
Moves a suspended query thread to the QUE_THR_RUNNING state and may release
a single worker thread to execute it. This function should be used to end
the wait state of a query thread waiting for a lock or a stored procedure
completion. */
240
UNIV_INTERN
osku's avatar
osku committed
241 242 243
void
que_thr_end_wait(
/*=============*/
244
	que_thr_t*	thr,		/*!< in: query thread in the
osku's avatar
osku committed
245 246 247
					QUE_THR_LOCK_WAIT,
					or QUE_THR_PROCEDURE_WAIT, or
					QUE_THR_SIG_REPLY_WAIT state */
248
	que_thr_t**	next_thr)	/*!< in/out: next query thread to run;
osku's avatar
osku committed
249 250 251 252 253 254 255 256 257 258 259
					if the value which is passed in is
					a pointer to a NULL pointer, then the
					calling function can start running
					a new query thread; if NULL is passed
					as the parameter, it is ignored */
{
	ibool	was_active;

	ut_ad(mutex_own(&kernel_mutex));
	ut_ad(thr);
	ut_ad((thr->state == QUE_THR_LOCK_WAIT)
260 261
	      || (thr->state == QUE_THR_PROCEDURE_WAIT)
	      || (thr->state == QUE_THR_SIG_REPLY_WAIT));
osku's avatar
osku committed
262 263 264 265 266
	ut_ad(thr->run_node);

	thr->prev_node = thr->run_node;

	was_active = thr->is_active;
267

osku's avatar
osku committed
268 269 270 271 272
	que_thr_move_to_run_state(thr);

	if (was_active) {

		return;
273
	}
osku's avatar
osku committed
274 275 276 277 278 279 280

	if (next_thr && *next_thr == NULL) {
		*next_thr = thr;
	} else {
		ut_a(0);
		srv_que_task_enqueue_low(thr);
	}
281
}
osku's avatar
osku committed
282

283
/**********************************************************************//**
osku's avatar
osku committed
284
Same as que_thr_end_wait, but no parameter next_thr available. */
285
UNIV_INTERN
osku's avatar
osku committed
286 287 288
void
que_thr_end_wait_no_next_thr(
/*=========================*/
289
	que_thr_t*	thr)	/*!< in: query thread in the QUE_THR_LOCK_WAIT,
osku's avatar
osku committed
290 291 292 293 294 295 296 297 298 299
				or QUE_THR_PROCEDURE_WAIT, or
				QUE_THR_SIG_REPLY_WAIT state */
{
	ibool	was_active;

	ut_a(thr->state == QUE_THR_LOCK_WAIT);	/* In MySQL this is the
						only possible state here */
	ut_ad(mutex_own(&kernel_mutex));
	ut_ad(thr);
	ut_ad((thr->state == QUE_THR_LOCK_WAIT)
300 301
	      || (thr->state == QUE_THR_PROCEDURE_WAIT)
	      || (thr->state == QUE_THR_SIG_REPLY_WAIT));
osku's avatar
osku committed
302 303

	was_active = thr->is_active;
304

osku's avatar
osku committed
305 306 307 308 309 310 311 312 313
	que_thr_move_to_run_state(thr);

	if (was_active) {

		return;
	}

	/* In MySQL we let the OS thread (not just the query thread) to wait
	for the lock to be released: */
314

315 316 317
	if (thr != NULL) {
		srv_release_mysql_thread_if_suspended(thr);
	}
osku's avatar
osku committed
318 319 320 321

	/* srv_que_task_enqueue_low(thr); */
}

322
/**********************************************************************//**
osku's avatar
osku committed
323 324 325 326 327
Inits a query thread for a command. */
UNIV_INLINE
void
que_thr_init_command(
/*=================*/
328
	que_thr_t*	thr)	/*!< in: query thread */
osku's avatar
osku committed
329 330 331 332 333 334 335
{
	thr->run_node = thr;
	thr->prev_node = thr->common.parent;

	que_thr_move_to_run_state(thr);
}

336
/**********************************************************************//**
osku's avatar
osku committed
337 338 339
Starts execution of a command in a query fork. Picks a query thread which
is not in the QUE_THR_RUNNING state and moves it to that state. If none
can be chosen, a situation which may arise in parallelized fetches, NULL
340
is returned.
341 342 343
@return a query thread of the graph moved to QUE_THR_RUNNING state, or
NULL; the query thread should be executed by que_run_threads by the
caller */
344
UNIV_INTERN
osku's avatar
osku committed
345 346 347
que_thr_t*
que_fork_start_command(
/*===================*/
348
	que_fork_t*	fork)	/*!< in: a query fork */
osku's avatar
osku committed
349 350
{
	que_thr_t*	thr;
351 352
	que_thr_t*	suspended_thr = NULL;
	que_thr_t*	completed_thr = NULL;
osku's avatar
osku committed
353 354

	fork->state = QUE_FORK_ACTIVE;
355

osku's avatar
osku committed
356 357
	fork->last_sel_node = NULL;

358 359 360
	suspended_thr = NULL;
	completed_thr = NULL;

osku's avatar
osku committed
361 362 363 364
	/* Choose the query thread to run: usually there is just one thread,
	but in a parallelized select, which necessarily is non-scrollable,
	there may be several to choose from */

365 366 367 368
	/* First we try to find a query thread in the QUE_THR_COMMAND_WAIT
	state. Then we try to find a query thread in the QUE_THR_SUSPENDED
	state, finally we try to find a query thread in the QUE_THR_COMPLETED
	state */
369

osku's avatar
osku committed
370 371
	thr = UT_LIST_GET_FIRST(fork->thrs);

372 373 374 375 376
	/* We make a single pass over the thr list within which we note which
	threads are ready to run. */
	while (thr) {
		switch (thr->state) {
		case QUE_THR_COMMAND_WAIT:
osku's avatar
osku committed
377 378 379 380 381 382 383 384

			/* We have to send the initial message to query thread
			to start it */

			que_thr_init_command(thr);

			return(thr);

385
		case QUE_THR_SUSPENDED:
osku's avatar
osku committed
386 387 388
			/* In this case the execution of the thread was
			suspended: no initial message is needed because
			execution can continue from where it was left */
389 390 391
			if (!suspended_thr) {
				suspended_thr = thr;
			}
osku's avatar
osku committed
392

393 394 395 396 397 398 399 400 401 402 403
			break;

		case QUE_THR_COMPLETED:
			if (!completed_thr) {
				completed_thr = thr;
			}

			break;

		case QUE_THR_LOCK_WAIT:
			ut_error;
osku's avatar
osku committed
404 405 406 407 408 409

		}

		thr = UT_LIST_GET_NEXT(thrs, thr);
	}

410
	if (suspended_thr) {
411

412 413
		thr = suspended_thr;
		que_thr_move_to_run_state(thr);
osku's avatar
osku committed
414

415
	} else if (completed_thr) {
osku's avatar
osku committed
416

417 418
		thr = completed_thr;
		que_thr_init_command(thr);
osku's avatar
osku committed
419 420
	}

421
	return(thr);
osku's avatar
osku committed
422 423
}

424
/**********************************************************************//**
osku's avatar
osku committed
425 426 427
After signal handling is finished, returns control to a query graph error
handling routine. (Currently, just returns the control to the root of the
graph so that the graph can communicate an error message to the client.) */
428
UNIV_INTERN
osku's avatar
osku committed
429 430 431
void
que_fork_error_handle(
/*==================*/
432 433
	trx_t*	trx __attribute__((unused)),	/*!< in: trx */
	que_t*	fork)	/*!< in: query graph which was run before signal
osku's avatar
osku committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
			handling started, NULL not allowed */
{
	que_thr_t*	thr;

	ut_ad(mutex_own(&kernel_mutex));
	ut_ad(trx->sess->state == SESS_ERROR);
	ut_ad(UT_LIST_GET_LEN(trx->reply_signals) == 0);
	ut_ad(UT_LIST_GET_LEN(trx->wait_thrs) == 0);

	thr = UT_LIST_GET_FIRST(fork->thrs);

	while (thr != NULL) {
		ut_ad(!thr->is_active);
		ut_ad(thr->state != QUE_THR_SIG_REPLY_WAIT);
		ut_ad(thr->state != QUE_THR_LOCK_WAIT);
449

osku's avatar
osku committed
450 451 452
		thr->run_node = thr;
		thr->prev_node = thr->child;
		thr->state = QUE_THR_COMPLETED;
453

osku's avatar
osku committed
454 455 456 457
		thr = UT_LIST_GET_NEXT(thrs, thr);
	}

	thr = UT_LIST_GET_FIRST(fork->thrs);
458

osku's avatar
osku committed
459 460 461 462 463 464
	que_thr_move_to_run_state(thr);

	ut_a(0);
	srv_que_task_enqueue_low(thr);
}

465
/****************************************************************//**
466
Tests if all the query threads in the same fork have a given state.
467 468
@return TRUE if all the query threads in the same fork were in the
given state */
osku's avatar
osku committed
469 470 471 472
UNIV_INLINE
ibool
que_fork_all_thrs_in_state(
/*=======================*/
473 474
	que_fork_t*	fork,	/*!< in: query fork */
	ulint		state)	/*!< in: state */
osku's avatar
osku committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
{
	que_thr_t*	thr_node;

	thr_node = UT_LIST_GET_FIRST(fork->thrs);

	while (thr_node != NULL) {
		if (thr_node->state != state) {

			return(FALSE);
		}

		thr_node = UT_LIST_GET_NEXT(thrs, thr_node);
	}

	return(TRUE);
}

492
/**********************************************************************//**
osku's avatar
osku committed
493 494 495 496 497
Calls que_graph_free_recursive for statements in a statement list. */
static
void
que_graph_free_stat_list(
/*=====================*/
498
	que_node_t*	node)	/*!< in: first query graph node in the list */
osku's avatar
osku committed
499 500 501 502 503 504 505 506
{
	while (node) {
		que_graph_free_recursive(node);

		node = que_node_get_next(node);
	}
}

507
/**********************************************************************//**
osku's avatar
osku committed
508 509
Frees a query graph, but not the heap where it was created. Does not free
explicit cursor declarations, they are freed in que_graph_free. */
510
UNIV_INTERN
osku's avatar
osku committed
511 512 513
void
que_graph_free_recursive(
/*=====================*/
514
	que_node_t*	node)	/*!< in: query graph node */
osku's avatar
osku committed
515 516 517 518 519 520 521 522 523
{
	que_fork_t*	fork;
	que_thr_t*	thr;
	undo_node_t*	undo;
	sel_node_t*	sel;
	ins_node_t*	ins;
	upd_node_t*	upd;
	tab_node_t*	cre_tab;
	ind_node_t*	cre_ind;
524
	purge_node_t*	purge;
525

osku's avatar
osku committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
	if (node == NULL) {

		return;
	}

	switch (que_node_get_type(node)) {

	case QUE_NODE_FORK:
		fork = node;

		thr = UT_LIST_GET_FIRST(fork->thrs);

		while (thr) {
			que_graph_free_recursive(thr);

			thr = UT_LIST_GET_NEXT(thrs, thr);
		}

		break;
	case QUE_NODE_THR:

		thr = node;

		if (thr->magic_n != QUE_THR_MAGIC_N) {
			fprintf(stderr,
551 552
				"que_thr struct appears corrupt;"
				" magic n %lu\n",
osku's avatar
osku committed
553
				(unsigned long) thr->magic_n);
554
			mem_analyze_corruption(thr);
osku's avatar
osku committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
			ut_error;
		}

		thr->magic_n = QUE_THR_MAGIC_FREED;

		que_graph_free_recursive(thr->child);

		break;
	case QUE_NODE_UNDO:

		undo = node;

		mem_heap_free(undo->heap);

		break;
	case QUE_NODE_SELECT:

		sel = node;

		sel_node_free_private(sel);

		break;
	case QUE_NODE_INSERT:

		ins = node;

		que_graph_free_recursive(ins->select);

		mem_heap_free(ins->entry_sys_heap);

		break;
586 587 588 589 590 591 592
	case QUE_NODE_PURGE:
		purge = node;

		mem_heap_free(purge->heap);

		break;

osku's avatar
osku committed
593 594 595 596 597
	case QUE_NODE_UPDATE:

		upd = node;

		if (upd->in_mysql_interface) {
598

osku's avatar
osku committed
599 600 601
			btr_pcur_free_for_mysql(upd->pcur);
		}

602
		que_graph_free_recursive(upd->cascade_node);
osku's avatar
osku committed
603 604 605 606

		if (upd->cascade_heap) {
			mem_heap_free(upd->cascade_heap);
		}
607

osku's avatar
osku committed
608 609 610 611 612 613 614
		que_graph_free_recursive(upd->select);

		mem_heap_free(upd->heap);

		break;
	case QUE_NODE_CREATE_TABLE:
		cre_tab = node;
615

osku's avatar
osku committed
616 617 618 619 620 621 622 623 624
		que_graph_free_recursive(cre_tab->tab_def);
		que_graph_free_recursive(cre_tab->col_def);
		que_graph_free_recursive(cre_tab->commit_node);

		mem_heap_free(cre_tab->heap);

		break;
	case QUE_NODE_CREATE_INDEX:
		cre_ind = node;
625

osku's avatar
osku committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
		que_graph_free_recursive(cre_ind->ind_def);
		que_graph_free_recursive(cre_ind->field_def);
		que_graph_free_recursive(cre_ind->commit_node);

		mem_heap_free(cre_ind->heap);

		break;
	case QUE_NODE_PROC:
		que_graph_free_stat_list(((proc_node_t*)node)->stat_list);

		break;
	case QUE_NODE_IF:
		que_graph_free_stat_list(((if_node_t*)node)->stat_list);
		que_graph_free_stat_list(((if_node_t*)node)->else_part);
		que_graph_free_stat_list(((if_node_t*)node)->elsif_list);

		break;
	case QUE_NODE_ELSIF:
		que_graph_free_stat_list(((elsif_node_t*)node)->stat_list);

		break;
	case QUE_NODE_WHILE:
		que_graph_free_stat_list(((while_node_t*)node)->stat_list);

		break;
	case QUE_NODE_FOR:
		que_graph_free_stat_list(((for_node_t*)node)->stat_list);

		break;

	case QUE_NODE_ASSIGNMENT:
657
	case QUE_NODE_EXIT:
osku's avatar
osku committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671
	case QUE_NODE_RETURN:
	case QUE_NODE_COMMIT:
	case QUE_NODE_ROLLBACK:
	case QUE_NODE_LOCK:
	case QUE_NODE_FUNC:
	case QUE_NODE_ORDER:
	case QUE_NODE_ROW_PRINTF:
	case QUE_NODE_OPEN:
	case QUE_NODE_FETCH:
		/* No need to do anything */

		break;
	default:
		fprintf(stderr,
672
			"que_node struct appears corrupt; type %lu\n",
osku's avatar
osku committed
673
			(unsigned long) que_node_get_type(node));
674
		mem_analyze_corruption(node);
osku's avatar
osku committed
675 676 677 678
		ut_error;
	}
}

679
/**********************************************************************//**
osku's avatar
osku committed
680
Frees a query graph. */
681
UNIV_INTERN
osku's avatar
osku committed
682 683 684
void
que_graph_free(
/*===========*/
685
	que_t*	graph)	/*!< in: query graph; we assume that the memory
osku's avatar
osku committed
686 687 688 689 690 691 692 693 694 695 696
			heap where this graph was created is private
			to this graph: if not, then use
			que_graph_free_recursive and free the heap
			afterwards! */
{
	ut_ad(graph);

	if (graph->sym_tab) {
		/* The following call frees dynamic memory allocated
		for variables etc. during execution. Frees also explicit
		cursor definitions. */
697

osku's avatar
osku committed
698 699 700
		sym_tab_free_private(graph->sym_tab);
	}

701 702 703 704
	if (graph->info && graph->info->graph_owns_us) {
		pars_info_free(graph->info);
	}

osku's avatar
osku committed
705 706 707 708 709
	que_graph_free_recursive(graph);

	mem_heap_free(graph->heap);
}

710
/****************************************************************//**
711 712
Performs an execution step on a thr node.
@return	query thread to run next, or NULL if none */
osku's avatar
osku committed
713 714 715 716
static
que_thr_t*
que_thr_node_step(
/*==============*/
717
	que_thr_t*	thr)	/*!< in: query thread where run_node must
osku's avatar
osku committed
718 719 720
				be the thread node itself */
{
	ut_ad(thr->run_node == thr);
721

osku's avatar
osku committed
722 723 724 725 726
	if (thr->prev_node == thr->common.parent) {
		/* If control to the node came from above, it is just passed
		on */

		thr->run_node = thr->child;
727

osku's avatar
osku committed
728 729 730 731 732 733
		return(thr);
	}

	mutex_enter(&kernel_mutex);

	if (que_thr_peek_stop(thr)) {
734

osku's avatar
osku committed
735 736 737 738 739 740
		mutex_exit(&kernel_mutex);

		return(thr);
	}

	/* Thread execution completed */
741

osku's avatar
osku committed
742
	thr->state = QUE_THR_COMPLETED;
743

osku's avatar
osku committed
744 745 746 747 748
	mutex_exit(&kernel_mutex);

	return(NULL);
}

749
/**********************************************************************//**
osku's avatar
osku committed
750 751 752 753 754 755 756 757 758
Moves a thread from another state to the QUE_THR_RUNNING state. Increments
the n_active_thrs counters of the query graph and transaction if thr was
not active.
***NOTE***: This and ..._mysql are  the only functions in which such a
transition is allowed to happen! */
static
void
que_thr_move_to_run_state(
/*======================*/
759
	que_thr_t*	thr)	/*!< in: an query thread */
osku's avatar
osku committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
{
	trx_t*	trx;

	ut_ad(thr->state != QUE_THR_RUNNING);

	trx = thr_get_trx(thr);

	if (!thr->is_active) {

		(thr->graph)->n_active_thrs++;

		trx->n_active_thrs++;

		thr->is_active = TRUE;

		ut_ad((thr->graph)->n_active_thrs == 1);
		ut_ad(trx->n_active_thrs == 1);
	}
778

osku's avatar
osku committed
779 780 781
	thr->state = QUE_THR_RUNNING;
}

782
/**********************************************************************//**
osku's avatar
osku committed
783 784 785
Decrements the query thread reference counts in the query graph and the
transaction. May start signal handling, e.g., a rollback.
*** NOTE ***:
786 787 788 789
This and que_thr_stop_for_mysql are the only functions where the reference
count can be decremented and this function may only be called from inside
que_run_threads or que_thr_check_if_switch! These restrictions exist to make
the rollback code easier to maintain. */
osku's avatar
osku committed
790 791 792 793
static
void
que_thr_dec_refer_count(
/*====================*/
794 795
	que_thr_t*	thr,		/*!< in: query thread */
	que_thr_t**	next_thr)	/*!< in/out: next query thread to run;
osku's avatar
osku committed
796 797 798
					if the value which is passed in is
					a pointer to a NULL pointer, then the
					calling function can start running
799
					a new query thread */
osku's avatar
osku committed
800 801 802 803 804
{
	que_fork_t*	fork;
	trx_t*		trx;
	ulint		fork_type;
	ibool		stopped;
805

osku's avatar
osku committed
806
	fork = thr->common.parent;
807
	trx = thr_get_trx(thr);
osku's avatar
osku committed
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822

	mutex_enter(&kernel_mutex);

	ut_a(thr->is_active);

	if (thr->state == QUE_THR_RUNNING) {

		stopped = que_thr_stop(thr);

		if (!stopped) {
			/* The reason for the thr suspension or wait was
			already canceled before we came here: continue
			running the thread */

			/* fputs("!!!!!!!! Wait already ended: continue thr\n",
823
			stderr); */
osku's avatar
osku committed
824 825

			if (next_thr && *next_thr == NULL) {
826 827 828 829 830 831
				/* Normally srv_suspend_mysql_thread resets
				the state to DB_SUCCESS before waiting, but
				in this case we have to do it here,
				otherwise nobody does it. */
				trx->error_state = DB_SUCCESS;

osku's avatar
osku committed
832 833
				*next_thr = thr;
			} else {
834
				ut_error;
osku's avatar
osku committed
835 836 837 838 839 840 841
				srv_que_task_enqueue_low(thr);
			}

			mutex_exit(&kernel_mutex);

			return;
		}
842
	}
osku's avatar
osku committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857

	ut_ad(fork->n_active_thrs == 1);
	ut_ad(trx->n_active_thrs == 1);

	fork->n_active_thrs--;
	trx->n_active_thrs--;

	thr->is_active = FALSE;

	if (trx->n_active_thrs > 0) {

		mutex_exit(&kernel_mutex);

		return;
	}
858

osku's avatar
osku committed
859 860 861 862 863 864
	fork_type = fork->fork_type;

	/* Check if all query threads in the same fork are completed */

	if (que_fork_all_thrs_in_state(fork, QUE_THR_COMPLETED)) {

865 866
		switch (fork_type) {
		case QUE_FORK_ROLLBACK:
osku's avatar
osku committed
867 868
			/* This is really the undo graph used in rollback,
			no roll_node in this graph */
869

osku's avatar
osku committed
870 871
			ut_ad(UT_LIST_GET_LEN(trx->signals) > 0);
			ut_ad(trx->handling_signals == TRUE);
872

osku's avatar
osku committed
873
			trx_finish_rollback_off_kernel(fork, trx, next_thr);
874
			break;
875

876 877 878
		case QUE_FORK_PURGE:
		case QUE_FORK_RECOVERY:
		case QUE_FORK_MYSQL_INTERFACE:
osku's avatar
osku committed
879 880

			/* Do nothing */
881
			break;
osku's avatar
osku committed
882

883
		default:
884
			ut_error;	/*!< not used in MySQL */
osku's avatar
osku committed
885 886 887 888 889
		}
	}

	if (UT_LIST_GET_LEN(trx->signals) > 0 && trx->n_active_thrs == 0) {

890
		/* If the trx is signaled and its query thread count drops to
osku's avatar
osku committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904
		zero, then we start processing a signal; from it we may get
		a new query thread to run */

		trx_sig_start_handle(trx, next_thr);
	}

	if (trx->handling_signals && UT_LIST_GET_LEN(trx->signals) == 0) {

		trx_end_signal_handling(trx);
	}

	mutex_exit(&kernel_mutex);
}

905
/**********************************************************************//**
osku's avatar
osku committed
906 907
Stops a query thread if graph or trx is in a state requiring it. The
conditions are tested in the order (1) graph, (2) trx. The kernel mutex has
908 909
to be reserved.
@return	TRUE if stopped */
910
UNIV_INTERN
osku's avatar
osku committed
911 912 913
ibool
que_thr_stop(
/*=========*/
914
	que_thr_t*	thr)	/*!< in: query thread */
osku's avatar
osku committed
915 916 917 918 919 920
{
	trx_t*	trx;
	que_t*	graph;
	ibool	ret	= TRUE;

	ut_ad(mutex_own(&kernel_mutex));
921

osku's avatar
osku committed
922 923 924 925 926 927 928 929 930 931 932 933
	graph = thr->graph;
	trx = graph->trx;

	if (graph->state == QUE_FORK_COMMAND_WAIT) {
		thr->state = QUE_THR_SUSPENDED;

	} else if (trx->que_state == TRX_QUE_LOCK_WAIT) {

		UT_LIST_ADD_FIRST(trx_thrs, trx->wait_thrs, thr);
		thr->state = QUE_THR_LOCK_WAIT;

	} else if (trx->error_state != DB_SUCCESS
934
		   && trx->error_state != DB_LOCK_WAIT) {
osku's avatar
osku committed
935 936 937 938 939

		/* Error handling built for the MySQL interface */
		thr->state = QUE_THR_COMPLETED;

	} else if (UT_LIST_GET_LEN(trx->signals) > 0
940
		   && graph->fork_type != QUE_FORK_ROLLBACK) {
osku's avatar
osku committed
941 942 943 944 945 946

		thr->state = QUE_THR_SUSPENDED;
	} else {
		ut_ad(graph->state == QUE_FORK_ACTIVE);

		ret = FALSE;
947
	}
osku's avatar
osku committed
948 949 950 951

	return(ret);
}

952
/**********************************************************************//**
osku's avatar
osku committed
953 954 955 956
A patch for MySQL used to 'stop' a dummy query thread used in MySQL. The
query thread is stopped and made inactive, except in the case where
it was put to the lock wait state in lock0lock.c, but the lock has already
been granted or the transaction chosen as a victim in deadlock resolution. */
957
UNIV_INTERN
osku's avatar
osku committed
958 959 960
void
que_thr_stop_for_mysql(
/*===================*/
961
	que_thr_t*	thr)	/*!< in: query thread */
osku's avatar
osku committed
962 963 964 965
{
	trx_t*	trx;

	trx = thr_get_trx(thr);
966

osku's avatar
osku committed
967 968 969 970 971
	mutex_enter(&kernel_mutex);

	if (thr->state == QUE_THR_RUNNING) {

		if (trx->error_state != DB_SUCCESS
972
		    && trx->error_state != DB_LOCK_WAIT) {
osku's avatar
osku committed
973 974 975 976 977 978 979 980 981 982 983 984 985

			/* Error handling built for the MySQL interface */
			thr->state = QUE_THR_COMPLETED;
		} else {
			/* It must have been a lock wait but the lock was
			already released, or this transaction was chosen
			as a victim in selective deadlock resolution */

			mutex_exit(&kernel_mutex);

			return;
		}
	}
986

osku's avatar
osku committed
987 988 989 990 991 992 993 994 995 996 997 998
	ut_ad(thr->is_active == TRUE);
	ut_ad(trx->n_active_thrs == 1);
	ut_ad(thr->graph->n_active_thrs == 1);

	thr->is_active = FALSE;
	(thr->graph)->n_active_thrs--;

	trx->n_active_thrs--;

	mutex_exit(&kernel_mutex);
}

999
/**********************************************************************//**
osku's avatar
osku committed
1000 1001 1002
Moves a thread from another state to the QUE_THR_RUNNING state. Increments
the n_active_thrs counters of the query graph and transaction if thr was
not active. */
1003
UNIV_INTERN
osku's avatar
osku committed
1004 1005 1006
void
que_thr_move_to_run_state_for_mysql(
/*================================*/
1007 1008
	que_thr_t*	thr,	/*!< in: an query thread */
	trx_t*		trx)	/*!< in: transaction */
osku's avatar
osku committed
1009 1010 1011
{
	if (thr->magic_n != QUE_THR_MAGIC_N) {
		fprintf(stderr,
1012
			"que_thr struct appears corrupt; magic n %lu\n",
osku's avatar
osku committed
1013 1014
			(unsigned long) thr->magic_n);

1015
		mem_analyze_corruption(thr);
osku's avatar
osku committed
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

		ut_error;
	}

	if (!thr->is_active) {

		thr->graph->n_active_thrs++;

		trx->n_active_thrs++;

		thr->is_active = TRUE;
	}
1028

osku's avatar
osku committed
1029 1030 1031
	thr->state = QUE_THR_RUNNING;
}

1032
/**********************************************************************//**
osku's avatar
osku committed
1033 1034
A patch for MySQL used to 'stop' a dummy query thread used in MySQL
select, when there is no error or lock wait. */
1035
UNIV_INTERN
osku's avatar
osku committed
1036 1037 1038
void
que_thr_stop_for_mysql_no_error(
/*============================*/
1039 1040
	que_thr_t*	thr,	/*!< in: query thread */
	trx_t*		trx)	/*!< in: transaction */
osku's avatar
osku committed
1041 1042 1043 1044 1045
{
	ut_ad(thr->state == QUE_THR_RUNNING);
	ut_ad(thr->is_active == TRUE);
	ut_ad(trx->n_active_thrs == 1);
	ut_ad(thr->graph->n_active_thrs == 1);
1046

osku's avatar
osku committed
1047 1048
	if (thr->magic_n != QUE_THR_MAGIC_N) {
		fprintf(stderr,
1049
			"que_thr struct appears corrupt; magic n %lu\n",
osku's avatar
osku committed
1050 1051
			(unsigned long) thr->magic_n);

1052
		mem_analyze_corruption(thr);
osku's avatar
osku committed
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

		ut_error;
	}

	thr->state = QUE_THR_COMPLETED;

	thr->is_active = FALSE;
	(thr->graph)->n_active_thrs--;

	trx->n_active_thrs--;
}

1065
/****************************************************************//**
1066
Get the first containing loop node (e.g. while_node_t or for_node_t) for the
1067 1068
given node, or NULL if the node is not within a loop.
@return	containing loop node, or NULL. */
1069
UNIV_INTERN
1070 1071 1072
que_node_t*
que_node_get_containing_loop_node(
/*==============================*/
1073
	que_node_t*	node)	/*!< in: node */
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
{
	ut_ad(node);

	for (;;) {
		ulint	type;

		node = que_node_get_parent(node);

		if (!node) {
			break;
		}

		type = que_node_get_type(node);

		if ((type == QUE_NODE_FOR) || (type == QUE_NODE_WHILE)) {
			break;
		}
	}

	return(node);
}

1096
/**********************************************************************//**
osku's avatar
osku committed
1097
Prints info of an SQL query graph node. */
1098
UNIV_INTERN
osku's avatar
osku committed
1099 1100 1101
void
que_node_print_info(
/*================*/
1102
	que_node_t*	node)	/*!< in: query graph node */
osku's avatar
osku committed
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
{
	ulint		type;
	const char*	str;

	type = que_node_get_type(node);

	if (type == QUE_NODE_SELECT) {
		str = "SELECT";
	} else if (type == QUE_NODE_INSERT) {
		str = "INSERT";
	} else if (type == QUE_NODE_UPDATE) {
		str = "UPDATE";
	} else if (type == QUE_NODE_WHILE) {
		str = "WHILE";
	} else if (type == QUE_NODE_ASSIGNMENT) {
		str = "ASSIGNMENT";
	} else if (type == QUE_NODE_IF) {
		str = "IF";
	} else if (type == QUE_NODE_FETCH) {
		str = "FETCH";
	} else if (type == QUE_NODE_OPEN) {
		str = "OPEN";
	} else if (type == QUE_NODE_PROC) {
		str = "STORED PROCEDURE";
	} else if (type == QUE_NODE_FUNC) {
		str = "FUNCTION";
	} else if (type == QUE_NODE_LOCK) {
		str = "LOCK";
	} else if (type == QUE_NODE_THR) {
		str = "QUERY THREAD";
	} else if (type == QUE_NODE_COMMIT) {
		str = "COMMIT";
	} else if (type == QUE_NODE_UNDO) {
		str = "UNDO ROW";
	} else if (type == QUE_NODE_PURGE) {
		str = "PURGE ROW";
	} else if (type == QUE_NODE_ROLLBACK) {
		str = "ROLLBACK";
	} else if (type == QUE_NODE_CREATE_TABLE) {
		str = "CREATE TABLE";
	} else if (type == QUE_NODE_CREATE_INDEX) {
		str = "CREATE INDEX";
	} else if (type == QUE_NODE_FOR) {
		str = "FOR LOOP";
	} else if (type == QUE_NODE_RETURN) {
		str = "RETURN";
1149 1150
	} else if (type == QUE_NODE_EXIT) {
		str = "EXIT";
osku's avatar
osku committed
1151 1152 1153 1154
	} else {
		str = "UNKNOWN NODE TYPE";
	}

1155 1156
	fprintf(stderr, "Node type %lu: %s, address %p\n",
		(ulong) type, str, (void*) node);
osku's avatar
osku committed
1157 1158
}

1159
/**********************************************************************//**
1160
Performs an execution step on a query thread.
1161 1162
@return query thread to run next: it may differ from the input
parameter if, e.g., a subprocedure call is made */
osku's avatar
osku committed
1163 1164 1165 1166
UNIV_INLINE
que_thr_t*
que_thr_step(
/*=========*/
1167
	que_thr_t*	thr)	/*!< in: query thread */
osku's avatar
osku committed
1168 1169 1170 1171 1172
{
	que_node_t*	node;
	que_thr_t*	old_thr;
	trx_t*		trx;
	ulint		type;
1173

1174 1175
	trx = thr_get_trx(thr);

osku's avatar
osku committed
1176
	ut_ad(thr->state == QUE_THR_RUNNING);
1177
	ut_a(trx->error_state == DB_SUCCESS);
osku's avatar
osku committed
1178 1179

	thr->resource++;
1180

osku's avatar
osku committed
1181
	node = thr->run_node;
1182
	type = que_node_get_type(node);
osku's avatar
osku committed
1183 1184

	old_thr = thr;
1185

osku's avatar
osku committed
1186 1187 1188 1189 1190 1191 1192 1193
#ifdef UNIV_DEBUG
	if (que_trace_on) {
		fputs("To execute: ", stderr);
		que_node_print_info(node);
	}
#endif
	if (type & QUE_NODE_CONTROL_STAT) {
		if ((thr->prev_node != que_node_get_parent(node))
1194
		    && que_node_get_next(thr->prev_node)) {
osku's avatar
osku committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210

			/* The control statements, like WHILE, always pass the
			control to the next child statement if there is any
			child left */

			thr->run_node = que_node_get_next(thr->prev_node);

		} else if (type == QUE_NODE_IF) {
			if_step(thr);
		} else if (type == QUE_NODE_FOR) {
			for_step(thr);
		} else if (type == QUE_NODE_PROC) {

			/* We can access trx->undo_no without reserving
			trx->undo_mutex, because there cannot be active query
			threads doing updating or inserting at the moment! */
1211

osku's avatar
osku committed
1212 1213
			if (thr->prev_node == que_node_get_parent(node)) {
				trx->last_sql_stat_start.least_undo_no
1214
					= trx->undo_no;
osku's avatar
osku committed
1215
			}
1216

osku's avatar
osku committed
1217 1218 1219
			proc_step(thr);
		} else if (type == QUE_NODE_WHILE) {
			while_step(thr);
1220 1221
		} else {
			ut_error;
osku's avatar
osku committed
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
		}
	} else if (type == QUE_NODE_ASSIGNMENT) {
		assign_step(thr);
	} else if (type == QUE_NODE_SELECT) {
		thr = row_sel_step(thr);
	} else if (type == QUE_NODE_INSERT) {
		thr = row_ins_step(thr);
	} else if (type == QUE_NODE_UPDATE) {
		thr = row_upd_step(thr);
	} else if (type == QUE_NODE_FETCH) {
		thr = fetch_step(thr);
	} else if (type == QUE_NODE_OPEN) {
		thr = open_step(thr);
	} else if (type == QUE_NODE_FUNC) {
		proc_eval_step(thr);

	} else if (type == QUE_NODE_LOCK) {

		ut_error;
1241
		/*
osku's avatar
osku committed
1242
		thr = que_lock_step(thr);
1243
		*/
osku's avatar
osku committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
	} else if (type == QUE_NODE_THR) {
		thr = que_thr_node_step(thr);
	} else if (type == QUE_NODE_COMMIT) {
		thr = trx_commit_step(thr);
	} else if (type == QUE_NODE_UNDO) {
		thr = row_undo_step(thr);
	} else if (type == QUE_NODE_PURGE) {
		thr = row_purge_step(thr);
	} else if (type == QUE_NODE_RETURN) {
		thr = return_step(thr);
1254 1255
	} else if (type == QUE_NODE_EXIT) {
		thr = exit_step(thr);
osku's avatar
osku committed
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
	} else if (type == QUE_NODE_ROLLBACK) {
		thr = trx_rollback_step(thr);
	} else if (type == QUE_NODE_CREATE_TABLE) {
		thr = dict_create_table_step(thr);
	} else if (type == QUE_NODE_CREATE_INDEX) {
		thr = dict_create_index_step(thr);
	} else if (type == QUE_NODE_ROW_PRINTF) {
		thr = row_printf_step(thr);
	} else {
		ut_error;
	}

1268 1269 1270 1271 1272
	if (type == QUE_NODE_EXIT) {
		old_thr->prev_node = que_node_get_containing_loop_node(node);
	} else {
		old_thr->prev_node = node;
	}
osku's avatar
osku committed
1273

1274 1275 1276 1277
	if (thr) {
		ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
	}

osku's avatar
osku committed
1278 1279 1280
	return(thr);
}

1281
/**********************************************************************//**
1282 1283
Run a query thread until it finishes or encounters e.g. a lock wait. */
static
osku's avatar
osku committed
1284
void
1285 1286
que_run_threads_low(
/*================*/
1287
	que_thr_t*	thr)	/*!< in: query thread */
osku's avatar
osku committed
1288 1289
{
	que_thr_t*	next_thr;
1290
	ulint		cumul_resource;
osku's avatar
osku committed
1291
	ulint		loop_count;
1292

osku's avatar
osku committed
1293
	ut_ad(thr->state == QUE_THR_RUNNING);
1294
	ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
osku's avatar
osku committed
1295 1296 1297 1298 1299 1300
	ut_ad(!mutex_own(&kernel_mutex));

	/* cumul_resource counts how much resources the OS thread (NOT the
	query thread) has spent in this function */

	loop_count = QUE_MAX_LOOPS_WITHOUT_CHECK;
1301
	cumul_resource = 0;
osku's avatar
osku committed
1302 1303 1304 1305 1306 1307 1308
loop:
	/* Check that there is enough space in the log to accommodate
	possible log entries by this query step; if the operation can touch
	more than about 4 pages, checks must be made also within the query
	step! */

	log_free_check();
1309

osku's avatar
osku committed
1310 1311 1312 1313 1314 1315 1316
	/* Perform the actual query step: note that the query thread
	may change if, e.g., a subprocedure call is made */

	/*-------------------------*/
	next_thr = que_thr_step(thr);
	/*-------------------------*/

1317 1318
	ut_a(!next_thr || (thr_get_trx(next_thr)->error_state == DB_SUCCESS));

osku's avatar
osku committed
1319 1320 1321 1322
	loop_count++;

	if (next_thr != thr) {
		ut_a(next_thr == NULL);
1323 1324 1325

		/* This can change next_thr to a non-NULL value if there was
		a lock wait that already completed. */
osku's avatar
osku committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
		que_thr_dec_refer_count(thr, &next_thr);

		if (next_thr == NULL) {

			return;
		}

		loop_count = QUE_MAX_LOOPS_WITHOUT_CHECK;

		thr = next_thr;
	}

	goto loop;
}
1340

1341
/**********************************************************************//**
1342
Run a query thread. Handles lock waits. */
1343
UNIV_INTERN
1344 1345 1346
void
que_run_threads(
/*============*/
1347
	que_thr_t*	thr)	/*!< in: query thread */
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
{
loop:
	ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
	que_run_threads_low(thr);

	mutex_enter(&kernel_mutex);

	switch (thr->state) {

	case QUE_THR_RUNNING:
		/* There probably was a lock wait, but it already ended
		before we came here: continue running thr */

		mutex_exit(&kernel_mutex);

		goto loop;

	case QUE_THR_LOCK_WAIT:
		mutex_exit(&kernel_mutex);

		/* The ..._mysql_... function works also for InnoDB's
		internal threads. Let us wait that the lock wait ends. */

		srv_suspend_mysql_thread(thr);

		if (thr_get_trx(thr)->error_state != DB_SUCCESS) {
			/* thr was chosen as a deadlock victim or there was
			a lock wait timeout */

			que_thr_dec_refer_count(thr, NULL);

			return;
		}

		goto loop;

	case QUE_THR_COMPLETED:
	case QUE_THR_COMMAND_WAIT:
		/* Do nothing */
		break;

	default:
		ut_error;
	}

	mutex_exit(&kernel_mutex);
}

1396
/*********************************************************************//**
1397 1398
Evaluate the given SQL.
@return	error code or DB_SUCCESS */
1399
UNIV_INTERN
1400 1401 1402
ulint
que_eval_sql(
/*=========*/
1403 1404
	pars_info_t*	info,	/*!< in: info struct, or NULL */
	const char*	sql,	/*!< in: SQL string */
1405
	ibool		reserve_dict_mutex,
1406
				/*!< in: if TRUE, acquire/release
1407
				dict_sys->mutex around call to pars_sql. */
1408
	trx_t*		trx)	/*!< in: trx */
1409 1410 1411 1412
{
	que_thr_t*	thr;
	que_t*		graph;

1413 1414 1415 1416 1417 1418
	ut_a(trx->error_state == DB_SUCCESS);

	if (reserve_dict_mutex) {
		mutex_enter(&dict_sys->mutex);
	}

1419
	graph = pars_sql(info, sql);
1420 1421 1422 1423 1424

	if (reserve_dict_mutex) {
		mutex_exit(&dict_sys->mutex);
	}

1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
	ut_a(graph);

	graph->trx = trx;
	trx->graph = NULL;

	graph->fork_type = QUE_FORK_MYSQL_INTERFACE;

	ut_a(thr = que_fork_start_command(graph));

	que_run_threads(thr);

	que_graph_free(graph);

	return(trx->error_state);
}