2 * Copyright 2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
39 #include <sys/types.h>
42 #include <sys/syscall.h>
48 #include <xseg/xseg.h>
50 #include <drivers/xseg_pthread.h>
54 static void *pthread_malloc(uint64_t size);
55 static void pthread_mfree(void *mem);
57 static long pthread_allocate(const char *name, uint64_t size)
60 fd = shm_open(name, O_RDWR | O_CREAT, 0770);
62 XSEGLOG("Cannot create shared segment: %s\n",
63 strerror_r(errno, errbuf, ERRSIZE));
67 r = lseek(fd, size -1, SEEK_SET);
70 XSEGLOG("Cannot seek into segment file: %s\n",
71 strerror_r(errno, errbuf, ERRSIZE));
76 r = write(fd, errbuf, 1);
79 XSEGLOG("Failed to set segment size: %s\n",
80 strerror_r(errno, errbuf, ERRSIZE));
88 static long pthread_deallocate(const char *name)
90 return shm_unlink(name);
93 static void *pthread_map(const char *name, uint64_t size, struct xseg *seg)
99 XSEGLOG("struct xseg * is not NULL. Ignoring...\n");
101 fd = shm_open(name, O_RDWR, 0000);
103 XSEGLOG("Failed to open '%s' for mapping: %s\n",
104 name, strerror_r(errno, errbuf, ERRSIZE));
108 xseg = mmap ( XSEG_BASE_AS_PTR,
110 PROT_READ | PROT_WRITE,
111 MAP_SHARED | MAP_FIXED /* | MAP_LOCKED */,
114 if (xseg == MAP_FAILED) {
115 XSEGLOG("Could not map segment: %s\n",
116 strerror_r(errno, errbuf, ERRSIZE));
124 static void pthread_unmap(void *ptr, uint64_t size)
126 struct xseg *xseg = ptr;
127 (void)munmap(xseg, xseg->segment_size);
131 static void handler(int signum)
133 static unsigned long counter;
134 printf("%lu: signal %d: this shouldn't have happened.\n", counter, signum);
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;
144 static void keys_init(void)
148 r = pthread_key_create(&pid_key, NULL);
154 r = pthread_key_create(&xpidx_key, NULL);
159 r = pthread_key_create(&mask_key, NULL);
165 r = pthread_key_create(&act_key, NULL);
171 once_quit = PTHREAD_ONCE_INIT;
174 #define INT_TO_POINTER(__myptr, __myint) \
176 unsigned long __foo__ = (unsigned long) __myint; \
177 __myptr = (void *) __foo__ ; \
180 #define POINTER_TO_INT(__myint, __myptr)\
182 unsigned long __foo__ = (unsigned long) __myptr; \
183 __myint = (int) __foo__ ; \
186 /* must be called by each thread */
187 static int pthread_local_signal_init(struct xseg *xseg, xport portno)
192 sigset_t *savedset, *set;
193 struct sigaction *act, *old_act;
195 savedset = pthread_malloc(sizeof(sigset_t));
198 set = pthread_malloc(sizeof(sigset_t));
202 act = pthread_malloc(sizeof(struct sigaction));
205 old_act = pthread_malloc(sizeof(struct sigaction));
209 pthread_once(&once_init, keys_init);
214 act->sa_handler = handler;
217 if(sigaction(SIGIO, act, old_act) < 0)
221 sigaddset(set, SIGIO);
223 r = pthread_sigmask(SIG_BLOCK, set, savedset);
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))
238 pthread_sigmask(SIG_BLOCK, savedset, NULL);
240 sigaction(SIGIO, old_act, NULL);
242 pthread_mfree(old_act);
248 pthread_mfree(savedset);
253 /* should be called by each thread which had initialized signals */
254 static void pthread_local_signal_quit(struct xseg *xseg, xport portno)
257 struct sigaction *old_act;
259 savedset = pthread_getspecific(act_key);
260 old_act = pthread_getspecific(mask_key);
262 sigaction(SIGIO, old_act, NULL);
264 pthread_sigmask(SIG_SETMASK, savedset, NULL);
267 static int pthread_remote_signal_init(void)
272 static void pthread_remote_signal_quit(void)
277 static int pthread_prepare_wait(struct xseg *xseg, uint32_t portno)
282 struct xseg_port *port = xseg_get_port(xseg, portno);
285 struct pthread_signal_desc *psd = xseg_get_signal_desc(xseg, port);
289 tmp = pthread_getspecific(pid_key);
290 POINTER_TO_INT(pid, tmp);
294 r = xpool_add(&psd->waiters, (xpool_index) pid, portno);
297 pthread_setspecific(xpidx_key, (void *)r);
301 static int pthread_cancel_wait(struct xseg *xseg, uint32_t portno)
306 xpool_index xpidx, r;
307 struct xseg_port *port = xseg_get_port(xseg, portno);
310 struct pthread_signal_desc *psd = xseg_get_signal_desc(xseg, port);
314 tmp = pthread_getspecific(pid_key);
315 POINTER_TO_INT(pid, tmp);
319 xpidx = (xpool_index) pthread_getspecific(xpidx_key);
321 r = xpool_remove(&psd->waiters, xpidx, &data, portno);
328 static int pthread_wait_signal(struct xseg *xseg, uint32_t usec_timeout)
335 sigaddset(&set, SIGIO);
337 ts.tv_sec = usec_timeout / 1000000;
338 ts.tv_nsec = 1000 * (usec_timeout - ts.tv_sec * 1000000);
340 r = sigtimedwait(&set, &siginfo, &ts);
344 return siginfo.si_signo;
347 static int pthread_signal(struct xseg *xseg, uint32_t portno)
352 struct xseg_port *port = xseg_get_port(xseg, portno);
355 struct pthread_signal_desc *psd = xseg_get_signal_desc(xseg, port);
359 idx = xpool_peek(&psd->waiters, &data, portno); //FIXME portno is not the caller but the callee
363 pid_t cue = (pid_t) data;
367 return syscall(SYS_tkill, cue, SIGIO);
370 static void *pthread_malloc(uint64_t size)
372 return malloc((size_t)size);
375 static void *pthread_realloc(void *mem, uint64_t size)
377 return realloc(mem, (size_t)size);
380 static void pthread_mfree(void *mem)
385 static struct xseg_type xseg_pthread = {
386 /* xseg_operations */
388 .mfree = pthread_mfree,
389 .allocate = pthread_allocate,
390 .deallocate = pthread_deallocate,
392 .unmap = pthread_unmap,
398 int pthread_init_signal_desc(struct xseg *xseg, void *sd)
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);
406 void pthread_quit_signal_desc(struct xseg *xseg, void *sd)
408 struct pthread_signal_desc *psd = (struct pthread_signal_desc *)sd;
409 xpool_clear(&psd->waiters, 1);
413 void * pthread_alloc_data(struct xseg *xseg)
415 struct xobject_h *sd_h = xseg_get_objh(xseg, MAGIC_PTHREAD_SD,
416 sizeof(struct pthread_signal_desc));
420 void pthread_free_data(struct xseg *xseg, void *data)
423 xseg_put_objh(xseg, (struct xobject_h *)data);
426 void *pthread_alloc_signal_desc(struct xseg *xseg, void *data)
428 struct xobject_h *sd_h = (struct xobject_h *) data;
431 struct pthread_signal_desc *psd = xobj_get_obj(sd_h, X_ALLOC);
438 void pthread_free_signal_desc(struct xseg *xseg, void *data, void *sd)
440 struct xobject_h *sd_h = (struct xobject_h *) data;
444 xobj_put_obj(sd_h, sd);
449 static struct xseg_peer xseg_peer_pthread = {
450 /* xseg_peer_operations */
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,
474 void xseg_pthread_init(void)
476 xseg_register_type(&xseg_pthread);
477 xseg_register_peer(&xseg_peer_pthread);