VirtualBox

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

Last change on this file since 19167 was 13837, checked in by vboxsync, 16 years ago

s/%Vr\([acfs]\)/%Rr\1/g - since I'm upsetting everyone anyway, better make the most of it...

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.5 KB
Line 
1/* $Id: thread-posix.cpp 13837 2008-11-05 02:54:02Z 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.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 (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 sigprocmask(SIG_BLOCK, &SigSet, NULL);
156 siginterrupt(RTTHREAD_POSIX_POKE_SIG, 1);
157
158 int rc = pthread_setspecific(g_SelfKey, pThread);
159 if (!rc)
160 return VINF_SUCCESS;
161 return VERR_FAILED_TO_SET_SELF_TLS;
162}
163
164
165/**
166 * Wrapper which unpacks the params and calls thread function.
167 */
168static void *rtThreadNativeMain(void *pvArgs)
169{
170 PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
171
172 /*
173 * Block SIGALRM - required for timer-posix.cpp.
174 * This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
175 * It will not help much if someone creates threads directly using pthread_create. :/
176 */
177 sigset_t SigSet;
178 sigemptyset(&SigSet);
179 sigaddset(&SigSet, SIGALRM);
180 sigprocmask(SIG_BLOCK, &SigSet, NULL);
181 siginterrupt(RTTHREAD_POSIX_POKE_SIG, 1);
182
183 int rc = pthread_setspecific(g_SelfKey, pThread);
184 AssertReleaseMsg(!rc, ("failed to set self TLS. rc=%d thread '%s'\n", rc, pThread->szName));
185
186 /*
187 * Call common main.
188 */
189 pthread_t Self = pthread_self();
190 Assert((uintptr_t)Self == (RTNATIVETHREAD)Self && (uintptr_t)Self != NIL_RTNATIVETHREAD);
191 rc = rtThreadMain(pThread, (uintptr_t)Self, &pThread->szName[0]);
192
193 pthread_setspecific(g_SelfKey, NULL);
194 pthread_exit((void *)rc);
195 return (void *)rc;
196}
197
198
199int rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
200{
201 /*
202 * Set the default stack size.
203 */
204 if (!pThread->cbStack)
205 pThread->cbStack = 512*1024;
206
207 /*
208 * Setup thread attributes.
209 */
210 pthread_attr_t ThreadAttr;
211 int rc = pthread_attr_init(&ThreadAttr);
212 if (!rc)
213 {
214 rc = pthread_attr_setdetachstate(&ThreadAttr, PTHREAD_CREATE_DETACHED);
215 if (!rc)
216 {
217 rc = pthread_attr_setstacksize(&ThreadAttr, pThread->cbStack);
218 if (!rc)
219 {
220 /*
221 * Create the thread.
222 */
223 pthread_t ThreadId;
224 rc = pthread_create(&ThreadId, &ThreadAttr, rtThreadNativeMain, pThread);
225 if (!rc)
226 {
227 *pNativeThread = (uintptr_t)ThreadId;
228 return VINF_SUCCESS;
229 }
230 }
231 }
232 pthread_attr_destroy(&ThreadAttr);
233 }
234 return RTErrConvertFromErrno(rc);
235}
236
237
238RTDECL(RTTHREAD) RTThreadSelf(void)
239{
240 PRTTHREADINT pThread = (PRTTHREADINT)pthread_getspecific(g_SelfKey);
241 /** @todo import alien threads? */
242 return pThread;
243}
244
245
246RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
247{
248 return (RTNATIVETHREAD)pthread_self();
249}
250
251
252RTDECL(int) RTThreadSleep(unsigned cMillies)
253{
254 LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
255 if (!cMillies)
256 {
257 /* pthread_yield() isn't part of SuS, thus this fun. */
258#ifdef RT_OS_DARWIN
259 pthread_yield_np();
260#elif defined(RT_OS_FREEBSD) /* void pthread_yield */
261 pthread_yield();
262#elif defined(RT_OS_SOLARIS)
263 sched_yield();
264#else
265 if (!pthread_yield())
266#endif
267 {
268 LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
269 return VINF_SUCCESS;
270 }
271 }
272 else
273 {
274 struct timespec ts;
275 struct timespec tsrem = {0,0};
276
277 ts.tv_nsec = (cMillies % 1000) * 1000000;
278 ts.tv_sec = cMillies / 1000;
279 if (!nanosleep(&ts, &tsrem))
280 {
281 LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
282 return VINF_SUCCESS;
283 }
284 }
285
286 int rc = RTErrConvertFromErrno(errno);
287 LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", rc, cMillies));
288 return rc;
289}
290
291
292RTDECL(bool) RTThreadYield(void)
293{
294 uint64_t u64TS = ASMReadTSC();
295#ifdef RT_OS_DARWIN
296 pthread_yield_np();
297#elif defined(RT_OS_SOLARIS)
298 sched_yield();
299#else
300 pthread_yield();
301#endif
302 u64TS = ASMReadTSC() - u64TS;
303 bool fRc = u64TS > 1500;
304 LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
305 return fRc;
306}
307
308
309RTR3DECL(uint64_t) RTThreadGetAffinity(void)
310{
311 return 1;
312}
313
314
315RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask)
316{
317 if (u64Mask != 1)
318 return VERR_INVALID_PARAMETER;
319 return VINF_SUCCESS;
320}
321
322
323RTDECL(int) RTThreadPoke(RTTHREAD hThread)
324{
325 AssertReturn(hThread != RTThreadSelf(), VERR_INVALID_PARAMETER);
326 PRTTHREADINT pThread = rtThreadGet(hThread);
327 AssertReturn(pThread, VERR_INVALID_HANDLE);
328
329 int rc = pthread_kill((pthread_t)(uintptr_t)pThread->Core.Key, RTTHREAD_POSIX_POKE_SIG);
330
331 rtThreadRelease(pThread);
332 return RTErrConvertFromErrno(rc);
333}
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