MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
os0thread.cc
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2011, Oracle and/or its affiliates. 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.,
15 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16 
17 *****************************************************************************/
18 
19 /**************************************************/
26 #include "os0thread.h"
27 #ifdef UNIV_NONINL
28 #include "os0thread.ic"
29 #endif
30 
31 #ifdef __WIN__
32 #include <windows.h>
33 #endif
34 
35 #ifndef UNIV_HOTBACKUP
36 #include "srv0srv.h"
37 #include "os0sync.h"
38 
39 /***************************************************************/
42 UNIV_INTERN
43 ibool
45 /*=========*/
46  os_thread_id_t a,
47  os_thread_id_t b)
48 {
49 #ifdef __WIN__
50  if (a == b) {
51  return(TRUE);
52  }
53 
54  return(FALSE);
55 #else
56  if (pthread_equal(a, b)) {
57  return(TRUE);
58  }
59 
60  return(FALSE);
61 #endif
62 }
63 
64 /****************************************************************/
68 UNIV_INTERN
69 ulint
71 /*=========*/
72  os_thread_id_t a)
73 {
74 #ifdef UNIV_HPUX10
75  /* In HP-UX-10.20 a pthread_t is a struct of 3 fields: field1, field2,
76  field3. We do not know if field1 determines the thread uniquely. */
77 
78  return((ulint)(a.field1));
79 #else
80  return((ulint) a);
81 #endif
82 }
83 
84 /*****************************************************************/
89 UNIV_INTERN
92 /*=======================*/
93 {
94 #ifdef __WIN__
95  return(GetCurrentThreadId());
96 #else
97  return(pthread_self());
98 #endif
99 }
100 
101 /****************************************************************/
106 UNIV_INTERN
107 os_thread_t
109 /*==================*/
110  os_thread_func_t func,
112  void* arg,
114  os_thread_id_t* thread_id)
116 {
117 #ifdef __WIN__
118  os_thread_t thread;
119  DWORD win_thread_id;
120 
122  os_thread_count++;
124 
125  thread = CreateThread(NULL, /* no security attributes */
126  0, /* default size stack */
127  func,
128  arg,
129  0, /* thread runs immediately */
130  &win_thread_id);
131 
132  if (thread_id) {
133  *thread_id = win_thread_id;
134  }
135 
136  return(thread);
137 #else
138  int ret;
139  os_thread_t pthread;
140  pthread_attr_t attr;
141 
142 #ifndef UNIV_HPUX10
143  pthread_attr_init(&attr);
144 #endif
145 
146 #ifdef UNIV_AIX
147  /* We must make sure a thread stack is at least 32 kB, otherwise
148  InnoDB might crash; we do not know if the default stack size on
149  AIX is always big enough. An empirical test on AIX-4.3 suggested
150  the size was 96 kB, though. */
151 
152  ret = pthread_attr_setstacksize(&attr,
153  (size_t)(PTHREAD_STACK_MIN
154  + 32 * 1024));
155  if (ret) {
156  fprintf(stderr,
157  "InnoDB: Error: pthread_attr_setstacksize"
158  " returned %d\n", ret);
159  exit(1);
160  }
161 #endif
163  os_thread_count++;
165 
166 #ifdef UNIV_HPUX10
167  ret = pthread_create(&pthread, pthread_attr_default, func, arg);
168 #else
169  ret = pthread_create(&pthread, &attr, func, arg);
170 #endif
171  if (ret) {
172  fprintf(stderr,
173  "InnoDB: Error: pthread_create returned %d\n", ret);
174  exit(1);
175  }
176 
177 #ifndef UNIV_HPUX10
178  pthread_attr_destroy(&attr);
179 #endif
180  if (thread_id) {
181  *thread_id = pthread;
182  }
183 
184  return(pthread);
185 #endif
186 }
187 
188 /*****************************************************************/
190 UNIV_INTERN
191 void
193 /*===========*/
194  void* exit_value)
196 {
197 #ifdef UNIV_DEBUG_THREAD_CREATION
198  fprintf(stderr, "Thread exits, id %lu\n",
200 #endif
201 
202 #ifdef UNIV_PFS_THREAD
203  pfs_delete_thread();
204 #endif
205 
207  os_thread_count--;
209 
210 #ifdef __WIN__
211  ExitThread((DWORD) exit_value);
212 #else
213  pthread_detach(pthread_self());
214  pthread_exit(exit_value);
215 #endif
216 }
217 
218 /*****************************************************************/
220 UNIV_INTERN
221 void
223 /*=================*/
224 {
225 #if defined(__WIN__)
226  SwitchToThread();
227 #elif (defined(HAVE_SCHED_YIELD) && defined(HAVE_SCHED_H))
228  sched_yield();
229 #elif defined(HAVE_PTHREAD_YIELD_ZERO_ARG)
230  pthread_yield();
231 #elif defined(HAVE_PTHREAD_YIELD_ONE_ARG)
232  pthread_yield(0);
233 #else
234  os_thread_sleep(0);
235 #endif
236 }
237 #endif /* !UNIV_HOTBACKUP */
238 
239 /*****************************************************************/
241 UNIV_INTERN
242 void
244 /*============*/
245  ulint tm)
246 {
247 #ifdef __WIN__
248  Sleep((DWORD) tm / 1000);
249 #else
250  struct timeval t;
251 
252  t.tv_sec = tm / 1000000;
253  t.tv_usec = tm % 1000000;
254 
255  select(0, NULL, NULL, NULL, &t);
256 #endif
257 }