xref: /illumos-gate/usr/src/lib/libfakekernel/common/thread.c (revision 45818ee124adeaaf947698996b4f4c722afc6d1f)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
14  */
15 
16 #include <sys/cmn_err.h>
17 #include <sys/thread.h>
18 #include <sys/zone.h>
19 
20 #define	_SYNCH_H	/* keep out <synch.h> */
21 #include <thread.h>
22 
23 /*
24  * Get the current kthread_t pointer.
25  */
26 kthread_t *
27 _curthread(void)
28 {
29 	thread_t tid;
30 
31 	tid = thr_self();
32 	return ((kthread_t *)(uintptr_t)tid);
33 }
34 
35 /*
36  * Create a thread.
37  *
38  * thread_create() blocks for memory if necessary.  It never fails.
39  */
40 /* ARGSUSED */
41 kthread_t *
42 thread_create(
43 	caddr_t	stk,
44 	size_t	stksize,
45 	void	(*func)(),
46 	void	*arg,
47 	size_t	len,
48 	struct proc *pp,
49 	int	state,
50 	pri_t	pri)
51 {
52 	void * (*thr_func)(void *);
53 	thread_t newtid;
54 	int thr_flags = 0;
55 	int rc;
56 
57 	thr_flags = THR_BOUND;
58 
59 	switch (state) {
60 	case TS_RUN:
61 	case TS_ONPROC:
62 		break;
63 	case TS_STOPPED:
64 		thr_flags |= THR_SUSPENDED;
65 		break;
66 	default:
67 		cmn_err(CE_PANIC, "thread_create: invalid state");
68 		break;
69 	}
70 
71 	thr_func = (void *(*)(void *))func;
72 	rc = thr_create(NULL, 0, thr_func, arg, thr_flags, &newtid);
73 	if (rc != 0)
74 		cmn_err(CE_PANIC, "thread_create failed, rc=%d", rc);
75 
76 	return ((void *)(uintptr_t)newtid);
77 }
78 
79 void
80 thread_exit(void)
81 {
82 	thr_exit(NULL);
83 }
84 
85 void
86 thread_join(kt_did_t id)
87 {
88 	thread_t thr_id;
89 
90 	thr_id = (thread_t)id;
91 	(void) thr_join(thr_id, NULL, NULL);
92 }
93 
94 void
95 tsignal(kthread_t *kt, int sig)
96 {
97 	thread_t tid = (thread_t)(uintptr_t)kt;
98 
99 	(void) thr_kill(tid, sig);
100 }
101 
102 
103 /*ARGSUSED*/
104 kthread_t *
105 zthread_create(
106     caddr_t stk,
107     size_t stksize,
108     void (*func)(),
109     void *arg,
110     size_t len,
111     pri_t pri)
112 {
113 	kthread_t *t;
114 
115 	t = thread_create(stk, stksize, func, arg, len, NULL, TS_RUN, pri);
116 
117 	return (t);
118 }
119 
120 void
121 zthread_exit(void)
122 {
123 	thread_exit();
124 	/* NOTREACHED */
125 }
126