24#define FUSE_USE_VERSION 31
44static unsigned fsel_open_mask;
45static const char fsel_hex_map[] =
"0123456789ABCDEF";
46static struct fuse *fsel_fuse;
48#define FSEL_CNT_MAX 10
51static pthread_mutex_t fsel_mutex;
52static unsigned fsel_poll_notify_mask;
53static struct fuse_pollhandle *fsel_poll_handle[FSEL_FILES];
54static unsigned fsel_cnt[FSEL_FILES];
55static _Atomic
bool fsel_stop =
false;
56static pthread_t fsel_producer_thread;
59static int fsel_path_index(
const char *path)
63 if (strlen(path) != 2 || path[0] !=
'/' || !isxdigit(ch) || islower(ch))
65 return ch <=
'9' ? ch -
'0' : ch -
'A' + 10;
68static void fsel_destroy(
void *private_data)
74 pthread_join(fsel_producer_thread, NULL);
77static int fsel_getattr(
const char *path,
struct stat *stbuf,
83 memset(stbuf, 0,
sizeof(
struct stat));
85 if (strcmp(path,
"/") == 0) {
86 stbuf->st_mode = S_IFDIR | 0555;
91 idx = fsel_path_index(path);
95 stbuf->st_mode = S_IFREG | 0444;
97 stbuf->st_size = fsel_cnt[idx];
101static int fsel_readdir(
const char *path,
void *buf,
fuse_fill_dir_t filler,
112 if (strcmp(path,
"/") != 0)
115 for (i = 0; i < FSEL_FILES; i++) {
116 name[0] = fsel_hex_map[i];
125 int idx = fsel_path_index(path);
129 if ((fi->
flags & O_ACCMODE) != O_RDONLY)
131 if (fsel_open_mask & (1 << idx))
133 fsel_open_mask |= (1 << idx);
147static int fsel_release(
const char *path,
struct fuse_file_info *fi)
153 fsel_open_mask &= ~(1 << idx);
157static int fsel_read(
const char *path,
char *buf,
size_t size, off_t offset,
165 pthread_mutex_lock(&fsel_mutex);
166 if (fsel_cnt[idx] < size)
167 size = fsel_cnt[idx];
168 printf(
"READ %X transferred=%zu cnt=%u\n", idx, size, fsel_cnt[idx]);
169 fsel_cnt[idx] -= size;
170 pthread_mutex_unlock(&fsel_mutex);
172 memset(buf, fsel_hex_map[idx], size);
177 struct fuse_pollhandle *ph,
unsigned *reventsp)
179 static unsigned polled_zero;
193 fsel_fuse = cxt->
fuse;
196 pthread_mutex_lock(&fsel_mutex);
199 struct fuse_pollhandle *oldph = fsel_poll_handle[idx];
204 fsel_poll_notify_mask |= (1 << idx);
205 fsel_poll_handle[idx] = ph;
210 printf(
"POLL %X cnt=%u polled_zero=%u\n",
211 idx, fsel_cnt[idx], polled_zero);
216 pthread_mutex_unlock(&fsel_mutex);
222 .getattr = fsel_getattr,
223 .readdir = fsel_readdir,
225 .release = fsel_release,
230static void *fsel_producer(
void *data)
232 const struct timespec interval = { 0, 250000000 };
233 unsigned idx = 0, nr = 1;
240 pthread_mutex_lock(&fsel_mutex);
248 for (i = 0, t = idx; i < nr;
249 i++, t = (t + FSEL_FILES / nr) % FSEL_FILES) {
250 if (fsel_cnt[t] == FSEL_CNT_MAX)
254 if (fsel_fuse && (fsel_poll_notify_mask & (1 << t))) {
255 struct fuse_pollhandle *ph;
257 printf(
"NOTIFY %X\n", t);
258 ph = fsel_poll_handle[t];
259 fuse_notify_poll(ph);
261 fsel_poll_notify_mask &= ~(1 << t);
262 fsel_poll_handle[t] = NULL;
266 idx = (idx + 1) % FSEL_FILES;
270 pthread_mutex_unlock(&fsel_mutex);
272 nanosleep(&interval, NULL);
278int main(
int argc,
char *argv[])
283 errno = pthread_mutex_init(&fsel_mutex, NULL);
285 perror(
"pthread_mutex_init");
289 errno = pthread_attr_init(&attr);
291 perror(
"pthread_attr_init");
295 errno = pthread_create(&fsel_producer_thread, &attr, fsel_producer, NULL);
297 perror(
"pthread_create");
301 ret = fuse_main(argc, argv, &fsel_oper, NULL);
struct fuse_context * fuse_get_context(void)
int(* fuse_fill_dir_t)(void *buf, const char *name, const struct stat *stbuf, off_t off, enum fuse_fill_dir_flags flags)
void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
void(* destroy)(void *private_data)