1
/******************************************************
2
The interface to the operating system thread control primitives
6
Created 9/8/1995 Heikki Tuuri
7
*******************************************************/
11
#include "os0thread.ic"
21
/*******************************************************************
22
Compares two thread ids for equality. */
27
/* out: TRUE if equal */
28
os_thread_id_t a, /* in: OS thread or thread id */
29
os_thread_id_t b) /* in: OS thread or thread id */
38
if (pthread_equal(a, b)) {
46
/********************************************************************
47
Converts an OS thread id to a ulint. It is NOT guaranteed that the ulint is
48
unique for the thread though! */
56
/* In HP-UX-10.20 a pthread_t is a struct of 3 fields: field1, field2,
57
field3. We do not know if field1 determines the thread uniquely. */
59
return((ulint)(a.field1));
65
/*********************************************************************
66
Returns the thread identifier of current thread. Currently the thread
67
identifier in Unix is the thread handle itself. Note that in HP-UX
68
pthread_t is a struct of 3 fields. */
71
os_thread_get_curr_id(void)
72
/*=======================*/
75
return(GetCurrentThreadId());
77
return(pthread_self());
81
/********************************************************************
82
Creates a new thread of execution. The execution starts from
83
the function given. The start function takes a void* parameter
84
and returns an ulint. */
89
/* out: handle to the thread */
93
ulint (*start_f)(void*), /* in: pointer to function
94
from which to start */
96
void* arg, /* in: argument to start
98
os_thread_id_t* thread_id) /* out: id of the created
105
os_mutex_enter(os_sync_mutex);
107
os_mutex_exit(os_sync_mutex);
109
thread = CreateThread(NULL, /* no security attributes */
110
0, /* default size stack */
111
(LPTHREAD_START_ROUTINE)start_f,
113
0, /* thread runs immediately */
116
if (srv_set_thread_priorities) {
118
/* Set created thread priority the same as a normal query
119
in MYSQL: we try to prevent starvation of threads by
120
assigning same priority QUERY_PRIOR to all */
122
ut_a(SetThreadPriority(thread, srv_query_thread_priority));
126
*thread_id = win_thread_id;
135
#if !(defined(UNIV_HOTBACKUP) && defined(UNIV_HPUX10))
136
pthread_attr_init(&attr);
140
/* We must make sure a thread stack is at least 32 kB, otherwise
141
InnoDB might crash; we do not know if the default stack size on
142
AIX is always big enough. An empirical test on AIX-4.3 suggested
143
the size was 96 kB, though. */
145
ret = pthread_attr_setstacksize(&attr,
146
(size_t)(PTHREAD_STACK_MIN
150
"InnoDB: Error: pthread_attr_setstacksize"
151
" returned %d\n", ret);
156
ret = pthread_attr_setstacksize(&attr,
157
(size_t) NW_THD_STACKSIZE);
160
"InnoDB: Error: pthread_attr_setstacksize"
161
" returned %d\n", ret);
165
os_mutex_enter(os_sync_mutex);
167
os_mutex_exit(os_sync_mutex);
169
#if defined(UNIV_HOTBACKUP) && defined(UNIV_HPUX10)
170
ret = pthread_create(&pthread, pthread_attr_default, start_f, arg);
172
ret = pthread_create(&pthread, &attr, start_f, arg);
176
"InnoDB: Error: pthread_create returned %d\n", ret);
180
#if !(defined(UNIV_HOTBACKUP) && defined(UNIV_HPUX10))
181
pthread_attr_destroy(&attr);
183
if (srv_set_thread_priorities) {
185
my_pthread_setprio(pthread, srv_query_thread_priority);
189
*thread_id = pthread;
196
/*********************************************************************
197
Exits the current thread. */
202
void* exit_value) /* in: exit value; in Windows this void*
203
is cast as a DWORD */
205
#ifdef UNIV_DEBUG_THREAD_CREATION
206
fprintf(stderr, "Thread exits, id %lu\n",
207
os_thread_pf(os_thread_get_curr_id()));
209
os_mutex_enter(os_sync_mutex);
211
os_mutex_exit(os_sync_mutex);
214
ExitThread((DWORD)exit_value);
216
pthread_exit(exit_value);
220
#ifdef HAVE_PTHREAD_JOIN
224
os_thread_id_t thread_id) /* in: id of the thread to join */
226
return(pthread_join(thread_id, NULL));
229
/*********************************************************************
230
Returns handle to the current thread. */
233
os_thread_get_curr(void)
234
/*====================*/
237
return(GetCurrentThread());
239
return(pthread_self());
243
/*********************************************************************
244
Advises the os to give up remainder of the thread's time slice. */
247
os_thread_yield(void)
248
/*=================*/
252
#elif (defined(HAVE_SCHED_YIELD) && defined(HAVE_SCHED_H))
254
#elif defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
256
#elif defined(HAVE_PTHREAD_YIELD_ONE_ARG)
263
/*********************************************************************
264
The thread sleeps at least the time given in microseconds. */
269
ulint tm) /* in: time in microseconds */
272
Sleep((DWORD) tm / 1000);
273
#elif defined(__NETWARE__)
278
t.tv_sec = tm / 1000000;
279
t.tv_usec = tm % 1000000;
281
select(0, NULL, NULL, NULL, &t);
285
/**********************************************************************
286
Sets a thread priority. */
289
os_thread_set_priority(
290
/*===================*/
291
os_thread_t handle, /* in: OS handle to the thread */
292
ulint pri) /* in: priority */
297
if (pri == OS_THREAD_PRIORITY_BACKGROUND) {
298
os_pri = THREAD_PRIORITY_BELOW_NORMAL;
299
} else if (pri == OS_THREAD_PRIORITY_NORMAL) {
300
os_pri = THREAD_PRIORITY_NORMAL;
301
} else if (pri == OS_THREAD_PRIORITY_ABOVE_NORMAL) {
302
os_pri = THREAD_PRIORITY_HIGHEST;
307
ut_a(SetThreadPriority(handle, os_pri));
314
/**********************************************************************
315
Gets a thread priority. */
318
os_thread_get_priority(
319
/*===================*/
321
os_thread_t handle __attribute__((unused)))
322
/* in: OS handle to the thread */
328
os_pri = GetThreadPriority(handle);
330
if (os_pri == THREAD_PRIORITY_BELOW_NORMAL) {
331
pri = OS_THREAD_PRIORITY_BACKGROUND;
332
} else if (os_pri == THREAD_PRIORITY_NORMAL) {
333
pri = OS_THREAD_PRIORITY_NORMAL;
334
} else if (os_pri == THREAD_PRIORITY_HIGHEST) {
335
pri = OS_THREAD_PRIORITY_ABOVE_NORMAL;
346
/**********************************************************************
347
Gets the last operating system error code for the calling thread. */
350
os_thread_get_last_error(void)
351
/*==========================*/
354
return(GetLastError());