summaryrefslogtreecommitdiff
path: root/source3/lib/asys/asys.c
blob: 9937d2482d54eb572fc69ef02d95b606d9814825 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Async syscalls
 * Copyright (C) Volker Lendecke 2012
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "asys.h"
#include <stdlib.h>
#include <errno.h>
#include "../pthreadpool/pthreadpool.h"

struct asys_pwrite_args {
	int fildes;
	const void *buf;
	size_t nbyte;
	off_t offset;
};

struct asys_pread_args {
	int fildes;
	void *buf;
	size_t nbyte;
	off_t offset;
};

struct asys_fsync_args {
	int fildes;
};

union asys_job_args {
	struct asys_pwrite_args pwrite_args;
	struct asys_pread_args pread_args;
	struct asys_fsync_args fsync_args;
};

struct asys_job {
	void *private_data;
	union asys_job_args args;
	ssize_t ret;
	int err;
	char busy;
	char canceled;
};

struct asys_context {
	struct pthreadpool *pool;
	int pthreadpool_fd;

	unsigned num_jobs;
	struct asys_job **jobs;
};

struct asys_creds_context {
	int dummy;
};

int asys_context_init(struct asys_context **pctx, unsigned max_parallel)
{
	struct asys_context *ctx;
	int ret;

	ctx = calloc(1, sizeof(struct asys_context));
	if (ctx == NULL) {
		return ENOMEM;
	}
	ret = pthreadpool_init(max_parallel, &ctx->pool);
	if (ret != 0) {
		free(ctx);
		return ret;
	}
	ctx->pthreadpool_fd = pthreadpool_signal_fd(ctx->pool);

	*pctx = ctx;
	return 0;
}

int asys_signalfd(struct asys_context *ctx)
{
	return ctx->pthreadpool_fd;
}

int asys_context_destroy(struct asys_context *ctx)
{
	int ret;
	unsigned i;

	for (i=0; i<ctx->num_jobs; i++) {
		if (ctx->jobs[i]->busy) {
			return EBUSY;
		}
	}

	ret = pthreadpool_destroy(ctx->pool);
	if (ret != 0) {
		return ret;
	}
	for (i=0; i<ctx->num_jobs; i++) {
		free(ctx->jobs[i]);
	}
	free(ctx->jobs);
	free(ctx);
	return 0;
}

static int asys_new_job(struct asys_context *ctx, int *jobid,
			struct asys_job **pjob)
{
	struct asys_job **tmp;
	struct asys_job *job;
	unsigned i;

	for (i=0; i<ctx->num_jobs; i++) {
		job = ctx->jobs[i];
		if (!job->busy) {
			job->err = 0;
			*pjob = job;
			*jobid = i;
			return 0;
		}
	}

	if (ctx->num_jobs+1 == 0) {
		return EBUSY; 	/* overflow */
	}

	tmp = realloc(ctx->jobs, sizeof(struct asys_job *)*(ctx->num_jobs+1));
	if (tmp == NULL) {
		return ENOMEM;
	}
	ctx->jobs = tmp;

	job = calloc(1, sizeof(struct asys_job));
	if (job == NULL) {
		return ENOMEM;
	}
	ctx->jobs[ctx->num_jobs] = job;

	*jobid = ctx->num_jobs;
	*pjob = job;
	ctx->num_jobs += 1;
	return 0;
}

static void asys_pwrite_do(void *private_data);

int asys_pwrite(struct asys_context *ctx, int fildes, const void *buf,
		size_t nbyte, off_t offset, void *private_data)
{
	struct asys_job *job;
	struct asys_pwrite_args *args;
	int jobid;
	int ret;

	ret = asys_new_job(ctx, &jobid, &job);
	if (ret != 0) {
		return ret;
	}
	job->private_data = private_data;

	args = &job->args.pwrite_args;
	args->fildes = fildes;
	args->buf = buf;
	args->nbyte = nbyte;
	args->offset = offset;

	ret = pthreadpool_add_job(ctx->pool, jobid, asys_pwrite_do, job);
	if (ret != 0) {
		return ret;
	}
	job->busy = 1;

	return 0;
}

static void asys_pwrite_do(void *private_data)
{
	struct asys_job *job = (struct asys_job *)private_data;
	struct asys_pwrite_args *args = &job->args.pwrite_args;

	job->ret = pwrite(args->fildes, args->buf, args->nbyte, args->offset);
	if (job->ret == -1) {
		job->err = errno;
	}
}

static void asys_pread_do(void *private_data);

int asys_pread(struct asys_context *ctx, int fildes, void *buf,
	       size_t nbyte, off_t offset, void *private_data)
{
	struct asys_job *job;
	struct asys_pread_args *args;
	int jobid;
	int ret;

	ret = asys_new_job(ctx, &jobid, &job);
	if (ret != 0) {
		return ret;
	}
	job->private_data = private_data;

	args = &job->args.pread_args;
	args->fildes = fildes;
	args->buf = buf;
	args->nbyte = nbyte;
	args->offset = offset;

	ret = pthreadpool_add_job(ctx->pool, jobid, asys_pread_do, job);
	if (ret != 0) {
		return ret;
	}
	job->busy = 1;

	return 0;
}

static void asys_pread_do(void *private_data)
{
	struct asys_job *job = (struct asys_job *)private_data;
	struct asys_pread_args *args = &job->args.pread_args;

	job->ret = pread(args->fildes, args->buf, args->nbyte, args->offset);
	if (job->ret == -1) {
		job->err = errno;
	}
}

static void asys_fsync_do(void *private_data);

int asys_fsync(struct asys_context *ctx, int fildes, void *private_data)
{
	struct asys_job *job;
	struct asys_fsync_args *args;
	int jobid;
	int ret;

	ret = asys_new_job(ctx, &jobid, &job);
	if (ret != 0) {
		return ret;
	}
	job->private_data = private_data;

	args = &job->args.fsync_args;
	args->fildes = fildes;

	ret = pthreadpool_add_job(ctx->pool, jobid, asys_fsync_do, job);
	if (ret != 0) {
		return ret;
	}
	job->busy = 1;

	return 0;
}

static void asys_fsync_do(void *private_data)
{
	struct asys_job *job = (struct asys_job *)private_data;
	struct asys_fsync_args *args = &job->args.fsync_args;

	job->ret = fsync(args->fildes);
	if (job->ret == -1) {
		job->err = errno;
	}
}

void asys_cancel(struct asys_context *ctx, void *private_data)
{
	unsigned i;

	for (i=0; i<ctx->num_jobs; i++) {
		struct asys_job *job = ctx->jobs[i];

		if (job->private_data == private_data) {
			job->canceled = 1;
		}
	}
}

int asys_result(struct asys_context *ctx, ssize_t *pret, int *perrno,
		void *pdata)
{
	void **pprivate_data = (void **)pdata;
	struct asys_job *job;
	int ret, jobid;

	ret = pthreadpool_finished_job(ctx->pool, &jobid);
	if (ret != 0) {
		return ret;
	}
	if ((jobid < 0) || (jobid >= ctx->num_jobs)) {
		return EIO;
	}

	job = ctx->jobs[jobid];

	if (job->canceled) {
		return ECANCELED;
	}

	*pret = job->ret;
	*perrno = job->err;
	*pprivate_data = job->private_data;
	job->busy = 0;
	return 0;
}