1
/* $Id: thread-os2.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
3
* innotek Portable Runtime - Threads, OS/2.
7
* Copyright (C) 2006-2007 innotek GmbH
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.
19
/*******************************************************************************
21
*******************************************************************************/
22
#define LOG_GROUP RTLOGGROUP_THREAD
31
#include <InnoTekLIBC/FastInfoBlocks.h>
33
#include <iprt/thread.h>
35
#include <iprt/assert.h>
36
#include <iprt/alloc.h>
38
#include <iprt/string.h>
40
#include "internal/thread.h"
43
/*******************************************************************************
45
*******************************************************************************/
46
/** Pointer to thread local memory which points to the current thread. */
47
static PRTTHREADINT *g_ppCurThread;
50
/*******************************************************************************
51
* Internal Functions *
52
*******************************************************************************/
53
static void rtThreadNativeMain(void *pvArgs);
56
int rtThreadNativeInit(void)
59
* Allocate thread local memory.
62
int rc = DosAllocThreadLocalMemory(1, &pul);
64
return VERR_NO_TLS_FOR_SELF;
65
g_ppCurThread = (PRTTHREADINT *)(void *)pul;
70
int rtThreadNativeAdopt(PRTTHREADINT pThread)
73
* Block SIGALRM - required for timer-posix.cpp.
74
* This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
75
* It will not help much if someone creates threads directly using pthread_create. :/
79
sigaddset(&SigSet, SIGALRM);
80
sigprocmask(SIG_BLOCK, &SigSet, NULL);
82
*g_ppCurThread = pThread;
88
* Wrapper which unpacks the params and calls thread function.
90
static void rtThreadNativeMain(void *pvArgs)
93
* Block SIGALRM - required for timer-posix.cpp.
94
* This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
95
* It will not help much if someone creates threads directly using pthread_create. :/
99
sigaddset(&SigSet, SIGALRM);
100
sigprocmask(SIG_BLOCK, &SigSet, NULL);
105
PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
106
*g_ppCurThread = pThread;
109
rtThreadMain(pThread, fibGetTidPid(), &pThread->szName[0]);
111
rtThreadMain(pThread, _gettid(), &pThread->szName[0]);
114
*g_ppCurThread = NULL;
119
int rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
122
* Default stack size.
124
if (!pThread->cbStack)
125
pThread->cbStack = 512*1024;
130
int iThreadId = _beginthread(rtThreadNativeMain, NULL, pThread->cbStack, pThread);
134
*pNativeThread = iThreadId | (fibGetPid() << 16);
136
*pNativeThread = iThreadId;
140
return RTErrConvertFromErrno(errno);
144
RTDECL(RTTHREAD) RTThreadSelf(void)
146
PRTTHREADINT pThread = *g_ppCurThread;
148
return (RTTHREAD)pThread;
149
/** @todo import alien threads? */
154
RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
157
return fibGetTidPid();
164
RTDECL(int) RTThreadSleep(unsigned cMillies)
166
LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
168
LogFlow(("RTThreadSleep: returning (cMillies=%d)\n", cMillies));
173
RTDECL(bool) RTThreadYield(void)
175
uint64_t u64TS = ASMReadTSC();
177
u64TS = ASMReadTSC() - u64TS;
178
bool fRc = u64TS > 1750;
179
LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
184
RTDECL(uint64_t) RTThreadGetAffinity(void)
192
int rc = DosQueryThreadAffinity(AFNTY_THREAD, &u.mpaff);
199
RTDECL(int) RTThreadSetAffinity(uint64_t u64Mask)
207
int rc = DosSetThreadAffinity(&u.mpaff);
210
return RTErrConvertFromOS2(rc);