summaryrefslogtreecommitdiff
path: root/usr/src/lib/libfakekernel/common/thread.c
blob: ba16f3b8d5c418b15f6f4f674a7620a76eaec693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
 */

#include <sys/cmn_err.h>
#include <sys/thread.h>
#include <sys/zone.h>

#define	_SYNCH_H	/* keep out <synch.h> */
#include <thread.h>

/*
 * Get the current kthread_t pointer.
 */
kthread_t *
_curthread(void)
{
	thread_t tid;

	tid = thr_self();
	return ((kthread_t *)(uintptr_t)tid);
}

/*
 * Create a thread.
 *
 * thread_create() blocks for memory if necessary.  It never fails.
 */
/* ARGSUSED */
kthread_t *
thread_create(
	caddr_t	stk,
	size_t	stksize,
	void	(*func)(),
	void	*arg,
	size_t	len,
	struct proc *pp,
	int	state,
	pri_t	pri)
{
	void * (*thr_func)(void *);
	thread_t newtid;
	int thr_flags = 0;
	int rc;

	thr_flags = THR_BOUND;

	switch (state) {
	case TS_RUN:
	case TS_ONPROC:
		break;
	case TS_STOPPED:
		thr_flags |= THR_SUSPENDED;
		break;
	default:
		cmn_err(CE_PANIC, "thread_create: invalid state");
		break;
	}

	thr_func = (void *(*)(void *))func;
	rc = thr_create(NULL, 0, thr_func, arg, thr_flags, &newtid);
	if (rc != 0)
		cmn_err(CE_PANIC, "thread_create failed, rc=%d", rc);

	return ((void *)(uintptr_t)newtid);
}

void
thread_exit(void)
{
	thr_exit(NULL);
}

void
thread_join(kt_did_t id)
{
	thread_t thr_id;

	thr_id = (thread_t)id;
	(void) thr_join(thr_id, NULL, NULL);
}

void
tsignal(kthread_t *kt, int sig)
{
	thread_t tid = (thread_t)(uintptr_t)kt;

	(void) thr_kill(tid, sig);
}


/*ARGSUSED*/
kthread_t *
zthread_create(
    caddr_t stk,
    size_t stksize,
    void (*func)(),
    void *arg,
    size_t len,
    pri_t pri)
{
	kthread_t *t;

	t = thread_create(stk, stksize, func, arg, len, NULL, TS_RUN, pri);

	return (t);
}

void
zthread_exit(void)
{
	thread_exit();
	/* NOTREACHED */
}