1 | /* $Id: thread2-posix.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads part 2, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
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 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_THREAD
|
---|
32 | #include <errno.h>
|
---|
33 | #include <pthread.h>
|
---|
34 | #include <unistd.h>
|
---|
35 | #if defined(RT_OS_SOLARIS)
|
---|
36 | # include <sched.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #include <iprt/thread.h>
|
---|
40 | #include <iprt/log.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
43 | # include <iprt/asm-amd64-x86.h>
|
---|
44 | #endif
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include "internal/thread.h"
|
---|
47 |
|
---|
48 |
|
---|
49 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
50 | {
|
---|
51 | return (RTNATIVETHREAD)pthread_self();
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
56 | {
|
---|
57 | LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
|
---|
58 | if (!cMillies)
|
---|
59 | {
|
---|
60 | /* pthread_yield() isn't part of SuS, thus this fun. */
|
---|
61 | #ifdef RT_OS_DARWIN
|
---|
62 | pthread_yield_np();
|
---|
63 | #elif defined(RT_OS_FREEBSD) /* void pthread_yield */
|
---|
64 | pthread_yield();
|
---|
65 | #elif defined(RT_OS_SOLARIS) || defined(RT_OS_HAIKU)
|
---|
66 | sched_yield();
|
---|
67 | #else
|
---|
68 | if (!pthread_yield())
|
---|
69 | #endif
|
---|
70 | {
|
---|
71 | LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
|
---|
72 | return VINF_SUCCESS;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | else
|
---|
76 | {
|
---|
77 | struct timespec ts;
|
---|
78 | struct timespec tsrem = {0,0};
|
---|
79 |
|
---|
80 | ts.tv_nsec = (cMillies % 1000) * 1000000;
|
---|
81 | ts.tv_sec = cMillies / 1000;
|
---|
82 | if (!nanosleep(&ts, &tsrem))
|
---|
83 | {
|
---|
84 | LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
|
---|
85 | return VINF_SUCCESS;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | int rc = RTErrConvertFromErrno(errno);
|
---|
90 | LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", rc, cMillies));
|
---|
91 | return rc;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | RTDECL(int) RTThreadSleepNoLog(RTMSINTERVAL cMillies)
|
---|
96 | {
|
---|
97 | if (!cMillies)
|
---|
98 | {
|
---|
99 | /* pthread_yield() isn't part of SuS, thus this fun. */
|
---|
100 | #ifdef RT_OS_DARWIN
|
---|
101 | pthread_yield_np();
|
---|
102 | #elif defined(RT_OS_FREEBSD) /* void pthread_yield */
|
---|
103 | pthread_yield();
|
---|
104 | #elif defined(RT_OS_SOLARIS) || defined(RT_OS_HAIKU)
|
---|
105 | sched_yield();
|
---|
106 | #else
|
---|
107 | if (!pthread_yield())
|
---|
108 | #endif
|
---|
109 | return VINF_SUCCESS;
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | struct timespec ts;
|
---|
114 | struct timespec tsrem = {0,0};
|
---|
115 |
|
---|
116 | ts.tv_nsec = (cMillies % 1000) * 1000000;
|
---|
117 | ts.tv_sec = cMillies / 1000;
|
---|
118 | if (!nanosleep(&ts, &tsrem))
|
---|
119 | return VINF_SUCCESS;
|
---|
120 | }
|
---|
121 |
|
---|
122 | return RTErrConvertFromErrno(errno);
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | RTDECL(bool) RTThreadYield(void)
|
---|
127 | {
|
---|
128 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
129 | uint64_t u64TS = ASMReadTSC();
|
---|
130 | #endif
|
---|
131 | #ifdef RT_OS_DARWIN
|
---|
132 | pthread_yield_np();
|
---|
133 | #elif defined(RT_OS_SOLARIS) || defined(RT_OS_HAIKU)
|
---|
134 | sched_yield();
|
---|
135 | #else
|
---|
136 | pthread_yield();
|
---|
137 | #endif
|
---|
138 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
139 | u64TS = ASMReadTSC() - u64TS;
|
---|
140 | bool fRc = u64TS > 1500;
|
---|
141 | LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
|
---|
142 | #else
|
---|
143 | bool fRc = true; /* PORTME: Add heuristics for determining whether the cpus was yielded. */
|
---|
144 | #endif
|
---|
145 | return fRc;
|
---|
146 | }
|
---|
147 |
|
---|