]>
Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* |
2 | * (c) Copyright 1990-1996 OPEN SOFTWARE FOUNDATION, INC. | |
3 | * (c) Copyright 1990-1996 HEWLETT-PACKARD COMPANY | |
4 | * (c) Copyright 1990-1996 DIGITAL EQUIPMENT CORPORATION | |
5 | * (c) Copyright 1991, 1992 Siemens-Nixdorf Information Systems | |
6 | * To anyone who acknowledges that this file is provided "AS IS" without | |
7 | * any express or implied warranty: permission to use, copy, modify, and | |
8 | * distribute this file for any purpose is hereby granted without fee, | |
9 | * provided that the above copyright notices and this notice appears in | |
10 | * all source code copies, and that none of the names listed above be used | |
11 | * in advertising or publicity pertaining to distribution of the software | |
12 | * without specific, written prior permission. None of these organizations | |
13 | * makes any representations about the suitability of this software for | |
14 | * any purpose. | |
15 | */ | |
16 | /* | |
17 | * Header file for thread synchrounous I/O | |
18 | */ | |
19 | ||
20 | #ifndef CMA_THREAD_IO | |
21 | #define CMA_THREAD_IO | |
22 | ||
23 | /* | |
24 | * INCLUDE FILES | |
25 | */ | |
26 | ||
27 | #include <cma_config.h> | |
28 | #include <sys/file.h> | |
29 | #include <cma.h> | |
30 | #include <sys/types.h> | |
31 | #include <sys/time.h> | |
32 | #include <cma_init.h> | |
33 | #include <cma_errors.h> | |
34 | ||
35 | /* | |
36 | * CONSTANTS | |
37 | */ | |
38 | ||
39 | /* | |
40 | * Define symbols which indicate whether to compile code for obsolete | |
41 | * "non-blocking mode" flags: FNDELAY and FNBLOCK. If the obsolete | |
42 | * symbols are defined, and if their replacement symbols are defined | |
43 | * and are different or if they are undefined, then define a symbol | |
44 | * that says to compile the code in; otherwise no code will be compiled | |
45 | * for these obsolete symbols. | |
46 | */ | |
47 | #ifdef FNDELAY | |
48 | # ifdef O_NDELAY | |
49 | # if O_NDELAY != FNDELAY | |
50 | # define _CMA_FNDELAY_ | |
51 | # endif | |
52 | # else | |
53 | # define _CMA_FNDELAY_ | |
54 | # endif | |
55 | #endif | |
56 | ||
57 | #ifdef FNBLOCK | |
58 | # ifdef O_NONBLOCK | |
59 | # if O_NONBLOCK != FNBLOCK | |
60 | # define _CMA_FNBLOCK_ | |
61 | # endif | |
62 | # else | |
63 | # define _CMA_FNBLOCK_ | |
64 | # endif | |
65 | #endif | |
66 | ||
67 | ||
68 | extern cma_t_boolean cma_is_open(int); | |
69 | /* | |
70 | * Maximum number of files (ie, max_fd+1) | |
71 | */ | |
72 | #define cma__c_mx_file FD_SETSIZE | |
73 | ||
74 | /* | |
75 | * Number of bits per file descriptor bit mask (ie number of bytes * bits/byte) | |
76 | */ | |
77 | #define cma__c_nbpm NFDBITS | |
78 | ||
79 | /* | |
80 | * TYPE DEFINITIONS | |
81 | */ | |
82 | ||
83 | typedef enum CMA__T_IO_TYPE { | |
84 | cma__c_io_read = 0, | |
85 | cma__c_io_write = 1, | |
86 | cma__c_io_except = 2 | |
87 | } cma__t_io_type; | |
88 | #define cma__c_max_io_type 2 | |
89 | ||
90 | /* | |
91 | * From our local <sys/types.h>: | |
92 | * | |
93 | * typedef long fd_mask; | |
94 | * | |
95 | * typedef struct fd_set { | |
96 | * fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)]; | |
97 | * } fd_set; | |
98 | * | |
99 | */ | |
100 | typedef fd_mask cma__t_mask; | |
101 | typedef fd_set cma__t_file_mask; | |
102 | ||
103 | ||
104 | /* | |
105 | * GLOBAL DATA | |
106 | */ | |
107 | ||
108 | /* | |
109 | * Maximum number of files (ie, max_fd+1) as determined by getdtablesize(). | |
110 | */ | |
111 | extern int cma__g_mx_file; | |
112 | ||
113 | /* | |
114 | * Number of submasks (ie "int" sized chunks) per file descriptor mask as | |
115 | * determined by getdtablesize(). | |
116 | */ | |
117 | extern int cma__g_nspm; | |
118 | ||
119 | /* | |
120 | * MACROS | |
121 | */ | |
122 | ||
123 | /* | |
124 | * Define a constant for the errno value which indicates that the requested | |
125 | * operation was not performed because it would block the process. | |
126 | */ | |
127 | # define cma__is_blocking(s) \ | |
128 | ((s == EAGAIN) || (s == EWOULDBLOCK) || (s == EINPROGRESS) || \ | |
129 | (s == EALREADY) || (s == EDEADLK)) | |
130 | ||
131 | /* | |
132 | * It is necessary to issue an I/O function, before calling cma__io_wait() | |
133 | * in the following cases: | |
134 | * | |
135 | * * This file descriptor has been set non-blocking by CMA | |
136 | * * This file descriptor has been set non-blocking by the user. | |
137 | */ | |
138 | ||
139 | #define cma__issue_io_call(fd) \ | |
140 | ( (cma__g_file[fd]->non_blocking) || \ | |
141 | (cma__g_file[fd]->user_fl.user_non_blocking) ) | |
142 | ||
143 | ||
144 | #define cma__set_user_nonblocking(flags) \ | |
145 | ||
146 | /* | |
147 | * Determine if the file is open | |
148 | */ | |
149 | /* | |
150 | * If the file gets closed while waiting for the mutex cma__g_file[rfd] | |
151 | * gets set to null. This results in a crash if NDEBUG is set to 0 | |
152 | * since cma__int_lock tries to dereference it to set the mutex ownership | |
153 | * after it gets the mutex. The following will still set the ownership | |
154 | * in cma__int_lock so we'll set it back to noone if cma__g_file is null | |
155 | * when we come back just in case it matters. It shouldn't since its no | |
156 | * longer in use but..... | |
157 | * Callers of this should recheck cma__g_file after the reservation to | |
158 | * make sure continueing makes sense. | |
159 | */ | |
160 | #define cma__fd_reserve(rfd) \ | |
161 | { \ | |
162 | cma__t_int_mutex *__mutex__; \ | |
163 | __mutex__ = cma__g_file[rfd]->mutex; \ | |
164 | cma__int_lock (__mutex__); \ | |
165 | if(cma__g_file[rfd] == (cma__t_file_obj *)cma_c_null_ptr) \ | |
166 | cma__int_unlock(__mutex__); \ | |
167 | } | |
168 | ||
169 | ||
170 | /* | |
171 | * Unreserve a file descriptor | |
172 | */ | |
173 | #define cma__fd_unreserve(ufd) cma__int_unlock (cma__g_file[ufd]->mutex) | |
174 | ||
175 | /* | |
176 | * AND together two select file descriptor masks | |
177 | */ | |
178 | #define cma__fdm_and(target,a,b) \ | |
179 | { \ | |
180 | int __i__ = cma__g_nspm; \ | |
181 | while (__i__--) \ | |
182 | (target)->fds_bits[__i__] = \ | |
183 | (a)->fds_bits[__i__] & (b)->fds_bits[__i__]; \ | |
184 | } | |
185 | ||
186 | /* | |
187 | * Clear a bit in a select file descriptor mask | |
188 | * | |
189 | * FD_CLR(n, p) := ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) | |
190 | */ | |
191 | #define cma__fdm_clr_bit(n,p) FD_CLR (n, p) | |
192 | ||
193 | /* | |
194 | * Copy the contents of one file descriptor mask into another. If the | |
195 | * destination operand is null, do nothing; if the source operand is null, | |
196 | * simply zero the destination. | |
197 | */ | |
198 | #define cma__fdm_copy(src,dst,nfds) { \ | |
199 | if (dst) \ | |
200 | if (src) { \ | |
201 | cma__t_mask *__s__ = (cma__t_mask *)(src); \ | |
202 | cma__t_mask *__d__ = (cma__t_mask *)(dst); \ | |
203 | int __i__; \ | |
204 | for (__i__ = 0; __i__ < (nfds); __i__ += cma__c_nbpm) \ | |
205 | *__d__++ = *__s__++; \ | |
206 | } \ | |
207 | else \ | |
208 | cma__fdm_zero (dst); \ | |
209 | } | |
210 | ||
211 | /* | |
212 | * To increment count for each bit set in fd - mask | |
213 | */ | |
214 | #define cma__fdm_count_bits(map,count) \ | |
215 | { \ | |
216 | int __i__ = cma__g_nspm; \ | |
217 | while (__i__--) { \ | |
218 | cma__t_mask __tm__; \ | |
219 | __tm__ = (map)->fds_bits[__i__]; \ | |
220 | while(__tm__) { \ | |
221 | (count)++; \ | |
222 | __tm__ &= ~(__tm__ & (-__tm__)); /* Assumes 2's comp */ \ | |
223 | } \ | |
224 | } \ | |
225 | } | |
226 | ||
227 | /* | |
228 | * Test if a bit is set in a select file descriptor mask | |
229 | * | |
230 | * FD_ISSET(n,p) := ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) | |
231 | */ | |
232 | #define cma__fdm_is_set(n,p) FD_ISSET (n, p) | |
233 | ||
234 | /* | |
235 | * OR together two select file descriptor masks | |
236 | */ | |
237 | #define cma__fdm_or(target,a,b) \ | |
238 | { \ | |
239 | int __i__ = cma__g_nspm; \ | |
240 | while (__i__--) \ | |
241 | (target)->fds_bits[__i__] = \ | |
242 | (a)->fds_bits[__i__] | (b)->fds_bits[__i__]; \ | |
243 | } | |
244 | ||
245 | /* | |
246 | * Set a bit in a select file descriptor mask | |
247 | * | |
248 | * FD_SET(n,p) := ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) | |
249 | */ | |
250 | #define cma__fdm_set_bit(n,p) FD_SET (n, p) | |
251 | ||
252 | /* | |
253 | * Clear a select file descriptor mask. | |
254 | */ | |
255 | #define cma__fdm_zero(n) \ | |
256 | cma__memset ((char *) n, 0, cma__g_nspm * sizeof(cma__t_mask)) | |
257 | ||
258 | ||
259 | \f | |
260 | /* | |
261 | * CMA "thread-synchronous" I/O read/write operations | |
262 | */ | |
263 | ||
264 | /* | |
265 | * Since all CMA "thread-synchronous" I/O (read or write) operations on | |
266 | * U*ix follow the exact same structure, the wrapper routines have been | |
267 | * condensed into a macro. | |
268 | * | |
269 | * The steps performed are as follows: | |
270 | * 1. Check that the file descriptor is a legitimate value. | |
271 | * 2. Check that the entry in the CMA file "database" which corresponds to | |
272 | * the file descriptor indicates that the "file" was "opened" by CMA. | |
273 | * 3. Reserve the file, to serialized access to files. This not only | |
274 | * simplifies things, but also defends against non-reentrancy. | |
275 | * 4. If the "file" is "set" for non-blocking I/O, check if we | |
276 | * have actually set the file non-blocking yet, and if not do so. | |
277 | * Then, issue the I/O operantion. | |
278 | * Success or failure is returned immediately, after unreserving the | |
279 | * file. If the error indicates that the operation would have caused | |
280 | * the process to block, continue to the next step. | |
281 | * 5. The I/O prolog adds this "file" to the global bit mask, which | |
282 | * represents all "files" which have threads waiting to perform I/O on | |
283 | * them, and causes the thread to block on the condition variable for | |
284 | * this "file". Periodically, a select is done on this global bit | |
285 | * mask, and the condition variables corresponding to "files" which | |
286 | * are ready for I/O are signaled, releasing those waiting threads to | |
287 | * perform their I/O. | |
288 | * 6. When the thread returns from the I/O prolog, it can (hopefully) | |
289 | * perform its operation without blocking the process. | |
290 | * 7. The I/O epilog clears the bit in the global mask and/or signals the | |
291 | * the next thread waiting for this "file", as appropriate. | |
292 | * 8. If the I/O failed, continue to loop. | |
293 | * 9. Finally, the "file" is unreserved, as we're done with it, and the | |
294 | * result of the operation is returned. | |
295 | * | |
296 | * | |
297 | * Note: currently, we believe that timeslicing which is based on the | |
298 | * virtual-time timer does not cause system calls to return EINTR. | |
299 | * Threfore, any EINTR returns are relayed directly to the caller. | |
300 | * On platforms which do not support a virtual-time timer, the code | |
301 | * should probably catch EINTR returns and restart the system call. | |
302 | */ | |
303 | ||
304 | /* | |
305 | * This macro is used for both read-type and write-type functions. | |
306 | * | |
307 | * Note: the second call to "func" may require being bracketed in a | |
308 | * cma__interrupt_disable/cma__interrupt_enable pair, but we'll | |
309 | * wait and see if this is necessary. | |
310 | */ | |
311 | #define cma__ts_func(func,fd,arglist,type,post_process) { \ | |
312 | cma_t_integer __res__; \ | |
313 | cma_t_boolean __done__ = cma_c_false; \ | |
314 | if ((fd < 0) || (fd >= cma__g_mx_file)) return (cma__set_errno (EBADF), -1); \ | |
315 | if (!cma__is_open(fd)) return (cma__set_errno (EBADF), -1); \ | |
316 | cma__fd_reserve (fd); \ | |
317 | if (!cma__is_open(fd)) return (cma__set_errno (EBADF), -1); \ | |
318 | if (cma__issue_io_call(fd)) {\ | |
319 | if ((!cma__g_file[fd]->set_non_blocking) && \ | |
320 | (cma__g_file[fd]->non_blocking == cma_c_true)) \ | |
321 | cma__set_nonblocking(fd); \ | |
322 | cma__interrupt_disable (0); \ | |
323 | TRY { \ | |
324 | __res__ = func arglist; \ | |
325 | } \ | |
326 | CATCH_ALL { \ | |
327 | cma__interrupt_enable (0); \ | |
328 | cma__fd_unreserve (fd); \ | |
329 | RERAISE; \ | |
330 | } \ | |
331 | ENDTRY \ | |
332 | cma__interrupt_enable (0); \ | |
333 | if ((__res__ != -1) \ | |
334 | || (!cma__is_blocking (errno)) \ | |
335 | || (cma__g_file[fd]->user_fl.user_non_blocking)) \ | |
336 | __done__ = cma_c_true; \ | |
337 | } \ | |
338 | if (__done__) { \ | |
339 | cma__fd_unreserve (fd); \ | |
340 | } \ | |
341 | else { \ | |
342 | TRY { \ | |
343 | cma__io_prolog (type, fd); \ | |
344 | while (!__done__) { \ | |
345 | cma__io_wait (type, fd); \ | |
346 | __res__ = func arglist; \ | |
347 | if ((__res__ != -1) \ | |
348 | || (!cma__is_blocking (errno)) \ | |
349 | || (cma__g_file[fd]->user_fl.user_non_blocking)) \ | |
350 | __done__ = cma_c_true; \ | |
351 | } \ | |
352 | } \ | |
353 | FINALLY { \ | |
354 | cma__io_epilog (type, fd); \ | |
355 | cma__fd_unreserve (fd); \ | |
356 | } \ | |
357 | ENDTRY \ | |
358 | } \ | |
359 | if (__res__ != -1) post_process; \ | |
360 | return __res__; \ | |
361 | } | |
362 | ||
363 | /* | |
364 | * Since most CMA "thread-synchronous" I/O ("open"-type) operations on | |
365 | * U*ix follow the exact same structure, the wrapper routines have been | |
366 | * condensed into a macro. | |
367 | * | |
368 | * The steps performed are as follows: | |
369 | * 1. Issue the open function. | |
370 | * 2. If the value returned indicates an error, return it to the caller. | |
371 | * 3. If the file descriptor returned is larger than what we think is the | |
372 | * maximum value (ie if it is too big for our database) then bugcheck. | |
373 | * 4. "Open" the "file" in the CMA file database. | |
374 | * 5. Return the file descriptor value to the caller. | |
375 | * | |
376 | * FIX-ME: for the time being, if the I/O operation returns EINTR, we | |
377 | * simply return it to the caller; eventually, we should catch this | |
378 | * and "do the right thing" (if we can figure out what that is). | |
379 | */ | |
380 | ||
381 | /* | |
382 | * This macro is used for all "open"-type functions which return a single file | |
383 | * desciptor by immediate value. | |
384 | */ | |
385 | #define cma__ts_open(func,arglist,post_process) { \ | |
386 | int __fd__; \ | |
387 | TRY { \ | |
388 | cma__int_init (); \ | |
389 | cma__int_lock (cma__g_io_data_mutex); \ | |
390 | __fd__ = func arglist; \ | |
391 | cma__int_unlock (cma__g_io_data_mutex); \ | |
392 | if (__fd__ >= 0 && __fd__ < cma__g_mx_file) \ | |
393 | post_process; \ | |
394 | } \ | |
395 | CATCH_ALL \ | |
396 | { \ | |
397 | cma__set_errno (EBADF); \ | |
398 | __fd__ = -1; \ | |
399 | } \ | |
400 | ENDTRY \ | |
401 | if (__fd__ >= cma__g_mx_file) \ | |
402 | cma__bugcheck ("cma__ts_open: fd is too large"); \ | |
403 | return __fd__; \ | |
404 | } | |
405 | /* | |
406 | * This macro is used for all "open"-type functions which return a pair of file | |
407 | * desciptors by reference parameter. | |
408 | */ | |
409 | #define cma__ts_open2(func,fdpair,arglist,post_process) { \ | |
410 | int __res__; \ | |
411 | TRY { \ | |
412 | cma__int_init (); \ | |
413 | cma__int_lock (cma__g_io_data_mutex); \ | |
414 | __res__ = func arglist; \ | |
415 | cma__int_unlock (cma__g_io_data_mutex); \ | |
416 | if (__res__ >= 0 && fdpair[0] < cma__g_mx_file \ | |
417 | && fdpair[1] < cma__g_mx_file) \ | |
418 | post_process; \ | |
419 | } \ | |
420 | CATCH_ALL \ | |
421 | { \ | |
422 | cma__set_errno (EBADF); \ | |
423 | __res__ = -1; \ | |
424 | } \ | |
425 | ENDTRY \ | |
426 | if ((fdpair[0] >= cma__g_mx_file) || (fdpair[1] >= cma__g_mx_file)) \ | |
427 | cma__bugcheck ("cma__ts_open2: one of fd's is too large"); \ | |
428 | return __res__; \ | |
429 | } | |
430 | ||
431 | /* | |
432 | * INTERNAL INTERFACES | |
433 | */ | |
434 | extern void cma__close_general (int); | |
435 | ||
436 | extern void cma__init_thread_io (void); | |
437 | ||
438 | extern cma_t_boolean cma__io_available (cma__t_io_type,int,struct timeval *); | |
439 | ||
440 | extern void cma__io_epilog (cma__t_io_type,int); | |
441 | ||
442 | extern void cma__io_prolog (cma__t_io_type,int); | |
443 | ||
444 | extern void cma__io_wait (cma__t_io_type,int); | |
445 | ||
446 | extern void cma__open_general (int); | |
447 | ||
448 | extern void cma__reinit_thread_io (int); | |
449 | ||
450 | extern void cma__set_nonblocking (int); | |
451 | ||
452 | extern void cma__set_user_nonblock_flags (int,int); | |
453 | ||
454 | extern cma_t_boolean cma__is_open (int); | |
455 | ||
456 | ||
457 | #endif |