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