. pci driver now returns devices, even when they have been pci_reserve()d
[minix3.git] / lib / other / asynchio.c
blobdf6eb6ea557a635b4cae6ffed7ea548bc4d17e8a
1 /* asyn_init(), asyn_read(), asyn_write(), asyn_ioctl(),
2 * asyn_wait(), asyn_synch(), asyn_close()
3 * Author: Kees J. Bot
4 * 26 Jan 1995
5 * Thise are just stub routines that are call compatible with
6 * the asynchio(3) library of Minix-vmd. See asynchio.h.
7 */
8 #define nil 0
9 #define alarm _alarm
10 #define ioctl _ioctl
11 #define read _read
12 #define sigaction _sigaction
13 #define sigfillset _sigfillset
14 #define time _time
15 #define write _write
16 #include <lib.h>
17 #include <time.h>
18 #include <sys/ioctl.h>
19 #include <sys/asynchio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <signal.h>
25 #define IO_IDLE 0
26 #define IO_INPROGRESS 1
27 #define IO_RESULT 2
29 #define OP_NOOP 0
30 #define OP_READ 1
31 #define OP_WRITE 2
32 #define OP_IOCTL 3
34 static asynchio_t *asyn_current;
36 void asyn_init(asynchio_t *asyn)
38 asyn->state= IO_IDLE;
39 asyn->op= OP_NOOP;
42 static ssize_t operation(int op, asynchio_t *asyn, int fd, int req,
43 void *data, ssize_t count)
45 switch (asyn->state) {
46 case IO_INPROGRESS:
47 if (asyn_current != asyn && asyn->op != op) abort();
48 /*FALL THROUGH*/
49 case IO_IDLE:
50 asyn_current= asyn;
51 asyn->op= op;
52 asyn->fd= fd;
53 asyn->req= req;
54 asyn->data= data;
55 asyn->count= count;
56 asyn->state= IO_INPROGRESS;
57 errno= EINPROGRESS;
58 return -1;
59 case IO_RESULT:
60 if (asyn_current != asyn && asyn->op != op) abort();
61 errno= asyn->errno;
62 return asyn->count;
66 ssize_t asyn_read(asynchio_t *asyn, int fd, void *buf, size_t len)
68 return operation(OP_READ, asyn, fd, 0, buf, len);
71 ssize_t asyn_write(asynchio_t *asyn, int fd, const void *buf, size_t len)
73 return operation(OP_WRITE, asyn, fd, 0, (void *) buf, len);
76 int asyn_ioctl(asynchio_t *asyn, int fd, unsigned long request, void *data)
78 return operation(OP_IOCTL, asyn, fd, request, data, 0);
81 static void time_out(int sig)
83 alarm(1);
86 int asyn_wait(asynchio_t *asyn, int flags, struct timeval *to)
88 time_t now;
89 unsigned old_timer, new_timer;
90 struct sigaction old_sa, new_sa;
92 if (asyn_current != asyn) abort();
93 if (flags & ASYN_NONBLOCK) abort();
95 if (asyn->state == IO_RESULT) {
96 asyn->state= IO_IDLE;
97 asyn->op= OP_NOOP;
98 return 0;
101 if (to != nil) {
102 now= time(nil);
103 if (to->tv_sec <= now) { errno= EINTR; return -1; }
104 old_timer= alarm(0);
105 new_sa.sa_handler= time_out;
106 sigfillset(&new_sa.sa_mask);
107 new_sa.sa_flags= 0;
108 sigaction(SIGALRM, &new_sa, &old_sa);
109 new_timer= to->tv_sec - now;
110 if (new_timer < old_timer) {
111 new_timer= old_timer;
113 alarm(new_timer);
115 switch (asyn->op) {
116 case OP_NOOP:
117 asyn->count= pause();
118 asyn->errno= errno;
119 case OP_READ:
120 asyn->count= read(asyn->fd, asyn->data, asyn->count);
121 asyn->errno= errno;
122 break;
123 case OP_WRITE:
124 asyn->count= write(asyn->fd, asyn->data, asyn->count);
125 asyn->errno= errno;
126 break;
127 case OP_IOCTL:
128 asyn->count= ioctl(asyn->fd, asyn->req, asyn->data);
129 asyn->errno= errno;
130 break;
132 if (to != nil) {
133 alarm(0);
134 sigaction(SIGALRM, &old_sa, (struct sigaction *)0);
135 alarm(old_timer);
138 if (asyn->count == -1 && asyn->errno == EINTR) {
139 errno= EINTR;
140 return -1;
141 } else {
142 asyn->state= IO_RESULT;
143 return 0;
147 int asyn_synch(asynchio_t *asyn, int fd)
151 int asyn_close(asynchio_t *asyn, int fd)
153 asyn_init(asyn);