forked from sustrik/libdill
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handle.c
156 lines (139 loc) · 5.05 KB
/
handle.c
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
/*
Copyright (c) 2016 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cr.h"
#include "handle.h"
#include "libdillimpl.h"
#include "utils.h"
#include "ctx.h"
struct dill_handle {
/* Table of virtual functions. */
struct hvfs *vfs;
/* Index of the next handle in the linked list of unused handles. -1 means
'the end of the list'. -2 means 'handle is in use'. */
int next;
/* Cache the last call to hquery. */
const void *type;
void *ptr;
};
#define CHECKHANDLE(h, err) \
if(dill_slow((h) < 0 || (h) >= ctx->nhandles ||\
ctx->handles[(h)].next != -2)) {\
errno = EBADF; return (err);}\
struct dill_handle *hndl = &ctx->handles[(h)];
int dill_ctx_handle_init(struct dill_ctx_handle *ctx) {
ctx->handles = NULL;
ctx->nhandles = 0;
ctx->unused = -1;
return 0;
}
void dill_ctx_handle_term(struct dill_ctx_handle *ctx) {
free(ctx->handles);
}
int hmake(struct hvfs *vfs) {
struct dill_ctx_handle *ctx = &dill_getctx->handle;
if(dill_slow(!vfs || !vfs->query || !vfs->close)) {
errno = EINVAL; return -1;}
/* Returns ECANCELED if shutting down. */
int rc = dill_canblock();
if(dill_slow(rc < 0)) return -1;
/* If there's no space for the new handle, expand the array. */
if(dill_slow(ctx->unused == -1)) {
/* Start with 256 handles, double the size when needed. */
int sz = ctx->nhandles ? ctx->nhandles * 2 : 256;
struct dill_handle *hndls =
realloc(ctx->handles, sz * sizeof(struct dill_handle));
if(dill_slow(!hndls)) {errno = ENOMEM; return -1;}
/* Add newly allocated handles to the list of unused handles. */
int i;
for(i = ctx->nhandles; i != sz - 1; ++i)
hndls[i].next = i + 1;
hndls[sz - 1].next = -1;
ctx->unused = ctx->nhandles;
/* Adjust the array. */
ctx->handles = hndls;
ctx->nhandles = sz;
}
/* Return first handle from the list of unused handles. */
int h = ctx->unused;
ctx->unused = ctx->handles[h].next;
vfs->refcount = 1;
ctx->handles[h].vfs = vfs;
ctx->handles[h].next = -2;
ctx->handles[h].type = NULL;
ctx->handles[h].ptr = NULL;
return h;
}
int hdup(int h) {
struct dill_ctx_handle *ctx = &dill_getctx->handle;
CHECKHANDLE(h, -1);
int refcount = hndl->vfs->refcount;
int res = hmake(hndl->vfs);
if(dill_slow(res < 0)) return -1;
ctx->handles[res].vfs->refcount = refcount + 1;
return res;
}
void *hquery(int h, const void *type) {
struct dill_ctx_handle *ctx = &dill_getctx->handle;
CHECKHANDLE(h, NULL);
/* Try and use the cached pointer first; otherwise do the expensive virtual call.*/
if(dill_fast(hndl->ptr != NULL && hndl->type == type))
return hndl->ptr;
else {
void *ptr = hndl->vfs->query(hndl->vfs, type);
if(dill_slow(!ptr)) return NULL;
/* Update cache. */
hndl->type = type;
hndl->ptr = ptr;
return ptr;
}
}
int hclose(int h) {
struct dill_ctx_handle *ctx = &dill_getctx->handle;
CHECKHANDLE(h, -1);
/* If there are multiple duplicates of this handle, just remove one reference. */
if(hndl->vfs->refcount > 1) {
--hndl->vfs->refcount;
return 0;
}
/* This will guarantee that blocking functions cannot be called anywhere
inside the context of the close. */
int old = dill_no_blocking(1);
/* Send the stop signal to the handle. */
hndl->vfs->close(hndl->vfs);
/* Restore the previous state. */
dill_no_blocking(old);
/* Mark the cache as invalid. */
hndl->ptr = NULL;
/* Return a handle to the shared pool. */
hndl->next = ctx->unused;
ctx->unused = h;
return 0;
}
int hdone(int h, int64_t deadline) {
struct dill_ctx_handle *ctx = &dill_getctx->handle;
CHECKHANDLE(h, -1);
if(dill_slow(!hndl->vfs->done)) {errno = ENOTSUP; return -1;}
return hndl->vfs->done(hndl->vfs, deadline);
}