VirtualBox

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

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

iprt/thread.h: Redefined RTThreadPreemptIsEnabled for systems without preemption (we keep count ourselves). Added RTThreadPreemptIsPossible and RTThreadIsInInterrupt. Fixed RTThreadPreemptIsEnabled on FreeBSD and Solaris/vbi.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1/* $Id: thread-r0drv-linux.c 21536 2009-07-13 14:49:39Z 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 return c == 0 && !in_atomic() && !irqs_disabled();
102#endif
103}
104RT_EXPORT_SYMBOL(RTThreadPreemptIsEnabled);
105
106
107RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
108{
109 Assert(hThread == NIL_RTTHREAD);
110#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 4)
111 return test_tsk_thread_flag(current, TIF_NEED_RESCHED);
112
113#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 20)
114 return need_resched();
115
116#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 1, 110)
117 return current->need_resched != 0;
118
119#else
120 return need_resched != 0;
121#endif
122}
123RT_EXPORT_SYMBOL(RTThreadPreemptIsPending);
124
125
126RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
127{
128 /* yes, RTThreadPreemptIsPending is reliable. */
129 return true;
130}
131RT_EXPORT_SYMBOL(RTThreadPreemptIsPendingTrusty);
132
133
134RTDECL(bool) RTThreadPreemptIsPossible(void)
135{
136#ifdef CONFIG_PREEMPT
137 return true; /* yes, kernel preemption is possible. */
138#else
139 return false; /* no kernel preemption */
140#endif
141}
142RT_EXPORT_SYMBOL(RTThreadPreemptIsPossible);
143
144
145RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
146{
147#ifdef CONFIG_PREEMPT
148 AssertPtr(pState);
149 Assert(pState->uchDummy != 42);
150 pState->uchDummy = 42;
151 preempt_disable();
152
153#else /* !CONFIG_PREEMPT */
154 int32_t c;
155 AssertPtr(pState);
156
157 /* Do our own accounting. */
158 c = ASMAtomicIncS32(&g_acPreemptDisabled[smp_processor_id()]);
159 AssertMsg(c > 0 && c < 32, ("%d\n", c));
160 pState->uchDummy = (unsigned char )c;
161#endif
162}
163RT_EXPORT_SYMBOL(RTThreadPreemptDisable);
164
165
166RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
167{
168#ifdef CONFIG_PREEMPT
169 AssertPtr(pState);
170 Assert(pState->uchDummy == 42);
171 pState->uchDummy = 0;
172 preempt_enable();
173
174#else
175 int32_t volatile *pc;
176 AssertPtr(pState);
177 AssertMsg(pState->uchDummy > 0 && pState->uchDummy < 32, ("%d\n", pState->uchDummy));
178
179 /* Do our own accounting. */
180 pc = &g_acPreemptDisabled[smp_processor_id()];
181 AssertMsg(pState->uchDummy == (uint32_t)*pc, ("uchDummy=%d *pc=%d \n", pState->uchDummy, *pc));
182 ASMAtomicUoWriteS32(pc, pState->uchDummy - 1);
183#endif
184}
185RT_EXPORT_SYMBOL(RTThreadPreemptRestore);
186
187
188RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
189{
190 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
191
192 return in_interrupt() != 0;
193}
194RT_EXPORT_SYMBOL(RTThreadIsInInterrupt);
195
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