VirtualBox

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

Last change on this file since 33358 was 33358, checked in by vboxsync, 14 years ago

Runtime/r0drv/linux: fix for debug builds on 2.4 kernels

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