~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Tags: innodb-plugin-1.0.3
InnoDB Plugin 1.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
2
 
3
 
Copyright (c) 1995, 2010, Innobase Oy. All Rights Reserved.
 
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
4
4
 
5
5
This program is free software; you can redistribute it and/or modify it under
6
6
the terms of the GNU General Public License as published by the Free Software
11
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
12
 
13
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., 51 Franklin
15
 
St, Fifth Floor, Boston, MA 02110-1301 USA
 
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
15
Place, Suite 330, Boston, MA 02111-1307 USA
16
16
 
17
17
*****************************************************************************/
18
18
 
19
 
/**************************************************//**
20
 
@file os/os0thread.c
 
19
/******************************************************
21
20
The interface to the operating system thread control primitives
22
21
 
23
22
Created 9/8/1995 Heikki Tuuri
30
29
 
31
30
#ifdef __WIN__
32
31
#include <windows.h>
33
 
#else
34
 
#include <sys/select.h>
35
32
#endif
36
33
 
37
 
#ifndef UNIV_HOTBACKUP
38
34
#include "srv0srv.h"
39
35
#include "os0sync.h"
40
36
 
41
 
/***************************************************************//**
42
 
Compares two thread ids for equality.
43
 
@return TRUE if equal */
 
37
/*******************************************************************
 
38
Compares two thread ids for equality. */
44
39
UNIV_INTERN
45
40
ibool
46
41
os_thread_eq(
47
42
/*=========*/
48
 
        os_thread_id_t  a,      /*!< in: OS thread or thread id */
49
 
        os_thread_id_t  b)      /*!< in: OS thread or thread id */
 
43
                                /* out: TRUE if equal */
 
44
        os_thread_id_t  a,      /* in: OS thread or thread id */
 
45
        os_thread_id_t  b)      /* in: OS thread or thread id */
50
46
{
51
47
#ifdef __WIN__
52
48
        if (a == b) {
63
59
#endif
64
60
}
65
61
 
66
 
/****************************************************************//**
 
62
/********************************************************************
67
63
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 */
 
64
unique for the thread though! */
70
65
UNIV_INTERN
71
66
ulint
72
67
os_thread_pf(
73
68
/*=========*/
74
 
        os_thread_id_t  a)      /*!< in: OS thread identifier */
 
69
        os_thread_id_t  a)
75
70
{
76
71
#ifdef UNIV_HPUX10
77
72
        /* In HP-UX-10.20 a pthread_t is a struct of 3 fields: field1, field2,
83
78
#endif
84
79
}
85
80
 
86
 
/*****************************************************************//**
 
81
/*********************************************************************
87
82
Returns the thread identifier of current thread. Currently the thread
88
83
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 */
 
84
pthread_t is a struct of 3 fields. */
91
85
UNIV_INTERN
92
86
os_thread_id_t
93
87
os_thread_get_curr_id(void)
100
94
#endif
101
95
}
102
96
 
103
 
/****************************************************************//**
 
97
/********************************************************************
104
98
Creates a new thread of execution. The execution starts from
105
99
the function given. The start function takes a void* parameter
106
 
and returns an ulint.
107
 
@return handle to the thread */
 
100
and returns an ulint. */
108
101
UNIV_INTERN
109
102
os_thread_t
110
103
os_thread_create(
111
104
/*=============*/
 
105
                                                /* out: handle to the thread */
112
106
#ifndef __WIN__
113
107
        os_posix_f_t            start_f,
114
108
#else
115
 
        ulint (*start_f)(void*),                /*!< in: pointer to function
 
109
        ulint (*start_f)(void*),                /* in: pointer to function
116
110
                                                from which to start */
117
111
#endif
118
 
        void*                   arg,            /*!< in: argument to start
 
112
        void*                   arg,            /* in: argument to start
119
113
                                                function */
120
 
        os_thread_id_t*         thread_id)      /*!< out: id of the created
 
114
        os_thread_id_t*         thread_id)      /* out: id of the created
121
115
                                                thread, or NULL */
122
116
{
123
117
#ifdef __WIN__
135
129
                              0,        /* thread runs immediately */
136
130
                              &win_thread_id);
137
131
 
 
132
        if (srv_set_thread_priorities) {
 
133
 
 
134
                /* Set created thread priority the same as a normal query
 
135
                in MYSQL: we try to prevent starvation of threads by
 
136
                assigning same priority QUERY_PRIOR to all */
 
137
 
 
138
                ut_a(SetThreadPriority(thread, srv_query_thread_priority));
 
139
        }
 
140
 
138
141
        if (thread_id) {
139
142
                *thread_id = win_thread_id;
140
143
        }
145
148
        os_thread_t     pthread;
146
149
        pthread_attr_t  attr;
147
150
 
148
 
#ifndef UNIV_HPUX10
 
151
#if !(defined(UNIV_HOTBACKUP) && defined(UNIV_HPUX10))
149
152
        pthread_attr_init(&attr);
150
153
#endif
151
154
 
165
168
                exit(1);
166
169
        }
167
170
#endif
 
171
#ifdef __NETWARE__
 
172
        ret = pthread_attr_setstacksize(&attr,
 
173
                                        (size_t) NW_THD_STACKSIZE);
 
174
        if (ret) {
 
175
                fprintf(stderr,
 
176
                        "InnoDB: Error: pthread_attr_setstacksize"
 
177
                        " returned %d\n", ret);
 
178
                exit(1);
 
179
        }
 
180
#endif
168
181
        os_mutex_enter(os_sync_mutex);
169
182
        os_thread_count++;
170
183
        os_mutex_exit(os_sync_mutex);
