1 | /* $Id: thread-os2.cpp 1190 2007-03-04 20:42:13Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - Threads, OS/2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.215389.xyz. 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. */
|
---|
51 | static PRTTHREADINT *g_ppCurThread;
|
---|
52 |
|
---|
53 |
|
---|
54 | /*******************************************************************************
|
---|
55 | * Internal Functions *
|
---|
56 | *******************************************************************************/
|
---|
57 | static void rtThreadNativeMain(void *pvArgs);
|
---|
58 |
|
---|
59 |
|
---|
60 | int 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 |
|
---|
74 | int 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 | */
|
---|
94 | static 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());
|
---|
114 | #else
|
---|
115 | rtThreadMain(pThread, _gettid());
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | *g_ppCurThread = NULL;
|
---|
119 | _endthread();
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | int 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 | *pNativeThread = iThreadId;
|
---|
138 | return VINF_SUCCESS;
|
---|
139 | }
|
---|
140 | return RTErrConvertFromErrno(errno);
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
145 | {
|
---|
146 | PRTTHREADINT pThread = *g_ppCurThread;
|
---|
147 | if (pThread)
|
---|
148 | return (RTTHREAD)pThread;
|
---|
149 | /** @todo import alien threads? */
|
---|
150 | AssertMsgFailed(("Thread not found\n"));
|
---|
151 | return NULL;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
156 | {
|
---|
157 | #ifdef fibGetTidPid
|
---|
158 | return fibGetTidPid();
|
---|
159 | #else
|
---|
160 | return _gettid();
|
---|
161 | #endif
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | RTDECL(int) RTThreadSleep(unsigned cMillies)
|
---|
166 | {
|
---|
167 | LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
|
---|
168 | DosSleep(cMillies);
|
---|
169 | LogFlow(("RTThreadSleep: returning (cMillies=%d)\n", cMillies));
|
---|
170 | return VINF_SUCCESS;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | RTDECL(bool) RTThreadYield(void)
|
---|
175 | {
|
---|
176 | uint64_t u64TS = ASMReadTSC();
|
---|
177 | DosSleep(0);
|
---|
178 | u64TS = ASMReadTSC() - u64TS;
|
---|
179 | bool fRc = u64TS > 1750;
|
---|
180 | LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
|
---|
181 | return fRc;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | RTDECL(uint64_t) RTThreadGetAffinity(void)
|
---|
186 | {
|
---|
187 | union
|
---|
188 | {
|
---|
189 | uint64_t u64;
|
---|
190 | MPAFFINITY mpaff;
|
---|
191 | } u;
|
---|
192 |
|
---|
193 | int rc = DosQueryThreadAffinity(AFNTY_THREAD, &u.mpaff);
|
---|
194 | if (rc)
|
---|
195 | u.u64 = 1;
|
---|
196 | return u.u64;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | RTDECL(int) RTThreadSetAffinity(uint64_t u64Mask)
|
---|
201 | {
|
---|
202 | union
|
---|
203 | {
|
---|
204 | uint64_t u64;
|
---|
205 | MPAFFINITY mpaff;
|
---|
206 | } u;
|
---|
207 | u.u64 = u64Mask;
|
---|
208 | int rc = DosSetThreadAffinity(&u.mpaff);
|
---|
209 | if (!rc)
|
---|
210 | return VINF_SUCCESS;
|
---|
211 | return RTErrConvertFromOS2(rc);
|
---|
212 | }
|
---|
213 |
|
---|