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