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