summaryrefslogtreecommitdiffstats
path: root/src/ioq.c
blob: e09c2a91596a33dd69e7579a378d045fabbc905c (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD

#include "ioq.h"
#include "dir.h"
#include "list.h"
#include "lock.h"
#include "sanity.h"
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>

/**
 * An I/O queue request.
 */
struct ioq_req {
	/** Base file descriptor for openat(). */
	int dfd;
	/** Relative path to dfd. */
	const char *path;

	/** Arbitrary user data. */
	void *ptr;
};

/**
 * An I/O queue command.
 */
struct ioq_cmd {
	union {
		struct ioq_req req;
		struct ioq_res res;
	};

	struct ioq_cmd *next;
};

/**
 * An MPMC queue of I/O commands.
 */
struct ioqq {
	pthread_mutex_t mutex;
	pthread_cond_t cond;

	bool stop;

	struct ioq_cmd *head;
	struct ioq_cmd **tail;
};

static struct ioqq *ioqq_create(void) {
	struct ioqq *ioqq = malloc(sizeof(*ioqq));
	if (!ioqq) {
		goto fail;
	}

	if (mutex_init(&ioqq->mutex, NULL) != 0) {
		goto fail_free;
	}

	if (cond_init(&ioqq->cond, NULL) != 0) {
		goto fail_mutex;
	}

	ioqq->stop = false;
	SLIST_INIT(ioqq);
	return ioqq;

fail_mutex:
	mutex_destroy(&ioqq->mutex);
fail_free:
	free(ioqq);
fail:
	return NULL;
}

/** Push a command onto the queue. */
static void ioqq_push(struct ioqq *ioqq, struct ioq_cmd *cmd) {
	mutex_lock(&ioqq->mutex);
	SLIST_APPEND(ioqq, cmd);
	mutex_unlock(&ioqq->mutex);
	cond_signal(&ioqq->cond);
}

/** Pop a command from a queue. */
static struct ioq_cmd *ioqq_pop(struct ioqq *ioqq) {
	mutex_lock(&ioqq->mutex);

	while (!ioqq->stop && !ioqq->head) {
		cond_wait(&ioqq->cond, &ioqq->mutex);
	}

	struct ioq_cmd *cmd = SLIST_POP(ioqq);
	mutex_unlock(&ioqq->mutex);
	return cmd;
}

/** Pop a command from a queue without blocking. */
static struct ioq_cmd *ioqq_trypop(struct ioqq *ioqq) {
	if (!mutex_trylock(&ioqq->mutex)) {
		return NULL;
	}

	struct ioq_cmd *cmd = SLIST_POP(ioqq);
	mutex_unlock(&ioqq->mutex);
	return cmd;
}

/** Stop a queue, waking up any waiters. */
static void ioqq_stop(struct ioqq *ioqq) {
	mutex_lock(&ioqq->mutex);
	ioqq->stop = true;
	mutex_unlock(&ioqq->mutex);
	cond_broadcast(&ioqq->cond);
}

static void ioqq_destroy(struct ioqq *ioqq) {
	if (ioqq) {
		cond_destroy(&ioqq->cond);
		mutex_destroy(&ioqq->mutex);
		free(ioqq);
	}
}

struct ioq {
	/** The depth of the queue. */
	size_t depth;
	/** The current size of the queue. */
	size_t size;

	/** Pending I/O requests. */
	struct ioqq *pending;
	/** Ready I/O responses. */
	struct ioqq *ready;

	/** The number of background threads. */
	size_t nthreads;
	/** The background threads themselves. */
	pthread_t *threads;
};

/** Background thread entry point. */
static void *ioq_work(void *ptr) {
	struct ioq *ioq = ptr;

	while (true) {
		struct ioq_cmd *cmd = ioqq_pop(ioq->pending);
		if (!cmd) {
			break;
		}

		struct ioq_req req = cmd->req;
		sanitize_uninit(cmd);

		struct ioq_res *res = &cmd->res;
		res->dir = bfs_opendir(req.dfd, req.path);
		res->error = errno;
		ioqq_push(ioq->ready, cmd);
	}

	return NULL;
}

struct ioq *ioq_create(size_t depth, size_t threads) {
	struct ioq *ioq = malloc(sizeof(*ioq));
	if (!ioq) {
		goto fail;
	}

	ioq->depth = depth;
	ioq->size = 0;
	ioq->pending = NULL;
	ioq->ready = NULL;
	ioq->nthreads = 0;

	ioq->pending = ioqq_create();
	if (!ioq->pending) {
		goto fail;
	}

	ioq->ready = ioqq_create();
	if (!ioq->ready) {
		goto fail;
	}

	ioq->threads = malloc(threads * sizeof(ioq->threads[0]));
	if (!ioq->threads) {
		goto fail;
	}

	for (size_t i = 0; i < threads; ++i) {
		errno = pthread_create(&ioq->threads[i], NULL, ioq_work, ioq);
		if (errno != 0) {
			goto fail;
		}
		++ioq->nthreads;
	}

	return ioq;

	int err;
fail:
	err = errno;
	ioq_destroy(ioq);
	errno = err;
	return NULL;
}

int ioq_opendir(struct ioq *ioq, int dfd, const char *path, void *ptr) {
	if (ioq->size >= ioq->depth) {
		return -1;
	}

	struct ioq_cmd *cmd = malloc(sizeof(*cmd));
	if (!cmd) {
		return -1;
	}

	struct ioq_req *req = &cmd->req;
	req->dfd = dfd;
	req->path = path;
	req->ptr = ptr;

	++ioq->size;
	ioqq_push(ioq->pending, cmd);
	return 0;
}

struct ioq_res *ioq_pop(struct ioq *ioq) {
	if (ioq->size == 0) {
		return NULL;
	}

	struct ioq_cmd *cmd = ioqq_pop(ioq->ready);
	if (!cmd) {
		return NULL;
	}

	--ioq->size;
	return &cmd->res;
}

struct ioq_res *ioq_trypop(struct ioq *ioq) {
	if (ioq->size == 0) {
		return NULL;
	}

	struct ioq_cmd *cmd = ioqq_trypop(ioq->ready);
	if (!cmd) {
		return NULL;
	}

	--ioq->size;
	return &cmd->res;
}

void ioq_free(struct ioq *ioq, struct ioq_res *res) {
	struct ioq_cmd *cmd = (struct ioq_cmd *)res;
	free(cmd);
}

void ioq_destroy(struct ioq *ioq) {
	if (!ioq) {
		return;
	}

	if (ioq->pending) {
		ioqq_stop(ioq->pending);
	}

	for (size_t i = 0; i < ioq->nthreads; ++i) {
		if (pthread_join(ioq->threads[i], NULL) != 0) {
			abort();
		}
	}
	free(ioq->threads);

	ioqq_destroy(ioq->ready);
	ioqq_destroy(ioq->pending);

	free(ioq);
}