summaryrefslogtreecommitdiff
path: root/usr/src/lib/varpd/svp/common/libvarpd_svp_loop.c
blob: 18a79b9dff9a90136574e568bd9091bcface7a57 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * 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 2015 Joyent, Inc.
 */

/*
 * Event loop mechanism for our backend.
 *
 * For more information, see the big theory statement in
 * lib/varpd/svp/common/libvarpd_svp.c.
 */

#include <unistd.h>
#include <thread.h>
#include <port.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <umem.h>

#include <libvarpd_svp.h>

typedef struct svp_event_loop {
	int		sel_port;	/* RO */
	int		sel_nthread;	/* RO */
	thread_t	*sel_threads;	/* RO */
	boolean_t	sel_stop;	/* svp_elock */
	timer_t		sel_hosttimer;
} svp_event_loop_t;

static svp_event_loop_t svp_event;
static mutex_t svp_elock = ERRORCHECKMUTEX;

/* ARGSUSED */
static void *
svp_event_thr(void *arg)
{
	for (;;) {
		int ret;
		port_event_t pe;
		svp_event_t *sep;

		mutex_enter(&svp_elock);
		if (svp_event.sel_stop == B_TRUE) {
			mutex_exit(&svp_elock);
			break;
		}
		mutex_exit(&svp_elock);

		ret = port_get(svp_event.sel_port, &pe, NULL);
		if (ret != 0) {
			switch (errno) {
			case EFAULT:
			case EBADF:
			case EINVAL:
				libvarpd_panic("unexpected port_get errno: %d",
				    errno);
			default:
				break;
			}
		}

		if (pe.portev_user == NULL)
			libvarpd_panic("received event (%p) without "
			    "protev_user set", &pe);
		sep = (svp_event_t *)pe.portev_user;
		sep->se_func(&pe, sep->se_arg);
	}

	return (NULL);
}

int
svp_event_associate(svp_event_t *sep, int fd)
{
	int ret;

	ret = port_associate(svp_event.sel_port, PORT_SOURCE_FD, fd,
	    sep->se_events, sep);
	if (ret != 0) {
		switch (errno) {
		case EBADF:
		case EBADFD:
		case EINVAL:
		case EAGAIN:
			libvarpd_panic("unexpected port_associate error: %d",
			    errno);
		default:
			ret = errno;
			break;
		}
	}

	return (ret);
}

/* ARGSUSED */
int
svp_event_dissociate(svp_event_t *sep, int fd)
{
	int ret;

	ret = port_dissociate(svp_event.sel_port, PORT_SOURCE_FD, fd);
	if (ret != 0) {
		if (errno != ENOENT)
			libvarpd_panic("unexpected port_dissociate error: %d",
			    errno);
		ret = errno;
	}
	return (ret);
}

int
svp_event_inject(svp_event_t *user)
{
	return (port_send(svp_event.sel_port, 0, user));
}

int
svp_event_timer_init(svp_event_t *sep)
{
	port_notify_t pn;
	struct sigevent evp;
	struct itimerspec ts;

	pn.portnfy_port = svp_event.sel_port;
	pn.portnfy_user = sep;
	evp.sigev_notify = SIGEV_PORT;
	evp.sigev_value.sival_ptr = &pn;

	if (timer_create(CLOCK_REALTIME, &evp, &svp_event.sel_hosttimer) != 0)
		return (errno);

	ts.it_value.tv_sec = svp_tickrate;
	ts.it_value.tv_nsec = 0;
	ts.it_interval.tv_sec = svp_tickrate;
	ts.it_interval.tv_nsec = 0;

	if (timer_settime(svp_event.sel_hosttimer, TIMER_RELTIME, &ts,
	    NULL) != 0) {
		int ret = errno;
		(void) timer_delete(svp_event.sel_hosttimer);
		return (ret);
	}

	return (0);
}

int
svp_event_init(void)
{
	long i, ncpus;

	svp_event.sel_port = port_create();
	if (svp_event.sel_port == -1)
		return (errno);

	ncpus = sysconf(_SC_NPROCESSORS_ONLN) * 2 + 1;
	if (ncpus <= 0)
		libvarpd_panic("sysconf for nprocs failed... %d/%d",
		    ncpus, errno);

	svp_event.sel_threads = umem_alloc(sizeof (thread_t) * ncpus,
	    UMEM_DEFAULT);
	if (svp_event.sel_threads == NULL) {
		int ret = errno;
		(void) timer_delete(svp_event.sel_hosttimer);
		(void) close(svp_event.sel_port);
		svp_event.sel_port = -1;
		return (ret);
	}

	for (i = 0; i < ncpus; i++) {
		int ret;
		thread_t *thr = &svp_event.sel_threads[i];

		ret = thr_create(NULL, 0, svp_event_thr, NULL,
		    THR_DETACHED | THR_DAEMON, thr);
		if (ret != 0) {
			ret = errno;
			(void) timer_delete(svp_event.sel_hosttimer);
			(void) close(svp_event.sel_port);
			svp_event.sel_port = -1;
			return (errno);
		}
	}

	return (0);
}

void
svp_event_fini(void)
{
	mutex_enter(&svp_elock);
	svp_event.sel_stop = B_TRUE;
	mutex_exit(&svp_elock);

	(void) timer_delete(svp_event.sel_hosttimer);
	(void) close(svp_event.sel_port);
}