]> git.proxmox.com Git - mirror_spl.git/blob - module/spl/spl-thread.c
9ad04416106e3129e1d22c048c15865c23cbb8b2
[mirror_spl.git] / module / spl / spl-thread.c
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
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
10 *
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
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
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * Solaris Porting Layer (SPL) Thread Implementation.
25 */
26
27 #include <sys/thread.h>
28 #include <sys/kmem.h>
29 #include <sys/tsd.h>
30
31 /*
32 * Thread interfaces
33 */
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;
44
45 static int
46 thread_generic_wrapper(void *arg)
47 {
48 thread_priv_t *tp = (thread_priv_t *)arg;
49 void (*func)(void *);
50 void *args;
51
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));
59
60 if (func)
61 func(args);
62
63 return (0);
64 }
65
66 void
67 __thread_exit(void)
68 {
69 tsd_exit();
70 complete_and_exit(NULL, 0);
71 /* Unreachable */
72 }
73 EXPORT_SYMBOL(__thread_exit);
74
75 /*
76 * thread_create() may block forever if it cannot create a thread or
77 * allocate memory. This is preferable to returning a NULL which Solaris
78 * style callers likely never check for... since it can't fail.
79 */
80 kthread_t *
81 __thread_create(caddr_t stk, size_t stksize, thread_func_t func,
82 const char *name, void *args, size_t len, proc_t *pp,
83 int state, pri_t pri)
84 {
85 thread_priv_t *tp;
86 struct task_struct *tsk;
87 char *p;
88
89 /* Option pp is simply ignored */
90 /* Variable stack size unsupported */
91 ASSERT(stk == NULL);
92
93 tp = kmem_alloc(sizeof (thread_priv_t), KM_PUSHPAGE);
94 if (tp == NULL)
95 return (NULL);
96
97 tp->tp_magic = TP_MAGIC;
98 tp->tp_name_size = strlen(name) + 1;
99
100 tp->tp_name = kmem_alloc(tp->tp_name_size, KM_PUSHPAGE);
101 if (tp->tp_name == NULL) {
102 kmem_free(tp, sizeof (thread_priv_t));
103 return (NULL);
104 }
105
106 strncpy(tp->tp_name, name, tp->tp_name_size);
107
108 /*
109 * Strip trailing "_thread" from passed name which will be the func
110 * name since the exposed API has no parameter for passing a name.
111 */
112 p = strstr(tp->tp_name, "_thread");
113 if (p)
114 p[0] = '\0';
115
116 tp->tp_func = func;
117 tp->tp_args = args;
118 tp->tp_len = len;
119 tp->tp_state = state;
120 tp->tp_pri = pri;
121
122 tsk = spl_kthread_create(thread_generic_wrapper, (void *)tp,
123 "%s", tp->tp_name);
124 if (IS_ERR(tsk))
125 return (NULL);
126
127 wake_up_process(tsk);
128 return ((kthread_t *)tsk);
129 }
130 EXPORT_SYMBOL(__thread_create);
131
132 /*
133 * spl_kthread_create - Wrapper providing pre-3.13 semantics for
134 * kthread_create() in which it is not killable and less likely
135 * to return -ENOMEM.
136 */
137 struct task_struct *
138 spl_kthread_create(int (*func)(void *), void *data, const char namefmt[], ...)
139 {
140 struct task_struct *tsk;
141 va_list args;
142 char name[TASK_COMM_LEN];
143
144 va_start(args, namefmt);
145 vsnprintf(name, sizeof (name), namefmt, args);
146 va_end(args);
147 do {
148 tsk = kthread_create(func, data, "%s", name);
149 if (IS_ERR(tsk)) {
150 if (signal_pending(current)) {
151 clear_thread_flag(TIF_SIGPENDING);
152 continue;
153 }
154 if (PTR_ERR(tsk) == -ENOMEM)
155 continue;
156 return (NULL);
157 } else
158 return (tsk);
159 } while (1);
160 }
161 EXPORT_SYMBOL(spl_kthread_create);