8 #include <sys/syscall.h>
13 #include <xseg/xseg.h>
19 static long posix_allocate(const char *name, uint64_t size)
22 fd = shm_open(name, O_RDWR | O_CREAT, 0770);
24 LOGMSG("Cannot create shared segment: %s\n",
25 strerror_r(errno, errbuf, ERRSIZE));
29 r = lseek(fd, size -1, SEEK_SET);
32 LOGMSG("Cannot seek into segment file: %s\n",
33 strerror_r(errno, errbuf, ERRSIZE));
38 r = write(fd, errbuf, 1);
41 LOGMSG("Failed to set segment size: %s\n",
42 strerror_r(errno, errbuf, ERRSIZE));
50 static long posix_deallocate(const char *name)
52 return shm_unlink(name);
55 static void *posix_map(const char *name, uint64_t size)
59 fd = shm_open(name, O_RDWR, 0000);
61 LOGMSG("Failed to open '%s' for mapping: %s\n",
62 name, strerror_r(errno, errbuf, ERRSIZE));
66 xseg = mmap ( XSEG_BASE_AS_PTR,
68 PROT_READ | PROT_WRITE,
69 MAP_SHARED | MAP_FIXED /* | MAP_LOCKED */,
72 if (xseg == MAP_FAILED) {
73 LOGMSG("Could not map segment: %s\n",
74 strerror_r(errno, errbuf, ERRSIZE));
82 static void posix_unmap(void *ptr, uint64_t size)
84 struct xseg *xseg = ptr;
85 (void)munmap(xseg, xseg->segment_size);
89 static void handler(int signum)
91 static unsigned long counter;
92 printf("%lu: signal %d: this shouldn't have happend.\n", counter, signum);
96 static sigset_t savedset, set;
99 static int posix_signal_init(void)
103 h = signal(SIGIO, handler);
108 sigaddset(&set, SIGIO);
110 r = sigprocmask(SIG_BLOCK, &set, &savedset);
114 pid = syscall(SYS_gettid);
118 static void posix_signal_quit(void)
120 signal(SIGIO, SIG_DFL);
121 sigprocmask(SIG_SETMASK, &savedset, NULL);
124 static int posix_prepare_wait(struct xseg_port *port)
130 static int posix_cancel_wait(struct xseg_port *port)
136 static int posix_wait_signal(struct xseg_port *port, uint32_t usec_timeout)
142 ts.tv_sec = usec_timeout / 1000000;
143 ts.tv_nsec = 1000 * (usec_timeout - ts.tv_sec * 1000000);
145 r = sigtimedwait(&set, &siginfo, &ts);
149 return siginfo.si_signo;
152 static int posix_signal(struct xseg_port *port)
154 union sigval sigval = {0};
155 pid_t cue = (pid_t)port->waitcue;
158 sigqueue(cue, SIGIO, sigval);
159 /* XXX: on error what? */
163 static void *posix_malloc(uint64_t size)
165 return malloc((size_t)size);
168 static void *posix_realloc(void *mem, uint64_t size)
170 return realloc(mem, (size_t)size);
173 static void posix_mfree(void *mem)
178 static struct xseg_type xseg_posix = {
179 /* xseg_operations */
181 .malloc = posix_malloc,
182 .realloc = posix_realloc,
183 .mfree = posix_mfree,
184 .allocate = posix_allocate,
185 .deallocate = posix_deallocate,
187 .unmap = posix_unmap,
193 static struct xseg_peer xseg_peer_posix = {
194 /* xseg_peer_operations */
196 .signal_init = posix_signal_init,
197 .signal_quit = posix_signal_quit,
198 .prepare_wait = posix_prepare_wait,
199 .cancel_wait = posix_cancel_wait,
200 .wait_signal = posix_wait_signal,
201 .signal = posix_signal,
202 .malloc = posix_malloc,
203 .realloc = posix_realloc,
204 .mfree = posix_mfree,
210 void xseg_posix_init(void)
212 xseg_register_type(&xseg_posix);
213 xseg_register_peer(&xseg_peer_posix);