VirtualBox

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

Last change on this file since 44535 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.0 KB
Line 
1/* $Id: thread-r0drv-linux.c 44528 2013-02-04 14:27:54Z vboxsync $ */
2/** @file
3 * IPRT - Threads, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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)
37# include <iprt/asm-amd64-x86.h>
38#endif
39#include <iprt/assert.h>
40#include <iprt/err.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 long cJiffies = msecs_to_jiffies(cMillies);
63 set_current_state(TASK_INTERRUPTIBLE);
64 cJiffies = schedule_timeout(cJiffies);
65 if (!cJiffies)
66 return VINF_SUCCESS;
67 return VERR_INTERRUPTED;
68}
69
70
71RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
72{
73 return rtR0ThreadLnxSleepCommon(cMillies);
74}
75RT_EXPORT_SYMBOL(RTThreadSleep);
76
77
78RTDECL(int) RTThreadSleepNoLog(RTMSINTERVAL cMillies)
79{
80 return rtR0ThreadLnxSleepCommon(cMillies);
81}
82RT_EXPORT_SYMBOL(RTThreadSleepNoLog);
83
84
85RTDECL(bool) RTThreadYield(void)
86{
87#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 20)
88 yield();
89#else
90 set_current_state(TASK_RUNNING);
91 sys_sched_yield();
92 schedule();
93#endif
94 return true;
95}
96RT_EXPORT_SYMBOL(RTThreadYield);
97
98
99RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
100{
101#ifdef CONFIG_PREEMPT
102 Assert(hThread == NIL_RTTHREAD);
103# ifdef preemptible
104 return preemptible();
105# else
106 return preempt_count() == 0 && !in_atomic() && !irqs_disabled();
107# endif
108#else
109 int32_t c;
110
111 Assert(hThread == NIL_RTTHREAD);
112 c = g_acPreemptDisabled[smp_processor_id()];
113 AssertMsg(c >= 0 && c < 32, ("%d\n", c));
114 if (c != 0)
115 return false;
116# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 32)
117 if (in_atomic())
118 return false;
119# endif
120# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 28)
121 if (irqs_disabled())
122 return false;
123# else
124 if (!ASMIntAreEnabled())
125 return false;
126# endif
127 return true;
128#endif
129}
130RT_EXPORT_SYMBOL(RTThreadPreemptIsEnabled);
131
132
133RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
134{
135 Assert(hThread == NIL_RTTHREAD);
136#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 4)
137 return !!test_tsk_thread_flag(current, TIF_NEED_RESCHED);
138
139#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 20)
140 return !!need_resched();
141
142#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 1, 110)
143 return current->need_resched != 0;
144
145#else
146 return need_resched != 0;
147#endif
148}
149RT_EXPORT_SYMBOL(RTThreadPreemptIsPending);
150
151
152RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
153{
154 /* yes, RTThreadPreemptIsPending is reliable. */
155 return true;
156}
157RT_EXPORT_SYMBOL(RTThreadPreemptIsPendingTrusty);
158
159
160RTDECL(bool) RTThreadPreemptIsPossible(void)
161{
162#ifdef CONFIG_PREEMPT
163 return true; /* yes, kernel preemption is possible. */
164#else
165 return false; /* no kernel preemption */
166#endif
167}
168RT_EXPORT_SYMBOL(RTThreadPreemptIsPossible);
169
170
171RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
172{
173#ifdef CONFIG_PREEMPT
174 AssertPtr(pState);
175 Assert(pState->u32Reserved == 0);
176 pState->u32Reserved = 42;
177 preempt_disable();
178 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
179
180#else /* !CONFIG_PREEMPT */
181 int32_t c;
182 AssertPtr(pState);
183 Assert(pState->u32Reserved == 0);
184
185 /* Do our own accounting. */
186 c = ASMAtomicIncS32(&g_acPreemptDisabled[smp_processor_id()]);
187 AssertMsg(c > 0 && c < 32, ("%d\n", c));
188 pState->u32Reserved = c;
189 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
190#endif
191}
192RT_EXPORT_SYMBOL(RTThreadPreemptDisable);
193
194
195RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
196{
197#ifdef CONFIG_PREEMPT
198 AssertPtr(pState);
199 Assert(pState->u32Reserved == 42);
200 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
201 preempt_enable();
202
203#else
204 int32_t volatile *pc;
205 AssertPtr(pState);
206 AssertMsg(pState->u32Reserved > 0 && pState->u32Reserved < 32, ("%d\n", pState->u32Reserved));
207 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
208
209 /* Do our own accounting. */
210 pc = &g_acPreemptDisabled[smp_processor_id()];
211 AssertMsg(pState->u32Reserved == (uint32_t)*pc, ("u32Reserved=%d *pc=%d \n", pState->u32Reserved, *pc));
212 ASMAtomicUoWriteS32(pc, pState->u32Reserved - 1);
213#endif
214 pState->u32Reserved = 0;
215}
216RT_EXPORT_SYMBOL(RTThreadPreemptRestore);
217
218
219RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
220{
221 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
222
223 return in_interrupt() != 0;
224}
225RT_EXPORT_SYMBOL(RTThreadIsInInterrupt);
226
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