]> git.proxmox.com Git - mirror_spl-debian.git/blame - module/spl/spl-thread.c
Refresh autogen.sh products
[mirror_spl-debian.git] / module / spl / spl-thread.c
CommitLineData
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
BB
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
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
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Thread Implementation.
25\*****************************************************************************/
715f6251 26
f4b37741 27#include <sys/thread.h>
115aed0d 28#include <sys/kmem.h>
55abb092 29#include <spl-debug.h>
f1ca4da6 30
b17edc10
BB
31#ifdef SS_DEBUG_SUBSYS
32#undef SS_DEBUG_SUBSYS
937879f1 33#endif
34
b17edc10 35#define SS_DEBUG_SUBSYS SS_THREAD
937879f1 36
f1ca4da6 37/*
38 * Thread interfaces
39 */
40typedef struct thread_priv_s {
41 unsigned long tp_magic; /* Magic */
a97df54e 42 int tp_name_size; /* Name size */
43 char *tp_name; /* Name (without _thread suffix) */
f1ca4da6 44 void (*tp_func)(void *); /* Registered function */
45 void *tp_args; /* Args to be passed to function */
46 size_t tp_len; /* Len to be passed to function */
47 int tp_state; /* State to start thread at */
48 pri_t tp_pri; /* Priority to start threat at */
f1ca4da6 49} thread_priv_t;
50
f1b59d26 51static int
f1ca4da6 52thread_generic_wrapper(void *arg)
53{
54 thread_priv_t *tp = (thread_priv_t *)arg;
55 void (*func)(void *);
56 void *args;
f1ca4da6 57
937879f1 58 ASSERT(tp->tp_magic == TP_MAGIC);
f1ca4da6 59 func = tp->tp_func;
60 args = tp->tp_args;
f1ca4da6 61 set_current_state(tp->tp_state);
115aed0d 62 set_user_nice((kthread_t *)get_current(), PRIO_TO_NICE(tp->tp_pri));
a97df54e 63 kmem_free(tp->tp_name, tp->tp_name_size);
64 kmem_free(tp, sizeof(thread_priv_t));
f1ca4da6 65
f1ca4da6 66 if (func)
67 func(args);
68
69 return 0;
70}
71
72void
73__thread_exit(void)
74{
b17edc10
BB
75 SENTRY;
76 SEXIT;
3d061e9d 77 complete_and_exit(NULL, 0);
937879f1 78 /* Unreachable */
f1ca4da6 79}
f1b59d26 80EXPORT_SYMBOL(__thread_exit);
f1ca4da6 81
82/* thread_create() may block forever if it cannot create a thread or
83 * allocate memory. This is preferable to returning a NULL which Solaris
84 * style callers likely never check for... since it can't fail. */
85kthread_t *
51f443a0 86__thread_create(caddr_t stk, size_t stksize, thread_func_t func,
ae4c36ad 87 const char *name, void *args, size_t len, proc_t *pp,
968eccd1 88 int state, pri_t pri)
f1ca4da6 89{
115aed0d 90 thread_priv_t *tp;
968eccd1 91 struct task_struct *tsk;
a97df54e 92 char *p;
b17edc10 93 SENTRY;
f1ca4da6 94
95 /* Option pp is simply ignored */
96 /* Variable stack size unsupported */
937879f1 97 ASSERT(stk == NULL);
f1ca4da6 98
115aed0d 99 tp = kmem_alloc(sizeof(thread_priv_t), KM_SLEEP);
100 if (tp == NULL)
b17edc10 101 SRETURN(NULL);
f1ca4da6 102
115aed0d 103 tp->tp_magic = TP_MAGIC;
a97df54e 104 tp->tp_name_size = strlen(name) + 1;
105
106 tp->tp_name = kmem_alloc(tp->tp_name_size, KM_SLEEP);
107 if (tp->tp_name == NULL) {
108 kmem_free(tp, sizeof(thread_priv_t));
b17edc10 109 SRETURN(NULL);
a97df54e 110 }
111
112 strncpy(tp->tp_name, name, tp->tp_name_size);
113
114 /* Strip trailing "_thread" from passed name which will be the func
115 * name since the exposed API has no parameter for passing a name.
116 */
117 p = strstr(tp->tp_name, "_thread");
118 if (p)
119 p[0] = '\0';
120
115aed0d 121 tp->tp_func = func;
122 tp->tp_args = args;
123 tp->tp_len = len;
124 tp->tp_state = state;
125 tp->tp_pri = pri;
f1ca4da6 126
d0bd694c
BB
127 tsk = kthread_create(thread_generic_wrapper, (void *)tp,
128 "%s", tp->tp_name);
968eccd1 129 if (IS_ERR(tsk)) {
b17edc10
BB
130 SERROR("Failed to create thread: %ld\n", PTR_ERR(tsk));
131 SRETURN(NULL);
968eccd1 132 }
133
134 wake_up_process(tsk);
b17edc10 135 SRETURN((kthread_t *)tsk);
f1ca4da6 136}
f1b59d26 137EXPORT_SYMBOL(__thread_create);