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 #ifdef UNIV_LINUX
41 #include <sys/types.h>
42 #endif
43 
44 /* Maximum number of threads which can be created in the program;
45 this is also the size of the wait slot array for MySQL threads which
46 can wait inside InnoDB */
47 
48 #define	OS_THREAD_MAX_N		srv_max_n_threads
49 
50 /* Possible fixed priorities for threads */
51 #define OS_THREAD_PRIORITY_NONE		100
52 #define OS_THREAD_PRIORITY_BACKGROUND	1
53 #define OS_THREAD_PRIORITY_NORMAL	2
54 #define OS_THREAD_PRIORITY_ABOVE_NORMAL	3
55 
56 #ifdef __WIN__
57 typedef void*			os_thread_t;
58 typedef DWORD			os_thread_id_t;	/*!< In Windows the thread id
59 						is an unsigned long int */
60 typedef os_thread_id_t		os_tid_t;
61 extern "C"  {
62 typedef LPTHREAD_START_ROUTINE	os_thread_func_t;
63 }
64 
65 /** Macro for specifying a Windows thread start function. */
66 #define DECLARE_THREAD(func)	WINAPI func
67 
68 /** Required to get around a build error on Windows. Even though our functions
69 are defined/declared as WINAPI f(LPVOID a); the compiler complains that they
70 are defined as: os_thread_ret_t (__cdecl*)(void*). Because our functions
71 don't access the arguments and don't return any value, we should be safe. */
72 #define os_thread_create(f,a,i)	\
73 	os_thread_create_func(reinterpret_cast<os_thread_func_t>(f), a, i)
74 
75 #else
76 
77 typedef pthread_t		os_thread_t;
78 typedef os_thread_t		os_thread_id_t;	/*!< In Unix we use the thread
79 						handle itself as the id of
80 						the thread */
81 #ifdef UNIV_LINUX
82 typedef pid_t			os_tid_t;	/*!< An alias for pid_t on
83 						Linux, where setpriority()
84 						accepts thread id of this type
85 						and not pthread_t */
86 #else
87 typedef os_thread_id_t		os_tid_t;
88 #endif
89 
90 extern "C"  { typedef void*	(*os_thread_func_t)(void*); }
91 
92 /** Macro for specifying a POSIX thread start function. */
93 #define DECLARE_THREAD(func)	func
94 #define os_thread_create(f,a,i)	os_thread_create_func(f, a, i)
95 
96 #endif /* __WIN__ */
97 
98 /* Define a function pointer type to use in a typecast */
99 typedef void* (*os_posix_f_t) (void*);
100 
101 #ifdef HAVE_PSI_INTERFACE
102 /* Define for performance schema registration key */
103 typedef unsigned int    mysql_pfs_key_t;
104 #endif
105 
106 /***************************************************************//**
107 Compares two thread ids for equality.
108 @return	TRUE if equal */
109 UNIV_INTERN
110 ibool
111 os_thread_eq(
112 /*=========*/
113 	os_thread_id_t	a,	/*!< in: OS thread or thread id */
114 	os_thread_id_t	b);	/*!< in: OS thread or thread id */
115 /****************************************************************//**
116 Converts an OS thread id to a ulint. It is NOT guaranteed that the ulint is
117 unique for the thread though!
118 @return	thread identifier as a number */
119 UNIV_INTERN
120 ulint
121 os_thread_pf(
122 /*=========*/
123 	os_thread_id_t	a);	/*!< in: OS thread identifier */
124 /****************************************************************//**
125 Creates a new thread of execution. The execution starts from
126 the function given. The start function takes a void* parameter
127 and returns a ulint.
128 NOTE: We count the number of threads in os_thread_exit(). A created
129 thread should always use that to exit and not use return() to exit.
130 @return	handle to the thread */
131 UNIV_INTERN
132 os_thread_t
133 os_thread_create_func(
134 /*==================*/
135 	os_thread_func_t	func,		/*!< in: pointer to function
136 						from which to start */
137 	void*			arg,		/*!< in: argument to start
138 						function */
139 	os_thread_id_t*		thread_id);	/*!< out: id of the created
140 						thread, or NULL */
141 
142 /** Waits until the specified thread completes and joins it.
143 Its return value is ignored.
144 @param[in,out]	thread	thread to join */
145 UNIV_INTERN
146 void
147 os_thread_join(
148 	os_thread_t	thread);
149 
150 /*****************************************************************//**
151 Exits the current thread. */
152 UNIV_INTERN
153 void
154 os_thread_exit(
155 /*===========*/
156 	void*	exit_value,	/*!< in: exit value; in Windows this void*
157 				is cast as a DWORD */
158 	bool	detach = true)	/*!< in: if true, the thread will be detached
159 				right before exiting. If false, another thread
160 				is responsible for joining this thread. */
161 	UNIV_COLD MY_ATTRIBUTE((noreturn));
162 /*****************************************************************//**
163 Returns the thread identifier of current thread.
164 @return	current thread identifier */
165 UNIV_INTERN
166 os_thread_id_t
167 os_thread_get_curr_id(void);
168 /*========================*/
169 /*****************************************************************//**
170 Returns the system-specific thread identifier of current thread.  On Linux,
171 returns tid.  On other systems currently returns os_thread_get_curr_id().
172 
173 @return	current thread identifier */
174 UNIV_INTERN
175 os_tid_t
176 os_thread_get_tid(void);
177 /*=====================*/
178 /*****************************************************************//**
179 Advises the os to give up remainder of the thread's time slice. */
180 UNIV_INTERN
181 void
182 os_thread_yield(void);
183 /*=================*/
184 /*****************************************************************//**
185 The thread sleeps at least the time given in microseconds. */
186 UNIV_INTERN
187 void
188 os_thread_sleep(
189 /*============*/
190 	ulint	tm);	/*!< in: time in microseconds */
191 /*****************************************************************//**
192 Set relative scheduling priority for a given thread on Linux.  Currently a
193 no-op on other systems.
194 
195 @return An actual thread priority after the update  */
196 UNIV_INTERN
197 ulint
198 os_thread_set_priority(
199 /*===================*/
200 	os_tid_t	thread_id,		/*!< in: thread id */
201 	ulint		relative_priority);	/*!< in: system-specific
202 						priority value */
203 
204 #ifndef UNIV_NONINL
205 #include "os0thread.ic"
206 #endif
207 
208 #endif
209