1 | /* $Id: thread-r0drv-linux.c 19937 2009-05-23 12:43:34Z 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.alldomusa.eu.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 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include "the-linux-kernel.h"
|
---|
35 |
|
---|
36 | #include <iprt/thread.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
42 | {
|
---|
43 | return (RTNATIVETHREAD)current;
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | RTDECL(int) RTThreadSleep(unsigned cMillies)
|
---|
48 | {
|
---|
49 | long cJiffies = msecs_to_jiffies(cMillies);
|
---|
50 | set_current_state(TASK_INTERRUPTIBLE);
|
---|
51 | cJiffies = schedule_timeout(cJiffies);
|
---|
52 | if (!cJiffies)
|
---|
53 | return VINF_SUCCESS;
|
---|
54 | return VERR_INTERRUPTED;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | RTDECL(bool) RTThreadYield(void)
|
---|
59 | {
|
---|
60 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 20)
|
---|
61 | yield();
|
---|
62 | #else
|
---|
63 | set_current_state(TASK_RUNNING);
|
---|
64 | sys_sched_yield();
|
---|
65 | schedule();
|
---|
66 | #endif
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
72 | {
|
---|
73 | Assert(hThread == NIL_RTTHREAD);
|
---|
74 | return !in_atomic() && !irqs_disabled();
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
|
---|
79 | {
|
---|
80 | Assert(hThread == NIL_RTTHREAD);
|
---|
81 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 4)
|
---|
82 | return test_tsk_thread_flag(current, TIF_NEED_RESCHED);
|
---|
83 |
|
---|
84 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 20)
|
---|
85 | return need_resched();
|
---|
86 |
|
---|
87 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 1, 110)
|
---|
88 | return current->need_resched != 0;
|
---|
89 |
|
---|
90 | #else
|
---|
91 | return need_resched != 0;
|
---|
92 | #endif
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
97 | {
|
---|
98 | AssertPtr(pState);
|
---|
99 | Assert(pState->uchDummy != 42);
|
---|
100 | pState->uchDummy = 42;
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * Note: This call is a NOP if CONFIG_PREEMPT is not enabled in the Linux kernel
|
---|
104 | * configuration. In that case, schedule() is only called need_resched() is set
|
---|
105 | * which is tested just before we return to R3 (not when returning from R0 to R0).
|
---|
106 | */
|
---|
107 | preempt_disable();
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
112 | {
|
---|
113 | AssertPtr(pState);
|
---|
114 | Assert(pState->uchDummy == 42);
|
---|
115 | pState->uchDummy = 0;
|
---|
116 |
|
---|
117 | preempt_enable();
|
---|
118 | }
|
---|
119 |
|
---|