If the "FUSE_THREAD_STACK" environment is set, initialize the stack size of threads...
[fuse.git] / lib / fuse_loop_mt.c
blobc458d1e4f95d1e753626fa5fe499f0d1203ebe08
1 /*
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.
7 */
9 #include "fuse_lowlevel.h"
10 #include "fuse_misc.h"
11 #include "fuse_kernel.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <signal.h>
18 #include <semaphore.h>
19 #include <errno.h>
20 #include <sys/time.h>
22 /* Environment var controlling the thread stack size */
23 #define ENVNAME_THREAD_STACK "FUSE_THREAD_STACK"
25 struct fuse_worker {
26 struct fuse_worker *prev;
27 struct fuse_worker *next;
28 pthread_t thread_id;
29 size_t bufsize;
30 char *buf;
31 struct fuse_mt *mt;
34 struct fuse_mt {
35 pthread_mutex_t lock;
36 int numworker;
37 int numavail;
38 struct fuse_session *se;
39 struct fuse_chan *prevch;
40 struct fuse_worker main;
41 sem_t finish;
42 int exit;
43 int error;
46 static void list_add_worker(struct fuse_worker *w, struct fuse_worker *next)
48 struct fuse_worker *prev = next->prev;
49 w->next = next;
50 w->prev = prev;
51 prev->next = w;
52 next->prev = w;
55 static void list_del_worker(struct fuse_worker *w)
57 struct fuse_worker *prev = w->prev;
58 struct fuse_worker *next = w->next;
59 prev->next = next;
60 next->prev = prev;
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)) {
71 int isforget = 0;
72 struct fuse_chan *ch = mt->prevch;
73 int res = fuse_chan_recv(&ch, w->buf, w->bufsize);
74 if (res == -EINTR)
75 continue;
76 if (res <= 0) {
77 if (res < 0) {
78 fuse_session_exit(mt->se);
79 mt->error = -1;
81 break;
84 pthread_mutex_lock(&mt->lock);
85 if (mt->exit) {
86 pthread_mutex_unlock(&mt->lock);
87 return NULL;
91 * This disgusting hack is needed so that zillions of threads
92 * are not created on a burst of FORGET messages
94 if (((struct fuse_in_header *) w->buf)->opcode == FUSE_FORGET)
95 isforget = 1;
97 if (!isforget)
98 mt->numavail--;
99 if (mt->numavail == 0)
100 fuse_start_thread(mt);
101 pthread_mutex_unlock(&mt->lock);
103 fuse_session_process(mt->se, w->buf, res, ch);
105 pthread_mutex_lock(&mt->lock);
106 if (!isforget)
107 mt->numavail++;
108 if (mt->numavail > 10) {
109 if (mt->exit) {
110 pthread_mutex_unlock(&mt->lock);
111 return NULL;
113 list_del_worker(w);
114 mt->numavail--;
115 mt->numworker--;
116 pthread_mutex_unlock(&mt->lock);
118 pthread_detach(w->thread_id);
119 free(w->buf);
120 free(w);
121 return NULL;
123 pthread_mutex_unlock(&mt->lock);
126 sem_post(&mt->finish);
127 pause();
129 return NULL;
132 static int fuse_start_thread(struct fuse_mt *mt)
134 sigset_t oldset;
135 sigset_t newset;
136 int res;
137 pthread_attr_t attr;
138 char *stack_size;
139 struct fuse_worker *w = malloc(sizeof(struct fuse_worker));
140 if (!w) {
141 fprintf(stderr, "fuse: failed to allocate worker structure\n");
142 return -1;
144 memset(w, 0, sizeof(struct fuse_worker));
145 w->bufsize = fuse_chan_bufsize(mt->prevch);
146 w->buf = malloc(w->bufsize);
147 w->mt = mt;
148 if (!w->buf) {
149 fprintf(stderr, "fuse: failed to allocate read buffer\n");
150 free(w);
151 return -1;
154 /* Override default stack size */
155 pthread_attr_init(&attr);
156 stack_size = getenv(ENVNAME_THREAD_STACK);
157 if (stack_size && pthread_attr_setstacksize(&attr, atoi(stack_size)))
158 fprintf(stderr, "fuse: invalid stack size: %s\n", stack_size);
160 /* Disallow signal reception in worker threads */
161 sigemptyset(&newset);
162 sigaddset(&newset, SIGTERM);
163 sigaddset(&newset, SIGINT);
164 sigaddset(&newset, SIGHUP);
165 sigaddset(&newset, SIGQUIT);
166 pthread_sigmask(SIG_BLOCK, &newset, &oldset);
167 res = pthread_create(&w->thread_id, &attr, fuse_do_work, w);
168 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
169 pthread_attr_destroy(&attr);
170 if (res != 0) {
171 fprintf(stderr, "fuse: error creating thread: %s\n",
172 strerror(res));
173 free(w->buf);
174 free(w);
175 return -1;
177 list_add_worker(w, &mt->main);
178 mt->numavail ++;
179 mt->numworker ++;
181 return 0;
184 static void fuse_join_worker(struct fuse_mt *mt, struct fuse_worker *w)
186 pthread_join(w->thread_id, NULL);
187 pthread_mutex_lock(&mt->lock);
188 list_del_worker(w);
189 pthread_mutex_unlock(&mt->lock);
190 free(w->buf);
191 free(w);
194 int fuse_session_loop_mt(struct fuse_session *se)
196 int err;
197 struct fuse_mt mt;
198 struct fuse_worker *w;
200 memset(&mt, 0, sizeof(struct fuse_mt));
201 mt.se = se;
202 mt.prevch = fuse_session_next_chan(se, NULL);
203 mt.error = 0;
204 mt.numworker = 0;
205 mt.numavail = 0;
206 mt.main.thread_id = pthread_self();
207 mt.main.prev = mt.main.next = &mt.main;
208 sem_init(&mt.finish, 0, 0);
209 fuse_mutex_init(&mt.lock);
211 pthread_mutex_lock(&mt.lock);
212 err = fuse_start_thread(&mt);
213 pthread_mutex_unlock(&mt.lock);
214 if (!err) {
215 /* sem_wait() is interruptible */
216 while (!fuse_session_exited(se))
217 sem_wait(&mt.finish);
219 for (w = mt.main.next; w != &mt.main; w = w->next)
220 pthread_cancel(w->thread_id);
221 mt.exit = 1;
222 pthread_mutex_unlock(&mt.lock);
224 while (mt.main.next != &mt.main)
225 fuse_join_worker(&mt, mt.main.next);
227 err = mt.error;
230 pthread_mutex_destroy(&mt.lock);
231 sem_destroy(&mt.finish);
232 fuse_session_reset(se);
233 return err;