]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the SPL: Solaris Porting Layer. | |
3 | * | |
4 | * Copyright (c) 2008 Lawrence Livermore National Security, LLC. | |
5 | * Produced at Lawrence Livermore National Laboratory | |
6 | * Written by: | |
7 | * Brian Behlendorf <behlendorf1@llnl.gov>, | |
8 | * Herb Wartens <wartens2@llnl.gov>, | |
9 | * Jim Garlick <garlick@llnl.gov> | |
10 | * UCRL-CODE-235197 | |
11 | * | |
12 | * This is free software; you can redistribute it and/or modify it | |
13 | * under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
16 | * | |
17 | * This is distributed in the hope that it will be useful, but WITHOUT | |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License along | |
23 | * with this program; if not, write to the Free Software Foundation, Inc., | |
24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
25 | */ | |
26 | ||
27 | #include <sys/thread.h> | |
28 | #include <sys/kmem.h> | |
29 | ||
30 | #ifdef DEBUG_SUBSYSTEM | |
31 | #undef DEBUG_SUBSYSTEM | |
32 | #endif | |
33 | ||
34 | #define DEBUG_SUBSYSTEM S_THREAD | |
35 | ||
36 | /* | |
37 | * Thread interfaces | |
38 | */ | |
39 | typedef struct thread_priv_s { | |
40 | unsigned long tp_magic; /* Magic */ | |
41 | int tp_name_size; /* Name size */ | |
42 | char *tp_name; /* Name (without _thread suffix) */ | |
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 */ | |
48 | } thread_priv_t; | |
49 | ||
50 | static int | |
51 | thread_generic_wrapper(void *arg) | |
52 | { | |
53 | thread_priv_t *tp = (thread_priv_t *)arg; | |
54 | void (*func)(void *); | |
55 | void *args; | |
56 | ||
57 | ASSERT(tp->tp_magic == TP_MAGIC); | |
58 | func = tp->tp_func; | |
59 | args = tp->tp_args; | |
60 | set_current_state(tp->tp_state); | |
61 | set_user_nice((kthread_t *)get_current(), PRIO_TO_NICE(tp->tp_pri)); | |
62 | kmem_free(tp->tp_name, tp->tp_name_size); | |
63 | kmem_free(tp, sizeof(thread_priv_t)); | |
64 | ||
65 | if (func) | |
66 | func(args); | |
67 | ||
68 | return 0; | |
69 | } | |
70 | ||
71 | void | |
72 | __thread_exit(void) | |
73 | { | |
74 | ENTRY; | |
75 | EXIT; | |
76 | do_exit(0); | |
77 | /* Unreachable */ | |
78 | } | |
79 | EXPORT_SYMBOL(__thread_exit); | |
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. */ | |
84 | kthread_t * | |
85 | __thread_create(caddr_t stk, size_t stksize, thread_func_t func, | |
86 | const char *name, void *args, size_t len, int *pp, | |
87 | int state, pri_t pri) | |
88 | { | |
89 | thread_priv_t *tp; | |
90 | DEFINE_WAIT(wait); | |
91 | struct task_struct *tsk; | |
92 | char *p; | |
93 | ENTRY; | |
94 | ||
95 | /* Option pp is simply ignored */ | |
96 | /* Variable stack size unsupported */ | |
97 | ASSERT(stk == NULL); | |
98 | ||
99 | tp = kmem_alloc(sizeof(thread_priv_t), KM_SLEEP); | |
100 | if (tp == NULL) | |
101 | RETURN(NULL); | |
102 | ||
103 | tp->tp_magic = TP_MAGIC; | |
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)); | |
109 | RETURN(NULL); | |
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 | ||
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; | |
126 | ||
127 | tsk = kthread_create(thread_generic_wrapper, (void *)tp, tp->tp_name); | |
128 | if (IS_ERR(tsk)) { | |
129 | CERROR("Failed to create thread: %ld\n", PTR_ERR(tsk)); | |
130 | RETURN(NULL); | |
131 | } | |
132 | ||
133 | wake_up_process(tsk); | |
134 | RETURN((kthread_t *)tsk); | |
135 | } | |
136 | EXPORT_SYMBOL(__thread_create); |