1 | /* $Id: thread2-r0drv-linux.c 86542 2020-10-12 13:35:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 2), Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 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/errcore.h>
|
---|
37 | #include "internal/thread.h"
|
---|
38 |
|
---|
39 | #if RTLNX_VER_MIN(4,11,0)
|
---|
40 | #include <uapi/linux/sched/types.h>
|
---|
41 | #endif /* >= KERNEL_VERSION(4, 11, 0) */
|
---|
42 |
|
---|
43 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
44 | {
|
---|
45 | return rtThreadGetByNative((RTNATIVETHREAD)current);
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | DECLHIDDEN(int) rtThreadNativeInit(void)
|
---|
50 | {
|
---|
51 | return VINF_SUCCESS;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
|
---|
56 | {
|
---|
57 | int iSchedClass = SCHED_FIFO;
|
---|
58 | int rc = VINF_SUCCESS;
|
---|
59 | struct sched_param Param = { 0 };
|
---|
60 |
|
---|
61 | RT_NOREF_PV(pThread);
|
---|
62 | #if RTLNX_VER_MIN(5,9,0)
|
---|
63 | RT_NOREF_PV(iSchedClass);
|
---|
64 | RT_NOREF_PV(Param);
|
---|
65 | #endif
|
---|
66 | switch (enmType)
|
---|
67 | {
|
---|
68 | case RTTHREADTYPE_IO:
|
---|
69 | #if RTLNX_VER_MAX(5,9,0)
|
---|
70 | /* Set max. priority to preempt all other threads on this CPU. */
|
---|
71 | Param.sched_priority = MAX_RT_PRIO - 1;
|
---|
72 | #else
|
---|
73 | /* Effectively changes prio to 50 */
|
---|
74 | sched_set_fifo(current);
|
---|
75 | #endif
|
---|
76 | break;
|
---|
77 | case RTTHREADTYPE_TIMER:
|
---|
78 | #if RTLNX_VER_MAX(5,9,0)
|
---|
79 | Param.sched_priority = 1; /* not 0 just in case */
|
---|
80 | #else
|
---|
81 | /* Just one above SCHED_NORMAL class */
|
---|
82 | sched_set_fifo_low(current);
|
---|
83 | #endif
|
---|
84 | break;
|
---|
85 | default:
|
---|
86 | /* pretend success instead of VERR_NOT_SUPPORTED */
|
---|
87 | return rc;
|
---|
88 | }
|
---|
89 | #if RTLNX_VER_MAX(5,9,0)
|
---|
90 | if ((sched_setscheduler(current, iSchedClass, &Param)) != 0) {
|
---|
91 | rc = VERR_GENERAL_FAILURE;
|
---|
92 | }
|
---|
93 | #endif
|
---|
94 | return rc;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
99 | {
|
---|
100 | RT_NOREF_PV(pThread);
|
---|
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 RTLNX_VER_MIN(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 RTLNX_VER_MIN(2,6,4)
|
---|
139 | struct task_struct *NativeThread;
|
---|
140 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
141 |
|
---|
142 | RT_ASSERT_PREEMPTIBLE();
|
---|
143 |
|
---|
144 | NativeThread = kthread_run(rtThreadNativeMain, pThreadInt, "iprt-%s", pThreadInt->szName);
|
---|
145 |
|
---|
146 | if (!IS_ERR(NativeThread))
|
---|
147 | {
|
---|
148 | *pNativeThread = (RTNATIVETHREAD)NativeThread;
|
---|
149 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
150 | return VINF_SUCCESS;
|
---|
151 | }
|
---|
152 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
153 | return VERR_GENERAL_FAILURE;
|
---|
154 | #else
|
---|
155 | return VERR_NOT_IMPLEMENTED;
|
---|
156 | #endif
|
---|
157 | }
|
---|
158 |
|
---|