summaryrefslogtreecommitdiff
path: root/usr/src/lib/libfakekernel/common/thread.c
blob: 31421a723b32b3146cb6a8dbca084c3799633e2f (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * 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.
 * Copyright 2017 RackTop Systems.
 */

#include <sys/cmn_err.h>
#include <sys/thread.h>
#include <sys/zone.h>
#include <sys/proc.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 *))(uintptr_t)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 */
}

void
tsd_create(uint_t *keyp, void (*destructor)(void *))
{
	VERIFY0(thr_keycreate(keyp, destructor));
}

/*ARGSUSED*/
void
tsd_destroy(uint_t *keyp)
{}

void *
tsd_get(uint_t key)
{
	void *value;

	return (thr_getspecific(key, &value) ? NULL : value);
}

int
tsd_set(uint_t key, void *value)
{
	return (thr_setspecific(key, value));
}