~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/os/os0thread.c

  • Committer: Brian Aker
  • Date: 2010-01-27 18:58:12 UTC
  • Revision ID: brian@gaz-20100127185812-n62n0vwetnx8jrjy
Remove dead code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 
 
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
 
4
 
 
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.
 
8
 
 
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.
 
12
 
 
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
 
16
 
 
17
*****************************************************************************/
 
18
 
 
19
/**************************************************//**
 
20
@file os/os0thread.c
 
21
The interface to the operating system thread control primitives
 
22
 
 
23
Created 9/8/1995 Heikki Tuuri
 
24
*******************************************************/
 
25
 
 
26
#include "os0thread.h"
 
27
#ifdef UNIV_NONINL
 
28
#include "os0thread.ic"
 
29
#endif
 
30
 
 
31
#ifdef __WIN__
 
32
#include <windows.h>
 
33
#endif
 
34
 
 
35
#ifndef UNIV_HOTBACKUP
 
36
#include "srv0srv.h"
 
37
#include "os0sync.h"
 
38
 
 
39
/***************************************************************//**
 
40
Compares two thread ids for equality.
 
41
@return TRUE if equal */
 
42
UNIV_INTERN
 
43
ibool
 
44
os_thread_eq(
 
45
/*=========*/
 
46
        os_thread_id_t  a,      /*!< in: OS thread or thread id */
 
47
        os_thread_id_t  b)      /*!< in: OS thread or thread id */
 
48
{
 
49
#ifdef __WIN__
 
50
        if (a == b) {
 
51
                return(TRUE);
 
52
        }
 
53
 
 
54
        return(FALSE);
 
55
#else
 
56
        if (pthread_equal(a, b)) {
 
57
                return(TRUE);
 
58
        }
 
59
 
 
60
        return(FALSE);
 
61
#endif
 
62
}
 
63
 
 
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 */
 
68
UNIV_INTERN
 
69
ulint
 
70
os_thread_pf(
 
71
/*=========*/
 
72
        os_thread_id_t  a)      /*!< in: OS thread identifier */
 
73
{
 
74
#ifdef UNIV_HPUX10
 
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. */
 
77
 
 
78
        return((ulint)(a.field1));
 
79
#else
 
80
        return((ulint)a);
 
81
#endif
 
82
}
 
83
 
 
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 */
 
89
UNIV_INTERN
 
90
os_thread_id_t
 
91
os_thread_get_curr_id(void)
 
92
/*=======================*/
 
93
{
 
94
#ifdef __WIN__
 
95
        return(GetCurrentThreadId());
 
96
#else
 
97
        return(pthread_self());
 
98
#endif
 
99
}
 
100
 
 
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 */
 
106
UNIV_INTERN
 
107
os_thread_t
 
108
os_thread_create(
 
109
/*=============*/
 
110
#ifndef __WIN__
 
111
        os_posix_f_t            start_f,
 
112
#else
 
113
        ulint (*start_f)(void*),                /*!< in: pointer to function
 
114
                                                from which to start */
 
115
#endif
 
116
        void*                   arg,            /*!< in: argument to start
 
117
                                                function */
 
118
        os_thread_id_t*         thread_id)      /*!< out: id of the created
 
119
                                                thread, or NULL */
 
