1/*****************************************************************************
2
3Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
4Copyright (c) 2017, MariaDB Corporation.
5
6This program is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free Software
8Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful, but WITHOUT
11ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program; if not, write to the Free Software Foundation, Inc.,
1651 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17
18*****************************************************************************/
19
20/**************************************************//**
21@file include/os0thread.h
22The interface to the operating system
23process and thread control primitives
24
25Created 9/8/1995 Heikki Tuuri
26*******************************************************/
27
28#ifndef os0thread_h
29#define os0thread_h
30
31#include "univ.i"
32
33/* Possible fixed priorities for threads */
34#define OS_THREAD_PRIORITY_NONE 100
35#define OS_THREAD_PRIORITY_BACKGROUND 1
36#define OS_THREAD_PRIORITY_NORMAL 2
37#define OS_THREAD_PRIORITY_ABOVE_NORMAL 3
38
39#ifdef _WIN32
40typedef DWORD os_thread_t;
41typedef DWORD os_thread_id_t; /*!< In Windows the thread id
42 is an unsigned long int */
43extern "C" {
44typedef LPTHREAD_START_ROUTINE os_thread_func_t;
45}
46
47/** Macro for specifying a Windows thread start function. */
48#define DECLARE_THREAD(func) WINAPI func
49
50#define os_thread_create(f,a,i) \
51 os_thread_create_func(f, a, i)
52
53#else
54
55typedef pthread_t os_thread_t;
56typedef pthread_t os_thread_id_t; /*!< In Unix we use the thread
57 handle itself as the id of
58 the thread */
59extern "C" { typedef void* (*os_thread_func_t)(void*); }
60
61/** Macro for specifying a POSIX thread start function. */
62#define DECLARE_THREAD(func) func
63#define os_thread_create(f,a,i) os_thread_create_func(f, a, i)
64
65#endif /* _WIN32 */
66
67/* Define a function pointer type to use in a typecast */
68typedef void* (*os_posix_f_t) (void*);
69
70#ifdef HAVE_PSI_INTERFACE
71/* Define for performance schema registration key */
72typedef unsigned int mysql_pfs_key_t;
73#endif /* HAVE_PSI_INTERFACE */
74
75/** Number of threads active. */
76extern ulint os_thread_count;
77
78/***************************************************************//**
79Compares two thread ids for equality.
80@return TRUE if equal */
81ibool
82os_thread_eq(
83/*=========*/
84 os_thread_id_t a, /*!< in: OS thread or thread id */
85 os_thread_id_t b); /*!< in: OS thread or thread id */
86/****************************************************************//**
87Converts an OS thread id to a ulint. It is NOT guaranteed that the ulint is
88unique for the thread though!
89@return thread identifier as a number */
90ulint
91os_thread_pf(
92/*=========*/
93 os_thread_id_t a); /*!< in: OS thread identifier */
94/****************************************************************//**
95Creates a new thread of execution. The execution starts from
96the function given.
97NOTE: We count the number of threads in os_thread_exit(). A created
98thread should always use that to exit so thatthe thread count will be
99decremented.
100We do not return an error code because if there is one, we crash here. */
101os_thread_t
102os_thread_create_func(
103/*==================*/
104 os_thread_func_t func, /*!< in: pointer to function
105 from which to start */
106 void* arg, /*!< in: argument to start
107 function */
108 os_thread_id_t* thread_id); /*!< out: id of the created
109 thread, or NULL */
110
111/** Waits until the specified thread completes and joins it.
112Its return value is ignored.
113@param[in,out] thread thread to join */
114void
115os_thread_join(
116 os_thread_id_t thread);
117
118/** Exits the current thread.
119@param[in] detach if true, the thread will be detached right before
120exiting. If false, another thread is responsible for joining this thread */
121ATTRIBUTE_NORETURN ATTRIBUTE_COLD
122void os_thread_exit(bool detach = true);
123
124/*****************************************************************//**
125Returns the thread identifier of current thread.
126@return current thread identifier */
127os_thread_id_t
128os_thread_get_curr_id(void);
129/*========================*/
130/*****************************************************************//**
131Advises the os to give up remainder of the thread's time slice. */
132void
133os_thread_yield(void);
134/*=================*/
135/*****************************************************************//**
136The thread sleeps at least the time given in microseconds. */
137void
138os_thread_sleep(
139/*============*/
140 ulint tm); /*!< in: time in microseconds */
141
142#endif
143