2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB.
9 #include "fuse_lowlevel.h"
10 #include "fuse_misc.h"
11 #include "fuse_kernel.h"
19 #include <semaphore.h>
23 /* Environment var controlling the thread stack size */
24 #define ENVNAME_THREAD_STACK "FUSE_THREAD_STACK"
27 struct fuse_worker
*prev
;
28 struct fuse_worker
*next
;
39 struct fuse_session
*se
;
40 struct fuse_chan
*prevch
;
41 struct fuse_worker main
;
47 static void list_add_worker(struct fuse_worker
*w
, struct fuse_worker
*next
)
49 struct fuse_worker
*prev
= next
->prev
;
56 static void list_del_worker(struct fuse_worker
*w
)
58 struct fuse_worker
*prev
= w
->prev
;
59 struct fuse_worker
*next
= w
->next
;
64 static int fuse_loop_start_thread(struct fuse_mt
*mt
);
66 static void *fuse_do_work(void *data
)
68 struct fuse_worker
*w
= (struct fuse_worker
*) data
;
69 struct fuse_mt
*mt
= w
->mt
;
71 while (!fuse_session_exited(mt
->se
)) {
73 struct fuse_buf fbuf
= {
79 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
80 res
= fuse_session_receive_buf(mt
->se
, &fbuf
, mt
->prevch
);
81 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, NULL
);
86 fuse_session_exit(mt
->se
);
92 pthread_mutex_lock(&mt
->lock
);
94 pthread_mutex_unlock(&mt
->lock
);
99 * This disgusting hack is needed so that zillions of threads
100 * are not created on a burst of FORGET messages
102 if (!(fbuf
.flags
& FUSE_BUF_IS_FD
)) {
103 struct fuse_in_header
*in
= fbuf
.mem
;
105 if (in
->opcode
== FUSE_FORGET
||
106 in
->opcode
== FUSE_BATCH_FORGET
)
112 if (mt
->numavail
== 0)
113 fuse_loop_start_thread(mt
);
114 pthread_mutex_unlock(&mt
->lock
);
116 fuse_session_process_buf(mt
->se
, &fbuf
, mt
->prevch
);
118 pthread_mutex_lock(&mt
->lock
);
121 if (mt
->numavail
> 10) {
123 pthread_mutex_unlock(&mt
->lock
);
129 pthread_mutex_unlock(&mt
->lock
);
131 pthread_detach(w
->thread_id
);
136 pthread_mutex_unlock(&mt
->lock
);
139 sem_post(&mt
->finish
);
144 int fuse_start_thread(pthread_t
*thread_id
, void *(*func
)(void *), void *arg
)
152 /* Override default stack size */
153 pthread_attr_init(&attr
);
154 stack_size
= getenv(ENVNAME_THREAD_STACK
);
155 if (stack_size
&& pthread_attr_setstacksize(&attr
, atoi(stack_size
)))
156 fprintf(stderr
, "fuse: invalid stack size: %s\n", stack_size
);
158 /* Disallow signal reception in worker threads */
159 sigemptyset(&newset
);
160 sigaddset(&newset
, SIGTERM
);
161 sigaddset(&newset
, SIGINT
);
162 sigaddset(&newset
, SIGHUP
);
163 sigaddset(&newset
, SIGQUIT
);
164 pthread_sigmask(SIG_BLOCK
, &newset
, &oldset
);
165 res
= pthread_create(thread_id
, &attr
, func
, arg
);
166 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
167 pthread_attr_destroy(&attr
);
169 fprintf(stderr
, "fuse: error creating thread: %s\n",
177 static int fuse_loop_start_thread(struct fuse_mt
*mt
)
180 struct fuse_worker
*w
= malloc(sizeof(struct fuse_worker
));
182 fprintf(stderr
, "fuse: failed to allocate worker structure\n");
185 memset(w
, 0, sizeof(struct fuse_worker
));
186 w
->bufsize
= fuse_chan_bufsize(mt
->prevch
);
187 w
->buf
= malloc(w
->bufsize
);
190 fprintf(stderr
, "fuse: failed to allocate read buffer\n");
195 res
= fuse_start_thread(&w
->thread_id
, fuse_do_work
, w
);
201 list_add_worker(w
, &mt
->main
);
208 static void fuse_join_worker(struct fuse_mt
*mt
, struct fuse_worker
*w
)
210 pthread_join(w
->thread_id
, NULL
);
211 pthread_mutex_lock(&mt
->lock
);
213 pthread_mutex_unlock(&mt
->lock
);
218 int fuse_session_loop_mt(struct fuse_session
*se
)
222 struct fuse_worker
*w
;
224 memset(&mt
, 0, sizeof(struct fuse_mt
));
226 mt
.prevch
= fuse_session_chan(se
);
230 mt
.main
.thread_id
= pthread_self();
231 mt
.main
.prev
= mt
.main
.next
= &mt
.main
;
232 sem_init(&mt
.finish
, 0, 0);
233 fuse_mutex_init(&mt
.lock
);
235 pthread_mutex_lock(&mt
.lock
);
236 err
= fuse_loop_start_thread(&mt
);
237 pthread_mutex_unlock(&mt
.lock
);
239 /* sem_wait() is interruptible */
240 while (!fuse_session_exited(se
))
241 sem_wait(&mt
.finish
);
243 pthread_mutex_lock(&mt
.lock
);
244 for (w
= mt
.main
.next
; w
!= &mt
.main
; w
= w
->next
)
245 pthread_cancel(w
->thread_id
);
247 pthread_mutex_unlock(&mt
.lock
);
249 while (mt
.main
.next
!= &mt
.main
)
250 fuse_join_worker(&mt
, mt
.main
.next
);
255 pthread_mutex_destroy(&mt
.lock
);
256 sem_destroy(&mt
.finish
);
257 fuse_session_reset(se
);