120
{
 
121
#ifdef __WIN__
 
122
        os_thread_t     thread;
 
123
        DWORD           win_thread_id;
 
124
 
 
125
        os_mutex_enter(os_sync_mutex);
 
126
        os_thread_count++;
 
127
        os_mutex_exit(os_sync_mutex);
 
128
 
 
129
        thread = CreateThread(NULL,     /* no security attributes */
 
130
                              0,        /* default size stack */
 
131
                              (LPTHREAD_START_ROUTINE)start_f,
 
132
                              arg,
 
133
                              0,        /* thread runs immediately */
 
134
                              &win_thread_id);
 
135
 
 
136
        if (srv_set_thread_priorities) {
 
137
 
 
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 */
 
141
 
 
142
                ut_a(SetThreadPriority(thread, srv_query_thread_priority));
 
143
        }
 
144
 
 
145
        if (thread_id) {
 
146
                *thread_id = win_thread_id;
 
147
        }
 
148
 
 
149
        return(thread);
 
150
#else
 
151
        int             ret;
 
152
        os_thread_t     pthread;
 
153
        pthread_attr_t  attr;
 
154
 
 
155
#ifndef UNIV_HPUX10
 
156
        pthread_attr_init(&attr);
 
157
#endif
 
158
 
 
159
#ifdef UNIV_AIX
 
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. */
 
164
 
 
165
        ret = pthread_attr_setstacksize(&attr,
 
166
                                        (size_t)(PTHREAD_STACK_MIN
 
167
                                                 + 32 * 1024));
 
168
        if (ret) {
 
169
                fprintf(stderr,
 
170
                        "InnoDB: Error: pthread_attr_setstacksize"
 
171
                        " returned %d\n", ret);
 
172
                exit(1);
 
173
        }
 
174
#endif
 
175
#ifdef __NETWARE__
 
176
        ret = pthread_attr_setstacksize(&attr,
 
177
                                        (size_t) NW_THD_STACKSIZE);
 
178
        if (ret) {
 
179
                fprintf(stderr,
 
180
                        "InnoDB: Error: pthread_attr_setstacksize"
 
181
                        " returned %d\n", ret);
 
182
                exit(1);
 
183
        }
 
184
#endif
 
185
        os_mutex_enter(os_sync_mutex);
 
186
        os_thread_count++;
 
187
        os_mutex_exit(os_sync_mutex);
 
188
 
 
189
#ifdef UNIV_HPUX10
 
190
        ret = pthread_create(&pthread, pthread_attr_default, start_f, arg);
 
191
#else
 
192
        ret = pthread_create(&pthread, &attr, start_f, arg);
 
193
#endif
 
194
        if (ret) {
 
195
                fprintf(stderr,
 
196
                        "InnoDB: Error: pthread_create returned %d\n", ret);
 
197
                exit(1);
 
198
        }
 
199
 
 
200
#ifndef UNIV_HPUX10
 
201
        pthread_attr_destroy(&attr);
 
202
#endif
 
203
        if (srv_set_thread_priorities) {
 
204
 
 
205
                  struct sched_param tmp_sched_param;
 
206
 
 
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);
 
210
        }
 
211
 
 
212
        if (thread_id) {
 
213
                *thread_id = pthread;
 
214
        }
 
215
 
 
216
        return(pthread);
 
217
#endif
 
218
}
 
219
 
 
220
/*****************************************************************//**
 
221
Exits the current thread. */
 
222
UNIV_INTERN
 
223
void
 
224
os_thread_exit(
 
225
/*===========*/
 
226
        void*   exit_value)     /*!< in: exit value; in Windows this void*
 
227
                                is cast as a DWORD */
 
228
{
 
229
#ifdef UNIV_DEBUG_THREAD_CREATION
 
230
        fprintf(stderr, "Thread exits, id %lu\n",
 
231
                os_thread_pf(os_thread_get_curr_id()));
 
232
#endif
 
233
        os_mutex_enter(os_sync_mutex);
 
234
        os_thread_count--;
 
235
        os_mutex_exit(os_sync_mutex);
 
236
 
 
237
#ifdef __WIN__
 
238
        ExitThread((DWORD)exit_value);
 
239
#else
 
240
        pthread_exit(exit_value);
 
241
#endif
 
242
}
 
243
 
 
244
/*****************************************************************//**
 
245
Returns handle to the current thread.
 
246
@return current thread handle */
 
247
UNIV_INTERN
 
248
os_thread_t
 
249
os_thread_get_curr(void)
 
