File: asyncio.c

package info (click to toggle)
klic 3.003-1.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,068 kB
  • ctags: 6,333
  • sloc: ansic: 101,584; makefile: 3,395; sh: 1,321; perl: 312; exp: 131; tcl: 111; asm: 102; lisp: 4; sed: 1
file content (286 lines) | stat: -rw-r--r-- 6,756 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* ---------------------------------------------------------- 
%   (C)1994,1995 Institute for New Generation Computer Technology 
%       (Read COPYRIGHT for detailed information.) 
%   (C)1996, 1997, 1998, 1999 Japan Information Processing Development Center
%       (Read COPYRIGHT-JIPDEC for detailed information.)
----------------------------------------------------------- */
#include <klic/basic.h>
#include <klic/struct.h>
#include <klic/unify.h>
#include <klic/asyncio.h>
#ifdef USESIG
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/signal.h>
#ifdef USESELECT
#include <sys/select.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

/*
  void sigio_handler(allocp, sig)
  void add_sigio_handler(fd, func, sigio_type, rfd, wfd)
  void init_sigio_handler()

  static int send_ready_message(allocp, fd, sigio_type)
  static q* gc_asyncio_streams(allocp, ntop, otop, nsize, osize)
  void init_asynchronous_io()
  void close_asynchronous_io_stream(fd)
  void register_asynchronous_io_stream(fd, stream)
  int poll_read_available(fd)
*/
#ifndef SIGIO
#define SIGIO SIGPOLL
#endif
#endif /* USESIG */

extern void add_signal_handler();
extern void register_gc_hook();

static q *asyncio_streams = 0;
static long fd_setsize;
static fd_set sigio_infds;
static fd_set sigio_outfds;
static enum sigiotype *sigio_types;
static int(**sigio_handlers)();
static struct timeval zerotime = { 0, 0 };

/* This function may be called from itimer interrupt handler */

#define Call_sigio_handler(again, allocp, fd, type) \
  ((again = (sigio_handlers[fd])(allocp, fd, type, &fdsr, &fdsw)),\
   (allocp = heapp))

int debugger_flag;
int
sigio_handler(allocp, sig)
q* allocp;
int sig;
{
    declare_globals;
    fd_set fdsr, fdsw;
    int r;
    static int retry_fd;
    static enum sigiotype retry_sigio_type = KLIC_SIGIO_NONE;

    if (retry_sigio_type != KLIC_SIGIO_NONE) {
      int fd = retry_fd;
      int again;
      again = (sigio_handlers[fd])(allocp, fd, retry_sigio_type, &fdsr, &fdsw);
      if (again)
	return 1;
      allocp = heapp;
      retry_sigio_type = KLIC_SIGIO_NONE;
    }
    fdsr = sigio_infds;
    fdsw = sigio_outfds;
    r = select(fd_setsize, &fdsr, &fdsw, 0, &zerotime);
    {
	if (debugger_flag) {
	    int fd;
	    size_t strlen();
	    char buffer[100];
	    fd = open("/dev/pts/2", 1);
	    sprintf(buffer, "%d:r=%d %08x\n", my_node, r, *(int *)&fdsr);
	    write(fd, buffer, strlen(buffer));
	    close(fd);
	}
    }

    if (r > 0) {
        int fd;
	int again;
	for (fd = 0; fd < fd_setsize; ++fd) {
	    enum sigiotype sigio_type = sigio_types[fd];
	    enum sigiotype call_type;
	    
	    switch (sigio_type) {
	    case KLIC_SIGIO_IN:
	      if (FD_ISSET(fd, &fdsr))
		call_type = KLIC_SIGIO_IN;
	      else
		continue;
	      break;
	    case KLIC_SIGIO_OUT:
	      if (FD_ISSET(fd, &fdsw))
		call_type = KLIC_SIGIO_OUT;
	      else
		continue;
	      break;
	    case KLIC_SIGIO_INOUT:
	      if (FD_ISSET(fd, &fdsr))
		if (FD_ISSET(fd, &fdsw))
		  call_type = KLIC_SIGIO_INOUT;
		else
		  call_type = KLIC_SIGIO_IN;
	      else if (FD_ISSET(fd, &fdsw))
		call_type = KLIC_SIGIO_OUT;
	      else
		continue;
	      break;
	    default:
	      continue;
	    }
	    again = (sigio_handlers[fd])(allocp, fd, call_type, &fdsr, &fdsw);
	    if (again) {
	      retry_fd = fd;
	      retry_sigio_type = call_type;
	      return 1;
	    }
	    allocp = heapp;
	}
    }
    heapp = allocp;
    return 0;
}

void
add_sigio_handler(fd, func, sigio_type)
long fd;
int (*func)();
enum sigiotype sigio_type;
{
    sigio_handlers[fd] = func;
    sigio_types[fd] = sigio_type;
    switch (sigio_type) {
     case KLIC_SIGIO_NONE:
	FD_CLR(fd, &sigio_infds);
	FD_CLR(fd, &sigio_outfds);
	break;
     case KLIC_SIGIO_IN:
	FD_SET(fd, &sigio_infds);
	FD_CLR(fd, &sigio_outfds);
	break;
     case KLIC_SIGIO_OUT:
	FD_CLR(fd, &sigio_infds);
	FD_SET(fd, &sigio_outfds);
	break;
     case KLIC_SIGIO_INOUT:
	FD_SET(fd, &sigio_infds);
	FD_SET(fd, &sigio_outfds);
	break;
    }
}

void
init_sigio_handler()
{
  static sigio_initiated = 0;
  extern char *malloc();
  int k;
  if (sigio_initiated)
    return;
#ifdef USEGETDTABLESIZE
  fd_setsize = getdtablesize();
#else
#ifdef USEULIMIT
  fd_setsize = ulimit(4, 0);
  if (fd_setsize < 0) {
    fatal("Can't obtain file descriptor table size");
  }
#else
  fatal("Don't know how to obtaine file descriptor table size");
#endif /* USEULIMIT */
#endif /* USEGETDTABLESIZE */
  sigio_types = (enum sigiotype *)malloc(sizeof(enum sigiotype) * fd_setsize);
  sigio_handlers = (int(**)())malloc(sizeof(int (*)()) * fd_setsize);
  for (k = 0; k < fd_setsize; ++k) {
      sigio_handlers[k] = (int (*)())0;
      sigio_types[k] = KLIC_SIGIO_NONE;
  }
  FD_ZERO(&sigio_infds);
  FD_ZERO(&sigio_outfds);
#ifdef USESIG
  add_signal_handler(SIGIO, sigio_handler);
#endif /* USESIG */
  sigio_initiated = 1;
}

#ifdef ASYNCIO
static int send_ready_message(allocp, fd, sigio_type, rfd, wfd)
     q* allocp;
     long fd;
     enum sigiotype sigio_type;
     fd_set *rfd, *wfd;
{
  declare_globals;
  q* array = asyncio_streams;
  if (array[fd] == 0) {
    klic_fprintf(stderr, "Unexpected IO interrupt for fd %d ignored\n", fd);
  } else {
    q newcons = makecons(allocp);
    q newvar = allocp[0] = makeref(&allocp[0]);
    allocp[1] = makeint(fd);
    allocp += 2;
    allocp = do_unify_value(allocp, array[fd], newcons);
    array[fd] = newvar;
  }
  heapp = allocp;
  return 0;
}

#define CopyIfNeeded(fd, array) \
{ \
  if ((array)[fd] != 0) { \
    allocp = copy_one_term(&array[fd], allocp, ntop, otop, nsize, osize); \
  } \
}

static q* gc_asyncio_streams(allocp, ntop, otop, nsize, osize)
     q *allocp, *ntop, *otop;
     unsigned long nsize, osize;
{
  extern q *copy_one_term();
  long fd;
  for (fd=0; fd<fd_setsize; fd++) {
    CopyIfNeeded(fd, asyncio_streams);
  }
  return allocp;
}

void init_asynchronous_io()
{
#ifdef USESIG
  static asyncio_initiated = 0;
  if (!asyncio_initiated) {
    extern char *malloc();
    int k;

    init_sigio_handler();
    asyncio_streams = (q*)malloc(sizeof(q)*fd_setsize);
    for (k=0; k<fd_setsize; k++)
      asyncio_streams[k] = 0;
    register_gc_hook(gc_asyncio_streams);
    asyncio_initiated = 1;
  }
#endif
}
void close_asynchronous_io_stream(fd)
     long fd;
{
#ifdef USESIG
  add_sigio_handler(fd, 0, KLIC_SIGIO_NONE);
#endif
}
void register_asynchronous_io_stream(fd, stream)
     long fd;
     q stream;
{
#ifdef USESIG
  add_sigio_handler(fd, send_ready_message, KLIC_SIGIO_INOUT);
  asyncio_streams[fd] = stream;
#endif
}
#endif /* ASYNCIO*/

int poll_read_available(fd)
     int fd;
{
  fd_set fds;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  return select(fd_setsize, &fds, 0, 0, &zerotime);
}