Fix compilation warnings
[archipelago] / xseg / drivers / user / xseg_pthread.c
1 /*
2  * Copyright 2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *   2. Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and
30  * documentation are those of the authors and should not be
31  * interpreted as representing official policies, either expressed
32  * or implied, of GRNET S.A.
33  */
34
35 #define _GNU_SOURCE
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/mman.h>
42 #include <sys/syscall.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <string.h>
46 #include <signal.h>
47 #include <sys/util.h>
48 #include <xseg/xseg.h>
49 #include <pthread.h>
50 #include <drivers/xseg_pthread.h>
51 #define ERRSIZE 512
52 char errbuf[ERRSIZE];
53
54 static void *pthread_malloc(uint64_t size);
55 static void pthread_mfree(void *mem);
56
57 static long pthread_allocate(const char *name, uint64_t size)
58 {
59         int fd, r;
60         fd = shm_open(name, O_RDWR | O_CREAT, 0770);
61         if (fd < 0) {
62                 XSEGLOG("Cannot create shared segment: %s\n",
63                         strerror_r(errno, errbuf, ERRSIZE));
64                 return fd;
65         }
66
67         r = lseek(fd, size -1, SEEK_SET);
68         if (r < 0) {
69                 close(fd);
70                 XSEGLOG("Cannot seek into segment file: %s\n",
71                         strerror_r(errno, errbuf, ERRSIZE));
72                 return r;
73         }
74
75         errbuf[0] = 0;
76         r = write(fd, errbuf, 1);
77         if (r != 1) {
78                 close(fd);
79                 XSEGLOG("Failed to set segment size: %s\n",
80                         strerror_r(errno, errbuf, ERRSIZE));
81                 return r;
82         }
83
84         close(fd);
85         return 0;
86 }
87
88 static long pthread_deallocate(const char *name)
89 {
90         return shm_unlink(name);
91 }
92
93 static void *pthread_map(const char *name, uint64_t size, struct xseg *seg)
94 {
95         struct xseg *xseg;
96         int fd;
97
98         if (seg)
99                 XSEGLOG("struct xseg * is not NULL. Ignoring...\n");
100
101         fd = shm_open(name, O_RDWR, 0000);
102         if (fd < 0) {
103                 XSEGLOG("Failed to open '%s' for mapping: %s\n",
104                         name, strerror_r(errno, errbuf, ERRSIZE));
105                 return NULL;
106         }
107
108         xseg = mmap (   XSEG_BASE_AS_PTR,
109                         size,
110                         PROT_READ | PROT_WRITE,
111                         MAP_SHARED | MAP_FIXED /* | MAP_LOCKED */,
112                         fd, 0   );
113
114         if (xseg == MAP_FAILED) {
115                 XSEGLOG("Could not map segment: %s\n",
116                         strerror_r(errno, errbuf, ERRSIZE));
117                 return NULL;
118         }
119
120         close(fd);
121         return xseg;
122 }
123
124 static void pthread_unmap(void *ptr, uint64_t size)
125 {
126         struct xseg *xseg = ptr;
127         (void)munmap(xseg, xseg->segment_size);
128 }
129
130
131 static void handler(int signum)
132 {
133         static unsigned long counter;
134         printf("%lu: signal %d: this shouldn't have happened.\n", counter, signum);
135         counter ++;
136 }
137
138 static pthread_key_t pid_key, xpidx_key;
139 static pthread_key_t mask_key, act_key;
140 static pthread_once_t once_init = PTHREAD_ONCE_INIT;
141 static pthread_once_t once_quit = PTHREAD_ONCE_INIT;
142 static int isInit;
143
144 static void keys_init(void) 
145 {
146         int r;
147         
148         r = pthread_key_create(&pid_key, NULL);
149         if (r < 0) {
150                 isInit = 0;
151                 return;
152         }
153         
154         r = pthread_key_create(&xpidx_key, NULL);
155         if (r < 0) {
156                 isInit = 0;
157                 return;
158         }
159         r = pthread_key_create(&mask_key, NULL);
160         if (r < 0) {
161                 isInit = 0;
162                 return;
163         }
164         
165         r = pthread_key_create(&act_key, NULL);
166         if (r < 0) {
167                 isInit = 0;
168                 return;
169         }
170         isInit = 1;
171         once_quit = PTHREAD_ONCE_INIT;
172 }
173
174 #define INT_TO_POINTER(__myptr, __myint) \
175         do {\
176                 unsigned long __foo__ = (unsigned long) __myint; \
177                 __myptr = (void *) __foo__ ; \
178         } while (0)
179
180 #define POINTER_TO_INT(__myint, __myptr)\
181         do { \
182                 unsigned long __foo__ = (unsigned long) __myptr; \
183                 __myint = (int) __foo__ ; \
184         } while (0)
185
186 /* must be called by each thread */
187 static int pthread_local_signal_init(struct xseg *xseg, xport portno)
188 {
189         int r;
190         pid_t pid;
191         void *tmp;
192         sigset_t *savedset, *set;
193         struct sigaction *act, *old_act;
194
195         savedset = pthread_malloc(sizeof(sigset_t));
196         if (!savedset)
197                 goto err1;
198         set = pthread_malloc(sizeof(sigset_t));
199         if (!set)
200                 goto err2;
201
202         act = pthread_malloc(sizeof(struct sigaction));
203         if (!act)
204                 goto err3;
205         old_act = pthread_malloc(sizeof(struct sigaction));
206         if (!old_act)
207                 goto err4;
208
209         pthread_once(&once_init, keys_init);
210         if (!isInit)
211                 goto err5;
212
213         sigemptyset(set);
214         act->sa_handler = handler;
215         act->sa_mask = *set;
216         act->sa_flags = 0;
217         if(sigaction(SIGIO, act, old_act) < 0)
218                 goto err5;
219
220         
221         sigaddset(set, SIGIO);
222
223         r = pthread_sigmask(SIG_BLOCK, set, savedset);
224         if (r < 0) 
225                 goto err6;
226
227
228         pid = syscall(SYS_gettid);
229         INT_TO_POINTER(tmp, pid);
230         if (!pthread_setspecific(pid_key, tmp) ||
231                         pthread_setspecific(mask_key, savedset) ||
232                         pthread_setspecific(act_key, old_act))
233                 goto err7;
234
235         return 0;
236
237 err7:
238         pthread_sigmask(SIG_BLOCK, savedset, NULL);
239 err6:
240         sigaction(SIGIO, old_act, NULL);
241 err5:
242         pthread_mfree(old_act);
243 err4:
244         pthread_mfree(act);
245 err3:
246         pthread_mfree(set);
247 err2:
248         pthread_mfree(savedset);
249 err1:
250         return -1;
251 }
252
253 /* should be called by each thread which had initialized signals */
254 static void pthread_local_signal_quit(struct xseg *xseg, xport portno)
255 {
256         sigset_t *savedset;
257         struct sigaction *old_act;
258
259         savedset = pthread_getspecific(act_key);
260         old_act = pthread_getspecific(mask_key);
261         if (old_act)
262                 sigaction(SIGIO, old_act, NULL);
263         if (savedset)
264                 pthread_sigmask(SIG_SETMASK, savedset, NULL);
265 }
266
267 static int pthread_remote_signal_init(void)
268 {
269         return 0;
270 }
271
272 static void pthread_remote_signal_quit(void)
273 {
274         return;
275 }
276
277 static int pthread_prepare_wait(struct xseg *xseg, uint32_t portno)
278 {
279         void * tmp;
280         pid_t pid;
281         xpool_index r;
282         struct xseg_port *port = xseg_get_port(xseg, portno);
283         if (!port) 
284                 return -1;
285         struct pthread_signal_desc *psd = xseg_get_signal_desc(xseg, port);
286         if (!psd)
287                 return -1;
288
289         tmp = pthread_getspecific(pid_key);
290         POINTER_TO_INT(pid, tmp);
291         if (!pid)
292                 return -1;
293
294         r = xpool_add(&psd->waiters, (xpool_index) pid, portno); 
295         if (r == NoIndex)
296                 return -1;
297         pthread_setspecific(xpidx_key, (void *)r);
298         return 0;
299 }
300
301 static int pthread_cancel_wait(struct xseg *xseg, uint32_t portno)
302 {
303         void * tmp;
304         pid_t pid;
305         xpool_data data;
306         xpool_index xpidx, r;
307         struct xseg_port *port = xseg_get_port(xseg, portno);
308         if (!port) 
309                 return -1;
310         struct pthread_signal_desc *psd = xseg_get_signal_desc(xseg, port);
311         if (!psd)
312                 return -1;
313         
314         tmp = pthread_getspecific(pid_key);
315         POINTER_TO_INT(pid, tmp);
316         if (!pid)
317                 return -1;
318
319         xpidx = (xpool_index) pthread_getspecific(xpidx_key);
320
321         r = xpool_remove(&psd->waiters, xpidx, &data, portno);
322         if (r == NoIndex)
323                 return -1;
324         
325         return 0;
326 }
327
328 static int pthread_wait_signal(struct xseg *xseg, uint32_t usec_timeout)
329 {
330         int r;
331         siginfo_t siginfo;
332         struct timespec ts;
333         sigset_t set;
334         sigemptyset(&set);
335         sigaddset(&set, SIGIO);
336
337         ts.tv_sec = usec_timeout / 1000000;
338         ts.tv_nsec = 1000 * (usec_timeout - ts.tv_sec * 1000000);
339
340         r = sigtimedwait(&set, &siginfo, &ts);
341         if (r < 0)
342                 return r;
343
344         return siginfo.si_signo;
345 }
346
347 static int pthread_signal(struct xseg *xseg, uint32_t portno)
348 {
349         xpool_data data;
350         xpool_index idx;
351
352         struct xseg_port *port = xseg_get_port(xseg, portno);
353         if (!port) 
354                 return -1;
355         struct pthread_signal_desc *psd = xseg_get_signal_desc(xseg, port);
356         if (!psd)
357                 return -1;
358
359         idx = xpool_peek(&psd->waiters, &data, portno); //FIXME portno is not the caller but the callee
360         if (idx == NoIndex) 
361                 return 0;
362
363         pid_t cue = (pid_t) data;
364         if (!cue)
365                 return 0;
366
367         return syscall(SYS_tkill, cue, SIGIO);
368 }
369
370 static void *pthread_malloc(uint64_t size)
371 {
372         return malloc((size_t)size);
373 }
374
375 static void *pthread_realloc(void *mem, uint64_t size)
376 {
377         return realloc(mem, (size_t)size);
378 }
379
380 static void pthread_mfree(void *mem)
381 {
382         free(mem);
383 }
384
385 static struct xseg_type xseg_pthread = {
386         /* xseg_operations */
387         {
388                 .mfree          = pthread_mfree,
389                 .allocate       = pthread_allocate,
390                 .deallocate     = pthread_deallocate,
391                 .map            = pthread_map,
392                 .unmap          = pthread_unmap,
393         },
394         /* name */
395         "pthread"
396 };
397
398 int pthread_init_signal_desc(struct xseg *xseg, void *sd)
399 {
400         struct pthread_signal_desc *psd = (struct pthread_signal_desc *)sd;
401         xpool_init(&psd->waiters, MAX_WAITERS, psd->bufs);
402         xpool_clear(&psd->waiters, 1);
403         return 0;
404 }
405
406 void pthread_quit_signal_desc(struct xseg *xseg, void *sd)
407 {
408         struct pthread_signal_desc *psd = (struct pthread_signal_desc *)sd;
409         xpool_clear(&psd->waiters, 1);
410         return;
411 }
412
413 void * pthread_alloc_data(struct xseg *xseg)
414 {
415         struct xobject_h *sd_h = xseg_get_objh(xseg, MAGIC_PTHREAD_SD,
416                                 sizeof(struct pthread_signal_desc));
417         return sd_h;
418 }
419
420 void pthread_free_data(struct xseg *xseg, void *data)
421 {
422         if (data)
423                 xseg_put_objh(xseg, (struct xobject_h *)data);
424 }
425
426 void *pthread_alloc_signal_desc(struct xseg *xseg, void *data)
427 {
428         struct xobject_h *sd_h = (struct xobject_h *) data;
429         if (!sd_h)
430                 return NULL;
431         struct pthread_signal_desc *psd = xobj_get_obj(sd_h, X_ALLOC);
432         if (!psd)
433                 return NULL;
434         return psd;
435
436 }
437
438 void pthread_free_signal_desc(struct xseg *xseg, void *data, void *sd)
439 {
440         struct xobject_h *sd_h = (struct xobject_h *) data;
441         if (!sd_h)
442                 return;
443         if (sd)
444                 xobj_put_obj(sd_h, sd);
445         return;
446 }
447
448
449 static struct xseg_peer xseg_peer_pthread = {
450         /* xseg_peer_operations */
451         {
452                 .init_signal_desc   = pthread_init_signal_desc,
453                 .quit_signal_desc   = pthread_quit_signal_desc,
454                 .alloc_data         = pthread_alloc_data,
455                 .free_data          = pthread_free_data,
456                 .alloc_signal_desc  = pthread_alloc_signal_desc,
457                 .free_signal_desc   = pthread_free_signal_desc,
458                 .local_signal_init  = pthread_local_signal_init,
459                 .local_signal_quit  = pthread_local_signal_quit,
460                 .remote_signal_init = pthread_remote_signal_init,
461                 .remote_signal_quit = pthread_remote_signal_quit,
462                 .prepare_wait   = pthread_prepare_wait,
463                 .cancel_wait    = pthread_cancel_wait,
464                 .wait_signal    = pthread_wait_signal,
465                 .signal         = pthread_signal,
466                 .malloc         = pthread_malloc,
467                 .realloc        = pthread_realloc,
468                 .mfree          = pthread_mfree,
469         },
470         /* name */
471         "pthread"
472 };
473
474 void xseg_pthread_init(void)
475 {
476         xseg_register_type(&xseg_pthread);
477         xseg_register_peer(&xseg_peer_pthread);
478 }
479