VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/os2/thread-os2.cpp@ 34670

Last change on this file since 34670 was 34256, checked in by vboxsync, 14 years ago

IPRT: Must clear the TLS entry holding RTTHREAD before freeing the structure or the electric fence heap may cause a crash when blocking.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1/* $Id: thread-os2.cpp 34256 2010-11-22 15:55:00Z vboxsync $ */
2/** @file
3 * IPRT - Threads, OS/2.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP RTLOGGROUP_THREAD
32#define INCL_BASE
33#include <os2.h>
34#undef RT_MAX
35
36#include <errno.h>
37#include <process.h>
38#include <stdlib.h>
39#include <signal.h>
40#include <InnoTekLIBC/FastInfoBlocks.h>
41#include <InnoTekLIBC/thread.h>
42
43#include <iprt/thread.h>
44#include <iprt/log.h>
45#include <iprt/assert.h>
46#include <iprt/alloc.h>
47#include <iprt/asm-amd64-x86.h>
48#include <iprt/string.h>
49#include <iprt/err.h>
50#include "internal/thread.h"
51
52
53/*******************************************************************************
54* Global Variables *
55*******************************************************************************/
56/** Pointer to thread local memory which points to the current thread. */
57static PRTTHREADINT *g_ppCurThread;
58
59
60/*******************************************************************************
61* Internal Functions *
62*******************************************************************************/
63static void rtThreadNativeMain(void *pvArgs);
64
65
66int rtThreadNativeInit(void)
67{
68 /*
69 * Allocate thread local memory.
70 */
71 PULONG pul;
72 int rc = DosAllocThreadLocalMemory(1, &pul);
73 if (rc)
74 return VERR_NO_TLS_FOR_SELF;
75 g_ppCurThread = (PRTTHREADINT *)(void *)pul;
76 return VINF_SUCCESS;
77}
78
79
80int rtThreadNativeAdopt(PRTTHREADINT pThread)
81{
82 /*
83 * Block SIGALRM - required for timer-posix.cpp.
84 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
85 * It will not help much if someone creates threads directly using pthread_create. :/
86 */
87 sigset_t SigSet;
88 sigemptyset(&SigSet);
89 sigaddset(&SigSet, SIGALRM);
90 sigprocmask(SIG_BLOCK, &SigSet, NULL);
91
92 *g_ppCurThread = pThread;
93 return VINF_SUCCESS;
94}
95
96
97void rtThreadNativeDestroy(PRTTHREADINT pThread)
98{
99 if (pThread == *g_ppCurThread)
100 *g_ppCurThread = NULL;
101}
102
103
104/**
105 * Wrapper which unpacks the params and calls thread function.
106 */
107static void rtThreadNativeMain(void *pvArgs)
108{
109 /*
110 * Block SIGALRM - required for timer-posix.cpp.
111 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
112 * It will not help much if someone creates threads directly using pthread_create. :/
113 */
114 sigset_t SigSet;
115 sigemptyset(&SigSet);
116 sigaddset(&SigSet, SIGALRM);
117 sigprocmask(SIG_BLOCK, &SigSet, NULL);
118
119 /*
120 * Call common main.
121 */
122 PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
123 *g_ppCurThread = pThread;
124
125#ifdef fibGetTidPid
126 rtThreadMain(pThread, fibGetTidPid(), &pThread->szName[0]);
127#else
128 rtThreadMain(pThread, _gettid(), &pThread->szName[0]);
129#endif
130
131 *g_ppCurThread = NULL;
132 _endthread();
133}
134
135
136int rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
137{
138 /*
139 * Default stack size.
140 */
141 if (!pThread->cbStack)
142 pThread->cbStack = 512*1024;
143
144 /*
145 * Create the thread.
146 */
147 int iThreadId = _beginthread(rtThreadNativeMain, NULL, pThread->cbStack, pThread);
148 if (iThreadId > 0)
149 {
150#ifdef fibGetTidPid
151 *pNativeThread = iThreadId | (fibGetPid() << 16);
152#else
153 *pNativeThread = iThreadId;
154#endif
155 return VINF_SUCCESS;
156 }
157 return RTErrConvertFromErrno(errno);
158}
159
160
161RTDECL(RTTHREAD) RTThreadSelf(void)
162{
163 PRTTHREADINT pThread = *g_ppCurThread;
164 if (pThread)
165 return (RTTHREAD)pThread;
166 /** @todo import alien threads? */
167 return NULL;
168}
169
170
171RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
172{
173#ifdef fibGetTidPid
174 return fibGetTidPid();
175#else
176 return _gettid();
177#endif
178}
179
180
181RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
182{
183 LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
184 DosSleep(cMillies);
185 LogFlow(("RTThreadSleep: returning (cMillies=%d)\n", cMillies));
186 return VINF_SUCCESS;
187}
188
189
190RTDECL(bool) RTThreadYield(void)
191{
192 uint64_t u64TS = ASMReadTSC();
193 DosSleep(0);
194 u64TS = ASMReadTSC() - u64TS;
195 bool fRc = u64TS > 1750;
196 LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
197 return fRc;
198}
199
200
201RTDECL(uint64_t) RTThreadGetAffinity(void)
202{
203 union
204 {
205 uint64_t u64;
206 MPAFFINITY mpaff;
207 } u;
208
209 int rc = DosQueryThreadAffinity(AFNTY_THREAD, &u.mpaff);
210 if (rc)
211 u.u64 = 1;
212 return u.u64;
213}
214
215
216RTDECL(int) RTThreadSetAffinity(uint64_t u64Mask)
217{
218 union
219 {
220 uint64_t u64;
221 MPAFFINITY mpaff;
222 } u;
223 u.u64 = u64Mask;
224 int rc = DosSetThreadAffinity(&u.mpaff);
225 if (!rc)
226 return VINF_SUCCESS;
227 return RTErrConvertFromOS2(rc);
228}
229
230
231RTR3DECL(RTTLS) RTTlsAlloc(void)
232{
233 AssertCompile(NIL_RTTLS == -1);
234 return __libc_TLSAlloc();
235}
236
237
238RTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor)
239{
240 int rc;
241 int iTls = __libc_TLSAlloc();
242 if (iTls != -1)
243 {
244 if ( !pfnDestructor
245 || __libc_TLSDestructor(iTls, (void (*)(void *, int, unsigned))pfnDestructor, 0) != -1)
246 {
247 *piTls = iTls;
248 return VINF_SUCCESS;
249 }
250
251 rc = RTErrConvertFromErrno(errno);
252 __libc_TLSFree(iTls);
253 }
254 else
255 rc = RTErrConvertFromErrno(errno);
256
257 *piTls = NIL_RTTLS;
258 return rc;
259}
260
261
262RTR3DECL(int) RTTlsFree(RTTLS iTls)
263{
264 if (iTls == NIL_RTTLS)
265 return VINF_SUCCESS;
266 if (__libc_TLSFree(iTls) != -1)
267 return VINF_SUCCESS;
268 return RTErrConvertFromErrno(errno);
269}
270
271
272RTR3DECL(void *) RTTlsGet(RTTLS iTls)
273{
274 return __libc_TLSGet(iTls);
275}
276
277
278RTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue)
279{
280 int rc = VINF_SUCCESS;
281 void *pv = __libc_TLSGet(iTls);
282 if (RT_UNLIKELY(!pv))
283 {
284 errno = 0;
285 pv = __libc_TLSGet(iTls);
286 if (!pv && errno)
287 rc = RTErrConvertFromErrno(errno);
288 }
289
290 *ppvValue = pv;
291 return rc;
292}
293
294
295RTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue)
296{
297 if (__libc_TLSSet(iTls, pvValue) != -1)
298 return VINF_SUCCESS;
299 return RTErrConvertFromErrno(errno);
300}
301
302
303RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime)
304{
305 return VERR_NOT_IMPLEMENTED;
306}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette