]>
Commit | Line | Data |
---|---|---|
4b393c50 | 1 | /* |
716154c5 BB |
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 | 22 | * with the SPL. If not, see <http://www.gnu.org/licenses/>. |
5461eefe | 23 | * |
716154c5 | 24 | * Solaris Porting Layer (SPL) Thread Implementation. |
4b393c50 | 25 | */ |
715f6251 | 26 | |
f4b37741 | 27 | #include <sys/thread.h> |
115aed0d | 28 | #include <sys/kmem.h> |
9fe45dc1 | 29 | #include <sys/tsd.h> |
e5db3134 | 30 | #include <linux/simd.h> |
937879f1 | 31 | |
f1ca4da6 BB |
32 | /* |
33 | * Thread interfaces | |
34 | */ | |
35 | typedef struct thread_priv_s { | |
36 | unsigned long tp_magic; /* Magic */ | |
5461eefe BB |
37 | int tp_name_size; /* Name size */ |
38 | char *tp_name; /* Name (without _thread suffix) */ | |
f1ca4da6 BB |
39 | void (*tp_func)(void *); /* Registered function */ |
40 | void *tp_args; /* Args to be passed to function */ | |
41 | size_t tp_len; /* Len to be passed to function */ | |
42 | int tp_state; /* State to start thread at */ | |
43 | pri_t tp_pri; /* Priority to start threat at */ | |
f1ca4da6 BB |
44 | } thread_priv_t; |
45 | ||
f1b59d26 | 46 | static int |
f1ca4da6 BB |
47 | thread_generic_wrapper(void *arg) |
48 | { | |
49 | thread_priv_t *tp = (thread_priv_t *)arg; | |
50 | void (*func)(void *); | |
51 | void *args; | |
f1ca4da6 | 52 | |
937879f1 | 53 | ASSERT(tp->tp_magic == TP_MAGIC); |
f1ca4da6 BB |
54 | func = tp->tp_func; |
55 | args = tp->tp_args; | |
f1ca4da6 | 56 | set_current_state(tp->tp_state); |
44e406d7 | 57 | set_user_nice((kthread_t *)current, PRIO_TO_NICE(tp->tp_pri)); |
e5db3134 | 58 | kfpu_initialize(); |
a97df54e | 59 | kmem_free(tp->tp_name, tp->tp_name_size); |
5461eefe | 60 | kmem_free(tp, sizeof (thread_priv_t)); |
f1ca4da6 | 61 | |
f1ca4da6 BB |
62 | if (func) |
63 | func(args); | |
64 | ||
5461eefe | 65 | return (0); |
f1ca4da6 BB |
66 | } |
67 | ||
68 | void | |
69 | __thread_exit(void) | |
70 | { | |
9fe45dc1 | 71 | tsd_exit(); |
3d061e9d | 72 | complete_and_exit(NULL, 0); |
937879f1 | 73 | /* Unreachable */ |
f1ca4da6 | 74 | } |
f1b59d26 | 75 | EXPORT_SYMBOL(__thread_exit); |
f1ca4da6 | 76 | |
5461eefe BB |
77 | /* |
78 | * thread_create() may block forever if it cannot create a thread or | |
f1ca4da6 | 79 | * allocate memory. This is preferable to returning a NULL which Solaris |
5461eefe BB |
80 | * style callers likely never check for... since it can't fail. |
81 | */ | |
f1ca4da6 | 82 | kthread_t * |
51f443a0 | 83 | __thread_create(caddr_t stk, size_t stksize, thread_func_t func, |
3673d032 | 84 | const char *name, void *args, size_t len, proc_t *pp, int state, pri_t pri) |
f1ca4da6 | 85 | { |
115aed0d | 86 | thread_priv_t *tp; |
968eccd1 | 87 | struct task_struct *tsk; |
a97df54e | 88 | char *p; |
f1ca4da6 BB |
89 | |
90 | /* Option pp is simply ignored */ | |
91 | /* Variable stack size unsupported */ | |
937879f1 | 92 | ASSERT(stk == NULL); |
f1ca4da6 | 93 | |
5461eefe | 94 | tp = kmem_alloc(sizeof (thread_priv_t), KM_PUSHPAGE); |
115aed0d | 95 | if (tp == NULL) |
8d9a23e8 | 96 | return (NULL); |
f1ca4da6 | 97 | |
115aed0d | 98 | tp->tp_magic = TP_MAGIC; |
a97df54e BB |
99 | tp->tp_name_size = strlen(name) + 1; |
100 | ||
3050c931 | 101 | tp->tp_name = kmem_alloc(tp->tp_name_size, KM_PUSHPAGE); |
5461eefe BB |
102 | if (tp->tp_name == NULL) { |
103 | kmem_free(tp, sizeof (thread_priv_t)); | |
8d9a23e8 | 104 | return (NULL); |
a97df54e BB |
105 | } |
106 | ||
107 | strncpy(tp->tp_name, name, tp->tp_name_size); | |
108 | ||
5461eefe BB |
109 | /* |
110 | * Strip trailing "_thread" from passed name which will be the func | |
a97df54e BB |
111 | * name since the exposed API has no parameter for passing a name. |
112 | */ | |
113 | p = strstr(tp->tp_name, "_thread"); | |
114 | if (p) | |
115 | p[0] = '\0'; | |
116 | ||
115aed0d BB |
117 | tp->tp_func = func; |
118 | tp->tp_args = args; | |
119 | tp->tp_len = len; | |
120 | tp->tp_state = state; | |
121 | tp->tp_pri = pri; | |
f1ca4da6 | 122 | |
17a527cb | 123 | tsk = spl_kthread_create(thread_generic_wrapper, (void *)tp, |
5461eefe | 124 | "%s", tp->tp_name); |
8d9a23e8 BB |
125 | if (IS_ERR(tsk)) |
126 | return (NULL); | |
968eccd1 BB |
127 | |
128 | wake_up_process(tsk); | |
8d9a23e8 | 129 | return ((kthread_t *)tsk); |
f1ca4da6 | 130 | } |
f1b59d26 | 131 | EXPORT_SYMBOL(__thread_create); |
ed650dee TC |
132 | |
133 | /* | |
134 | * spl_kthread_create - Wrapper providing pre-3.13 semantics for | |
135 | * kthread_create() in which it is not killable and less likely | |
136 | * to return -ENOMEM. | |
137 | */ | |
138 | struct task_struct * | |
139 | spl_kthread_create(int (*func)(void *), void *data, const char namefmt[], ...) | |
140 | { | |
141 | struct task_struct *tsk; | |
142 | va_list args; | |
3ceb71e8 | 143 | char name[TASK_COMM_LEN]; |
ed650dee TC |
144 | |
145 | va_start(args, namefmt); | |
5461eefe | 146 | vsnprintf(name, sizeof (name), namefmt, args); |
3ceb71e8 | 147 | va_end(args); |
ed650dee | 148 | do { |
3ceb71e8 | 149 | tsk = kthread_create(func, data, "%s", name); |
ed650dee TC |
150 | if (IS_ERR(tsk)) { |
151 | if (signal_pending(current)) { | |
152 | clear_thread_flag(TIF_SIGPENDING); | |
153 | continue; | |
154 | } | |
155 | if (PTR_ERR(tsk) == -ENOMEM) | |
156 | continue; | |
157 | return (NULL); | |
158 | } else | |
159 | return (tsk); | |
160 | } while (1); | |
161 | } | |
162 | EXPORT_SYMBOL(spl_kthread_create); |