summaryrefslogtreecommitdiff
path: root/storage/ndb/src/common/portlib/win32/NdbThread.c
blob: 760ed47218a3542439d8b31f33190ca10aef18e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Copyright (c) 2003-2005 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */

#include <ndb_global.h>
#include "NdbThread.h"
#include <process.h>

#define MAX_THREAD_NAME 16

typedef unsigned (WINAPI* NDB_WIN32_THREAD_FUNC)(void*);


struct NdbThread 
{ 
    HANDLE hThread;
    unsigned nThreadId;
    char thread_name[MAX_THREAD_NAME];
};


struct NdbThread* NdbThread_Create(NDB_THREAD_FUNC *p_thread_func,
                                   NDB_THREAD_ARG *p_thread_arg,
                                   const NDB_THREAD_STACKSIZE thread_stack_size,
                                   const char* p_thread_name,
                                   NDB_THREAD_PRIO thread_prio)
{
    struct NdbThread* tmpThread;
    unsigned initflag;
    int nPriority = 0;

    if(!p_thread_func)
        return 0;
    
    tmpThread = (struct NdbThread*)malloc(sizeof(struct NdbThread));
    if(!tmpThread)
        return 0;
    
    strncpy((char*)&tmpThread->thread_name, p_thread_name, MAX_THREAD_NAME);
    
    switch(thread_prio)
    {
    case NDB_THREAD_PRIO_HIGHEST: nPriority=THREAD_PRIORITY_HIGHEST; break;
    case NDB_THREAD_PRIO_HIGH: nPriority=THREAD_PRIORITY_ABOVE_NORMAL; break;
    case NDB_THREAD_PRIO_MEAN: nPriority=THREAD_PRIORITY_NORMAL; break;
    case NDB_THREAD_PRIO_LOW: nPriority=THREAD_PRIORITY_BELOW_NORMAL; break;
    case NDB_THREAD_PRIO_LOWEST: nPriority=THREAD_PRIORITY_LOWEST; break;
    }
    initflag = (nPriority ? CREATE_SUSPENDED : 0);
    
    tmpThread->hThread = (HANDLE)_beginthreadex(0, thread_stack_size,
        (NDB_WIN32_THREAD_FUNC)p_thread_func, p_thread_arg, 
        initflag, &tmpThread->nThreadId);
    
    if(nPriority && tmpThread->hThread)
    {
        SetThreadPriority(tmpThread->hThread, nPriority);
        ResumeThread (tmpThread->hThread);
    }
    
    assert(tmpThread->hThread);
    return tmpThread;
}


void NdbThread_Destroy(struct NdbThread** p_thread)
{
    CloseHandle((*p_thread)->hThread);
    (*p_thread)->hThread = 0;
    free(*p_thread); 
    *p_thread = 0;
}


int NdbThread_WaitFor(struct NdbThread* p_wait_thread, void** status)
{
    void *local_status = 0;
    if (status == 0)
        status = &local_status;
    
    if(WaitForSingleObject(p_wait_thread->hThread, INFINITE) == WAIT_OBJECT_0
        && GetExitCodeThread(p_wait_thread->hThread, (LPDWORD)status))
    {
        CloseHandle(p_wait_thread->hThread);
        p_wait_thread->hThread = 0;
        return 0;
    }
    return -1; 
}


void NdbThread_Exit(int status)
{
    _endthreadex((DWORD) status);
}


int NdbThread_SetConcurrencyLevel(int level)
{
    return 0;
}