-
Notifications
You must be signed in to change notification settings - Fork 2
/
filelock.c
246 lines (192 loc) · 4.18 KB
/
filelock.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
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
/*
* Really simple exclusive file locking based on filename.
* No hash indexing, just a list, so only works well for < 100 files or
* so. But that's more than what fio needs, so should be fine.
*/
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "flist.h"
#include "filelock.h"
#include "smalloc.h"
#include "fio_sem.h"
#include "hash.h"
#include "log.h"
struct fio_filelock {
uint32_t hash;
struct fio_sem lock;
struct flist_head list;
unsigned int references;
};
#define MAX_FILELOCKS 1024
static struct filelock_data {
struct flist_head list;
struct fio_sem lock;
struct flist_head free_list;
struct fio_filelock ffs[MAX_FILELOCKS];
} *fld;
static void put_filelock(struct fio_filelock *ff)
{
flist_add(&ff->list, &fld->free_list);
}
static struct fio_filelock *__get_filelock(void)
{
struct fio_filelock *ff;
if (flist_empty(&fld->free_list))
return NULL;
ff = flist_first_entry(&fld->free_list, struct fio_filelock, list);
flist_del_init(&ff->list);
return ff;
}
static struct fio_filelock *get_filelock(int trylock, int *retry)
{
struct fio_filelock *ff;
do {
ff = __get_filelock();
if (ff || trylock)
break;
fio_sem_up(&fld->lock);
usleep(1000);
fio_sem_down(&fld->lock);
*retry = 1;
} while (1);
return ff;
}
int fio_filelock_init(void)
{
int i;
fld = smalloc(sizeof(*fld));
if (!fld)
return 1;
INIT_FLIST_HEAD(&fld->list);
INIT_FLIST_HEAD(&fld->free_list);
if (__fio_sem_init(&fld->lock, FIO_SEM_UNLOCKED))
goto err;
for (i = 0; i < MAX_FILELOCKS; i++) {
struct fio_filelock *ff = &fld->ffs[i];
if (__fio_sem_init(&ff->lock, FIO_SEM_UNLOCKED))
goto err;
flist_add_tail(&ff->list, &fld->free_list);
}
return 0;
err:
fio_filelock_exit();
return 1;
}
void fio_filelock_exit(void)
{
if (!fld)
return;
assert(flist_empty(&fld->list));
__fio_sem_remove(&fld->lock);
while (!flist_empty(&fld->free_list)) {
struct fio_filelock *ff;
ff = flist_first_entry(&fld->free_list, struct fio_filelock, list);
flist_del_init(&ff->list);
__fio_sem_remove(&ff->lock);
}
sfree(fld);
fld = NULL;
}
static struct fio_filelock *fio_hash_find(uint32_t hash)
{
struct flist_head *entry;
struct fio_filelock *ff;
flist_for_each(entry, &fld->list) {
ff = flist_entry(entry, struct fio_filelock, list);
if (ff->hash == hash)
return ff;
}
return NULL;
}
static struct fio_filelock *fio_hash_get(uint32_t hash, int trylock)
{
struct fio_filelock *ff;
ff = fio_hash_find(hash);
if (!ff) {
int retry = 0;
ff = get_filelock(trylock, &retry);
if (!ff)
return NULL;
/*
* If we dropped the main lock, re-lookup the hash in case
* someone else added it meanwhile. If it's now there,
* just return that.
*/
if (retry) {
struct fio_filelock *__ff;
__ff = fio_hash_find(hash);
if (__ff) {
put_filelock(ff);
return __ff;
}
}
ff->hash = hash;
ff->references = 0;
flist_add(&ff->list, &fld->list);
}
return ff;
}
static bool __fio_lock_file(const char *fname, int trylock)
{
struct fio_filelock *ff;
uint32_t hash;
hash = jhash(fname, strlen(fname), 0);
fio_sem_down(&fld->lock);
ff = fio_hash_get(hash, trylock);
if (ff)
ff->references++;
fio_sem_up(&fld->lock);
if (!ff) {
assert(trylock);
return true;
}
if (!trylock) {
fio_sem_down(&ff->lock);
return false;
}
if (!fio_sem_down_trylock(&ff->lock))
return false;
fio_sem_down(&fld->lock);
/*
* If we raced and the only reference to the lock is us, we can
* grab it
*/
if (ff->references != 1) {
ff->references--;
ff = NULL;
}
fio_sem_up(&fld->lock);
if (ff) {
fio_sem_down(&ff->lock);
return false;
}
return true;
}
bool fio_trylock_file(const char *fname)
{
return __fio_lock_file(fname, 1);
}
void fio_lock_file(const char *fname)
{
__fio_lock_file(fname, 0);
}
void fio_unlock_file(const char *fname)
{
struct fio_filelock *ff;
uint32_t hash;
hash = jhash(fname, strlen(fname), 0);
fio_sem_down(&fld->lock);
ff = fio_hash_find(hash);
if (ff) {
int refs = --ff->references;
fio_sem_up(&ff->lock);
if (!refs) {
flist_del_init(&ff->list);
put_filelock(ff);
}
} else
log_err("fio: file not found for unlocking\n");
fio_sem_up(&fld->lock);
}