1
/*****************************************************************************
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
*****************************************************************************/
19
/**************************************************//**
21
The interface to the operating system thread control primitives
23
Created 9/8/1995 Heikki Tuuri
24
*******************************************************/
26
#include "os0thread.h"
28
#include "os0thread.ic"
35
#ifndef UNIV_HOTBACKUP
39
/***************************************************************//**
40
Compares two thread ids for equality.
41
@return TRUE if equal */
46
os_thread_id_t a, /*!< in: OS thread or thread id */
47
os_thread_id_t b) /*!< in: OS thread or thread id */
56
if (pthread_equal(a, b)) {
64
/****************************************************************//**
65
Converts an OS thread id to a ulint. It is NOT guaranteed that the ulint is
66
unique for the thread though!
67
@return thread identifier as a number */
72
os_thread_id_t a) /*!< in: OS thread identifier */
75
/* In HP-UX-10.20 a pthread_t is a struct of 3 fields: field1, field2,
76
field3. We do not know if field1 determines the thread uniquely. */
78
return((ulint)(a.field1));
84
/*****************************************************************//**
85
Returns the thread identifier of current thread. Currently the thread
86
identifier in Unix is the thread handle itself. Note that in HP-UX
87
pthread_t is a struct of 3 fields.
88
@return current thread identifier */
91
os_thread_get_curr_id(void)
92
/*=======================*/
95
return(GetCurrentThreadId());
97
return(pthread_self());
101
/****************************************************************//**
102
Creates a new thread of execution. The execution starts from
103
the function given. The start function takes a void* parameter
104
and returns an ulint.
105
@return handle to the thread */
111
os_posix_f_t start_f,
113
ulint (*start_f)(void*), /*!< in: pointer to function
114
from which to start */
116
void* arg, /*!< in: argument to start
118
os_thread_id_t* thread_id) /*!< out: id of the created
125
os_mutex_enter(os_sync_mutex);
127
os_mutex_exit(os_sync_mutex);
129
thread = CreateThread(NULL, /* no security attributes */
130
0, /* default size stack */
131
(LPTHREAD_START_ROUTINE)start_f,
133
0, /* thread runs immediately */
136
if (srv_set_thread_priorities) {
138
/* Set created thread priority the same as a normal query
139
in MYSQL: we try to prevent starvation of threads by
140
assigning same priority QUERY_PRIOR to all */
142
ut_a(SetThreadPriority(thread, srv_query_thread_priority));
146
*thread_id = win_thread_id;
156
pthread_attr_init(&attr);
160
/* We must make sure a thread stack is at least 32 kB, otherwise
161
InnoDB might crash; we do not know if the default stack size on
162
AIX is always big enough. An empirical test on AIX-4.3 suggested
163
the size was 96 kB, though. */
165
ret = pthread_attr_setstacksize(&attr,
166
(size_t)(PTHREAD_STACK_MIN
170
"InnoDB: Error: pthread_attr_setstacksize"
171
" returned %d\n", ret);
176
ret = pthread_attr_setstacksize(&attr,
177
(size_t) NW_THD_STACKSIZE);
180
"InnoDB: Error: pthread_attr_setstacksize"
181
" returned %d\n", ret);
185
os_mutex_enter(os_sync_mutex);
187
os_mutex_exit(os_sync_mutex);
190
ret = pthread_create(&pthread, pthread_attr_default, start_f, arg);
192
ret = pthread_create(&pthread, &attr, start_f, arg);
196
"InnoDB: Error: pthread_create returned %d\n", ret);
201
pthread_attr_destroy(&attr);
203
if (srv_set_thread_priorities) {
205
struct sched_param tmp_sched_param;
207
memset(&tmp_sched_param, 0, sizeof(tmp_sched_param));
208
tmp_sched_param.sched_priority= srv_query_thread_priority;
209
(void)pthread_setschedparam(pthread, SCHED_OTHER, &tmp_sched_param);
213
*thread_id = pthread;
220
/*****************************************************************//**
221
Exits the current thread. */
226
void* exit_value) /*!< in: exit value; in Windows this void*
227
is cast as a DWORD */
229
#ifdef UNIV_DEBUG_THREAD_CREATION
230
fprintf(stderr, "Thread exits, id %lu\n",
231
os_thread_pf(os_thread_get_curr_id()));
233
os_mutex_enter(os_sync_mutex);
235
os_mutex_exit(os_sync_mutex);
238
ExitThread((DWORD)exit_value);
240
pthread_exit(exit_value);
244
/*****************************************************************//**
245
Returns handle to the current thread.
246
@return current thread handle */
249
os_thread_get_curr(void)
250
/*====================*/
253
return(GetCurrentThread());
255
return(pthread_self());
259
/*****************************************************************//**
260
Advises the os to give up remainder of the thread's time slice. */
263
os_thread_yield(void)
264
/*=================*/
268
#elif (defined(HAVE_SCHED_YIELD) && defined(HAVE_SCHED_H))
270
#elif defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
272
#elif defined(HAVE_PTHREAD_YIELD_ONE_ARG)
278
#endif /* !UNIV_HOTBACKUP */
280
/*****************************************************************//**
281
The thread sleeps at least the time given in microseconds. */
286
ulint tm) /*!< in: time in microseconds */
289
Sleep((DWORD) tm / 1000);
290
#elif defined(__NETWARE__)
295
t.tv_sec = tm / 1000000;
296
t.tv_usec = tm % 1000000;
298
select(0, NULL, NULL, NULL, &t);
302
#ifndef UNIV_HOTBACKUP
303
/******************************************************************//**
304
Sets a thread priority. */
307
os_thread_set_priority(
308
/*===================*/
309
os_thread_t handle, /*!< in: OS handle to the thread */
310
ulint pri) /*!< in: priority */
315
if (pri == OS_THREAD_PRIORITY_BACKGROUND) {
316
os_pri = THREAD_PRIORITY_BELOW_NORMAL;
317
} else if (pri == OS_THREAD_PRIORITY_NORMAL) {
318
os_pri = THREAD_PRIORITY_NORMAL;
319
} else if (pri == OS_THREAD_PRIORITY_ABOVE_NORMAL) {
320
os_pri = THREAD_PRIORITY_HIGHEST;
325
ut_a(SetThreadPriority(handle, os_pri));
332
/******************************************************************//**
333
Gets a thread priority.
337
os_thread_get_priority(
338
/*===================*/
339
os_thread_t handle __attribute__((unused)))
340
/*!< in: OS handle to the thread */
346
os_pri = GetThreadPriority(handle);
348
if (os_pri == THREAD_PRIORITY_BELOW_NORMAL) {
349
pri = OS_THREAD_PRIORITY_BACKGROUND;
350
} else if (os_pri == THREAD_PRIORITY_NORMAL) {
351
pri = OS_THREAD_PRIORITY_NORMAL;
352
} else if (os_pri == THREAD_PRIORITY_HIGHEST) {
353
pri = OS_THREAD_PRIORITY_ABOVE_NORMAL;
364
/******************************************************************//**
365
Gets the last operating system error code for the calling thread.
366
@return last error on Windows, 0 otherwise */
369
os_thread_get_last_error(void)
370
/*==========================*/
373
return(GetLastError());
378
#endif /* !UNIV_HOTBACKUP */