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