-
Notifications
You must be signed in to change notification settings - Fork 9
/
file.c
60 lines (43 loc) · 1.07 KB
/
file.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
#include "file.h"
#include <stddef.h>
#include <pthread.h>
#define DEFAULT_FD_NUM 3
#define MAX_FD_COUNT 1024
static void* fd_table[MAX_FD_COUNT] = {0};
static pthread_spinlock_t lock = PTHREAD_PROCESS_SHARED;
int get_fd(void) {
int ret = -1;
pthread_spin_lock(&lock);
for (int fd = DEFAULT_FD_NUM; fd < MAX_FD_COUNT; ++fd) {
// if ((fd_table[fd/8] & (0x1 << (fd % 8))) == 0) {
// fd_table[fd/8] |= (0x1 << (fd % 8));
// return fd;
// }
if (fd_table[fd] == NULL) {
ret = fd;
break;
}
}
pthread_spin_unlock(&lock);
return ret;
}
int put_fd(int fd) {
if (fd >= MAX_FD_COUNT)
return -1;
// fd_table[fd/8] &= ~(0x1 << (fd % 8));
pthread_spin_lock(&lock);
fd_table[fd] = NULL;
pthread_spin_unlock(&lock);
return 0;
}
int set_fd(int fd, void* data) {
pthread_spin_lock(&lock);
fd_table[fd] = data;
pthread_spin_unlock(&lock);
return 0;
}
void* find_fd(int fd) {
if (fd > MAX_FD_COUNT)
return NULL;
return fd_table[fd];
}