~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2008-12-06 22:41:03 UTC
  • mto: (656.1.7 devel)
  • mto: This revision was merged to the branch mainline in revision 665.
  • Revision ID: monty@inaugust.com-20081206224103-jdouqwt9hb0f01y1
Moved non-working tests into broken suite for easier running of working tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 
3
 
Copyright (C) 1995, 2010, 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., 51 Franklin
15
 
St, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
*****************************************************************************/
18
 
 
19
 
/**************************************************//**
20
 
@file os/os0thread.c
 
1
/******************************************************
21
2
The interface to the operating system thread control primitives
22
3
 
 
4
(c) 1995 Innobase Oy
 
5
 
23
6
Created 9/8/1995 Heikki Tuuri
24
7
*******************************************************/
25
8
 
30
13
 
31
14
#ifdef __WIN__
32
15
#include <windows.h>
33
 
#else
34
 
#include <sys/select.h>
35
16
#endif
36
17
 
37
 
#ifndef UNIV_HOTBACKUP
38
18
#include "srv0srv.h"
39
19
#include "os0sync.h"
40
20
 
41
 
/***************************************************************//**
42
 
Compares two thread ids for equality.
43
 
@return TRUE if equal */
 
21
/*******************************************************************
 
22
Compares two thread ids for equality. */
44
23
UNIV_INTERN
45
24
ibool
46
25
os_thread_eq(
47
26
/*=========*/
48
 
        os_thread_id_t  a,      /*!< in: OS thread or thread id */
49
 
        os_thread_id_t  b)      /*!< in: OS thread or thread id */
 
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 */
50
30
{
51
31
#ifdef __WIN__
52
32
        if (a == b) {
63
43
#endif
64
44
}
65
45
 
66
 
/****************************************************************//**
 
46
/********************************************************************
67
47
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 */
 
48
unique for the thread though! */
70
49
UNIV_INTERN
71
50
ulint
72
51
os_thread_pf(
73
52
/*=========*/
74
 
        os_thread_id_t  a)      /*!< in: OS thread identifier */
 
53
        os_thread_id_t  a)
75
54
{
76
55
#ifdef UNIV_HPUX10
77
56
        /* In HP-UX-10.20 a pthread_t is a struct of 3 fields: field1, field2,
83
62
#endif
84
63
}
85
64
 
86
 
/*****************************************************************//**
 
65
/*********************************************************************
87
66
Returns the thread identifier of current thread. Currently the thread
88
67
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 */
 
68
pthread_t is a struct of 3 fields. */
91
69
UNIV_INTERN
92
70
os_thread_id_t
93
71
os_thread_get_curr_id(void)
100
78
#endif
101
79
}
102
80
 
103
 
/****************************************************************//**
 
81
/********************************************************************
104
82
Creates a new thread of execution. The execution starts from
105
83
the function given. The start function takes a void* parameter
106
 
and returns an ulint.
107
 
@return handle to the thread */
 
84
and returns an ulint. */
108
85
UNIV_INTERN
109
86
os_thread_t
110
87
os_thread_create(
111
88
/*=============*/
 
89
                                                /* out: handle to the thread */
112
90
#ifndef __WIN__
113
91
        os_posix_f_t            start_f,
114
92
#else
115
 
        ulint (*start_f)(void*),                /*!< in: pointer to function
 
93
        ulint (*start_f)(void*),                /* in: pointer to function
116
94
                                                from which to start */
117
95
#endif
118
 
        void*                   arg,            /*!< in: argument to start
 
96
        void*                   arg,            /* in: argument to start
119
97
                                                function */
120
 
        os_thread_id_t*         thread_id)      /*!< out: id of the created
 
98
        os_thread_id_t*         thread_id)      /* out: id of the created
121
99
                                                thread, or NULL */
122
100
{
123
101
#ifdef __WIN__
135
113
                              0,        /* thread runs immediately */
136
114
                              &win_thread_id);
137
115
 
 
116
        if (srv_set_thread_priorities) {
 
117
 
 
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 */
 
121
 
 
122
                ut_a(SetThreadPriority(thread, srv_query_thread_priority));
 
123
        }
 
124
 
138
125
        if (thread_id) {
139
126
                *thread_id = win_thread_id;
140
127
        }
145
132
        os_thread_t     pthread;
146
133
        pthread_attr_t  attr;
147
134
 
148
 
#ifndef UNIV_HPUX10
 
135
#if !(defined(UNIV_HOTBACKUP) && defined(UNIV_HPUX10))
149
136
        pthread_attr_init(&attr);
150
137
#endif
151
138
 
165
152
                exit(1);
166
153
        }
167
154
#endif
 
155
#ifdef __NETWARE__
 
156
        ret = pthread_attr_setstacksize(&attr,
 
157
                                        (size_t) NW_THD_STACKSIZE);
 
158
        if (ret) {
 
159
                fprintf(stderr,
 
160
                        "InnoDB: Error: pthread_attr_setstacksize"
 
161
                        " returned %d\n", ret);
 
162
                exit(1);
 
163
        }
 
164
#endif
168
165
        os_mutex_enter(os_sync_mutex);
169
166
        os_thread_count++;
170
167
        os_mutex_exit(os_sync_mutex);
171
168
 
172
 
#ifdef UNIV_HPUX10
 
169
#if defined(UNIV_HOTBACKUP) && defined(UNIV_HPUX10)
173
170
        ret = pthread_create(&pthread, pthread_attr_default, start_f, arg);
174
171
#else
175
172
        ret = pthread_create(&pthread, &attr, start_f, arg);
180
177
                exit(1);
