VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/thread-r0drv-linux.c@ 77120

Last change on this file since 77120 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.7 KB
Line 
1/* $Id: thread-r0drv-linux.c 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Threads, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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#include "the-linux-kernel.h"
32#include "internal/iprt.h"
33#include <iprt/thread.h>
34
35#include <iprt/asm.h>
36#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 28) || defined(CONFIG_X86_SMAP)
37# include <iprt/asm-amd64-x86.h>
38#endif
39#include <iprt/assert.h>
40#include <iprt/errcore.h>
41#include <iprt/mp.h>
42
43
44/*********************************************************************************************************************************
45* Global Variables *
46*********************************************************************************************************************************/
47#ifndef CONFIG_PREEMPT
48/** Per-cpu preemption counters. */
49static int32_t volatile g_acPreemptDisabled[NR_CPUS];
50#endif
51
52
53RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
54{
55 return (RTNATIVETHREAD)current;
56}
57RT_EXPORT_SYMBOL(RTThreadNativeSelf);
58
59
60static int rtR0ThreadLnxSleepCommon(RTMSINTERVAL cMillies)
61{
62 IPRT_LINUX_SAVE_EFL_AC();
63 long cJiffies = msecs_to_jiffies(cMillies);
64 set_current_state(TASK_INTERRUPTIBLE);
65 cJiffies = schedule_timeout(cJiffies);
66 IPRT_LINUX_RESTORE_EFL_AC();
67 if (!cJiffies)
68 return VINF_SUCCESS;
69 return VERR_INTERRUPTED;
70}
71
72
73RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
74{
75 return rtR0ThreadLnxSleepCommon(cMillies);
76}
77RT_EXPORT_SYMBOL(RTThreadSleep);
78
79
80RTDECL(int) RTThreadSleepNoLog(RTMSINTERVAL cMillies)
81{
82 return rtR0ThreadLnxSleepCommon(cMillies);
83}
84RT_EXPORT_SYMBOL(RTThreadSleepNoLog);
85
86
87RTDECL(bool) RTThreadYield(void)
88{
89 IPRT_LINUX_SAVE_EFL_AC();
90#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 20)
91 yield();
92#else
93 /** @todo r=ramshankar: Can we use cond_resched() instead? */
94 set_current_state(TASK_RUNNING);
95 sys_sched_yield();
96 schedule();
97#endif
98 IPRT_LINUX_RESTORE_EFL_AC();
99 return true;
100}
101RT_EXPORT_SYMBOL(RTThreadYield);
102
103
104RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
105{
106#ifdef CONFIG_PREEMPT
107 Assert(hThread == NIL_RTTHREAD); RT_NOREF_PV(hThread);
108# ifdef preemptible
109 return preemptible();
110# else
111 return preempt_count() == 0 && !in_atomic() && !irqs_disabled();
112# endif
113#else
114 int32_t c;
115
116 Assert(hThread == NIL_RTTHREAD);
117 c = g_acPreemptDisabled[smp_processor_id()];
118 AssertMsg(c >= 0 && c < 32, ("%d\n", c));
119 if (c != 0)
120 return false;
121# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 32)
122 if (in_atomic())
123 return false;
124# endif
125# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 28)
126 if (irqs_disabled())
127 return false;
128# else
129 if (!ASMIntAreEnabled())
130 return false;
131# endif
132 return true;
133#endif
134}
135RT_EXPORT_SYMBOL(RTThreadPreemptIsEnabled);
136
137
138RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
139{
140 Assert(hThread == NIL_RTTHREAD); RT_NOREF_PV(hThread);
141#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 4)
142 return !!test_tsk_thread_flag(current, TIF_NEED_RESCHED);
143
144#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 20)
145 return !!need_resched();
146
147#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 1, 110)
148 return current->need_resched != 0;
149
150#else
151 return need_resched != 0;
152#endif
153}
154RT_EXPORT_SYMBOL(RTThreadPreemptIsPending);
155
156
157RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
158{
159 /* yes, RTThreadPreemptIsPending is reliable. */
160 return true;
161}
162RT_EXPORT_SYMBOL(RTThreadPreemptIsPendingTrusty);
163
164
165RTDECL(bool) RTThreadPreemptIsPossible(void)
166{
167#ifdef CONFIG_PREEMPT
168 return true; /* Yes, kernel preemption is possible. */
169#else
170 return false; /* No kernel preemption (or CONFIG_PREEMPT_VOLUNTARY). */
171#endif
172}
173RT_EXPORT_SYMBOL(RTThreadPreemptIsPossible);
174
175
176RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
177{
178#ifdef CONFIG_PREEMPT
179 AssertPtr(pState);
180 Assert(pState->u32Reserved == 0);
181 pState->u32Reserved = 42;
182 /* This ASSUMES that CONFIG_PREEMPT_COUNT is always defined with CONFIG_PREEMPT. */
183 preempt_disable();
184 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
185
186#else /* !CONFIG_PREEMPT */
187 int32_t c;
188 AssertPtr(pState);
189 Assert(pState->u32Reserved == 0);
190
191 /* Do our own accounting. */
192 c = ASMAtomicIncS32(&g_acPreemptDisabled[smp_processor_id()]);
193 AssertMsg(c > 0 && c < 32, ("%d\n", c));
194 pState->u32Reserved = c;
195 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
196#endif
197}
198RT_EXPORT_SYMBOL(RTThreadPreemptDisable);
199
200
201RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
202{
203#ifdef CONFIG_PREEMPT
204 IPRT_LINUX_SAVE_EFL_AC(); /* paranoia */
205 AssertPtr(pState);
206 Assert(pState->u32Reserved == 42);
207 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
208 preempt_enable();
209 IPRT_LINUX_RESTORE_EFL_ONLY_AC(); /* paranoia */
210
211#else
212 int32_t volatile *pc;
213 AssertPtr(pState);
214 AssertMsg(pState->u32Reserved > 0 && pState->u32Reserved < 32, ("%d\n", pState->u32Reserved));
215 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
216
217 /* Do our own accounting. */
218 pc = &g_acPreemptDisabled[smp_processor_id()];
219 AssertMsg(pState->u32Reserved == (uint32_t)*pc, ("u32Reserved=%d *pc=%d \n", pState->u32Reserved, *pc));
220 ASMAtomicUoWriteS32(pc, pState->u32Reserved - 1);
221#endif
222 pState->u32Reserved = 0;
223}
224RT_EXPORT_SYMBOL(RTThreadPreemptRestore);
225
226
227RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
228{
229 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
230
231 return in_interrupt() != 0;
232}
233RT_EXPORT_SYMBOL(RTThreadIsInInterrupt);
234
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