1 7966faa9 2004-09-23 devnull #include <u.h>
2 7966faa9 2004-09-23 devnull #include <libc.h>
3 7966faa9 2004-09-23 devnull #include <thread.h>
4 7966faa9 2004-09-23 devnull #include "ioproc.h"
8 76193d7c 2003-09-30 devnull STACK = 8192,
12 76193d7c 2003-09-30 devnull iointerrupt(Ioproc *io)
14 76193d7c 2003-09-30 devnull if(!io->inuse)
16 76193d7c 2003-09-30 devnull threadint(io->tid);
19 76193d7c 2003-09-30 devnull static void
20 76193d7c 2003-09-30 devnull xioproc(void *a)
22 76193d7c 2003-09-30 devnull Ioproc *io, *x;
25 76193d7c 2003-09-30 devnull * first recvp acquires the ioproc.
26 76193d7c 2003-09-30 devnull * second tells us that the data is ready.
29 76193d7c 2003-09-30 devnull while(recv(io->c, &x) == -1)
31 76193d7c 2003-09-30 devnull if(x == 0) /* our cue to leave */
33 76193d7c 2003-09-30 devnull assert(x == io);
35 76193d7c 2003-09-30 devnull /* caller is now committed -- even if interrupted he'll return */
36 76193d7c 2003-09-30 devnull while(recv(io->creply, &x) == -1)
38 76193d7c 2003-09-30 devnull if(x == 0) /* caller backed out */
39 76193d7c 2003-09-30 devnull continue;
40 76193d7c 2003-09-30 devnull assert(x == io);
42 76193d7c 2003-09-30 devnull io->ret = io->op(&io->arg);
43 76193d7c 2003-09-30 devnull if(io->ret < 0)
44 76193d7c 2003-09-30 devnull rerrstr(io->err, sizeof io->err);
45 76193d7c 2003-09-30 devnull while(send(io->creply, &io) == -1)
47 76193d7c 2003-09-30 devnull while(recv(io->creply, &x) == -1)
53 76193d7c 2003-09-30 devnull ioproc(void)
55 76193d7c 2003-09-30 devnull Ioproc *io;
57 76193d7c 2003-09-30 devnull io = mallocz(sizeof(*io), 1);
58 76193d7c 2003-09-30 devnull if(io == nil)
59 76193d7c 2003-09-30 devnull sysfatal("ioproc malloc: %r");
60 76193d7c 2003-09-30 devnull io->c = chancreate(sizeof(void*), 0);
61 76193d7c 2003-09-30 devnull io->creply = chancreate(sizeof(void*), 0);
62 76193d7c 2003-09-30 devnull io->tid = proccreate(xioproc, io, STACK);
63 76193d7c 2003-09-30 devnull return io;
67 76193d7c 2003-09-30 devnull closeioproc(Ioproc *io)
69 76193d7c 2003-09-30 devnull if(io == nil)
71 76193d7c 2003-09-30 devnull iointerrupt(io);
72 76193d7c 2003-09-30 devnull while(send(io->c, 0) == -1)
74 76193d7c 2003-09-30 devnull chanfree(io->c);
75 76193d7c 2003-09-30 devnull chanfree(io->creply);
76 76193d7c 2003-09-30 devnull free(io);