181
178
        }
182
179
 
183
 
#ifndef UNIV_HPUX10
 
180
#if !(defined(UNIV_HOTBACKUP) && defined(UNIV_HPUX10))
184
181
        pthread_attr_destroy(&attr);
185
182
#endif
 
183
        if (srv_set_thread_priorities) {
 
184
 
 
185
                  struct sched_param tmp_sched_param;
 
186
 
 
187
                  memset(&tmp_sched_param, 0, sizeof(tmp_sched_param));
 
188
                  tmp_sched_param.sched_priority= srv_query_thread_priority;
 
189
                  (void)pthread_setschedparam(pthread, SCHED_OTHER, &tmp_sched_param);
 
190
        }
186
191
 
187
192
        if (thread_id) {
188
193
                *thread_id = pthread;
192
197
#endif
193
198
}
194
199
 
195
 
/*****************************************************************//**
 
200
/*********************************************************************
196
201
Exits the current thread. */
197
202
UNIV_INTERN
198
203
void
199
204
os_thread_exit(
200
205
/*===========*/
201
 
        void*   exit_value)     /*!< in: exit value; in Windows this void*
 
206
        void*   exit_value)     /* in: exit value; in Windows this void*
202
207
                                is cast as a DWORD */
203
208
{
204
209
#ifdef UNIV_DEBUG_THREAD_CREATION
205
210
        fprintf(stderr, "Thread exits, id %lu\n",
206
211
                os_thread_pf(os_thread_get_curr_id()));
207
212
#endif
208
 
 
209
 
#ifdef UNIV_PFS_THREAD
210
 
        pfs_delete_thread();
211
 
#endif
212
 
 
213
213
        os_mutex_enter(os_sync_mutex);
214
214
        os_thread_count--;
215
215
        os_mutex_exit(os_sync_mutex);
217
217
#ifdef __WIN__
218
218
        ExitThread((DWORD)exit_value);
219
219
#else
220
 
        pthread_detach(pthread_self());
221
220
        pthread_exit(exit_value);
222
221
#endif
223
222
}
224
223
 
225
 
/*****************************************************************//**
226
 
Returns handle to the current thread.
227
 
@return current thread handle */
 
224
/*********************************************************************
 
225
Returns handle to the current thread. */
228
226
UNIV_INTERN
229
227
os_thread_t
230
228
os_thread_get_curr(void)
237
235
#endif
238
236
}
239
237
 
240
 
/*****************************************************************//**
 
238
/*********************************************************************
241
239
Advises the os to give up remainder of the thread's time slice. */
242
240
UNIV_INTERN
243
241
void
245
243
/*=================*/
246
244
{
247
245
#if defined(__WIN__)
248
 
        SwitchToThread();
 
246
        Sleep(0);
249
247
#elif (defined(HAVE_SCHED_YIELD) && defined(HAVE_SCHED_H))
250
248
        sched_yield();
251
249
#elif defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
256
254
        os_thread_sleep(0);
257
255
#endif
258
256
}
259
 
#endif /* !UNIV_HOTBACKUP */
260
257
 
261
 
/*****************************************************************//**
 
258
/*********************************************************************
262
259
The thread sleeps at least the time given in microseconds. */
263
260
UNIV_INTERN
264
261
void
265
262
os_thread_sleep(
266
263
/*============*/
267
 
        ulint   tm)     /*!< in: time in microseconds */
 
264
        ulint   tm)     /* in: time in microseconds */
268
265
{
269
266
#ifdef __WIN__
270
267
        Sleep((DWORD) tm / 1000);
 
268
#elif defined(__NETWARE__)
 
269
        delay(tm / 1000);
271
270
#else
272
271
        struct timeval  t;
273
272
 
278
277
#endif
279
278
}
280
279
 
281
 
#ifndef UNIV_HOTBACKUP
282
 
/******************************************************************//**
 
280
/**********************************************************************
283
281
Sets a thread priority. */
284
282
UNIV_INTERN
285
283
void
286
284
os_thread_set_priority(
287
285
/*===================*/
288
 
        os_thread_t     handle, /*!< in: OS handle to the thread */
289
 
        ulint           pri)    /*!< in: priority */
 
286
        os_thread_t     handle, /* in: OS handle to the thread */
 
287
        ulint           pri)    /* in: priority */
290
288
{
291
289
#ifdef __WIN__
292
290
        int     os_pri;
308
306
#endif
309
307
}
310
308
 
311
 
/******************************************************************//**
312
 
Gets a thread priority.
313
 
@return priority */
 
309
/**********************************************************************
 
310
Gets a thread priority. */
314
311
UNIV_INTERN
315
312
ulint
316
313
os_thread_get_priority(
317
314
/*===================*/
318
 
        os_thread_t     /*handle __attribute__((unused))*/)
319
 
                                /*!< in: OS handle to the thread */
 
315
                                /* out: priority */
 
316
        os_thread_t     handle __attribute__((unused)))
 
317
                                /* in: OS handle to the thread */
320
318
{
321
319
#ifdef __WIN__
322
320
        int     os_pri;
340
338
#endif
341
339
}
342
340
 
343
 
/******************************************************************//**
344
 
Gets the last operating system error code for the calling thread.
345
 
@return last error on Windows, 0 otherwise */
 
341
/**********************************************************************
 
342
Gets the last operating system error code for the calling thread. */
346
343
UNIV_INTERN
347
344
ulint
348
345
os_thread_get_last_error(void)
354
351
        return(0);
355
352
#endif
356
353
}
357
 
#endif /* !UNIV_HOTBACKUP */