VirtualBox

Ticket #5501: thread-r0drv-linux.c

File thread-r0drv-linux.c, 6.4 KB (added by aeichner, 15 years ago)

hrtimers

Line 
1/* $Id: thread-r0drv-linux.c 50921 2009-08-11 09:41:58Z bird $ */
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#include <iprt/thread.h>
38
39#include <iprt/asm.h>
40#include <iprt/assert.h>
41#include <iprt/err.h>
42#include <iprt/mp.h>
43
44/* We use the API of Linux 2.6.28+ (schedule_hrtimeout()) */
45#if !defined(RT_USE_LINUX_HRTIMER) \
46 && LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28) \
47 && 1 /* currently disabled */
48# define RT_USE_LINUX_HRTIMER
49#endif
50
51/*******************************************************************************
52* Global Variables *
53*******************************************************************************/
54#ifndef CONFIG_PREEMPT
55/** Per-cpu preemption counters. */
56static int32_t volatile g_acPreemptDisabled[NR_CPUS];
57#endif
58
59
60RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
61{
62 return (RTNATIVETHREAD)current;
63}
64RT_EXPORT_SYMBOL(RTThreadNativeSelf);
65
66
67RTDECL(int) RTThreadSleep(unsigned cMillies)
68{
69#ifdef RT_USE_LINUX_HRTIMER
70 ktime_t Kt = cMillies == RT_INDEFINITE_WAIT ? ktime_set(KTIME_SEC_MAX, 0) : ktime_add_ns(ktime_get(), cMillies * UINT64_C(1000000));
71 int rc = schedule_hrtimeout(&Kt, cMillies == RT_INDEFINITE_WAIT ? HRTIMER_MODE_REL : HRTIMER_MODE_ABS);
72 if (!rc)
73 return VINF_SUCCESS;
74 return VERR_INTERRUPTED;
75#else
76 long cJiffies = msecs_to_jiffies(cMillies);
77 set_current_state(TASK_INTERRUPTIBLE);
78 cJiffies = schedule_timeout(cJiffies);
79 if (!cJiffies)
80 return VINF_SUCCESS;
81 return VERR_INTERRUPTED;
82#endif
83}
84RT_EXPORT_SYMBOL(RTThreadSleep);
85
86
87RTDECL(bool) RTThreadYield(void)
88{
89#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 20)
90 yield();
91#else
92 set_current_state(TASK_RUNNING);
93 sys_sched_yield();
94 schedule();
95#endif
96 return true;
97}
98RT_EXPORT_SYMBOL(RTThreadYield);
99
100
101RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
102{
103#ifdef CONFIG_PREEMPT
104 Assert(hThread == NIL_RTTHREAD);
105# ifdef preemptible
106 return preemptible();
107# else
108 return preempt_count() == 0 && !in_atomic() && !irqs_disabled();
109# endif
110#else
111 int32_t c;
112
113 Assert(hThread == NIL_RTTHREAD);
114 c = g_acPreemptDisabled[smp_processor_id()];
115 AssertMsg(c >= 0 && c < 32, ("%d\n", c));
116 if (c != 0)
117 return false;
118# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 32)
119 if (in_atomic())
120 return false;
121# endif
122# if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 28)
123 if (irqs_disabled())
124 return false;
125# else
126 if (!ASMIntAreEnabled())
127 return false;
128# endif
129 return true;
130#endif
131}
132RT_EXPORT_SYMBOL(RTThreadPreemptIsEnabled);
133
134
135RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
136{
137 Assert(hThread == NIL_RTTHREAD);
138#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 4)
139 return !!test_tsk_thread_flag(current, TIF_NEED_RESCHED);
140
141#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 20)
142 return !!need_resched();
143
144#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 1, 110)
145 return current->need_resched != 0;
146
147#else
148 return need_resched != 0;
149#endif
150}
151RT_EXPORT_SYMBOL(RTThreadPreemptIsPending);
152
153
154RTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
155{
156 /* yes, RTThreadPreemptIsPending is reliable. */
157 return true;
158}
159RT_EXPORT_SYMBOL(RTThreadPreemptIsPendingTrusty);
160
161
162RTDECL(bool) RTThreadPreemptIsPossible(void)
163{
164#ifdef CONFIG_PREEMPT
165 return true; /* yes, kernel preemption is possible. */
166#else
167 return false; /* no kernel preemption */
168#endif
169}
170RT_EXPORT_SYMBOL(RTThreadPreemptIsPossible);
171
172
173RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
174{
175#ifdef CONFIG_PREEMPT
176 AssertPtr(pState);
177 Assert(pState->u32Reserved == 0);
178 pState->u32Reserved = 42;
179 preempt_disable();
180 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
181
182#else /* !CONFIG_PREEMPT */
183 int32_t c;
184 AssertPtr(pState);
185 Assert(pState->u32Reserved == 0);
186
187 /* Do our own accounting. */
188 c = ASMAtomicIncS32(&g_acPreemptDisabled[smp_processor_id()]);
189 AssertMsg(c > 0 && c < 32, ("%d\n", c));
190 pState->u32Reserved = c;
191 RT_ASSERT_PREEMPT_CPUID_DISABLE(pState);
192#endif
193}
194RT_EXPORT_SYMBOL(RTThreadPreemptDisable);
195
196
197RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
198{
199#ifdef CONFIG_PREEMPT
200 AssertPtr(pState);
201 Assert(pState->u32Reserved == 42);
202 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
203 preempt_enable();
204
205#else
206 int32_t volatile *pc;
207 AssertPtr(pState);
208 AssertMsg(pState->u32Reserved > 0 && pState->u32Reserved < 32, ("%d\n", pState->u32Reserved));
209 RT_ASSERT_PREEMPT_CPUID_RESTORE(pState);
210
211 /* Do our own accounting. */
212 pc = &g_acPreemptDisabled[smp_processor_id()];
213 AssertMsg(pState->u32Reserved == (uint32_t)*pc, ("u32Reserved=%d *pc=%d \n", pState->u32Reserved, *pc));
214 ASMAtomicUoWriteS32(pc, pState->u32Reserved - 1);
215#endif
216 pState->u32Reserved = 0;
217}
218RT_EXPORT_SYMBOL(RTThreadPreemptRestore);
219
220
221RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread)
222{
223 Assert(hThread == NIL_RTTHREAD); NOREF(hThread);
224
225 return in_interrupt() != 0;
226}
227RT_EXPORT_SYMBOL(RTThreadIsInInterrupt);
228

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy