1 | /* $Id: thread2-r0drv-linux.c 56290 2015-06-09 14:01:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 2), Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/thread.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include "internal/thread.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
41 | {
|
---|
42 | return rtThreadGetByNative((RTNATIVETHREAD)current);
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | DECLHIDDEN(int) rtThreadNativeInit(void)
|
---|
47 | {
|
---|
48 | return VINF_SUCCESS;
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
|
---|
53 | {
|
---|
54 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
|
---|
55 | /* See comment near MAX_RT_PRIO in linux/sched.h for details on
|
---|
56 | sched_priority. */
|
---|
57 | int iSchedClass = SCHED_NORMAL;
|
---|
58 | struct sched_param Param = { .sched_priority = MAX_PRIO - 1 };
|
---|
59 | switch (enmType)
|
---|
60 | {
|
---|
61 | case RTTHREADTYPE_INFREQUENT_POLLER:
|
---|
62 | Param.sched_priority = MAX_RT_PRIO + 5;
|
---|
63 | break;
|
---|
64 |
|
---|
65 | case RTTHREADTYPE_EMULATION:
|
---|
66 | Param.sched_priority = MAX_RT_PRIO + 4;
|
---|
67 | break;
|
---|
68 |
|
---|
69 | case RTTHREADTYPE_DEFAULT:
|
---|
70 | Param.sched_priority = MAX_RT_PRIO + 3;
|
---|
71 | break;
|
---|
72 |
|
---|
73 | case RTTHREADTYPE_MSG_PUMP:
|
---|
74 | Param.sched_priority = MAX_RT_PRIO + 2;
|
---|
75 | break;
|
---|
76 |
|
---|
77 | case RTTHREADTYPE_IO:
|
---|
78 | iSchedClass = SCHED_FIFO;
|
---|
79 | Param.sched_priority = MAX_RT_PRIO - 1;
|
---|
80 | break;
|
---|
81 |
|
---|
82 | case RTTHREADTYPE_TIMER:
|
---|
83 | iSchedClass = SCHED_FIFO;
|
---|
84 | Param.sched_priority = 1; /* not 0 just in case */
|
---|
85 | break;
|
---|
86 |
|
---|
87 | default:
|
---|
88 | AssertMsgFailed(("enmType=%d\n", enmType));
|
---|
89 | return VERR_INVALID_PARAMETER;
|
---|
90 | }
|
---|
91 |
|
---|
92 | sched_setscheduler(current, iSchedClass, &Param);
|
---|
93 | #endif
|
---|
94 |
|
---|
95 | return VINF_SUCCESS;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
100 | {
|
---|
101 | return VERR_NOT_IMPLEMENTED;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread)
|
---|
106 | {
|
---|
107 | /** @todo fix RTThreadWait/RTR0Term race on linux. */
|
---|
108 | RTThreadSleep(1); NOREF(pThread);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
|
---|
113 | {
|
---|
114 | NOREF(pThread);
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 4)
|
---|
119 | /**
|
---|
120 | * Native kernel thread wrapper function.
|
---|
121 | *
|
---|
122 | * This will forward to rtThreadMain and do termination upon return.
|
---|
123 | *
|
---|
124 | * @param pvArg Pointer to the argument package.
|
---|
125 | */
|
---|
126 | static int rtThreadNativeMain(void *pvArg)
|
---|
127 | {
|
---|
128 | PRTTHREADINT pThread = (PRTTHREADINT)pvArg;
|
---|
129 |
|
---|
130 | rtThreadMain(pThread, (RTNATIVETHREAD)current, &pThread->szName[0]);
|
---|
131 | return 0;
|
---|
132 | }
|
---|
133 | #endif
|
---|
134 |
|
---|
135 |
|
---|
136 | DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread)
|
---|
137 | {
|
---|
138 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 4)
|
---|
139 | struct task_struct *NativeThread;
|
---|
140 |
|
---|
141 | RT_ASSERT_PREEMPTIBLE();
|
---|
142 |
|
---|
143 | NativeThread = kthread_run(rtThreadNativeMain, pThreadInt, "iprt-%s", pThreadInt->szName);
|
---|
144 |
|
---|
145 | if (IS_ERR(NativeThread))
|
---|
146 | return VERR_GENERAL_FAILURE;
|
---|
147 |
|
---|
148 | *pNativeThread = (RTNATIVETHREAD)NativeThread;
|
---|
149 | return VINF_SUCCESS;
|
---|
150 | #else
|
---|
151 | return VERR_NOT_IMPLEMENTED;
|
---|
152 | #endif
|
---|
153 | }
|
---|
154 |
|
---|