171
184
 
172
 
#ifdef UNIV_HPUX10
 
185
#if defined(UNIV_HOTBACKUP) && defined(UNIV_HPUX10)
173
186
        ret = pthread_create(&pthread, pthread_attr_default, start_f, arg);
174
187
#else
175
188
        ret = pthread_create(&pthread, &attr, start_f, arg);
180
193
                exit(1);
181
194
        }
182
195
 
183
 
#ifndef UNIV_HPUX10
 
196
#if !(defined(UNIV_HOTBACKUP) && defined(UNIV_HPUX10))
184
197
        pthread_attr_destroy(&attr);
185
198
#endif
 
199
        if (srv_set_thread_priorities) {
 
200
 
 
201
                my_pthread_setprio(pthread, srv_query_thread_priority);
 
202
        }
186
203
 
187
204
        if (thread_id) {
188
205
                *thread_id = pthread;
192
209
#endif
193
210
}
194
211
 
195
 
/*****************************************************************//**
 
212
/*********************************************************************
196
213
Exits the current thread. */
197
214
UNIV_INTERN
198
215
void
199
216
os_thread_exit(
200
217
/*===========*/
201
 
        void*   exit_value)     /*!< in: exit value; in Windows this void*
 
218
        void*   exit_value)     /* in: exit value; in Windows this void*
202
219
                                is cast as a DWORD */
203
220
{
204
221
#ifdef UNIV_DEBUG_THREAD_CREATION
205
222
        fprintf(stderr, "Thread exits, id %lu\n",
206
223
                os_thread_pf(os_thread_get_curr_id()));
207
224
#endif
208
 
 
209
 
#ifdef UNIV_PFS_THREAD
210
 
        pfs_delete_thread();
211
 
#endif
212
 
 
213
225
        os_mutex_enter(os_sync_mutex);
214
226
        os_thread_count--;
215
227
        os_mutex_exit(os_sync_mutex);
217
229
#ifdef __WIN__
218
230
        ExitThread((DWORD)exit_value);
219
231
#else
220
 
        pthread_detach(pthread_self());
221
232
        pthread_exit(exit_value);
222
233
#endif
223
234
}
224
235
 
225
 
/*****************************************************************//**
226
 
Returns handle to the current thread.
227
 
@return current thread handle */
 
236
/*********************************************************************
 
237
Returns handle to the current thread. */
228
238
UNIV_INTERN
229
239
os_thread_t
230
240
os_thread_get_curr(void)
237
247
#endif
238
248
}
239
249
 
240
 
/*****************************************************************//**
 
250
/*********************************************************************
241
251
Advises the os to give up remainder of the thread's time slice. */
242
252
UNIV_INTERN
243
253
void
245
255
/*=================*/
246
256
{
247
257
#if defined(__WIN__)
248
 
        SwitchToThread();
 
258
        Sleep(0);
249
259
#elif (defined(HAVE_SCHED_YIELD) && defined(HAVE_SCHED_H))
250
260
        sched_yield();
251
261
#elif defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
256
266
        os_thread_sleep(0);
257
267
#endif
258
268
}
259
 
#endif /* !UNIV_HOTBACKUP */
260
269
 
261
 
/*****************************************************************//**
 
270
/*********************************************************************
262
271
The thread sleeps at least the time given in microseconds. */
263
272
UNIV_INTERN
264
273
void
265
274
os_thread_sleep(
266
275
/*============*/
267
 
        ulint   tm)     /*!< in: time in microseconds */
 
276
        ulint   tm)     /* in: time in microseconds */
268
277
{
269
278
#ifdef __WIN__
270
279
        Sleep((DWORD) tm / 1000);
 
280
#elif defined(__NETWARE__)
 
281
        delay(tm / 1000);
271
282
#else
272
283
        struct timeval  t;
273
284
 
278
289
#endif
279
290
}
280
291
 
281
 
#ifndef UNIV_HOTBACKUP
282
 
/******************************************************************//**
 
292
/**********************************************************************
283
293
Sets a thread priority. */
284
294
UNIV_INTERN
285
295
void
286
296
os_thread_set_priority(
287
297
/*===================*/
288
 
        os_thread_t     handle, /*!< in: OS handle to the thread */
289
 
        ulint           pri)    /*!< in: priority */
 
298
        os_thread_t     handle, /* in: OS handle to the thread */
 
299
        ulint           pri)    /* in: priority */
290
300
{
291
301
#ifdef __WIN__
292
302
        int     os_pri;
308
318
#endif
309
319
}
310
320
 
311
 
/******************************************************************//**
312
 
Gets a thread priority.
313
 
@return priority */
 
321
/**********************************************************************
 
322
Gets a thread priority. */
314
323
UNIV_INTERN
315
324
ulint
316
325
os_thread_get_priority(
317
326
/*===================*/
 
327
                                /* out: priority */
318
328
        os_thread_t     handle __attribute__((unused)))
319
 
                                /*!< in: OS handle to the thread */
 
329
                                /* in: OS handle to the thread */
320
330
{
321
331
#ifdef __WIN__
322
332
        int     os_pri;
340
350
#endif
341
351
}
342
352
 
343
 
/******************************************************************//**
344
 
Gets the last operating system error code for the calling thread.
345
 
@return last error on Windows, 0 otherwise */
 
353
/**********************************************************************
 
354
Gets the last operating system error code for the calling thread. */
346
355
UNIV_INTERN
347
356
ulint
348
357
os_thread_get_last_error(void)
354
363
        return(0);
355
364
#endif
356
365
}
357
 
#endif /* !UNIV_HOTBACKUP */