]> git.proxmox.com Git - mirror_zfs.git/blame - module/spl/spl-thread.c
Support post-3.13 kthread_create() semantics.
[mirror_zfs.git] / module / spl / spl-thread.c
CommitLineData
716154c5
BB
1/*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
715f6251
BB
6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
715f6251 10 *
716154c5
BB
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251
BB
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Thread Implementation.
25\*****************************************************************************/
715f6251 26
f4b37741 27#include <sys/thread.h>
115aed0d 28#include <sys/kmem.h>
9fe45dc1 29#include <sys/tsd.h>
55abb092 30#include <spl-debug.h>
f1ca4da6 31
b17edc10
BB
32#ifdef SS_DEBUG_SUBSYS
33#undef SS_DEBUG_SUBSYS
937879f1
BB
34#endif
35
b17edc10 36#define SS_DEBUG_SUBSYS SS_THREAD
937879f1 37
f1ca4da6
BB
38/*
39 * Thread interfaces
40 */
41typedef struct thread_priv_s {
42 unsigned long tp_magic; /* Magic */
a97df54e
BB
43 int tp_name_size; /* Name size */
44 char *tp_name; /* Name (without _thread suffix) */
f1ca4da6
BB
45 void (*tp_func)(void *); /* Registered function */
46 void *tp_args; /* Args to be passed to function */
47 size_t tp_len; /* Len to be passed to function */
48 int tp_state; /* State to start thread at */
49 pri_t tp_pri; /* Priority to start threat at */
f1ca4da6
BB
50} thread_priv_t;
51
f1b59d26 52static int
f1ca4da6
BB
53thread_generic_wrapper(void *arg)
54{
55 thread_priv_t *tp = (thread_priv_t *)arg;
56 void (*func)(void *);
57 void *args;
f1ca4da6 58
937879f1 59 ASSERT(tp->tp_magic == TP_MAGIC);
f1ca4da6
BB
60 func = tp->tp_func;
61 args = tp->tp_args;
f1ca4da6 62 set_current_state(tp->tp_state);
44e406d7 63 set_user_nice((kthread_t *)current, PRIO_TO_NICE(tp->tp_pri));
a97df54e
BB
64 kmem_free(tp->tp_name, tp->tp_name_size);
65 kmem_free(tp, sizeof(thread_priv_t));
f1ca4da6 66
f1ca4da6
BB
67 if (func)
68 func(args);
69
70 return 0;
71}
72
73void
74__thread_exit(void)
75{
b17edc10
BB
76 SENTRY;
77 SEXIT;
9fe45dc1 78 tsd_exit();
3d061e9d 79 complete_and_exit(NULL, 0);
937879f1 80 /* Unreachable */
f1ca4da6 81}
f1b59d26 82EXPORT_SYMBOL(__thread_exit);
f1ca4da6
BB
83
84/* thread_create() may block forever if it cannot create a thread or
85 * allocate memory. This is preferable to returning a NULL which Solaris
86 * style callers likely never check for... since it can't fail. */
87kthread_t *
51f443a0 88__thread_create(caddr_t stk, size_t stksize, thread_func_t func,
ae4c36ad 89 const char *name, void *args, size_t len, proc_t *pp,
968eccd1 90 int state, pri_t pri)
f1ca4da6 91{
115aed0d 92 thread_priv_t *tp;
968eccd1 93 struct task_struct *tsk;
a97df54e 94 char *p;
b17edc10 95 SENTRY;
f1ca4da6
BB
96
97 /* Option pp is simply ignored */
98 /* Variable stack size unsupported */
937879f1 99 ASSERT(stk == NULL);
f1ca4da6 100
3050c931 101 tp = kmem_alloc(sizeof(thread_priv_t), KM_PUSHPAGE);
115aed0d 102 if (tp == NULL)
b17edc10 103 SRETURN(NULL);
f1ca4da6 104
115aed0d 105 tp->tp_magic = TP_MAGIC;
a97df54e
BB
106 tp->tp_name_size = strlen(name) + 1;
107
3050c931 108 tp->tp_name = kmem_alloc(tp->tp_name_size, KM_PUSHPAGE);
a97df54e
BB
109 if (tp->tp_name == NULL) {
110 kmem_free(tp, sizeof(thread_priv_t));
b17edc10 111 SRETURN(NULL);
a97df54e
BB
112 }
113
114 strncpy(tp->tp_name, name, tp->tp_name_size);
115
116 /* Strip trailing "_thread" from passed name which will be the func
117 * name since the exposed API has no parameter for passing a name.
118 */
119 p = strstr(tp->tp_name, "_thread");
120 if (p)
121 p[0] = '\0';
122
115aed0d
BB
123 tp->tp_func = func;
124 tp->tp_args = args;
125 tp->tp_len = len;
126 tp->tp_state = state;
127 tp->tp_pri = pri;
f1ca4da6 128
17a527cb 129 tsk = spl_kthread_create(thread_generic_wrapper, (void *)tp,
d0bd694c 130 "%s", tp->tp_name);
968eccd1 131 if (IS_ERR(tsk)) {
b17edc10
BB
132 SERROR("Failed to create thread: %ld\n", PTR_ERR(tsk));
133 SRETURN(NULL);
968eccd1
BB
134 }
135
136 wake_up_process(tsk);
b17edc10 137 SRETURN((kthread_t *)tsk);
f1ca4da6 138}
f1b59d26 139EXPORT_SYMBOL(__thread_create);