2014-02-26 19:11:54 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
|
2016-06-21 14:21:03 +02:00
|
|
|
Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
|
2016-04-22 10:50:45 +02:00
|
|
|
Copyright (c) 2017, MariaDB Corporation.
|
2014-02-26 19:11:54 +01:00
|
|
|
|
|
|
|
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 Street, Suite 500, Boston, MA 02110-1335 USA
|
|
|
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/**************************************************//**
|
|
|
|
@file include/os0thread.h
|
|
|
|
The interface to the operating system
|
|
|
|
process and thread control primitives
|
|
|
|
|
|
|
|
Created 9/8/1995 Heikki Tuuri
|
|
|
|
*******************************************************/
|
|
|
|
|
|
|
|
#ifndef os0thread_h
|
|
|
|
#define os0thread_h
|
|
|
|
|
|
|
|
#include "univ.i"
|
|
|
|
|
|
|
|
/* Maximum number of threads which can be created in the program;
|
|
|
|
this is also the size of the wait slot array for MySQL threads which
|
|
|
|
can wait inside InnoDB */
|
|
|
|
|
|
|
|
#define OS_THREAD_MAX_N srv_max_n_threads
|
|
|
|
|
|
|
|
/* Possible fixed priorities for threads */
|
|
|
|
#define OS_THREAD_PRIORITY_NONE 100
|
|
|
|
#define OS_THREAD_PRIORITY_BACKGROUND 1
|
|
|
|
#define OS_THREAD_PRIORITY_NORMAL 2
|
|
|
|
#define OS_THREAD_PRIORITY_ABOVE_NORMAL 3
|
|
|
|
|
2016-08-12 10:17:45 +02:00
|
|
|
#ifdef _WIN32
|
2016-09-13 20:23:14 +02:00
|
|
|
typedef DWORD os_thread_t;
|
2014-02-26 19:11:54 +01:00
|
|
|
typedef DWORD os_thread_id_t; /*!< In Windows the thread id
|
|
|
|
is an unsigned long int */
|
|
|
|
extern "C" {
|
|
|
|
typedef LPTHREAD_START_ROUTINE os_thread_func_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Macro for specifying a Windows thread start function. */
|
|
|
|
#define DECLARE_THREAD(func) WINAPI func
|
|
|
|
|
|
|
|
/** Required to get around a build error on Windows. Even though our functions
|
|
|
|
are defined/declared as WINAPI f(LPVOID a); the compiler complains that they
|
|
|
|
are defined as: os_thread_ret_t (__cdecl*)(void*). Because our functions
|
|
|
|
don't access the arguments and don't return any value, we should be safe. */
|
|
|
|
#define os_thread_create(f,a,i) \
|
|
|
|
os_thread_create_func(reinterpret_cast<os_thread_func_t>(f), a, i)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
typedef pthread_t os_thread_t;
|
2016-08-12 10:17:45 +02:00
|
|
|
typedef pthread_t os_thread_id_t; /*!< In Unix we use the thread
|
2014-02-26 19:11:54 +01:00
|
|
|
handle itself as the id of
|
|
|
|
the thread */
|
|
|
|
extern "C" { typedef void* (*os_thread_func_t)(void*); }
|
|
|
|
|
|
|
|
/** Macro for specifying a POSIX thread start function. */
|
|
|
|
#define DECLARE_THREAD(func) func
|
|
|
|
#define os_thread_create(f,a,i) os_thread_create_func(f, a, i)
|
|
|
|
|
2016-08-12 10:17:45 +02:00
|
|
|
#endif /* _WIN32 */
|
2014-02-26 19:11:54 +01:00
|
|
|
|
|
|
|
/* Define a function pointer type to use in a typecast */
|
|
|
|
typedef void* (*os_posix_f_t) (void*);
|
|
|
|
|
|
|
|
#ifdef HAVE_PSI_INTERFACE
|
|
|
|
/* Define for performance schema registration key */
|
|
|
|
typedef unsigned int mysql_pfs_key_t;
|
2016-08-12 10:17:45 +02:00
|
|
|
#endif /* HAVE_PSI_INTERFACE */
|
|
|
|
|
|
|
|
/** Number of threads active. */
|
|
|
|
extern ulint os_thread_count;
|
2014-02-26 19:11:54 +01:00
|
|
|
|
|
|
|
/***************************************************************//**
|
|
|
|
Compares two thread ids for equality.
|
2016-08-12 10:17:45 +02:00
|
|
|
@return TRUE if equal */
|
2014-02-26 19:11:54 +01:00
|
|
|
ibool
|
|
|
|
os_thread_eq(
|
|
|
|
/*=========*/
|
|
|
|
os_thread_id_t a, /*!< in: OS thread or thread id */
|
|
|
|
os_thread_id_t b); /*!< in: OS thread or thread id */
|
|
|
|
/****************************************************************//**
|
|
|
|
Converts an OS thread id to a ulint. It is NOT guaranteed that the ulint is
|
|
|
|
unique for the thread though!
|
2016-08-12 10:17:45 +02:00
|
|
|
@return thread identifier as a number */
|
2014-02-26 19:11:54 +01:00
|
|
|
ulint
|
|
|
|
os_thread_pf(
|
|
|
|
/*=========*/
|
|
|
|
os_thread_id_t a); /*!< in: OS thread identifier */
|
|
|
|
/****************************************************************//**
|
|
|
|
Creates a new thread of execution. The execution starts from
|
2016-08-12 10:17:45 +02:00
|
|
|
the function given.
|
2014-02-26 19:11:54 +01:00
|
|
|
NOTE: We count the number of threads in os_thread_exit(). A created
|
2016-08-12 10:17:45 +02:00
|
|
|
thread should always use that to exit so thatthe thread count will be
|
|
|
|
decremented.
|
|
|
|
We do not return an error code because if there is one, we crash here. */
|
2014-02-26 19:11:54 +01:00
|
|
|
os_thread_t
|
|
|
|
os_thread_create_func(
|
|
|
|
/*==================*/
|
|
|
|
os_thread_func_t func, /*!< in: pointer to function
|
|
|
|
from which to start */
|
|
|
|
void* arg, /*!< in: argument to start
|
|
|
|
function */
|
|
|
|
os_thread_id_t* thread_id); /*!< out: id of the created
|
|
|
|
thread, or NULL */
|
|
|
|
|
2016-09-06 08:43:16 +02:00
|
|
|
/** Exits the current thread. */
|
2014-02-26 19:11:54 +01:00
|
|
|
void
|
2016-09-06 08:43:16 +02:00
|
|
|
os_thread_exit()
|
2016-06-21 14:21:03 +02:00
|
|
|
UNIV_COLD MY_ATTRIBUTE((noreturn));
|
2016-09-06 08:43:16 +02:00
|
|
|
|
2014-02-26 19:11:54 +01:00
|
|
|
/*****************************************************************//**
|
|
|
|
Returns the thread identifier of current thread.
|
2016-08-12 10:17:45 +02:00
|
|
|
@return current thread identifier */
|
2014-02-26 19:11:54 +01:00
|
|
|
os_thread_id_t
|
|
|
|
os_thread_get_curr_id(void);
|
|
|
|
/*========================*/
|
|
|
|
/*****************************************************************//**
|
|
|
|
Advises the os to give up remainder of the thread's time slice. */
|
|
|
|
void
|
|
|
|
os_thread_yield(void);
|
|
|
|
/*=================*/
|
|
|
|
/*****************************************************************//**
|
|
|
|
The thread sleeps at least the time given in microseconds. */
|
|
|
|
void
|
|
|
|
os_thread_sleep(
|
|
|
|
/*============*/
|
|
|
|
ulint tm); /*!< in: time in microseconds */
|
|
|
|
|
2016-08-12 10:17:45 +02:00
|
|
|
/**
|
|
|
|
Initializes OS thread management data structures. */
|
|
|
|
void
|
|
|
|
os_thread_init();
|
|
|
|
/*============*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
Frees OS thread management data structures. */
|
|
|
|
void
|
|
|
|
os_thread_free();
|
|
|
|
/*============*/
|
|
|
|
|
|
|
|
/*****************************************************************//**
|
|
|
|
Check if there are threads active.
|
|
|
|
@return true if the thread count > 0. */
|
|
|
|
bool
|
|
|
|
os_thread_active();
|
|
|
|
/*==============*/
|
|
|
|
|
2014-02-26 19:11:54 +01:00
|
|
|
#endif
|