VirtualBox

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

Last change on this file since 5999 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/* $Id: thread-os2.cpp 5999 2007-12-07 15:05:06Z 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 (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
42#include <iprt/thread.h>
43#include <iprt/log.h>
44#include <iprt/assert.h>
45#include <iprt/alloc.h>
46#include <iprt/asm.h>
47#include <iprt/string.h>
48#include <iprt/err.h>
49#include "internal/thread.h"
50
51
52/*******************************************************************************
53* Global Variables *
54*******************************************************************************/
55/** Pointer to thread local memory which points to the current thread. */
56static PRTTHREADINT *g_ppCurThread;
57
58
59/*******************************************************************************
60* Internal Functions *
61*******************************************************************************/
62static void rtThreadNativeMain(void *pvArgs);
63
64
65int rtThreadNativeInit(void)
66{
67 /*
68 * Allocate thread local memory.
69 */
70 PULONG pul;
71 int rc = DosAllocThreadLocalMemory(1, &pul);
72 if (rc)
73 return VERR_NO_TLS_FOR_SELF;
74 g_ppCurThread = (PRTTHREADINT *)(void *)pul;
75 return VINF_SUCCESS;
76}
77
78
79int rtThreadNativeAdopt(PRTTHREADINT pThread)
80{
81 /*
82 * Block SIGALRM - required for timer-posix.cpp.
83 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
84 * It will not help much if someone creates threads directly using pthread_create. :/
85 */
86 sigset_t SigSet;
87 sigemptyset(&SigSet);
88 sigaddset(&SigSet, SIGALRM);
89 sigprocmask(SIG_BLOCK, &SigSet, NULL);
90
91 *g_ppCurThread = pThread;
92 return VINF_SUCCESS;
93}
94
95
96/**
97 * Wrapper which unpacks the params and calls thread function.
98 */
99static void rtThreadNativeMain(void *pvArgs)
100{
101 /*
102 * Block SIGALRM - required for timer-posix.cpp.
103 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
104 * It will not help much if someone creates threads directly using pthread_create. :/
105 */
106 sigset_t SigSet;
107 sigemptyset(&SigSet);
108 sigaddset(&SigSet, SIGALRM);
109 sigprocmask(SIG_BLOCK, &SigSet, NULL);
110
111 /*
112 * Call common main.
113 */
114 PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
115 *g_ppCurThread = pThread;
116
117#ifdef fibGetTidPid
118 rtThreadMain(pThread, fibGetTidPid(), &pThread->szName[0]);
119#else
120 rtThreadMain(pThread, _gettid(), &pThread->szName[0]);
121#endif
122
123 *g_ppCurThread = NULL;
124 _endthread();
125}
126
127
128int rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
129{
130 /*
131 * Default stack size.
132 */
133 if (!pThread->cbStack)
134 pThread->cbStack = 512*1024;
135
136 /*
137 * Create the thread.
138 */
139 int iThreadId = _beginthread(rtThreadNativeMain, NULL, pThread->cbStack, pThread);
140 if (iThreadId > 0)
141 {
142#ifdef fibGetTidPid
143 *pNativeThread = iThreadId | (fibGetPid() << 16);
144#else
145 *pNativeThread = iThreadId;
146#endif
147 return VINF_SUCCESS;
148 }
149 return RTErrConvertFromErrno(errno);
150}
151
152
153RTDECL(RTTHREAD) RTThreadSelf(void)
154{
155 PRTTHREADINT pThread = *g_ppCurThread;
156 if (pThread)
157 return (RTTHREAD)pThread;
158 /** @todo import alien threads? */
159 return NULL;
160}
161
162
163RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
164{
165#ifdef fibGetTidPid
166 return fibGetTidPid();
167#else
168 return _gettid();
169#endif
170}
171
172
173RTDECL(int) RTThreadSleep(unsigned cMillies)
174{
175 LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
176 DosSleep(cMillies);
177 LogFlow(("RTThreadSleep: returning (cMillies=%d)\n", cMillies));
178 return VINF_SUCCESS;
179}
180
181
182RTDECL(bool) RTThreadYield(void)
183{
184 uint64_t u64TS = ASMReadTSC();
185 DosSleep(0);
186 u64TS = ASMReadTSC() - u64TS;
187 bool fRc = u64TS > 1750;
188 LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
189 return fRc;
190}
191
192
193RTDECL(uint64_t) RTThreadGetAffinity(void)
194{
195 union
196 {
197 uint64_t u64;
198 MPAFFINITY mpaff;
199 } u;
200
201 int rc = DosQueryThreadAffinity(AFNTY_THREAD, &u.mpaff);
202 if (rc)
203 u.u64 = 1;
204 return u.u64;
205}
206
207
208RTDECL(int) RTThreadSetAffinity(uint64_t u64Mask)
209{
210 union
211 {
212 uint64_t u64;
213 MPAFFINITY mpaff;
214 } u;
215 u.u64 = u64Mask;
216 int rc = DosSetThreadAffinity(&u.mpaff);
217 if (!rc)
218 return VINF_SUCCESS;
219 return RTErrConvertFromOS2(rc);
220}
221
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