VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/thread-posix.cpp@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1/* $Id: thread-posix.cpp 1 1970-01-01 00:00:00Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - Threads, POSIX.
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#include <errno.h>
28#include <pthread.h>
29#include <signal.h>
30
31#include <iprt/thread.h>
32#include <iprt/log.h>
33#include <iprt/assert.h>
34#include <iprt/asm.h>
35#include <iprt/err.h>
36#include "internal/thread.h"
37
38
39/*******************************************************************************
40* Header Files *
41*******************************************************************************/
42/** The pthread key in which we store the pointer to our own PRTTHREAD structure. */
43static pthread_key_t g_SelfKey;
44
45
46/*******************************************************************************
47* Internal Functions *
48*******************************************************************************/
49static void *rtThreadNativeMain(void *pvArgs);
50static void rtThreadKeyDestruct(void *pvValue);
51
52
53int rtThreadNativeInit(void)
54{
55 /*
56 * Allocate the TLS (key in posix terms) where we store the pointer to
57 * a threads RTTHREADINT structure.
58 */
59
60 int rc = pthread_key_create(&g_SelfKey, rtThreadKeyDestruct);
61 if (!rc)
62 return VINF_SUCCESS;
63 return VERR_NO_TLS_FOR_SELF;
64}
65
66
67/**
68 * Destructor called when a thread terminates.
69 * @param pvValue The key value. PRTTHREAD in our case.
70 */
71static void rtThreadKeyDestruct(void *pvValue)
72{
73 /*
74 * Deal with alien threads.
75 */
76 PRTTHREADINT pThread = (PRTTHREADINT)pvValue;
77 if (pThread->fIntFlags & RTTHREADINT_FLAGS_ALIEN)
78 {
79 pthread_setspecific(g_SelfKey, pThread);
80 rtThreadTerminate(pThread, 0);
81 pthread_setspecific(g_SelfKey, NULL);
82 }
83}
84
85
86/**
87 * Adopts a thread, this is called immediately after allocating the
88 * thread structure.
89 *
90 * @param pThread Pointer to the thread structure.
91 */
92int rtThreadNativeAdopt(PRTTHREADINT pThread)
93{
94 /*
95 * Block SIGALRM - required for timer-posix.cpp.
96 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
97 * It will not help much if someone creates threads directly using pthread_create. :/
98 */
99 sigset_t SigSet;
100 sigemptyset(&SigSet);
101 sigaddset(&SigSet, SIGALRM);
102 sigprocmask(SIG_BLOCK, &SigSet, NULL);
103
104 int rc = pthread_setspecific(g_SelfKey, pThread);
105 if (!rc)
106 return VINF_SUCCESS;
107 return VERR_FAILED_TO_SET_SELF_TLS;
108}
109
110
111/**
112 * Wrapper which unpacks the params and calls thread function.
113 */
114static void *rtThreadNativeMain(void *pvArgs)
115{
116 PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
117
118 /*
119 * Block SIGALRM - required for timer-posix.cpp.
120 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
121 * It will not help much if someone creates threads directly using pthread_create. :/
122 */
123 sigset_t SigSet;
124 sigemptyset(&SigSet);
125 sigaddset(&SigSet, SIGALRM);
126 sigprocmask(SIG_BLOCK, &SigSet, NULL);
127
128 int rc = pthread_setspecific(g_SelfKey, pThread);
129 AssertReleaseMsg(!rc, ("failed to set self TLS. rc=%d thread '%s'\n", rc, pThread->szName));
130
131 /*
132 * Call common main.
133 */
134 pthread_t Self = pthread_self();
135 Assert((uintptr_t)Self == (RTNATIVETHREAD)Self && (uintptr_t)Self != NIL_RTNATIVETHREAD);
136 rc = rtThreadMain(pThread, (uintptr_t)Self);
137
138 pthread_setspecific(g_SelfKey, NULL);
139 pthread_exit((void *)rc);
140 return (void *)rc;
141}
142
143
144int rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
145{
146 /*
147 * Set the default stack size.
148 */
149 if (!pThread->cbStack)
150 pThread->cbStack = 512*1024;
151
152 /*
153 * Setup thread attributes.
154 */
155 pthread_attr_t ThreadAttr;
156 int rc = pthread_attr_init(&ThreadAttr);
157 if (!rc)
158 {
159 rc = pthread_attr_setdetachstate(&ThreadAttr, PTHREAD_CREATE_DETACHED);
160 if (!rc)
161 {
162 rc = pthread_attr_setstacksize(&ThreadAttr, pThread->cbStack);
163 if (!rc)
164 {
165 /*
166 * Create the thread.
167 */
168 pthread_t ThreadId;
169 rc = pthread_create(&ThreadId, &ThreadAttr, rtThreadNativeMain, pThread);
170 if (!rc)
171 {
172 *pNativeThread = (uintptr_t)ThreadId;
173 return VINF_SUCCESS;
174 }
175 }
176 }
177 pthread_attr_destroy(&ThreadAttr);
178 }
179 return RTErrConvertFromErrno(rc);
180}
181
182
183RTDECL(RTTHREAD) RTThreadSelf(void)
184{
185 PRTTHREADINT pThread = (PRTTHREADINT)pthread_getspecific(g_SelfKey);
186 /** @todo import alien threads? */
187 return pThread;
188}
189
190
191RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
192{
193 return (RTNATIVETHREAD)pthread_self();
194}
195
196
197RTDECL(int) RTThreadSleep(unsigned cMillies)
198{
199 LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
200 if (!cMillies)
201 {
202#ifdef __DARWIN__
203 pthread_yield_np();
204#else
205 if (!pthread_yield())
206#endif
207 {
208 LogFlow(("RTThreadSleep: returning %Vrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
209 return VINF_SUCCESS;
210 }
211 }
212 else
213 {
214 struct timespec ts;
215 struct timespec tsrem = {0,0};
216
217 ts.tv_nsec = (cMillies % 1000) * 1000000;
218 ts.tv_sec = cMillies / 1000;
219 if (!nanosleep(&ts, &tsrem))
220 {
221 LogFlow(("RTThreadSleep: returning %Vrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
222 return VINF_SUCCESS;
223 }
224 }
225
226 int rc = RTErrConvertFromErrno(errno);
227 LogFlow(("RTThreadSleep: returning %Vrc (cMillies=%d)\n", rc, cMillies));
228 return rc;
229}
230
231
232RTDECL(bool) RTThreadYield(void)
233{
234 uint64_t u64TS = ASMReadTSC();
235#ifdef __DARWIN__
236 pthread_yield_np();
237#else
238 pthread_yield();
239#endif
240 u64TS = ASMReadTSC() - u64TS;
241 bool fRc = u64TS > 1500;
242 LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
243 return fRc;
244}
245
246
247RTR3DECL(uint64_t) RTThreadGetAffinity(void)
248{
249 return 1;
250}
251
252
253RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask)
254{
255 if (u64Mask != 1)
256 return VERR_INVALID_PARAMETER;
257 return VINF_SUCCESS;
258}
259
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