Linux 6.12 compat: META
[zfs.git] / module / os / linux / spl / spl-thread.c
blob7b0ce30c78849d4f7791af7c322de663cdd43d57
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>.
6 * UCRL-CODE-235197
8 * This file is part of the SPL, Solaris Porting Layer.
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.
15 * The SPL is distributed in the hope that it will be useful, but WITHOUT
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.
20 * You should have received a copy of the GNU General Public License along
21 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 * Solaris Porting Layer (SPL) Thread Implementation.
26 #include <sys/thread.h>
27 #include <sys/kmem.h>
28 #include <sys/tsd.h>
29 #include <sys/string.h>
32 * Thread interfaces
34 typedef struct thread_priv_s {
35 unsigned long tp_magic; /* Magic */
36 int tp_name_size; /* Name size */
37 char *tp_name; /* Name (without _thread suffix) */
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 */
43 } thread_priv_t;
45 static int
46 thread_generic_wrapper(void *arg)
48 thread_priv_t *tp = (thread_priv_t *)arg;
49 void (*func)(void *);
50 void *args;
52 ASSERT(tp->tp_magic == TP_MAGIC);
53 func = tp->tp_func;
54 args = tp->tp_args;
55 set_current_state(tp->tp_state);
56 set_user_nice((kthread_t *)current, PRIO_TO_NICE(tp->tp_pri));
57 kmem_free(tp->tp_name, tp->tp_name_size);
58 kmem_free(tp, sizeof (thread_priv_t));
60 if (func)
61 func(args);
63 return (0);
67 * thread_create() may block forever if it cannot create a thread or
68 * allocate memory. This is preferable to returning a NULL which Solaris
69 * style callers likely never check for... since it can't fail.
71 kthread_t *
72 __thread_create(caddr_t stk, size_t stksize, thread_func_t func,
73 const char *name, void *args, size_t len, proc_t *pp, int state, pri_t pri)
75 thread_priv_t *tp;
76 struct task_struct *tsk;
77 char *p;
79 /* Option pp is simply ignored */
80 /* Variable stack size unsupported */
81 ASSERT(stk == NULL);
83 tp = kmem_alloc(sizeof (thread_priv_t), KM_PUSHPAGE);
84 if (tp == NULL)
85 return (NULL);
87 tp->tp_magic = TP_MAGIC;
88 tp->tp_name_size = strlen(name) + 1;
90 tp->tp_name = kmem_alloc(tp->tp_name_size, KM_PUSHPAGE);
91 if (tp->tp_name == NULL) {
92 kmem_free(tp, sizeof (thread_priv_t));
93 return (NULL);
96 strlcpy(tp->tp_name, name, tp->tp_name_size);
99 * Strip trailing "_thread" from passed name which will be the func
100 * name since the exposed API has no parameter for passing a name.
102 p = strstr(tp->tp_name, "_thread");
103 if (p)
104 p[0] = '\0';
106 tp->tp_func = func;
107 tp->tp_args = args;
108 tp->tp_len = len;
109 tp->tp_state = state;
110 tp->tp_pri = pri;
112 tsk = spl_kthread_create(thread_generic_wrapper, (void *)tp,
113 "%s", tp->tp_name);
114 if (IS_ERR(tsk))
115 return (NULL);
117 wake_up_process(tsk);
118 return ((kthread_t *)tsk);
120 EXPORT_SYMBOL(__thread_create);
123 * spl_kthread_create - Wrapper providing pre-3.13 semantics for
124 * kthread_create() in which it is not killable and less likely
125 * to return -ENOMEM.
127 struct task_struct *
128 spl_kthread_create(int (*func)(void *), void *data, const char namefmt[], ...)
130 struct task_struct *tsk;
131 va_list args;
132 char name[TASK_COMM_LEN];
134 va_start(args, namefmt);
135 vsnprintf(name, sizeof (name), namefmt, args);
136 va_end(args);
137 do {
138 tsk = kthread_create(func, data, "%s", name);
139 if (IS_ERR(tsk)) {
140 if (signal_pending(current)) {
141 clear_thread_flag(TIF_SIGPENDING);
142 continue;
144 if (PTR_ERR(tsk) == -ENOMEM)
145 continue;
146 return (NULL);
147 } else {
148 return (tsk);
150 } while (1);
152 EXPORT_SYMBOL(spl_kthread_create);
155 * Extract the next pending signal from p_sig into p_cursig; stop the process
156 * if a stop has been requested or if a traced signal is pending.
159 issig(void)
162 if (!signal_pending(current))
163 return (0);
165 spl_kernel_siginfo_t __info;
166 sigset_t set;
167 siginitsetinv(&set, 1ULL << (SIGSTOP - 1) | 1ULL << (SIGTSTP - 1));
168 sigorsets(&set, &current->blocked, &set);
170 spin_lock_irq(&current->sighand->siglock);
171 #if defined(HAVE_DEQUEUE_SIGNAL_4ARG)
172 enum pid_type __type;
173 if (dequeue_signal(current, &set, &__info, &__type) != 0) {
174 #elif defined(HAVE_DEQUEUE_SIGNAL_3ARG_TYPE)
175 enum pid_type __type;
176 if (dequeue_signal(&set, &__info, &__type) != 0) {
177 #else
178 if (dequeue_signal(current, &set, &__info) != 0) {
179 #endif
180 spin_unlock_irq(&current->sighand->siglock);
181 kernel_signal_stop();
184 * Dequeued SIGSTOP/SIGTSTP.
185 * Check if process has other singal pending.
187 if (signal_pending(current))
188 return (1);
190 return (0);
193 spin_unlock_irq(&current->sighand->siglock);
195 return (1);
198 EXPORT_SYMBOL(issig);