fuse_buf_copy: check if buffers are the same
[fuse.git] / lib / fuse_loop_mt.c
bloba76713b0808d0fac1c3f82ca8b89ccea9a2bbc19
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 struct fuse_buf fbuf = {
74 .mem = w->buf,
75 .size = w->bufsize,
77 int res;
79 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
80 res = fuse_session_receive_buf(mt->se, &fbuf, &ch);
81 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
82 if (res == -EINTR)
83 continue;
84 if (res <= 0) {
85 if (res < 0) {
86 fuse_session_exit(mt->se);
87 mt->error = -1;
89 break;
92 pthread_mutex_lock(&mt->lock);
93 if (mt->exit) {
94 pthread_mutex_unlock(&mt->lock);
95 return NULL;
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)
104 isforget = 1;
106 if (!isforget)
107 mt->numavail--;
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);
115 if (!isforget)
116 mt->numavail++;
117 if (mt->numavail > 10) {
118 if (mt->exit) {
119 pthread_mutex_unlock(&mt->lock);
120 return NULL;
122 list_del_worker(w);
123 mt->numavail--;
124 mt->numworker--;
125 pthread_mutex_unlock(&mt->lock);
127 pthread_detach(w->thread_id);
128 free(w->buf);
129 free(w);
130 return NULL;
132 pthread_mutex_unlock(&mt->lock);
135 sem_post(&mt->finish);
136 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
137 pause();
139 return NULL;
142 static int fuse_start_thread(struct fuse_mt *mt)
144 sigset_t oldset;
145 sigset_t newset;
146 int res;
147 pthread_attr_t attr;
148 char *stack_size;
149 struct fuse_worker *w = malloc(sizeof(struct fuse_worker));
150 if (!w) {
151 fprintf(stderr, "fuse: failed to allocate worker structure\n");
152 return -1;
154 memset(w, 0, sizeof(struct fuse_worker));
155 w->bufsize = fuse_chan_bufsize(mt->prevch);
156 w->buf = malloc(w->bufsize);
157 w->mt = mt;
158 if (!w->buf) {
159 fprintf(stderr, "fuse: failed to allocate read buffer\n");
160 free(w);
161 return -1;
164 /* Override default stack size */
165 pthread_attr_init(&attr);
166 stack_size = getenv(ENVNAME_THREAD_STACK);
167 if (stack_size && pthread_attr_setstacksize(&attr, atoi(stack_size)))
168 fprintf(stderr, "fuse: invalid stack size: %s\n", stack_size);
170 /* Disallow signal reception in worker threads */
171 sigemptyset(&newset);
172 sigaddset(&newset, SIGTERM);
173 sigaddset(&newset, SIGINT);
174 sigaddset(&newset, SIGHUP);
175 sigaddset(&newset, SIGQUIT);
176 pthread_sigmask(SIG_BLOCK, &newset, &oldset);
177 res = pthread_create(&w->thread_id, &attr, fuse_do_work, w);
178 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
179 pthread_attr_destroy(&attr);
180 if (res != 0) {
181 fprintf(stderr, "fuse: error creating thread: %s\n",
182 strerror(res));
183 free(w->buf);
184 free(w);
185 return -1;
187 list_add_worker(w, &mt->main);
188 mt->numavail ++;
189 mt->numworker ++;
191 return 0;
194 static void fuse_join_worker(struct fuse_mt *mt, struct fuse_worker *w)
196 pthread_join(w->thread_id, NULL);
197 pthread_mutex_lock(&mt->lock);
198 list_del_worker(w);
199 pthread_mutex_unlock(&mt->lock);
200 free(w->buf);
201 free(w);
204 int fuse_session_loop_mt(struct fuse_session *se)
206 int err;
207 struct fuse_mt mt;
208 struct fuse_worker *w;
210 memset(&mt, 0, sizeof(struct fuse_mt));
211 mt.se = se;
212 mt.prevch = fuse_session_next_chan(se, NULL);
213 mt.error = 0;
214 mt.numworker = 0;
215 mt.numavail = 0;
216 mt.main.thread_id = pthread_self();
217 mt.main.prev = mt.main.next = &mt.main;
218 sem_init(&mt.finish, 0, 0);
219 fuse_mutex_init(&mt.lock);
221 pthread_mutex_lock(&mt.lock);
222 err = fuse_start_thread(&mt);
223 pthread_mutex_unlock(&mt.lock);
224 if (!err) {
225 /* sem_wait() is interruptible */
226 while (!fuse_session_exited(se))
227 sem_wait(&mt.finish);
229 for (w = mt.main.next; w != &mt.main; w = w->next)
230 pthread_cancel(w->thread_id);
231 mt.exit = 1;
232 pthread_mutex_unlock(&mt.lock);
234 while (mt.main.next != &mt.main)
235 fuse_join_worker(&mt, mt.main.next);
237 err = mt.error;
240 pthread_mutex_destroy(&mt.lock);
241 sem_destroy(&mt.finish);
242 fuse_session_reset(se);
243 return err;