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) {
184
struct sched_param tmp_sched_param;
186
memset(&tmp_sched_param, 0, sizeof(tmp_sched_param));
187
tmp_sched_param.sched_priority= srv_query_thread_priority;
188
(void)pthread_setschedparam(pthread, SCHED_OTHER, &tmp_sched_param);
192
*thread_id = pthread;
199
/*********************************************************************
200
Exits the current thread. */
205
void* exit_value) /* in: exit value; in Windows this void*
206
is cast as a DWORD */
208
#ifdef UNIV_DEBUG_THREAD_CREATION
209
fprintf(stderr, "Thread exits, id %lu\n",
210
os_thread_pf(os_thread_get_curr_id()));
212
os_mutex_enter(os_sync_mutex);
214
os_mutex_exit(os_sync_mutex);
217
ExitThread((DWORD)exit_value);
219
pthread_exit(exit_value);
223
#ifdef HAVE_PTHREAD_JOIN
227
os_thread_id_t thread_id) /* in: id of the thread to join */
229
return(pthread_join(thread_id, NULL));
232
/*********************************************************************
233
Returns handle to the current thread. */
236
os_thread_get_curr(void)
237
/*====================*/
240
return(GetCurrentThread());
242
return(pthread_self());
246
/*********************************************************************
247
Advises the os to give up remainder of the thread's time slice. */
250
os_thread_yield(void)
251
/*=================*/
255
#elif (defined(HAVE_SCHED_YIELD) && defined(HAVE_SCHED_H))
257
#elif defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
259
#elif defined(HAVE_PTHREAD_YIELD_ONE_ARG)
266
/*********************************************************************
267
The thread sleeps at least the time given in microseconds. */
272
ulint tm) /* in: time in microseconds */
275
Sleep((DWORD) tm / 1000);
276
#elif defined(__NETWARE__)
281
t.tv_sec = tm / 1000000;
282
t.tv_usec = tm % 1000000;
284
select(0, NULL, NULL, NULL, &t);
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. */
321
os_thread_get_priority(
322
/*===================*/
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. */
353
os_thread_get_last_error(void)
354
/*==========================*/
357
return(GetLastError());