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
17 * Header file for thread synchrounous I/O
27 #include <cma_config.h>
28 #include <sys/select.h>
30 #include <sys/types.h>
33 #include <cma_errors.h>
40 * Maximum number of files (ie, max_fd+1)
42 #define cma__c_mx_file FD_SETSIZE
45 * Number of bits per file descriptor bit mask (ie number of bytes * bits/byte)
47 #define cma__c_nbpm NFDBITS
53 typedef enum CMA__T_IO_TYPE
{
58 #define cma__c_max_io_type 2
61 * From our local <sys/types.h>:
63 * typedef long fd_mask;
65 * typedef struct fd_set {
66 * fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
70 typedef fd_mask cma__t_mask
;
71 typedef fd_set cma__t_file_mask
;
80 * Maximum number of files (ie, max_fd+1) as determined by getdtablesize().
82 extern int cma__g_mx_file
;
85 * Number of submasks (ie "int" sized chunks) per file descriptor mask as
86 * determined by getdtablesize().
88 extern int cma__g_nspm
;
95 * Define a constant for the errno value which indicates that the requested
96 * operation was not performed because it would block the process.
98 # define cma__is_blocking(s) \
99 ((s == EAGAIN) || (s == EWOULDBLOCK) || (s == EINPROGRESS) || \
100 (s == EALREADY) || (s == EDEADLK))
103 * It is necessary to issue an I/O function, before calling cma__io_wait()
104 * in the following cases:
106 * * This file descriptor has been set non-blocking by CMA
107 * * This file descriptor has been set non-blocking by the user.
110 #define cma__issue_io_call(fd) \
111 ( (cma__g_file[fd]->non_blocking) || \
112 (cma__g_file[fd]->user_fl.user_non_blocking) )
115 #define cma__set_user_nonblocking(flags) \
118 * Determine if the file is open
121 * If the file gets closed while waiting for the mutex cma__g_file[rfd]
122 * gets set to null. This results in a crash if NDEBUG is set to 0
123 * since cma__int_lock tries to dereference it to set the mutex ownership
124 * after it gets the mutex. The following will still set the ownership
125 * in cma__int_lock so we'll set it back to noone if cma__g_file is null
126 * when we come back just in case it matters. It shouldn't since its no
127 * longer in use but.....
128 * Callers of this should recheck cma__g_file after the reservation to
129 * make sure continueing makes sense.
131 #define cma__fd_reserve(rfd) \
133 cma__t_int_mutex *__mutex__; \
134 __mutex__ = cma__g_file[rfd]->mutex; \
135 cma__int_lock (__mutex__); \
136 if(cma__g_file[rfd] == (cma__t_file_obj *)cma_c_null_ptr) \
137 cma__int_unlock(__mutex__); \
142 * Unreserve a file descriptor
144 #define cma__fd_unreserve(ufd) cma__int_unlock (cma__g_file[ufd]->mutex)
147 * AND together two select file descriptor masks
149 #define cma__fdm_and(target,a,b) \
151 int __i__ = cma__g_nspm; \
153 (target)->fds_bits[__i__] = \
154 (a)->fds_bits[__i__] & (b)->fds_bits[__i__]; \
158 * Clear a bit in a select file descriptor mask
160 * FD_CLR(n, p) := ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
162 #define cma__fdm_clr_bit(n,p) FD_CLR (n, p)
165 * Copy the contents of one file descriptor mask into another. If the
166 * destination operand is null, do nothing; if the source operand is null,
167 * simply zero the destination.
169 #define cma__fdm_copy(src,dst,nfds) { \
172 cma__t_mask *__s__ = (cma__t_mask *)(src); \
173 cma__t_mask *__d__ = (cma__t_mask *)(dst); \
175 for (__i__ = 0; __i__ < (nfds); __i__ += cma__c_nbpm) \
176 *__d__++ = *__s__++; \
179 cma__fdm_zero (dst); \
183 * To increment count for each bit set in fd - mask
185 #define cma__fdm_count_bits(map,count) \
187 int __i__ = cma__g_nspm; \
189 cma__t_mask __tm__; \
190 __tm__ = (map)->fds_bits[__i__]; \
193 __tm__ &= ~(__tm__ & (-__tm__)); /* Assumes 2's comp */ \
199 * Test if a bit is set in a select file descriptor mask
201 * FD_ISSET(n,p) := ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
203 #define cma__fdm_is_set(n,p) FD_ISSET (n, p)
206 * OR together two select file descriptor masks
208 #define cma__fdm_or(target,a,b) \
210 int __i__ = cma__g_nspm; \
212 (target)->fds_bits[__i__] = \
213 (a)->fds_bits[__i__] | (b)->fds_bits[__i__]; \
217 * Set a bit in a select file descriptor mask
219 * FD_SET(n,p) := ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
221 #define cma__fdm_set_bit(n,p) FD_SET (n, p)
224 * Clear a select file descriptor mask.
226 #define cma__fdm_zero(n) \
227 cma__memset ((char *) n, 0, cma__g_nspm * sizeof(cma__t_mask))
234 * CMA "thread-synchronous" I/O read/write operations
238 * Since all CMA "thread-synchronous" I/O (read or write) operations on
239 * U*ix follow the exact same structure, the wrapper routines have been
240 * condensed into a macro.
242 * The steps performed are as follows:
243 * 1. Check that the file descriptor is a legitimate value.
244 * 2. Check that the entry in the CMA file "database" which corresponds to
245 * the file descriptor indicates that the "file" was "opened" by CMA.
246 * 3. Reserve the file, to serialized access to files. This not only
247 * simplifies things, but also defends against non-reentrancy.
248 * 4. If the "file" is "set" for non-blocking I/O, check if we
249 * have actually set the file non-blocking yet, and if not do so.
250 * Then, issue the I/O operantion.
251 * Success or failure is returned immediately, after unreserving the
252 * file. If the error indicates that the operation would have caused
253 * the process to block, continue to the next step.
254 * 5. The I/O prolog adds this "file" to the global bit mask, which
255 * represents all "files" which have threads waiting to perform I/O on
256 * them, and causes the thread to block on the condition variable for
257 * this "file". Periodically, a select is done on this global bit
258 * mask, and the condition variables corresponding to "files" which
259 * are ready for I/O are signaled, releasing those waiting threads to
261 * 6. When the thread returns from the I/O prolog, it can (hopefully)
262 * perform its operation without blocking the process.
263 * 7. The I/O epilog clears the bit in the global mask and/or signals the
264 * the next thread waiting for this "file", as appropriate.
265 * 8. If the I/O failed, continue to loop.
266 * 9. Finally, the "file" is unreserved, as we're done with it, and the
267 * result of the operation is returned.
270 * Note: currently, we believe that timeslicing which is based on the
271 * virtual-time timer does not cause system calls to return EINTR.
272 * Threfore, any EINTR returns are relayed directly to the caller.
273 * On platforms which do not support a virtual-time timer, the code
274 * should probably catch EINTR returns and restart the system call.
278 * This macro is used for both read-type and write-type functions.
280 * Note: the second call to "func" may require being bracketed in a
281 * cma__interrupt_disable/cma__interrupt_enable pair, but we'll
282 * wait and see if this is necessary.
284 #define cma__ts_func(func,fd,arglist,type,post_process) { \
285 cma_t_integer __res__; \
286 cma_t_boolean __done__ = cma_c_false; \
287 if ((fd < 0) || (fd >= cma__g_mx_file)) return (cma__set_errno (EBADF), -1); \
288 if (!cma__is_open(fd)) return (cma__set_errno (EBADF), -1); \
289 cma__fd_reserve (fd); \
290 if (!cma__is_open(fd)) return (cma__set_errno (EBADF), -1); \
291 if (cma__issue_io_call(fd)) {\
292 if ((!cma__g_file[fd]->set_non_blocking) && \
293 (cma__g_file[fd]->non_blocking == cma_c_true)) \
294 cma__set_nonblocking(fd); \
295 cma__interrupt_disable (0); \
297 __res__ = func arglist; \
300 cma__interrupt_enable (0); \
301 cma__fd_unreserve (fd); \
305 cma__interrupt_enable (0); \
306 if ((__res__ != -1) \
307 || (!cma__is_blocking (errno)) \
308 || (cma__g_file[fd]->user_fl.user_non_blocking)) \
309 __done__ = cma_c_true; \
312 cma__fd_unreserve (fd); \
316 cma__io_prolog (type, fd); \
317 while (!__done__) { \
318 cma__io_wait (type, fd); \
319 __res__ = func arglist; \
320 if ((__res__ != -1) \
321 || (!cma__is_blocking (errno)) \
322 || (cma__g_file[fd]->user_fl.user_non_blocking)) \
323 __done__ = cma_c_true; \
327 cma__io_epilog (type, fd); \
328 cma__fd_unreserve (fd); \
332 if (__res__ != -1) post_process; \
337 * Since most CMA "thread-synchronous" I/O ("open"-type) operations on
338 * U*ix follow the exact same structure, the wrapper routines have been
339 * condensed into a macro.
341 * The steps performed are as follows:
342 * 1. Issue the open function.
343 * 2. If the value returned indicates an error, return it to the caller.
344 * 3. If the file descriptor returned is larger than what we think is the
345 * maximum value (ie if it is too big for our database) then bugcheck.
346 * 4. "Open" the "file" in the CMA file database.
347 * 5. Return the file descriptor value to the caller.
349 * FIX-ME: for the time being, if the I/O operation returns EINTR, we
350 * simply return it to the caller; eventually, we should catch this
351 * and "do the right thing" (if we can figure out what that is).
355 * This macro is used for all "open"-type functions which return a single file
356 * desciptor by immediate value.
358 #define cma__ts_open(func,arglist,post_process) { \
362 cma__int_lock (cma__g_io_data_mutex); \
363 __fd__ = func arglist; \
364 cma__int_unlock (cma__g_io_data_mutex); \
365 if (__fd__ >= 0 && __fd__ < cma__g_mx_file) \
370 cma__set_errno (EBADF); \
374 if (__fd__ >= cma__g_mx_file) \
375 cma__bugcheck ("cma__ts_open: fd is too large"); \
379 * This macro is used for all "open"-type functions which return a pair of file
380 * desciptors by reference parameter.
382 #define cma__ts_open2(func,fdpair,arglist,post_process) { \
386 cma__int_lock (cma__g_io_data_mutex); \
387 __res__ = func arglist; \
388 cma__int_unlock (cma__g_io_data_mutex); \
389 if (__res__ >= 0 && fdpair[0] < cma__g_mx_file \
390 && fdpair[1] < cma__g_mx_file) \
395 cma__set_errno (EBADF); \
399 if ((fdpair[0] >= cma__g_mx_file) || (fdpair[1] >= cma__g_mx_file)) \
400 cma__bugcheck ("cma__ts_open2: one of fd's is too large"); \
405 * INTERNAL INTERFACES
407 extern void cma__close_general (int);
410 cma__init_thread_io (void);
412 extern cma_t_boolean
cma__io_available (cma__t_io_type
,int,struct timeval
*);
414 extern void cma__io_epilog (cma__t_io_type
,int);
416 extern void cma__io_prolog (cma__t_io_type
,int);
418 extern void cma__io_wait (cma__t_io_type
,int);
420 extern void cma__open_general (int);
422 extern void cma__reinit_thread_io (int);
424 extern void cma__set_nonblocking (int);
426 extern void cma__set_user_nonblock_flags (int,int);
429 cma__is_open (int fd
);