VirtualBox

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

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

thread-r0drv-linux.c: shot at the 2.4 additions build breaks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1/* $Id: thread-r0drv-linux.c 21766 2009-07-22 12:13:22Z vboxsync $ */
2/** @file
3 * IPRT - Threads, Ring-0 Driver, Linux.
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#include "the-linux-kernel.h"
36#include "internal/iprt.h"
37
38#include <iprt/thread.h>
39#include <iprt/asm.h>
40#include <iprt/assert.h>
41#include <iprt/err.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(unsigned 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->uchDummy != 42);
163 pState->uchDummy = 42;
164 preempt_disable();
165
166#else /* !CONFIG_PREEMPT */
167 int32_t c;
168 AssertPtr(pState);
169
170 /* Do our own accounting. */
171 c = ASMAtomicIncS32(&g_acPreemptDisabled[smp_processor_id()]);
172 AssertMsg(c > 0 && c < 32, ("%d\n", c));
173 pState->uchDummy = (unsigned char )c;
174#endif
175}
176RT_EXPORT_SYMBOL(RTThreadPreemptDisable);
177
178
179RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
180{
181#ifdef CONFIG_PREEMPT
182 AssertPtr(pState);
183 Assert(pState->uchDummy == 42);
184 pState->uchDummy = 0;
185 preempt_enable();
186
187#else
188 int32_t volatile *pc;
189 AssertPtr(pState);
190 AssertMsg(pState->uchDummy > 0 && pState->uchDummy < 32, ("%d\n", pState->uchDummy));
191
192 /* Do our own accounting. */
193 pc = &g_acPreemptDisabled[smp_processor_id()];
194 AssertMsg(pState->uchDummy == (uint32_t)*pc, ("uchDummy=%d *pc=%d \n", pState->uchDummy, *pc));
195 ASMAtomicUoWriteS32(pc, pState->uchDummy - 1);
196#endif
197}
198RT_EXPORT_SYMBOL(RTThreadPreemptRestore);
199
200
201RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
202{
203 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
204
205 return in_interrupt() != 0;
206}
207RT_EXPORT_SYMBOL(RTThreadIsInInterrupt);
208
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette