1
/* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
/*****************************************************************************
17
** Simulation of posix threads calls for Windows
18
*****************************************************************************/
20
/* SAFE_MUTEX will not work until the thread structure is up to date */
23
#include "mysys_priv.h"
27
struct thread_start_parameter
34
Adapter to @c pthread_mutex_trylock()
36
@retval 0 Mutex was acquired
37
@retval EBUSY Mutex was already locked by a thread
40
win_pthread_mutex_trylock(pthread_mutex_t *mutex)
42
if (TryEnterCriticalSection(mutex))
44
/* Don't allow recursive lock */
45
if (mutex->RecursionCount > 1){
46
LeaveCriticalSection(mutex);
54
static unsigned int __stdcall pthread_start(void *p)
56
struct thread_start_parameter *par= (struct thread_start_parameter *)p;
57
pthread_handler func= par->func;
65
int pthread_create(pthread_t *thread_id, const pthread_attr_t *attr,
66
pthread_handler func, void *param)
69
struct thread_start_parameter *par;
70
unsigned int stack_size;
71
DBUG_ENTER("pthread_create");
73
par= (struct thread_start_parameter *)malloc(sizeof(*par));
79
stack_size= attr?attr->dwStackSize:0;
81
handle= _beginthreadex(NULL, stack_size , pthread_start, par, 0, thread_id);
84
DBUG_PRINT("info", ("thread id=%u",*thread_id));
86
/* Do not need thread handle, close it */
87
CloseHandle((HANDLE)handle);
92
("Can't create thread to handle request (error %d)",errno));
97
void pthread_exit(void *a)
102
int pthread_join(pthread_t thread, void **value_ptr)
107
handle= OpenThread(SYNCHRONIZE, FALSE, thread);
114
ret= WaitForSingleObject(handle, INFINITE);
116
if(ret != WAIT_OBJECT_0)
131
int pthread_cancel(pthread_t thread)
137
handle= OpenThread(THREAD_TERMINATE, FALSE, thread);
140
ok= TerminateThread(handle,0);
151
One time initialization. For simplicity, we assume initializer thread
152
does not exit within init_routine().
154
int my_pthread_once(my_pthread_once_t *once_control,
155
void (*init_routine)(void))
160
Do "dirty" read to find out if initialization is already done, to
161
save an interlocked operation in common case. Memory barriers are ensured by
162
Visual C++ volatile implementation.
164
if (*once_control == MY_PTHREAD_ONCE_DONE)
167
state= InterlockedCompareExchange(once_control, MY_PTHREAD_ONCE_INPROGRESS,
168
MY_PTHREAD_ONCE_INIT);
172
case MY_PTHREAD_ONCE_INIT:
173
/* This is initializer thread */
175
*once_control= MY_PTHREAD_ONCE_DONE;
178
case MY_PTHREAD_ONCE_INPROGRESS:
179
/* init_routine in progress. Wait for its completion */
180
while(*once_control == MY_PTHREAD_ONCE_INPROGRESS)
185
case MY_PTHREAD_ONCE_DONE: