VirtualBox

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

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

space

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.6 KB
Line 
1/* $Id: thread-posix.cpp 13661 2008-10-29 15:46:56Z vboxsync $ */
2/** @file
3 * IPRT - Threads, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 (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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_THREAD
36#include <errno.h>
37#include <pthread.h>
38#include <signal.h>
39#if defined(RT_OS_SOLARIS)
40# include <sched.h>
41#endif
42
43#include <iprt/thread.h>
44#include <iprt/log.h>
45#include <iprt/assert.h>
46#include <iprt/asm.h>
47#include <iprt/err.h>
48#include <iprt/string.h>
49#include "internal/thread.h"
50
51
52/*******************************************************************************
53* Defined Constants And Macros *
54*******************************************************************************/
55/** The signal we're using for RTThreadPoke. */
56#define RTTHREAD_POSIX_POKE_SIG SIGUSR2
57
58
59/*******************************************************************************
60* Global Variables *
61*******************************************************************************/
62/** The pthread key in which we store the pointer to our own PRTTHREAD structure. */
63static pthread_key_t g_SelfKey;
64
65
66/*******************************************************************************
67* Internal Functions *
68*******************************************************************************/
69static void *rtThreadNativeMain(void *pvArgs);
70static void rtThreadKeyDestruct(void *pvValue);
71static void rtThreadPosixPokeSignal(int iSignal);
72
73
74int rtThreadNativeInit(void)
75{
76 /*
77 * Allocate the TLS (key in posix terms) where we store the pointer to
78 * a threads RTTHREADINT structure.
79 */
80 int rc = pthread_key_create(&g_SelfKey, rtThreadKeyDestruct);
81 if (rc)
82 return VERR_NO_TLS_FOR_SELF;
83
84 /*
85 * Register the dummy signal handler for RTThreadPoke.
86 * (Assert may explode here, but at least we'll notice.)
87 */
88 struct sigaction SigAct;
89 memset(&SigAct, '\0', sizeof(SigAct));
90 SigAct.sa_handler = rtThreadPosixPokeSignal;
91 sigfillset(&SigAct.sa_mask);
92 SigAct.sa_flags = 0;
93
94 struct sigaction SigActOld;
95 if (!sigaction(RTTHREAD_POSIX_POKE_SIG, &SigAct, &SigActOld))
96 Assert(SigActOld.sa_handler == SIG_DFL);
97 else
98 {
99 rc = RTErrConvertFromErrno(errno);
100 AssertMsgFailed(("rc=%Rrc errno=%d\n", rc, errno));
101 pthread_key_delete(g_SelfKey);
102 g_SelfKey = 0;
103 }
104 return rc;
105}
106
107
108/**
109 * Destructor called when a thread terminates.
110 * @param pvValue The key value. PRTTHREAD in our case.
111 */
112static void rtThreadKeyDestruct(void *pvValue)
113{
114 /*
115 * Deal with alien threads.
116 */
117 PRTTHREADINT pThread = (PRTTHREADINT)pvValue;
118 if (pThread->fIntFlags & RTTHREADINT_FLAGS_ALIEN)
119 {
120 pthread_setspecific(g_SelfKey, pThread);
121 rtThreadTerminate(pThread, 0);
122 pthread_setspecific(g_SelfKey, NULL);
123 }
124}
125
126
127/**
128 * Dummy signal handler for the poke signal.
129 *
130 * @param iSignal The signal number.
131 */
132static void rtThreadPosixPokeSignal(int iSignal)
133{
134 Assert(iSignal == RTTHREAD_POSIX_POKE_SIG);
135 NOREF(iSignal);
136}
137
138
139/**
140 * Adopts a thread, this is called immediately after allocating the
141 * thread structure.
142 *
143 * @param pThread Pointer to the thread structure.
144 */
145int rtThreadNativeAdopt(PRTTHREADINT pThread)
146{
147 /*
148 * Block SIGALRM - required for timer-posix.cpp.
149 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
150 * It will not help much if someone creates threads directly using pthread_create. :/
151 */
152 sigset_t SigSet;
153 sigemptyset(&SigSet);
154 sigaddset(&SigSet, SIGALRM);
155 sigdelset(&SigSet, RTTHREAD_POSIX_POKE_SIG);
156 sigprocmask(SIG_BLOCK, &SigSet, NULL);
157 siginterrupt(RTTHREAD_POSIX_POKE_SIG, 1);
158
159 int rc = pthread_setspecific(g_SelfKey, pThread);
160 if (!rc)
161 return VINF_SUCCESS;
162 return VERR_FAILED_TO_SET_SELF_TLS;
163}
164
165
166/**
167 * Wrapper which unpacks the params and calls thread function.
168 */
169static void *rtThreadNativeMain(void *pvArgs)
170{
171 PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
172
173 /*
174 * Block SIGALRM - required for timer-posix.cpp.
175 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
176 * It will not help much if someone creates threads directly using pthread_create. :/
177 */
178 sigset_t SigSet;
179 sigemptyset(&SigSet);
180 sigaddset(&SigSet, SIGALRM);
181 sigdelset(&SigSet, RTTHREAD_POSIX_POKE_SIG);
182 sigprocmask(SIG_BLOCK, &SigSet, NULL);
183 siginterrupt(RTTHREAD_POSIX_POKE_SIG, 1);
184
185 int rc = pthread_setspecific(g_SelfKey, pThread);
186 AssertReleaseMsg(!rc, ("failed to set self TLS. rc=%d thread '%s'\n", rc, pThread->szName));
187
188 /*
189 * Call common main.
190 */
191 pthread_t Self = pthread_self();
192 Assert((uintptr_t)Self == (RTNATIVETHREAD)Self && (uintptr_t)Self != NIL_RTNATIVETHREAD);
193 rc = rtThreadMain(pThread, (uintptr_t)Self, &pThread->szName[0]);
194
195 pthread_setspecific(g_SelfKey, NULL);
196 pthread_exit((void *)rc);
197 return (void *)rc;
198}
199
200
201int rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
202{
203 /*
204 * Set the default stack size.
205 */
206 if (!pThread->cbStack)
207 pThread->cbStack = 512*1024;
208
209 /*
210 * Setup thread attributes.
211 */
212 pthread_attr_t ThreadAttr;
213 int rc = pthread_attr_init(&ThreadAttr);
214 if (!rc)
215 {
216 rc = pthread_attr_setdetachstate(&ThreadAttr, PTHREAD_CREATE_DETACHED);
217 if (!rc)
218 {
219 rc = pthread_attr_setstacksize(&ThreadAttr, pThread->cbStack);
220 if (!rc)
221 {
222 /*
223 * Create the thread.
224 */
225 pthread_t ThreadId;
226 rc = pthread_create(&ThreadId, &ThreadAttr, rtThreadNativeMain, pThread);
227 if (!rc)
228 {
229 *pNativeThread = (uintptr_t)ThreadId;
230 return VINF_SUCCESS;
231 }
232 }
233 }
234 pthread_attr_destroy(&ThreadAttr);
235 }
236 return RTErrConvertFromErrno(rc);
237}
238
239
240RTDECL(RTTHREAD) RTThreadSelf(void)
241{
242 PRTTHREADINT pThread = (PRTTHREADINT)pthread_getspecific(g_SelfKey);
243 /** @todo import alien threads? */
244 return pThread;
245}
246
247
248RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
249{
250 return (RTNATIVETHREAD)pthread_self();
251}
252
253
254RTDECL(int) RTThreadSleep(unsigned cMillies)
255{
256 LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
257 if (!cMillies)
258 {
259 /* pthread_yield() isn't part of SuS, thus this fun. */
260#ifdef RT_OS_DARWIN
261 pthread_yield_np();
262#elif defined(RT_OS_FREEBSD) /* void pthread_yield */
263 pthread_yield();
264#elif defined(RT_OS_SOLARIS)
265 sched_yield();
266#else
267 if (!pthread_yield())
268#endif
269 {
270 LogFlow(("RTThreadSleep: returning %Vrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
271 return VINF_SUCCESS;
272 }
273 }
274 else
275 {
276 struct timespec ts;
277 struct timespec tsrem = {0,0};
278
279 ts.tv_nsec = (cMillies % 1000) * 1000000;
280 ts.tv_sec = cMillies / 1000;
281 if (!nanosleep(&ts, &tsrem))
282 {
283 LogFlow(("RTThreadSleep: returning %Vrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
284 return VINF_SUCCESS;
285 }
286 }
287
288 int rc = RTErrConvertFromErrno(errno);
289 LogFlow(("RTThreadSleep: returning %Vrc (cMillies=%d)\n", rc, cMillies));
290 return rc;
291}
292
293
294RTDECL(bool) RTThreadYield(void)
295{
296 uint64_t u64TS = ASMReadTSC();
297#ifdef RT_OS_DARWIN
298 pthread_yield_np();
299#elif defined(RT_OS_SOLARIS)
300 sched_yield();
301#else
302 pthread_yield();
303#endif
304 u64TS = ASMReadTSC() - u64TS;
305 bool fRc = u64TS > 1500;
306 LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
307 return fRc;
308}
309
310
311RTR3DECL(uint64_t) RTThreadGetAffinity(void)
312{
313 return 1;
314}
315
316
317RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask)
318{
319 if (u64Mask != 1)
320 return VERR_INVALID_PARAMETER;
321 return VINF_SUCCESS;
322}
323
324
325RTDECL(int) RTThreadPoke(RTTHREAD hThread)
326{
327 AssertReturn(hThread != RTThreadSelf(), VERR_INVALID_PARAMETER);
328 PRTTHREADINT pThread = rtThreadGet(hThread);
329 AssertReturn(pThread, VERR_INVALID_HANDLE);
330
331 int rc = pthread_kill((pthread_t)(uintptr_t)pThread->Core.Key, RTTHREAD_POSIX_POKE_SIG);
332
333 rtThreadRelease(pThread);
334 return RTErrConvertFromErrno(rc);
335}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette