~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to innobase/os/os0thread.c

  • Committer: monty at mysql
  • Date: 2001-02-17 12:19:19 UTC
  • mto: (554.1.1)
  • mto: This revision was merged to the branch mainline in revision 556.
  • Revision ID: sp1r-monty@donna.mysql.com-20010217121919-07904
Added Innobase to source distribution

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************
 
2
The interface to the operating system
 
3
process and thread control primitives
 
4
 
 
5
(c) 1995 Innobase Oy
 
6
 
 
7
Created 9/8/1995 Heikki Tuuri
 
8
*******************************************************/
 
9
 
 
10
#include "os0thread.h"
 
11
#ifdef UNIV_NONINL
 
12
#include "os0thread.ic"
 
13
#endif
 
14
 
 
15
#ifdef __WIN__
 
16
#include <windows.h>
 
17
#endif
 
18
 
 
19
/*********************************************************************
 
20
Returns the thread identifier of current thread. */
 
21
 
 
22
os_thread_id_t
 
23
os_thread_get_curr_id(void)
 
24
/*=======================*/
 
25
{
 
26
#ifdef __WIN__
 
27
        return(GetCurrentThreadId());
 
28
#else
 
29
        return((os_thread_id_t) pthread_self());
 
30
#endif
 
31
}
 
32
 
 
33
/* Define a function pointer type to use in a typecast */
 
34
typedef void* (*os_posix_f_t) (void*);
 
35
 
 
36
/********************************************************************
 
37
Creates a new thread of execution. The execution starts from
 
38
the function given. The start function takes a void* parameter
 
39
and returns an ulint. */
 
40
 
 
41
os_thread_t
 
42
os_thread_create(
 
43
/*=============*/
 
44
                                                /* out: handle to the thread */
 
45
        ulint (*start_f)(void*),                /* in: pointer to function
 
46
                                                from which to start */
 
47
        void*                   arg,            /* in: argument to start
 
48
                                                function */
 
49
        os_thread_id_t*         thread_id)      /* out: id of created
 
50
                                                thread */       
 
51
{
 
52
#ifdef __WIN__
 
53
        os_thread_t     thread;
 
54
 
 
55
        thread = CreateThread(NULL,     /* no security attributes */
 
56
                                0,      /* default size stack */
 
57
                                (LPTHREAD_START_ROUTINE)start_f,
 
58
                                arg,
 
59
                                0,      /* thread runs immediately */
 
60
                                thread_id);
 
61
        ut_a(thread);
 
62
 
 
63
        return(thread);
 
64
#else
 
65
        int             ret;
 
66
        os_thread_t     pthread;
 
67
 
 
68
        /* Note that below we cast the start function returning an integer
 
69
        to a function returning a pointer: this may cause error
 
70
        if the return value is used somewhere! */
 
71
 
 
72
        ret = pthread_create(&pthread, NULL, (os_posix_f_t) start_f, arg);
 
73
 
 
74
        return(pthread);
 
75
#endif
 
76
}
 
77
 
 
78
/*********************************************************************
 
79
Returns handle to the current thread. */
 
80
 
 
81
os_thread_t
 
82
os_thread_get_curr(void)
 
83
/*=======================*/
 
84
{
 
85
#ifdef __WIN__
 
86
        return(GetCurrentThread());
 
87
#else
 
88
        return(pthread_self());
 
89
#endif
 
90
}
 
91
 
 
92
/*********************************************************************
 
93
Converts a thread id to a ulint. */
 
94
 
 
95
ulint
 
96
os_thread_conv_id_to_ulint(
 
97
/*=======================*/
 
98
                                /* out: converted to ulint */
 
99
        os_thread_id_t  id)     /* in: thread id */
 
100
{
 
101
        return((ulint)id);
 
102
}
 
103
        
 
104
/*********************************************************************
 
105
Advises the os to give up remainder of the thread's time slice. */
 
106
 
 
107
void
 
108
os_thread_yield(void)
 
109
/*=================*/
 
110
{
 
111
#ifdef __WIN__  
 
112
        Sleep(0);
 
113
#else
 
114
        os_thread_sleep(0);
 
115
#endif
 
116
}
 
117
 
 
118
/*********************************************************************
 
119
The thread sleeps at least the time given in microseconds. */
 
120
 
 
121
void
 
122
os_thread_sleep(
 
123
/*============*/
 
124
        ulint   tm)     /* in: time in microseconds */
 
125
{
 
126
#ifdef __WIN__
 
127
        Sleep(tm / 1000);
 
128
#else
 
129
        struct timeval  t;
 
130
 
 
131
        t.tv_sec = 0;
 
132
        t.tv_usec = tm;
 
133
        
 
134
        select(0, NULL, NULL, NULL, &t);
 
135
#endif
 
136
}
 
137
 
 
138
/**********************************************************************
 
139
Sets a thread priority. */
 
140
 
 
141
void
 
142
os_thread_set_priority(
 
143
/*===================*/
 
144
        os_thread_t     handle, /* in: OS handle to the thread */
 
145
        ulint           pri)    /* in: priority */
 
146
{
 
147
#ifdef __WIN__
 
148
        int     os_pri;
 
149
 
 
150
        if (pri == OS_THREAD_PRIORITY_BACKGROUND) {
 
151
                os_pri = THREAD_PRIORITY_BELOW_NORMAL;
 
152
        } else if (pri == OS_THREAD_PRIORITY_NORMAL) {
 
153
                os_pri = THREAD_PRIORITY_NORMAL;
 
154
        } else if (pri == OS_THREAD_PRIORITY_ABOVE_NORMAL) {
 
155
                os_pri = THREAD_PRIORITY_HIGHEST;
 
156
        } else {
 
157
                ut_error;
 
158
        }
 
159
 
 
160
        ut_a(SetThreadPriority(handle, os_pri));
 
161
#else
 
162
        UT_NOT_USED(handle);
 
163
        UT_NOT_USED(pri);
 
164
#endif
 
165
}
 
166
 
 
167
/**********************************************************************
 
168
Gets a thread priority. */
 
169
 
 
170
ulint
 
171
os_thread_get_priority(
 
172
/*===================*/
 
173
                                /* out: priority */
 
174
        os_thread_t     handle) /* in: OS handle to the thread */
 
175
{
 
176
#ifdef __WIN__
 
177
        int     os_pri;
 
178
        ulint   pri;
 
179
 
 
180
        os_pri = GetThreadPriority(handle);
 
181
 
 
182
        if (os_pri == THREAD_PRIORITY_BELOW_NORMAL) {
 
183
                pri = OS_THREAD_PRIORITY_BACKGROUND;
 
184
        } else if (os_pri == THREAD_PRIORITY_NORMAL) {
 
185
                pri = OS_THREAD_PRIORITY_NORMAL;
 
186
        } else if (os_pri == THREAD_PRIORITY_HIGHEST) {
 
187
                pri = OS_THREAD_PRIORITY_ABOVE_NORMAL;
 
188
        } else {
 
189
                ut_error;
 
190
        }
 
191
 
 
192
        return(pri);
 
193
#else
 
194
        return(0);
 
195
#endif
 
196
}
 
197
 
 
198
/**********************************************************************
 
199
Gets the last operating system error code for the calling thread. */
 
200
 
 
201
ulint
 
202
os_thread_get_last_error(void)
 
203
/*==========================*/
 
204
{
 
205
#ifdef __WIN__
 
206
        return(GetLastError());
 
207
#else
 
208
        return(0);
 
209
#endif
 
210
}