1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2017, 2020, MariaDB Corporation.
5 
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free Software
8 Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
17 
18 *****************************************************************************/
19 
20 /**************************************************//**
21 @file include/os0thread.h
22 The interface to the operating system
23 process and thread control primitives
24 
25 Created 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
40 typedef DWORD			os_thread_t;
41 typedef DWORD			os_thread_id_t;	/*!< In Windows the thread id
42 						is an unsigned long int */
43 extern "C"  {
44 typedef 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 #else
50 
51 typedef pthread_t		os_thread_t;
52 typedef pthread_t		os_thread_id_t;	/*!< In Unix we use the thread
53 						handle itself as the id of
54 						the thread */
55 extern "C"  { typedef void*	(*os_thread_func_t)(void*); }
56 
57 /** Macro for specifying a POSIX thread start function. */
58 #define DECLARE_THREAD(func)	func
59 #endif /* _WIN32 */
60 
61 /* Define a function pointer type to use in a typecast */
62 typedef void* (*os_posix_f_t) (void*);
63 
64 #ifdef HAVE_PSI_INTERFACE
65 /* Define for performance schema registration key */
66 typedef unsigned int    mysql_pfs_key_t;
67 #endif /* HAVE_PSI_INTERFACE */
68 
69 #ifndef _WIN32
70 #define os_thread_eq(a,b) pthread_equal(a, b)
71 #define os_thread_yield() sched_yield()
72 #define os_thread_get_curr_id() pthread_self()
73 #else
74 bool os_thread_eq(os_thread_id_t a, os_thread_id_t b);
75 void os_thread_yield();
76 os_thread_id_t os_thread_get_curr_id();
77 #endif
78 
79 /****************************************************************//**
80 Creates a new thread of execution. The execution starts from
81 the function given.
82 NOTE: We count the number of threads in os_thread_exit(). A created
83 thread should always use that to exit so thatthe thread count will be
84 decremented.
85 We do not return an error code because if there is one, we crash here. */
86 os_thread_t os_thread_create(os_thread_func_t func, void *arg= nullptr);
87 
88 /** Detach and terminate the current thread. */
89 ATTRIBUTE_NORETURN void os_thread_exit();
90 
91 /*****************************************************************//**
92 The thread sleeps at least the time given in microseconds. */
93 void
94 os_thread_sleep(
95 /*============*/
96 	ulint	tm);	/*!< in: time in microseconds */
97 
98 #endif
99