]>
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. |
715f6251 | 9 | * |
716154c5 BB |
10 | * The SPL is free software; you can redistribute it and/or modify it |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation; either version 2 of the License, or (at your | |
13 | * option) any later version. | |
14 | * | |
15 | * The SPL is distributed in the hope that it will be useful, but WITHOUT | |
715f6251 BB |
16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 | * for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License along | |
716154c5 | 21 | * with the SPL. If not, see <http://www.gnu.org/licenses/>. |
5461eefe | 22 | * |
716154c5 | 23 | * Solaris Porting Layer (SPL) Thread Implementation. |
4b393c50 | 24 | */ |
715f6251 | 25 | |
f4b37741 | 26 | #include <sys/thread.h> |
115aed0d | 27 | #include <sys/kmem.h> |
9fe45dc1 | 28 | #include <sys/tsd.h> |
937879f1 | 29 | |
f1ca4da6 BB |
30 | /* |
31 | * Thread interfaces | |
32 | */ | |
33 | typedef struct thread_priv_s { | |
34 | unsigned long tp_magic; /* Magic */ | |
5461eefe BB |
35 | int tp_name_size; /* Name size */ |
36 | char *tp_name; /* Name (without _thread suffix) */ | |
f1ca4da6 BB |
37 | void (*tp_func)(void *); /* Registered function */ |
38 | void *tp_args; /* Args to be passed to function */ | |
39 | size_t tp_len; /* Len to be passed to function */ | |
40 | int tp_state; /* State to start thread at */ | |
41 | pri_t tp_pri; /* Priority to start threat at */ | |
f1ca4da6 BB |
42 | } thread_priv_t; |
43 | ||
f1b59d26 | 44 | static int |
f1ca4da6 BB |
45 | thread_generic_wrapper(void *arg) |
46 | { | |
47 | thread_priv_t *tp = (thread_priv_t *)arg; | |
48 | void (*func)(void *); | |
49 | void *args; | |
f1ca4da6 | 50 | |
937879f1 | 51 | ASSERT(tp->tp_magic == TP_MAGIC); |
f1ca4da6 BB |
52 | func = tp->tp_func; |
53 | args = tp->tp_args; | |
f1ca4da6 | 54 | set_current_state(tp->tp_state); |
44e406d7 | 55 | set_user_nice((kthread_t *)current, PRIO_TO_NICE(tp->tp_pri)); |
a97df54e | 56 | kmem_free(tp->tp_name, tp->tp_name_size); |
5461eefe | 57 | kmem_free(tp, sizeof (thread_priv_t)); |
f1ca4da6 | 58 | |
f1ca4da6 BB |
59 | if (func) |
60 | func(args); | |
61 | ||
5461eefe | 62 | return (0); |
f1ca4da6 BB |
63 | } |
64 | ||
5461eefe BB |
65 | /* |
66 | * thread_create() may block forever if it cannot create a thread or | |
f1ca4da6 | 67 | * allocate memory. This is preferable to returning a NULL which Solaris |
5461eefe BB |
68 | * style callers likely never check for... since it can't fail. |
69 | */ | |
f1ca4da6 | 70 | kthread_t * |
51f443a0 | 71 | __thread_create(caddr_t stk, size_t stksize, thread_func_t func, |
3673d032 | 72 | const char *name, void *args, size_t len, proc_t *pp, int state, pri_t pri) |
f1ca4da6 | 73 | { |
115aed0d | 74 | thread_priv_t *tp; |
968eccd1 | 75 | struct task_struct *tsk; |
a97df54e | 76 | char *p; |
f1ca4da6 BB |
77 | |
78 | /* Option pp is simply ignored */ | |
79 | /* Variable stack size unsupported */ | |
937879f1 | 80 | ASSERT(stk == NULL); |
f1ca4da6 | 81 | |
5461eefe | 82 | tp = kmem_alloc(sizeof (thread_priv_t), KM_PUSHPAGE); |
115aed0d | 83 | if (tp == NULL) |
8d9a23e8 | 84 | return (NULL); |
f1ca4da6 | 85 | |
115aed0d | 86 | tp->tp_magic = TP_MAGIC; |
a97df54e BB |
87 | tp->tp_name_size = strlen(name) + 1; |
88 | ||
3050c931 | 89 | tp->tp_name = kmem_alloc(tp->tp_name_size, KM_PUSHPAGE); |
5461eefe BB |
90 | if (tp->tp_name == NULL) { |
91 | kmem_free(tp, sizeof (thread_priv_t)); | |
8d9a23e8 | 92 | return (NULL); |
a97df54e BB |
93 | } |
94 | ||
95 | strncpy(tp->tp_name, name, tp->tp_name_size); | |
96 | ||
5461eefe BB |
97 | /* |
98 | * Strip trailing "_thread" from passed name which will be the func | |
a97df54e BB |
99 | * name since the exposed API has no parameter for passing a name. |
100 | */ | |
101 | p = strstr(tp->tp_name, "_thread"); | |
102 | if (p) | |
103 | p[0] = '\0'; | |
104 | ||
115aed0d BB |
105 | tp->tp_func = func; |
106 | tp->tp_args = args; | |
107 | tp->tp_len = len; | |
108 | tp->tp_state = state; | |
109 | tp->tp_pri = pri; | |
f1ca4da6 | 110 | |
17a527cb | 111 | tsk = spl_kthread_create(thread_generic_wrapper, (void *)tp, |
5461eefe | 112 | "%s", tp->tp_name); |
8d9a23e8 BB |
113 | if (IS_ERR(tsk)) |
114 | return (NULL); | |
968eccd1 BB |
115 | |
116 | wake_up_process(tsk); | |
8d9a23e8 | 117 | return ((kthread_t *)tsk); |
f1ca4da6 | 118 | } |
f1b59d26 | 119 | EXPORT_SYMBOL(__thread_create); |
ed650dee TC |
120 | |
121 | /* | |
122 | * spl_kthread_create - Wrapper providing pre-3.13 semantics for | |
123 | * kthread_create() in which it is not killable and less likely | |
124 | * to return -ENOMEM. | |
125 | */ | |
126 | struct task_struct * | |
127 | spl_kthread_create(int (*func)(void *), void *data, const char namefmt[], ...) | |
128 | { | |
129 | struct task_struct *tsk; | |
130 | va_list args; | |
3ceb71e8 | 131 | char name[TASK_COMM_LEN]; |
ed650dee TC |
132 | |
133 | va_start(args, namefmt); | |
5461eefe | 134 | vsnprintf(name, sizeof (name), namefmt, args); |
3ceb71e8 | 135 | va_end(args); |
ed650dee | 136 | do { |
3ceb71e8 | 137 | tsk = kthread_create(func, data, "%s", name); |
ed650dee TC |
138 | if (IS_ERR(tsk)) { |
139 | if (signal_pending(current)) { | |
140 | clear_thread_flag(TIF_SIGPENDING); | |
141 | continue; | |
142 | } | |
143 | if (PTR_ERR(tsk) == -ENOMEM) | |
144 | continue; | |
145 | return (NULL); | |
8062b768 | 146 | } else { |
ed650dee | 147 | return (tsk); |
8062b768 | 148 | } |
ed650dee TC |
149 | } while (1); |
150 | } | |
151 | EXPORT_SYMBOL(spl_kthread_create); | |
414f7249 PD |
152 | |
153 | /* | |
154 | * The "why" argument indicates the allowable side-effects of the call: | |
155 | * | |
156 | * FORREAL: Extract the next pending signal from p_sig into p_cursig; | |
157 | * stop the process if a stop has been requested or if a traced signal | |
158 | * is pending. | |
159 | * | |
160 | * JUSTLOOKING: Don't stop the process, just indicate whether or not | |
161 | * a signal might be pending (FORREAL is needed to tell for sure). | |
162 | */ | |
163 | int | |
164 | issig(int why) | |
165 | { | |
166 | ASSERT(why == FORREAL || why == JUSTLOOKING); | |
167 | ||
168 | if (!signal_pending(current)) | |
169 | return (0); | |
170 | ||
171 | if (why != FORREAL) | |
172 | return (1); | |
173 | ||
174 | struct task_struct *task = current; | |
175 | spl_kernel_siginfo_t __info; | |
176 | sigset_t set; | |
177 | siginitsetinv(&set, 1ULL << (SIGSTOP - 1) | 1ULL << (SIGTSTP - 1)); | |
178 | sigorsets(&set, &task->blocked, &set); | |
179 | ||
180 | spin_lock_irq(&task->sighand->siglock); | |
181 | int ret; | |
a4623710 AZ |
182 | #ifdef HAVE_DEQUEUE_SIGNAL_4ARG |
183 | enum pid_type __type; | |
184 | if ((ret = dequeue_signal(task, &set, &__info, &__type)) != 0) { | |
185 | #else | |
414f7249 | 186 | if ((ret = dequeue_signal(task, &set, &__info)) != 0) { |
a4623710 | 187 | #endif |
414f7249 PD |
188 | #ifdef HAVE_SIGNAL_STOP |
189 | spin_unlock_irq(&task->sighand->siglock); | |
190 | kernel_signal_stop(); | |
191 | #else | |
192 | if (current->jobctl & JOBCTL_STOP_DEQUEUED) | |
193 | spl_set_special_state(TASK_STOPPED); | |
194 | ||
195 | spin_unlock_irq(¤t->sighand->siglock); | |
196 | ||
197 | schedule(); | |
198 | #endif | |
199 | return (0); | |
200 | } | |
201 | ||
202 | spin_unlock_irq(&task->sighand->siglock); | |
203 | ||
204 | return (1); | |
205 | } | |
206 | ||
207 | EXPORT_SYMBOL(issig); |