diff options
Diffstat (limited to 'include/os/linux/thread.c')
| -rw-r--r-- | include/os/linux/thread.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/include/os/linux/thread.c b/include/os/linux/thread.c new file mode 100644 index 0000000..c56bff6 --- /dev/null +++ b/include/os/linux/thread.c | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved. | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify it | ||
| 5 | * under the terms and conditions of the GNU General Public License, | ||
| 6 | * version 2, as published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT | ||
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 11 | * more details. | ||
| 12 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include <linux/kthread.h> | ||
| 18 | |||
| 19 | #include <nvgpu/thread.h> | ||
| 20 | #include <nvgpu/timers.h> | ||
| 21 | |||
| 22 | int nvgpu_thread_proxy(void *threaddata) | ||
| 23 | { | ||
| 24 | struct nvgpu_thread *thread = threaddata; | ||
| 25 | int ret = thread->fn(thread->data); | ||
| 26 | |||
| 27 | thread->running = false; | ||
| 28 | return ret; | ||
| 29 | } | ||
| 30 | |||
| 31 | int nvgpu_thread_create(struct nvgpu_thread *thread, | ||
| 32 | void *data, | ||
| 33 | int (*threadfn)(void *data), const char *name) | ||
| 34 | { | ||
| 35 | struct task_struct *task = kthread_create(nvgpu_thread_proxy, | ||
| 36 | thread, name); | ||
| 37 | if (IS_ERR(task)) | ||
| 38 | return PTR_ERR(task); | ||
| 39 | |||
| 40 | thread->task = task; | ||
| 41 | thread->fn = threadfn; | ||
| 42 | thread->data = data; | ||
| 43 | thread->running = true; | ||
| 44 | wake_up_process(task); | ||
| 45 | return 0; | ||
| 46 | }; | ||
| 47 | |||
| 48 | void nvgpu_thread_stop(struct nvgpu_thread *thread) | ||
| 49 | { | ||
| 50 | if (thread->task) { | ||
| 51 | kthread_stop(thread->task); | ||
| 52 | thread->task = NULL; | ||
| 53 | } | ||
| 54 | }; | ||
| 55 | |||
| 56 | bool nvgpu_thread_should_stop(struct nvgpu_thread *thread) | ||
| 57 | { | ||
| 58 | return kthread_should_stop(); | ||
| 59 | }; | ||
| 60 | |||
| 61 | bool nvgpu_thread_is_running(struct nvgpu_thread *thread) | ||
| 62 | { | ||
| 63 | return ACCESS_ONCE(thread->running); | ||
| 64 | }; | ||
| 65 | |||
| 66 | void nvgpu_thread_join(struct nvgpu_thread *thread) | ||
| 67 | { | ||
| 68 | while (ACCESS_ONCE(thread->running)) | ||
| 69 | nvgpu_msleep(10); | ||
| 70 | }; | ||
