1 76193d7c 2003-09-30 devnull #include "threadimpl.h"
5 76193d7c 2003-09-30 devnull STACK = 8192,
9 76193d7c 2003-09-30 devnull iointerrupt(Ioproc *io)
11 76193d7c 2003-09-30 devnull if(!io->inuse)
13 76193d7c 2003-09-30 devnull threadint(io->tid);
16 76193d7c 2003-09-30 devnull static void
17 76193d7c 2003-09-30 devnull xioproc(void *a)
19 76193d7c 2003-09-30 devnull Ioproc *io, *x;
22 76193d7c 2003-09-30 devnull * first recvp acquires the ioproc.
23 76193d7c 2003-09-30 devnull * second tells us that the data is ready.
26 76193d7c 2003-09-30 devnull while(recv(io->c, &x) == -1)
28 76193d7c 2003-09-30 devnull if(x == 0) /* our cue to leave */
30 76193d7c 2003-09-30 devnull assert(x == io);
32 76193d7c 2003-09-30 devnull /* caller is now committed -- even if interrupted he'll return */
33 76193d7c 2003-09-30 devnull while(recv(io->creply, &x) == -1)
35 76193d7c 2003-09-30 devnull if(x == 0) /* caller backed out */
36 76193d7c 2003-09-30 devnull continue;
37 76193d7c 2003-09-30 devnull assert(x == io);
39 76193d7c 2003-09-30 devnull io->ret = io->op(&io->arg);
40 76193d7c 2003-09-30 devnull if(io->ret < 0)
41 76193d7c 2003-09-30 devnull rerrstr(io->err, sizeof io->err);
42 76193d7c 2003-09-30 devnull while(send(io->creply, &io) == -1)
44 76193d7c 2003-09-30 devnull while(recv(io->creply, &x) == -1)
50 76193d7c 2003-09-30 devnull ioproc(void)
52 76193d7c 2003-09-30 devnull Ioproc *io;
54 76193d7c 2003-09-30 devnull io = mallocz(sizeof(*io), 1);
55 76193d7c 2003-09-30 devnull if(io == nil)
56 76193d7c 2003-09-30 devnull sysfatal("ioproc malloc: %r");
57 76193d7c 2003-09-30 devnull io->c = chancreate(sizeof(void*), 0);
58 76193d7c 2003-09-30 devnull io->creply = chancreate(sizeof(void*), 0);
59 76193d7c 2003-09-30 devnull io->tid = proccreate(xioproc, io, STACK);
60 76193d7c 2003-09-30 devnull return io;
64 76193d7c 2003-09-30 devnull closeioproc(Ioproc *io)
66 76193d7c 2003-09-30 devnull if(io == nil)
68 76193d7c 2003-09-30 devnull iointerrupt(io);
69 76193d7c 2003-09-30 devnull while(send(io->c, 0) == -1)
71 76193d7c 2003-09-30 devnull chanfree(io->c);
72 76193d7c 2003-09-30 devnull chanfree(io->creply);
73 76193d7c 2003-09-30 devnull free(io);