1
/*****************************************************************************
3
Copyright (c) 1995, 2010, 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 */
137
*thread_id = win_thread_id;
147
pthread_attr_init(&attr);
151
/* We must make sure a thread stack is at least 32 kB, otherwise
152
InnoDB might crash; we do not know if the default stack size on
153
AIX is always big enough. An empirical test on AIX-4.3 suggested
154
the size was 96 kB, though. */
156
ret = pthread_attr_setstacksize(&attr,
157
(size_t)(PTHREAD_STACK_MIN
161
"InnoDB: Error: pthread_attr_setstacksize"
162
" returned %d\n", ret);
166
os_mutex_enter(os_sync_mutex);
168
os_mutex_exit(os_sync_mutex);
171
ret = pthread_create(&pthread, pthread_attr_default, start_f, arg);
173
ret = pthread_create(&pthread, &attr, start_f, arg);
177
"InnoDB: Error: pthread_create returned %d\n", ret);
182
pthread_attr_destroy(&attr);
185
*thread_id = pthread;
192
/*****************************************************************//**
193
Exits the current thread. */
198
void* exit_value) /*!< in: exit value; in Windows this void*
199
is cast as a DWORD */
201
#ifdef UNIV_DEBUG_THREAD_CREATION
202
fprintf(stderr, "Thread exits, id %lu\n",
203
os_thread_pf(os_thread_get_curr_id()));
206
#ifdef UNIV_PFS_THREAD
210
os_mutex_enter(os_sync_mutex);
212
os_mutex_exit(os_sync_mutex);
215
ExitThread((DWORD)exit_value);
217
pthread_detach(pthread_self());
218
pthread_exit(exit_value);
222
/*****************************************************************//**
223
Advises the os to give up remainder of the thread's time slice. */
226
os_thread_yield(void)
227
/*=================*/
231
#elif (defined(HAVE_SCHED_YIELD) && defined(HAVE_SCHED_H))
233
#elif defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
235
#elif defined(HAVE_PTHREAD_YIELD_ONE_ARG)
241
#endif /* !UNIV_HOTBACKUP */
243
/*****************************************************************//**
244
The thread sleeps at least the time given in microseconds. */
249
ulint tm) /*!< in: time in microseconds */
252
Sleep((DWORD) tm / 1000);
256
t.tv_sec = tm / 1000000;
257
t.tv_usec = tm % 1000000;
259
select(0, NULL, NULL, NULL, &t);