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"
18 #include <semaphore.h>
22 /* Environment var controlling the thread stack size */
23 #define ENVNAME_THREAD_STACK "FUSE_THREAD_STACK"
26 struct fuse_worker
*prev
;
27 struct fuse_worker
*next
;
38 struct fuse_session
*se
;
39 struct fuse_chan
*prevch
;
40 struct fuse_worker main
;
46 static void list_add_worker(struct fuse_worker
*w
, struct fuse_worker
*next
)
48 struct fuse_worker
*prev
= next
->prev
;
55 static void list_del_worker(struct fuse_worker
*w
)
57 struct fuse_worker
*prev
= w
->prev
;
58 struct fuse_worker
*next
= w
->next
;
63 static int fuse_start_thread(struct fuse_mt
*mt
);
65 static void *fuse_do_work(void *data
)
67 struct fuse_worker
*w
= (struct fuse_worker
*) data
;
68 struct fuse_mt
*mt
= w
->mt
;
70 while (!fuse_session_exited(mt
->se
)) {
72 struct fuse_chan
*ch
= mt
->prevch
;
73 struct fuse_buf fbuf
= {
79 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
80 res
= fuse_session_receive_buf(mt
->se
, &fbuf
, &ch
);
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
*) fbuf
.mem
)->opcode
== FUSE_FORGET
)
108 if (mt
->numavail
== 0)
109 fuse_start_thread(mt
);
110 pthread_mutex_unlock(&mt
->lock
);
112 fuse_session_process_buf(mt
->se
, &fbuf
, ch
);
114 pthread_mutex_lock(&mt
->lock
);
117 if (mt
->numavail
> 10) {
119 pthread_mutex_unlock(&mt
->lock
);
125 pthread_mutex_unlock(&mt
->lock
);
127 pthread_detach(w
->thread_id
);
132 pthread_mutex_unlock(&mt
->lock
);
135 sem_post(&mt
->finish
);
140 static int fuse_start_thread(struct fuse_mt
*mt
)
147 struct fuse_worker
*w
= malloc(sizeof(struct fuse_worker
));
149 fprintf(stderr
, "fuse: failed to allocate worker structure\n");
152 memset(w
, 0, sizeof(struct fuse_worker
));
153 w
->bufsize
= fuse_chan_bufsize(mt
->prevch
);
154 w
->buf
= malloc(w
->bufsize
);
157 fprintf(stderr
, "fuse: failed to allocate read buffer\n");
162 /* Override default stack size */
163 pthread_attr_init(&attr
);
164 stack_size
= getenv(ENVNAME_THREAD_STACK
);
165 if (stack_size
&& pthread_attr_setstacksize(&attr
, atoi(stack_size
)))
166 fprintf(stderr
, "fuse: invalid stack size: %s\n", stack_size
);
168 /* Disallow signal reception in worker threads */
169 sigemptyset(&newset
);
170 sigaddset(&newset
, SIGTERM
);
171 sigaddset(&newset
, SIGINT
);
172 sigaddset(&newset
, SIGHUP
);
173 sigaddset(&newset
, SIGQUIT
);
174 pthread_sigmask(SIG_BLOCK
, &newset
, &oldset
);
175 res
= pthread_create(&w
->thread_id
, &attr
, fuse_do_work
, w
);
176 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
177 pthread_attr_destroy(&attr
);
179 fprintf(stderr
, "fuse: error creating thread: %s\n",
185 list_add_worker(w
, &mt
->main
);
192 static void fuse_join_worker(struct fuse_mt
*mt
, struct fuse_worker
*w
)
194 pthread_join(w
->thread_id
, NULL
);
195 pthread_mutex_lock(&mt
->lock
);
197 pthread_mutex_unlock(&mt
->lock
);
202 int fuse_session_loop_mt(struct fuse_session
*se
)
206 struct fuse_worker
*w
;
208 memset(&mt
, 0, sizeof(struct fuse_mt
));
210 mt
.prevch
= fuse_session_next_chan(se
, NULL
);
214 mt
.main
.thread_id
= pthread_self();
215 mt
.main
.prev
= mt
.main
.next
= &mt
.main
;
216 sem_init(&mt
.finish
, 0, 0);
217 fuse_mutex_init(&mt
.lock
);
219 pthread_mutex_lock(&mt
.lock
);
220 err
= fuse_start_thread(&mt
);
221 pthread_mutex_unlock(&mt
.lock
);
223 /* sem_wait() is interruptible */
224 while (!fuse_session_exited(se
))
225 sem_wait(&mt
.finish
);
227 for (w
= mt
.main
.next
; w
!= &mt
.main
; w
= w
->next
)
228 pthread_cancel(w
->thread_id
);
230 pthread_mutex_unlock(&mt
.lock
);
232 while (mt
.main
.next
!= &mt
.main
)
233 fuse_join_worker(&mt
, mt
.main
.next
);
238 pthread_mutex_destroy(&mt
.lock
);
239 sem_destroy(&mt
.finish
);
240 fuse_session_reset(se
);