1/*****************************************************************************
2
3Copyright (c) 1996, 2011, Oracle and/or its affiliates. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License, version 2.0,
7as published by the Free Software Foundation.
8
9This program is also distributed with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation.  The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have included with MySQL.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU General Public License, version 2.0, for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24
25*****************************************************************************/
26
27/**************************************************//**
28@file include/trx0trx.ic
29The transaction
30
31Created 3/26/1996 Heikki Tuuri
32*******************************************************/
33
34/**********************************************************************//**
35Determines if a transaction is in the given state.
36The caller must hold trx_sys->mutex, or it must be the thread
37that is serving a running transaction.
38A running transaction must be in trx_sys->ro_trx_list or trx_sys->rw_trx_list
39unless it is a non-locking autocommit read only transaction, which is only
40in trx_sys->mysql_trx_list.
41@return	TRUE if trx->state == state */
42UNIV_INLINE
43ibool
44trx_state_eq(
45/*=========*/
46	const trx_t*	trx,	/*!< in: transaction */
47	trx_state_t	state)	/*!< in: state;
48				if state != TRX_STATE_NOT_STARTED
49				asserts that
50				trx->state != TRX_STATE_NOT_STARTED */
51{
52#ifdef UNIV_DEBUG
53	switch (trx->state) {
54	case TRX_STATE_PREPARED:
55		ut_ad(!trx_is_autocommit_non_locking(trx));
56		return(trx->state == state);
57
58	case TRX_STATE_ACTIVE:
59		assert_trx_nonlocking_or_in_list(trx);
60		return(state == trx->state);
61
62	case TRX_STATE_COMMITTED_IN_MEMORY:
63		assert_trx_in_list(trx);
64		return(state == trx->state);
65
66	case TRX_STATE_NOT_STARTED:
67		/* This state is not allowed for running transactions. */
68		ut_a(state == TRX_STATE_NOT_STARTED);
69		ut_ad(!trx->in_rw_trx_list);
70		ut_ad(!trx->in_ro_trx_list);
71		return(state == trx->state);
72	}
73	ut_error;
74#endif /* UNIV_DEBUG */
75	return(trx->state == state);
76}
77
78/****************************************************************//**
79Retrieves the error_info field from a trx.
80@return	the error info */
81UNIV_INLINE
82const dict_index_t*
83trx_get_error_info(
84/*===============*/
85	const trx_t*	trx)	/*!< in: trx object */
86{
87	return(trx->error_info);
88}
89
90/*******************************************************************//**
91Retrieves transaction's que state in a human readable string. The string
92should not be free()'d or modified.
93@return	string in the data segment */
94UNIV_INLINE
95const char*
96trx_get_que_state_str(
97/*==================*/
98	const trx_t*	trx)	/*!< in: transaction */
99{
100	/* be sure to adjust TRX_QUE_STATE_STR_MAX_LEN if you change this */
101	switch (trx->lock.que_state) {
102	case TRX_QUE_RUNNING:
103		return("RUNNING");
104	case TRX_QUE_LOCK_WAIT:
105		return("LOCK WAIT");
106	case TRX_QUE_ROLLING_BACK:
107		return("ROLLING BACK");
108	case TRX_QUE_COMMITTING:
109		return("COMMITTING");
110	default:
111		return("UNKNOWN");
112	}
113}
114
115/**********************************************************************//**
116Determine if a transaction is a dictionary operation.
117@return	dictionary operation mode */
118UNIV_INLINE
119enum trx_dict_op_t
120trx_get_dict_operation(
121/*===================*/
122	const trx_t*	trx)	/*!< in: transaction */
123{
124	trx_dict_op_t op = static_cast<trx_dict_op_t>(trx->dict_operation);
125
126#ifdef UNIV_DEBUG
127	switch (op) {
128	case TRX_DICT_OP_NONE:
129	case TRX_DICT_OP_TABLE:
130	case TRX_DICT_OP_INDEX:
131		return(op);
132	}
133	ut_error;
134#endif /* UNIV_DEBUG */
135	return(op);
136}
137/**********************************************************************//**
138Flag a transaction a dictionary operation. */
139UNIV_INLINE
140void
141trx_set_dict_operation(
142/*===================*/
143	trx_t*			trx,	/*!< in/out: transaction */
144	enum trx_dict_op_t	op)	/*!< in: operation, not
145					TRX_DICT_OP_NONE */
146{
147#ifdef UNIV_DEBUG
148	enum trx_dict_op_t	old_op = trx_get_dict_operation(trx);
149
150	switch (op) {
151	case TRX_DICT_OP_NONE:
152		ut_error;
153		break;
154	case TRX_DICT_OP_TABLE:
155		switch (old_op) {
156		case TRX_DICT_OP_NONE:
157		case TRX_DICT_OP_INDEX:
158		case TRX_DICT_OP_TABLE:
159			goto ok;
160		}
161		ut_error;
162		break;
163	case TRX_DICT_OP_INDEX:
164		ut_ad(old_op == TRX_DICT_OP_NONE);
165		break;
166	}
167ok:
168#endif /* UNIV_DEBUG */
169
170	trx->ddl = true;
171	trx->dict_operation = op;
172}
173
174/********************************************************************//**
175In XtraDB it is impossible for a transaction to own a search latch outside of
176InnoDB code, so there is nothing to release on demand.  We keep this function to
177simplify maintenance.*/
178UNIV_INLINE
179void
180trx_search_latch_release_if_reserved(
181/*=================================*/
182	trx_t*	   trx MY_ATTRIBUTE((unused))) /*!< in: transaction */
183{
184	ut_ad(!trx->has_search_latch);
185}
186