VirtualBox

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

Last change on this file since 3460 was 3360, checked in by vboxsync, 18 years ago

Runtime: Removed the deadly assertion from RTThreadSelf() on OS/2.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1/* $Id: thread-os2.cpp 3360 2007-07-02 20:54:17Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Threads, OS/2.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP RTLOGGROUP_THREAD
27#define INCL_BASE
28#include <os2.h>
29#undef RT_MAX
30
31#include <errno.h>
32#include <process.h>
33#include <stdlib.h>
34#include <signal.h>
35#include <InnoTekLIBC/FastInfoBlocks.h>
36
37#include <iprt/thread.h>
38#include <iprt/log.h>
39#include <iprt/assert.h>
40#include <iprt/alloc.h>
41#include <iprt/asm.h>
42#include <iprt/string.h>
43#include <iprt/err.h>
44#include "internal/thread.h"
45
46
47/*******************************************************************************
48* Global Variables *
49*******************************************************************************/
50/** Pointer to thread local memory which points to the current thread. */
51static PRTTHREADINT *g_ppCurThread;
52
53
54/*******************************************************************************
55* Internal Functions *
56*******************************************************************************/
57static void rtThreadNativeMain(void *pvArgs);
58
59
60int rtThreadNativeInit(void)
61{
62 /*
63 * Allocate thread local memory.
64 */
65 PULONG pul;
66 int rc = DosAllocThreadLocalMemory(1, &pul);
67 if (rc)
68 return VERR_NO_TLS_FOR_SELF;
69 g_ppCurThread = (PRTTHREADINT *)(void *)pul;
70 return VINF_SUCCESS;
71}
72
73
74int rtThreadNativeAdopt(PRTTHREADINT pThread)
75{
76 /*
77 * Block SIGALRM - required for timer-posix.cpp.
78 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
79 * It will not help much if someone creates threads directly using pthread_create. :/
80 */
81 sigset_t SigSet;
82 sigemptyset(&SigSet);
83 sigaddset(&SigSet, SIGALRM);
84 sigprocmask(SIG_BLOCK, &SigSet, NULL);
85
86 *g_ppCurThread = pThread;
87 return VINF_SUCCESS;
88}
89
90
91/**
92 * Wrapper which unpacks the params and calls thread function.
93 */
94static void rtThreadNativeMain(void *pvArgs)
95{
96 /*
97 * Block SIGALRM - required for timer-posix.cpp.
98 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
99 * It will not help much if someone creates threads directly using pthread_create. :/
100 */
101 sigset_t SigSet;
102 sigemptyset(&SigSet);
103 sigaddset(&SigSet, SIGALRM);
104 sigprocmask(SIG_BLOCK, &SigSet, NULL);
105
106 /*
107 * Call common main.
108 */
109 PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
110 *g_ppCurThread = pThread;
111
112#ifdef fibGetTidPid
113 rtThreadMain(pThread, fibGetTidPid(), &pThread->szName[0]);
114#else
115 rtThreadMain(pThread, _gettid(), &pThread->szName[0]);
116#endif
117
118 *g_ppCurThread = NULL;
119 _endthread();
120}
121
122
123int rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
124{
125 /*
126 * Default stack size.
127 */
128 if (!pThread->cbStack)
129 pThread->cbStack = 512*1024;
130
131 /*
132 * Create the thread.
133 */
134 int iThreadId = _beginthread(rtThreadNativeMain, NULL, pThread->cbStack, pThread);
135 if (iThreadId > 0)
136 {
137#ifdef fibGetTidPid
138 *pNativeThread = iThreadId | (fibGetPid() << 16);
139#else
140 *pNativeThread = iThreadId;
141#endif
142 return VINF_SUCCESS;
143 }
144 return RTErrConvertFromErrno(errno);
145}
146
147
148RTDECL(RTTHREAD) RTThreadSelf(void)
149{
150 PRTTHREADINT pThread = *g_ppCurThread;
151 if (pThread)
152 return (RTTHREAD)pThread;
153 /** @todo import alien threads? */
154 return NULL;
155}
156
157
158RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
159{
160#ifdef fibGetTidPid
161 return fibGetTidPid();
162#else
163 return _gettid();
164#endif
165}
166
167
168RTDECL(int) RTThreadSleep(unsigned cMillies)
169{
170 LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
171 DosSleep(cMillies);
172 LogFlow(("RTThreadSleep: returning (cMillies=%d)\n", cMillies));
173 return VINF_SUCCESS;
174}
175
176
177RTDECL(bool) RTThreadYield(void)
178{
179 uint64_t u64TS = ASMReadTSC();
180 DosSleep(0);
181 u64TS = ASMReadTSC() - u64TS;
182 bool fRc = u64TS > 1750;
183 LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
184 return fRc;
185}
186
187
188RTDECL(uint64_t) RTThreadGetAffinity(void)
189{
190 union
191 {
192 uint64_t u64;
193 MPAFFINITY mpaff;
194 } u;
195
196 int rc = DosQueryThreadAffinity(AFNTY_THREAD, &u.mpaff);
197 if (rc)
198 u.u64 = 1;
199 return u.u64;
200}
201
202
203RTDECL(int) RTThreadSetAffinity(uint64_t u64Mask)
204{
205 union
206 {
207 uint64_t u64;
208 MPAFFINITY mpaff;
209 } u;
210 u.u64 = u64Mask;
211 int rc = DosSetThreadAffinity(&u.mpaff);
212 if (!rc)
213 return VINF_SUCCESS;
214 return RTErrConvertFromOS2(rc);
215}
216
Note: See TracBrowser for help on using the repository browser.

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