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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
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.
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
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 \*****************************************************************************/
27 #include <sys/thread.h>
30 #include <spl-debug.h>
32 #ifdef SS_DEBUG_SUBSYS
33 #undef SS_DEBUG_SUBSYS
36 #define SS_DEBUG_SUBSYS SS_THREAD
41 typedef struct thread_priv_s {
42 unsigned long tp_magic; /* Magic */
43 int tp_name_size; /* Name size */
44 char *tp_name; /* Name (without _thread suffix) */
45 void (*tp_func)(void *); /* Registered function */
46 void *tp_args; /* Args to be passed to function */
47 size_t tp_len; /* Len to be passed to function */
48 int tp_state; /* State to start thread at */
49 pri_t tp_pri; /* Priority to start threat at */
53 thread_generic_wrapper(void *arg)
55 thread_priv_t *tp = (thread_priv_t *)arg;
59 ASSERT(tp->tp_magic == TP_MAGIC);
62 set_current_state(tp->tp_state);
63 set_user_nice((kthread_t *)get_current(), PRIO_TO_NICE(tp->tp_pri));
64 kmem_free(tp->tp_name, tp->tp_name_size);
65 kmem_free(tp, sizeof(thread_priv_t));
79 complete_and_exit(NULL, 0);
82 EXPORT_SYMBOL(__thread_exit);
84 /* thread_create() may block forever if it cannot create a thread or
85 * allocate memory. This is preferable to returning a NULL which Solaris
86 * style callers likely never check for... since it can't fail. */
88 __thread_create(caddr_t stk, size_t stksize, thread_func_t func,
89 const char *name, void *args, size_t len, proc_t *pp,
93 struct task_struct *tsk;
97 /* Option pp is simply ignored */
98 /* Variable stack size unsupported */
101 tp = kmem_alloc(sizeof(thread_priv_t), KM_SLEEP);
105 tp->tp_magic = TP_MAGIC;
106 tp->tp_name_size = strlen(name) + 1;
108 tp->tp_name = kmem_alloc(tp->tp_name_size, KM_SLEEP);
109 if (tp->tp_name == NULL) {
110 kmem_free(tp, sizeof(thread_priv_t));
114 strncpy(tp->tp_name, name, tp->tp_name_size);
116 /* Strip trailing "_thread" from passed name which will be the func
117 * name since the exposed API has no parameter for passing a name.
119 p = strstr(tp->tp_name, "_thread");
126 tp->tp_state = state;
129 tsk = kthread_create(thread_generic_wrapper, (void *)tp,
132 SERROR("Failed to create thread: %ld\n", PTR_ERR(tsk));
136 wake_up_process(tsk);
137 SRETURN((kthread_t *)tsk);
139 EXPORT_SYMBOL(__thread_create);