250
/*====================*/
 
251
{
 
252
#ifdef __WIN__
 
253
        return(GetCurrentThread());
 
254
#else
 
255
        return(pthread_self());
 
256
#endif
 
257
}
 
258
 
 
259
/*****************************************************************//**
 
260
Advises the os to give up remainder of the thread's time slice. */
 
261
UNIV_INTERN
 
262
void
 
263
os_thread_yield(void)
 
264
/*=================*/
 
265
{
 
266
#if defined(__WIN__)
 
267
        Sleep(0);
 
268
#elif (defined(HAVE_SCHED_YIELD) && defined(HAVE_SCHED_H))
 
269
        sched_yield();
 
270
#elif defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
 
271
        pthread_yield();
 
272
#elif defined(HAVE_PTHREAD_YIELD_ONE_ARG)
 
273
        pthread_yield(0);
 
274
#else
 
275
        os_thread_sleep(0);
 
276
#endif
 
277
}
 
278
#endif /* !UNIV_HOTBACKUP */
 
279
 
 
280
/*****************************************************************//**
 
281
The thread sleeps at least the time given in microseconds. */
 
282
UNIV_INTERN
 
283
void
 
284
os_thread_sleep(
 
285
/*============*/
 
286
        ulint   tm)     /*!< in: time in microseconds */
 
287
{
 
288
#ifdef __WIN__
 
289
        Sleep((DWORD) tm / 1000);
 
290
#elif defined(__NETWARE__)
 
291
        delay(tm / 1000);
 
292
#else
 
293
        struct timeval  t;
 
294
 
 
295
        t.tv_sec = tm / 1000000;
 
296
        t.tv_usec = tm % 1000000;
 
297
 
 
298
        select(0, NULL, NULL, NULL, &t);
 
299
#endif
 
300
}
 
301
 
 
302
#ifndef UNIV_HOTBACKUP
 
303
/******************************************************************//**
 
304
Sets a thread priority. */
 
305
UNIV_INTERN
 
306
void
 
307
os_thread_set_priority(
 
308
/*===================*/
 
309
        os_thread_t     handle, /*!< in: OS handle to the thread */
 
310
        ulint           pri)    /*!< in: priority */
 
311
{
 
312
#ifdef __WIN__
 
313
        int     os_pri;
 
314
 
 
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;
 
321
        } else {
 
322
                ut_error;
 
323
        }
 
324
 
 
325
        ut_a(SetThreadPriority(handle, os_pri));
 
326
#else
 
327
        UT_NOT_USED(handle);
 
328
        UT_NOT_USED(pri);
 
329
#endif
 
330
}
 
331
 
 
332
/******************************************************************//**
 
333
Gets a thread priority.
 
334
@return priority */
 
335
UNIV_INTERN
 
336
ulint
 
337
os_thread_get_priority(
 
338
/*===================*/
 
339
        os_thread_t     handle __attribute__((unused)))
 
340
                                /*!< in: OS handle to the thread */
 
341
{
 
342
#ifdef __WIN__
 
343
        int     os_pri;
 
344
        ulint   pri;
 
345
 
 
346
        os_pri = GetThreadPriority(handle);
 
347
 
 
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;
 
354
        } else {
 
355
                ut_error;
 
356
        }
 
357
 
 
358
        return(pri);
 
359
#else
 
360
        return(0);
 
361
#endif
 
362
}
 
363
 
 
364
/******************************************************************//**
 
365
Gets the last operating system error code for the calling thread.
 
366
@return last error on Windows, 0 otherwise */
 
367
UNIV_INTERN
 
368
ulint
 
369
os_thread_get_last_error(void)
 
370
/*==========================*/
 
371
{
 
372
#ifdef __WIN__
 
373
        return(GetLastError());
 
374
#else
 
375
        return(0);
 
376
#endif
 
377
}
 
378
#endif /* !UNIV_HOTBACKUP */