8 #include <sys/syscall.h>
14 #include <xseg/xseg.h>
20 static void *pthread_malloc(uint64_t size);
21 static void pthread_mfree(void *mem);
23 static long pthread_allocate(const char *name, uint64_t size)
26 fd = shm_open(name, O_RDWR | O_CREAT, 0770);
28 XSEGLOG("Cannot create shared segment: %s\n",
29 strerror_r(errno, errbuf, ERRSIZE));
33 r = lseek(fd, size -1, SEEK_SET);
36 XSEGLOG("Cannot seek into segment file: %s\n",
37 strerror_r(errno, errbuf, ERRSIZE));
42 r = write(fd, errbuf, 1);
45 XSEGLOG("Failed to set segment size: %s\n",
46 strerror_r(errno, errbuf, ERRSIZE));
54 static long pthread_deallocate(const char *name)
56 return shm_unlink(name);
59 static void *pthread_map(const char *name, uint64_t size, struct xseg *seg)
65 XSEGLOG("struct xseg * is not NULL. Ignoring...\n");
67 fd = shm_open(name, O_RDWR, 0000);
69 XSEGLOG("Failed to open '%s' for mapping: %s\n",
70 name, strerror_r(errno, errbuf, ERRSIZE));
74 xseg = mmap ( XSEG_BASE_AS_PTR,
76 PROT_READ | PROT_WRITE,
77 MAP_SHARED | MAP_FIXED /* | MAP_LOCKED */,
80 if (xseg == MAP_FAILED) {
81 XSEGLOG("Could not map segment: %s\n",
82 strerror_r(errno, errbuf, ERRSIZE));
90 static void pthread_unmap(void *ptr, uint64_t size)
92 struct xseg *xseg = ptr;
93 (void)munmap(xseg, xseg->segment_size);
97 static void handler(int signum)
99 static unsigned long counter;
100 printf("%lu: signal %d: this shouldn't have happened.\n", counter, signum);
104 static pthread_key_t pid_key, xpidx_key;
105 static pthread_key_t mask_key, act_key;
106 static pthread_once_t once_init = PTHREAD_ONCE_INIT;
107 static pthread_once_t once_quit = PTHREAD_ONCE_INIT;
110 static void keys_init(void)
114 r = pthread_key_create(&pid_key, NULL);
120 r = pthread_key_create(&xpidx_key, NULL);
125 r = pthread_key_create(&mask_key, NULL);
131 r = pthread_key_create(&act_key, NULL);
137 once_quit = PTHREAD_ONCE_INIT;
140 #define INT_TO_POINTER(__myptr, __myint) \
142 unsigned long __foo__ = (unsigned long) __myint; \
143 __myptr = (void *) __foo__ ; \
146 #define POINTER_TO_INT(__myint, __myptr)\
148 unsigned long __foo__ = (unsigned long) __myptr; \
149 __myint = (int) __foo__ ; \
152 /* must be called by each thread */
153 static int pthread_local_signal_init(void)
158 sigset_t *savedset, *set;
159 struct sigaction *act, *old_act;
161 savedset = pthread_malloc(sizeof(sigset_t));
164 set = pthread_malloc(sizeof(sigset_t));
168 act = pthread_malloc(sizeof(struct sigaction));
171 old_act = pthread_malloc(sizeof(struct sigaction));
175 pthread_once(&once_init, keys_init);
180 act->sa_handler = handler;
183 if(sigaction(SIGIO, act, old_act) < 0)
187 sigaddset(set, SIGIO);
189 r = pthread_sigmask(SIG_BLOCK, set, savedset);
194 pid = syscall(SYS_gettid);
195 INT_TO_POINTER(tmp, pid);
196 if (!pthread_setspecific(pid_key, tmp) ||
197 pthread_setspecific(mask_key, savedset) ||
198 pthread_setspecific(act_key, old_act))
204 pthread_sigmask(SIG_BLOCK, savedset, NULL);
206 sigaction(SIGIO, old_act, NULL);
208 pthread_mfree(old_act);
214 pthread_mfree(savedset);
219 /* should be called by each thread which had initialized signals */
220 static void pthread_local_signal_quit(void)
223 struct sigaction *old_act;
225 savedset = pthread_getspecific(act_key);
226 old_act = pthread_getspecific(mask_key);
228 sigaction(SIGIO, old_act, NULL);
230 pthread_sigmask(SIG_SETMASK, savedset, NULL);
233 static int pthread_remote_signal_init(void)
238 static void pthread_remote_signal_quit(void)
243 static int pthread_prepare_wait(struct xseg *xseg, uint32_t portno)
248 struct xseg_port *port = xseg_get_port(xseg, portno);
252 tmp = pthread_getspecific(pid_key);
253 POINTER_TO_INT(pid, tmp);
257 r = xpool_add(&port->waiters, (xpool_index) pid, portno);
260 pthread_setspecific(xpidx_key, (void *)r);
264 static int pthread_cancel_wait(struct xseg *xseg, uint32_t portno)
269 xpool_index xpidx, r;
270 struct xseg_port *port = xseg_get_port(xseg, portno);
274 tmp = pthread_getspecific(pid_key);
275 POINTER_TO_INT(pid, tmp);
279 xpidx = (xpool_index) pthread_getspecific(xpidx_key);
281 r = xpool_remove(&port->waiters, xpidx, &data, portno);
288 static int pthread_wait_signal(struct xseg *xseg, uint32_t usec_timeout)
295 sigaddset(&set, SIGIO);
297 ts.tv_sec = usec_timeout / 1000000;
298 ts.tv_nsec = 1000 * (usec_timeout - ts.tv_sec * 1000000);
300 r = sigtimedwait(&set, &siginfo, &ts);
304 return siginfo.si_signo;
307 static int pthread_signal(struct xseg *xseg, uint32_t portno)
312 struct xseg_port *port = xseg_get_port(xseg, portno);
316 idx = xpool_peek(&port->waiters, &data, portno); //FIXME portno is not the caller but the callee
320 pid_t cue = (pid_t) data;
324 return syscall(SYS_tkill, cue, SIGIO);
327 static void *pthread_malloc(uint64_t size)
329 return malloc((size_t)size);
332 static void *pthread_realloc(void *mem, uint64_t size)
334 return realloc(mem, (size_t)size);
337 static void pthread_mfree(void *mem)
342 static struct xseg_type xseg_pthread = {
343 /* xseg_operations */
345 .mfree = pthread_mfree,
346 .allocate = pthread_allocate,
347 .deallocate = pthread_deallocate,
349 .unmap = pthread_unmap,
355 static struct xseg_peer xseg_peer_pthread = {
356 /* xseg_peer_operations */
358 .local_signal_init = pthread_local_signal_init,
359 .local_signal_quit = pthread_local_signal_quit,
360 .remote_signal_init = pthread_remote_signal_init,
361 .remote_signal_quit = pthread_remote_signal_quit,
362 .prepare_wait = pthread_prepare_wait,
363 .cancel_wait = pthread_cancel_wait,
364 .wait_signal = pthread_wait_signal,
365 .signal = pthread_signal,
366 .malloc = pthread_malloc,
367 .realloc = pthread_realloc,
368 .mfree = pthread_mfree,
374 void xseg_pthread_init(void)
376 xseg_register_type(&xseg_pthread);
377 xseg_register_peer(&xseg_peer_pthread);