1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation.  The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /**************************************************//**
28 @file include/os0thread.h
29 The interface to the operating system
30 process and thread control primitives
31 
32 Created 9/8/1995 Heikki Tuuri
33 *******************************************************/
34 
35 #ifndef os0thread_h
36 #define os0thread_h
37 
38 #include "univ.i"
39 
40 /* Maximum number of threads which can be created in the program;
41 this is also the size of the wait slot array for MySQL threads which
42 can wait inside InnoDB */
43 
44 #define	OS_THREAD_MAX_N		srv_max_n_threads
45 
46 /* Possible fixed priorities for threads */
47 #define OS_THREAD_PRIORITY_NONE		100
48 #define OS_THREAD_PRIORITY_BACKGROUND	1
49 #define OS_THREAD_PRIORITY_NORMAL	2
50 #define OS_THREAD_PRIORITY_ABOVE_NORMAL	3
51 
52 #ifdef __WIN__
53 typedef void*			os_thread_t;
54 typedef DWORD			os_thread_id_t;	/*!< In Windows the thread id
55 						is an unsigned long int */
56 extern "C"  {
57 typedef LPTHREAD_START_ROUTINE	os_thread_func_t;
58 }
59 
60 /** Macro for specifying a Windows thread start function. */
61 #define DECLARE_THREAD(func)	WINAPI func
62 
63 /** Required to get around a build error on Windows. Even though our functions
64 are defined/declared as WINAPI f(LPVOID a); the compiler complains that they
65 are defined as: os_thread_ret_t (__cdecl*)(void*). Because our functions
66 don't access the arguments and don't return any value, we should be safe. */
67 #define os_thread_create(f,a,i)	\
68 	os_thread_create_func(reinterpret_cast<os_thread_func_t>(f), a, i)
69 
70 #else
71 
72 typedef pthread_t		os_thread_t;
73 typedef os_thread_t		os_thread_id_t;	/*!< In Unix we use the thread
74 						handle itself as the id of
75 						the thread */
76 extern "C"  { typedef void*	(*os_thread_func_t)(void*); }
77 
78 /** Macro for specifying a POSIX thread start function. */
79 #define DECLARE_THREAD(func)	func
80 #define os_thread_create(f,a,i)	os_thread_create_func(f, a, i)
81 
82 #endif /* __WIN__ */
83 
84 /* Define a function pointer type to use in a typecast */
85 typedef void* (*os_posix_f_t) (void*);
86 
87 #ifdef HAVE_PSI_INTERFACE
88 /* Define for performance schema registration key */
89 typedef unsigned int    mysql_pfs_key_t;
90 #endif
91 
92 /***************************************************************//**
93 Compares two thread ids for equality.
94 @return	TRUE if equal */
95 UNIV_INTERN
96 ibool
97 os_thread_eq(
98 /*=========*/
99 	os_thread_id_t	a,	/*!< in: OS thread or thread id */
100 	os_thread_id_t	b);	/*!< in: OS thread or thread id */
101 /****************************************************************//**
102 Converts an OS thread id to a ulint. It is NOT guaranteed that the ulint is
103 unique for the thread though!
104 @return	thread identifier as a number */
105 UNIV_INTERN
106 ulint
107 os_thread_pf(
108 /*=========*/
109 	os_thread_id_t	a);	/*!< in: OS thread identifier */
110 /****************************************************************//**
111 Creates a new thread of execution. The execution starts from
112 the function given. The start function takes a void* parameter
113 and returns a ulint.
114 NOTE: We count the number of threads in os_thread_exit(). A created
115 thread should always use that to exit and not use return() to exit.
116 @return	handle to the thread */
117 UNIV_INTERN
118 os_thread_t
119 os_thread_create_func(
120 /*==================*/
121 	os_thread_func_t	func,		/*!< in: pointer to function
122 						from which to start */
123 	void*			arg,		/*!< in: argument to start
124 						function */
125 	os_thread_id_t*		thread_id);	/*!< out: id of the created
126 						thread, or NULL */
127 
128 /** Waits until the specified thread completes and joins it.
129 Its return value is ignored.
130 @param[in,out]	thread	thread to join */
131 UNIV_INTERN
132 void
133 os_thread_join(
134 	os_thread_t	thread);
135 
136 /*****************************************************************//**
137 Exits the current thread. */
138 UNIV_INTERN
139 void
140 os_thread_exit(
141 /*===========*/
142 	void*	exit_value,	/*!< in: exit value; in Windows this void*
143 				is cast as a DWORD */
144 	bool	detach = true)	/*!< in: if true, the thread will be detached
145 				right before exiting. If false, another thread
146 				is responsible for joining this thread. */
147 	UNIV_COLD MY_ATTRIBUTE((noreturn));
148 /*****************************************************************//**
149 Returns the thread identifier of current thread.
150 @return	current thread identifier */
151 UNIV_INTERN
152 os_thread_id_t
153 os_thread_get_curr_id(void);
154 /*========================*/
155 /*****************************************************************//**
156 Advises the os to give up remainder of the thread's time slice. */
157 UNIV_INTERN
158 void
159 os_thread_yield(void);
160 /*=================*/
161 /*****************************************************************//**
162 The thread sleeps at least the time given in microseconds. */
163 UNIV_INTERN
164 void
165 os_thread_sleep(
166 /*============*/
167 	ulint	tm);	/*!< in: time in microseconds */
168 
169 #ifndef UNIV_NONINL
170 #include "os0thread.ic"
171 #endif
172 
173 #endif
174