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"
34
#include <sys/select.h>
37
#ifndef UNIV_HOTBACKUP
41
/***************************************************************//**
42
Compares two thread ids for equality.
43
@return TRUE if equal */
48
os_thread_id_t a, /*!< in: OS thread or thread id */
49
os_thread_id_t b) /*!< in: OS thread or thread id */
58
if (pthread_equal(a, b)) {
66
/****************************************************************//**
67
Converts an OS thread id to a ulint. It is NOT guaranteed that the ulint is
68
unique for the thread though!
69
@return thread identifier as a number */
74
os_thread_id_t a) /*!< in: OS thread identifier */
77
/* In HP-UX-10.20 a pthread_t is a struct of 3 fields: field1, field2,
78
field3. We do not know if field1 determines the thread uniquely. */
80
return((ulint)(a.field1));
86
/*****************************************************************//**
87
Returns the thread identifier of current thread. Currently the thread
88
identifier in Unix is the thread handle itself. Note that in HP-UX
89
pthread_t is a struct of 3 fields.
90
@return current thread identifier */
93
os_thread_get_curr_id(void)
94
/*=======================*/
97
return(GetCurrentThreadId());
99
return(pthread_self());
103
/****************************************************************//**
104
Creates a new thread of execution. The execution starts from
105
the function given. The start function takes a void* parameter
106
and returns an ulint.
107
@return handle to the thread */
113
os_posix_f_t start_f,
115
ulint (*start_f)(void*), /*!< in: pointer to function
116
from which to start */
118
void* arg, /*!< in: argument to start
120
os_thread_id_t* thread_id) /*!< out: id of the created
127
os_mutex_enter(os_sync_mutex);
129
os_mutex_exit(os_sync_mutex);
131
thread = CreateThread(NULL, /* no security attributes */
132
0, /* default size stack */
133
(LPTHREAD_START_ROUTINE)start_f,
135
0, /* thread runs immediately */
139
*thread_id = win_thread_id;
149
pthread_attr_init(&attr);
153
/* We must make sure a thread stack is at least 32 kB, otherwise
154
InnoDB might crash; we do not know if the default stack size on
155
AIX is always big enough. An empirical test on AIX-4.3 suggested
156
the size was 96 kB, though. */
158
ret = pthread_attr_setstacksize(&attr,
159
(size_t)(PTHREAD_STACK_MIN
163
"InnoDB: Error: pthread_attr_setstacksize"
164
" returned %d\n", ret);
169
ret = pthread_attr_setstacksize(&attr,
170
(size_t) NW_THD_STACKSIZE);
173
"InnoDB: Error: pthread_attr_setstacksize"
174
" returned %d\n", ret);
178
os_mutex_enter(os_sync_mutex);
180
os_mutex_exit(os_sync_mutex);
183
ret = pthread_create(&pthread, pthread_attr_default, start_f, arg);
185
ret = pthread_create(&pthread, &attr, start_f, arg);
189
"InnoDB: Error: pthread_create returned %d\n", ret);
194
pthread_attr_destroy(&attr);
198
*thread_id = pthread;
205
/*****************************************************************//**
206
Exits the current thread. */
211
void* exit_value) /*!< in: exit value; in Windows this void*
212
is cast as a DWORD */
214
#ifdef UNIV_DEBUG_THREAD_CREATION
215
fprintf(stderr, "Thread exits, id %lu\n",
216
os_thread_pf(os_thread_get_curr_id()));
218
os_mutex_enter(os_sync_mutex);
220
os_mutex_exit(os_sync_mutex);
223
ExitThread((DWORD)exit_value);
225
pthread_exit(exit_value);
229
/*****************************************************************//**
230
Returns handle to the current thread.
231
@return current thread handle */
234
os_thread_get_curr(void)
235
/*====================*/
238
return(GetCurrentThread());
240
return(pthread_self());
244
/*****************************************************************//**
245
Advises the os to give up remainder of the thread's time slice. */
248
os_thread_yield(void)
249
/*=================*/
253
#elif (defined(HAVE_SCHED_YIELD) && defined(HAVE_SCHED_H))
255
#elif defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
257
#elif defined(HAVE_PTHREAD_YIELD_ONE_ARG)
263
#endif /* !UNIV_HOTBACKUP */
265
/*****************************************************************//**
266
The thread sleeps at least the time given in microseconds. */
271
ulint tm) /*!< in: time in microseconds */
274
Sleep((DWORD) tm / 1000);
275
#elif defined(__NETWARE__)
280
t.tv_sec = tm / 1000000;
281
t.tv_usec = tm % 1000000;
283
select(0, NULL, NULL, NULL, &t);
287
#ifndef UNIV_HOTBACKUP
288
/******************************************************************//**
289
Sets a thread priority. */
292
os_thread_set_priority(
293
/*===================*/
294
os_thread_t handle, /*!< in: OS handle to the thread */
295
ulint pri) /*!< in: priority */
300
if (pri == OS_THREAD_PRIORITY_BACKGROUND) {
301
os_pri = THREAD_PRIORITY_BELOW_NORMAL;
302
} else if (pri == OS_THREAD_PRIORITY_NORMAL) {
303
os_pri = THREAD_PRIORITY_NORMAL;
304
} else if (pri == OS_THREAD_PRIORITY_ABOVE_NORMAL) {
305
os_pri = THREAD_PRIORITY_HIGHEST;
310
ut_a(SetThreadPriority(handle, os_pri));
317
/******************************************************************//**
318
Gets a thread priority.
322
os_thread_get_priority(
323
/*===================*/
324
os_thread_t handle __attribute__((unused)))
325
/*!< in: OS handle to the thread */
331
os_pri = GetThreadPriority(handle);
333
if (os_pri == THREAD_PRIORITY_BELOW_NORMAL) {
334
pri = OS_THREAD_PRIORITY_BACKGROUND;
335
} else if (os_pri == THREAD_PRIORITY_NORMAL) {
336
pri = OS_THREAD_PRIORITY_NORMAL;
337
} else if (os_pri == THREAD_PRIORITY_HIGHEST) {
338
pri = OS_THREAD_PRIORITY_ABOVE_NORMAL;
349
/******************************************************************//**
350
Gets the last operating system error code for the calling thread.
351
@return last error on Windows, 0 otherwise */
354
os_thread_get_last_error(void)
355
/*==========================*/
358
return(GetLastError());
363
#endif /* !UNIV_HOTBACKUP */