mirror of
https://github.com/MariaDB/server.git
synced 2025-08-24 14:48:09 +03:00
From r5639 to r5685 Detailed revision comments: r5639 | marko | 2009-08-06 05:39:34 -0500 (Thu, 06 Aug 2009) | 3 lines branches/zip: mem_heap_block_free(): If innodb_use_sys_malloc is set, do not tell Valgrind that the memory is free, to avoid a bogus warning in Valgrind's built-in free() hook. r5642 | calvin | 2009-08-06 18:04:03 -0500 (Thu, 06 Aug 2009) | 2 lines branches/zip: remove duplicate "the" in comments. r5662 | marko | 2009-08-11 04:54:16 -0500 (Tue, 11 Aug 2009) | 1 line branches/zip: Bump the version number to 1.0.5 after releasing 1.0.4. r5663 | marko | 2009-08-11 06:42:37 -0500 (Tue, 11 Aug 2009) | 2 lines branches/zip: trx_general_rollback_for_mysql(): Remove the redundant parameter partial. If savept==NULL, partial==FALSE. r5670 | marko | 2009-08-12 08:16:37 -0500 (Wed, 12 Aug 2009) | 2 lines branches/zip: trx_undo_rec_copy(): Add const qualifier to undo_rec. This is a non-functional change. r5671 | marko | 2009-08-13 03:46:33 -0500 (Thu, 13 Aug 2009) | 5 lines branches/zip: ha_innobase::add_index(): Fix Bug #46557: after a successful operation, read innodb_table->flags from the newly created table object, not from the old one that was just freed. Approved by Sunny. r5681 | sunny | 2009-08-14 01:16:24 -0500 (Fri, 14 Aug 2009) | 3 lines branches/zip: When building HotBackup srv_use_sys_malloc is #ifdef out. We move access to the this variable within a !UNIV_HOTBACKUP block. r5684 | sunny | 2009-08-20 03:05:30 -0500 (Thu, 20 Aug 2009) | 10 lines branches/zip: Fix bug# 46650: Innodb assertion autoinc_lock == lock in lock_table_remove_low on INSERT SELECT We only store the autoinc locks that are granted in the transaction's autoinc lock vector. A transacton, that has been rolled back due to a deadlock because of an AUTOINC lock attempt, will not have added that lock to the vector. We need to check for that when we remove that lock. rb://145 Approved by Marko. r5685 | sunny | 2009-08-20 03:18:29 -0500 (Thu, 20 Aug 2009) | 2 lines branches/zip: Update the ChangeLog with r5684 change.
249 lines
6.2 KiB
C
249 lines
6.2 KiB
C
/*****************************************************************************
|
|
|
|
Copyright (c) 1995, 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
|
|
|
|
*****************************************************************************/
|
|
|
|
/**************************************************//**
|
|
@file thr/thr0loc.c
|
|
The thread local storage
|
|
|
|
Created 10/5/1995 Heikki Tuuri
|
|
*******************************************************/
|
|
|
|
#include "thr0loc.h"
|
|
#ifdef UNIV_NONINL
|
|
#include "thr0loc.ic"
|
|
#endif
|
|
|
|
#include "sync0sync.h"
|
|
#include "hash0hash.h"
|
|
#include "mem0mem.h"
|
|
#include "srv0srv.h"
|
|
|
|
/*
|
|
IMPLEMENTATION OF THREAD LOCAL STORAGE
|
|
======================================
|
|
|
|
The threads sometimes need private data which depends on the thread id.
|
|
This is implemented as a hash table, where the hash value is calculated
|
|
from the thread id, to prepare for a large number of threads. The hash table
|
|
is protected by a mutex. If you need modify the program and put new data to
|
|
the thread local storage, just add it to struct thr_local_struct in the
|
|
header file. */
|
|
|
|
/** Mutex protecting thr_local_hash */
|
|
static mutex_t thr_local_mutex;
|
|
|
|
/** The hash table. The module is not yet initialized when it is NULL. */
|
|
static hash_table_t* thr_local_hash = NULL;
|
|
|
|
/** Thread local data */
|
|
typedef struct thr_local_struct thr_local_t;
|
|
|
|
/** @brief Thread local data.
|
|
The private data for each thread should be put to
|
|
the structure below and the accessor functions written
|
|
for the field. */
|
|
struct thr_local_struct{
|
|
os_thread_id_t id; /*!< id of the thread which owns this struct */
|
|
os_thread_t handle; /*!< operating system handle to the thread */
|
|
ulint slot_no;/*!< the index of the slot in the thread table
|
|
for this thread */
|
|
ibool in_ibuf;/*!< TRUE if the thread is doing an ibuf
|
|
operation */
|
|
hash_node_t hash; /*!< hash chain node */
|
|
ulint magic_n;/*!< magic number (THR_LOCAL_MAGIC_N) */
|
|
};
|
|
|
|
/** The value of thr_local_struct::magic_n */
|
|
#define THR_LOCAL_MAGIC_N 1231234
|
|
|
|
/*******************************************************************//**
|
|
Returns the local storage struct for a thread.
|
|
@return local storage */
|
|
static
|
|
thr_local_t*
|
|
thr_local_get(
|
|
/*==========*/
|
|
os_thread_id_t id) /*!< in: thread id of the thread */
|
|
{
|
|
thr_local_t* local;
|
|
|
|
try_again:
|
|
ut_ad(thr_local_hash);
|
|
ut_ad(mutex_own(&thr_local_mutex));
|
|
|
|
/* Look for the local struct in the hash table */
|
|
|
|
local = NULL;
|
|
|
|
HASH_SEARCH(hash, thr_local_hash, os_thread_pf(id),
|
|
thr_local_t*, local,, os_thread_eq(local->id, id));
|
|
if (local == NULL) {
|
|
mutex_exit(&thr_local_mutex);
|
|
|
|
thr_local_create();
|
|
|
|
mutex_enter(&thr_local_mutex);
|
|
|
|
goto try_again;
|
|
}
|
|
|
|
ut_ad(local->magic_n == THR_LOCAL_MAGIC_N);
|
|
|
|
return(local);
|
|
}
|
|
|
|
/*******************************************************************//**
|
|
Gets the slot number in the thread table of a thread.
|
|
@return slot number */
|
|
UNIV_INTERN
|
|
ulint
|
|
thr_local_get_slot_no(
|
|
/*==================*/
|
|
os_thread_id_t id) /*!< in: thread id of the thread */
|
|
{
|
|
ulint slot_no;
|
|
thr_local_t* local;
|
|
|
|
mutex_enter(&thr_local_mutex);
|
|
|
|
local = thr_local_get(id);
|
|
|
|
slot_no = local->slot_no;
|
|
|
|
mutex_exit(&thr_local_mutex);
|
|
|
|
return(slot_no);
|
|
}
|
|
|
|
/*******************************************************************//**
|
|
Sets the slot number in the thread table of a thread. */
|
|
UNIV_INTERN
|
|
void
|
|
thr_local_set_slot_no(
|
|
/*==================*/
|
|
os_thread_id_t id, /*!< in: thread id of the thread */
|
|
ulint slot_no)/*!< in: slot number */
|
|
{
|
|
thr_local_t* local;
|
|
|
|
mutex_enter(&thr_local_mutex);
|
|
|
|
local = thr_local_get(id);
|
|
|
|
local->slot_no = slot_no;
|
|
|
|
mutex_exit(&thr_local_mutex);
|
|
}
|
|
|
|
/*******************************************************************//**
|
|
Returns pointer to the 'in_ibuf' field within the current thread local
|
|
storage.
|
|
@return pointer to the in_ibuf field */
|
|
UNIV_INTERN
|
|
ibool*
|
|
thr_local_get_in_ibuf_field(void)
|
|
/*=============================*/
|
|
{
|
|
thr_local_t* local;
|
|
|
|
mutex_enter(&thr_local_mutex);
|
|
|
|
local = thr_local_get(os_thread_get_curr_id());
|
|
|
|
mutex_exit(&thr_local_mutex);
|
|
|
|
return(&(local->in_ibuf));
|
|
}
|
|
|
|
/*******************************************************************//**
|
|
Creates a local storage struct for the calling new thread. */
|
|
UNIV_INTERN
|
|
void
|
|
thr_local_create(void)
|
|
/*==================*/
|
|
{
|
|
thr_local_t* local;
|
|
|
|
if (thr_local_hash == NULL) {
|
|
thr_local_init();
|
|
}
|
|
|
|
local = mem_alloc(sizeof(thr_local_t));
|
|
|
|
local->id = os_thread_get_curr_id();
|
|
local->handle = os_thread_get_curr();
|
|
local->magic_n = THR_LOCAL_MAGIC_N;
|
|
|
|
local->in_ibuf = FALSE;
|
|
|
|
mutex_enter(&thr_local_mutex);
|
|
|
|
HASH_INSERT(thr_local_t, hash, thr_local_hash,
|
|
os_thread_pf(os_thread_get_curr_id()),
|
|
local);
|
|
|
|
mutex_exit(&thr_local_mutex);
|
|
}
|
|
|
|
/*******************************************************************//**
|
|
Frees the local storage struct for the specified thread. */
|
|
UNIV_INTERN
|
|
void
|
|
thr_local_free(
|
|
/*===========*/
|
|
os_thread_id_t id) /*!< in: thread id */
|
|
{
|
|
thr_local_t* local;
|
|
|
|
mutex_enter(&thr_local_mutex);
|
|
|
|
/* Look for the local struct in the hash table */
|
|
|
|
HASH_SEARCH(hash, thr_local_hash, os_thread_pf(id),
|
|
thr_local_t*, local,, os_thread_eq(local->id, id));
|
|
if (local == NULL) {
|
|
mutex_exit(&thr_local_mutex);
|
|
|
|
return;
|
|
}
|
|
|
|
HASH_DELETE(thr_local_t, hash, thr_local_hash,
|
|
os_thread_pf(id), local);
|
|
|
|
mutex_exit(&thr_local_mutex);
|
|
|
|
ut_a(local->magic_n == THR_LOCAL_MAGIC_N);
|
|
|
|
mem_free(local);
|
|
}
|
|
|
|
/****************************************************************//**
|
|
Initializes the thread local storage module. */
|
|
UNIV_INTERN
|
|
void
|
|
thr_local_init(void)
|
|
/*================*/
|
|
{
|
|
|
|
ut_a(thr_local_hash == NULL);
|
|
|
|
thr_local_hash = hash_create(OS_THREAD_MAX_N + 100);
|
|
|
|
mutex_create(&thr_local_mutex, SYNC_THR_LOCAL);
|
|
}
|