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>
30 #include <sys/types.h>
33 #include <cma_errors.h>
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.
49 # if O_NDELAY != FNDELAY
50 # define _CMA_FNDELAY_
53 # define _CMA_FNDELAY_
59 # if O_NONBLOCK != FNBLOCK
60 # define _CMA_FNBLOCK_
63 # define _CMA_FNBLOCK_
68 extern cma_t_boolean
cma_is_open(int);
70 * Maximum number of files (ie, max_fd+1)
72 #define cma__c_mx_file FD_SETSIZE
75 * Number of bits per file descriptor bit mask (ie number of bytes * bits/byte)
77 #define cma__c_nbpm NFDBITS
83 typedef enum CMA__T_IO_TYPE
{
88 #define cma__c_max_io_type 2
91 * From our local <sys/types.h>:
93 * typedef long fd_mask;
95 * typedef struct fd_set {
96 * fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
100 typedef fd_mask cma__t_mask
;
101 typedef fd_set cma__t_file_mask
;
109 * Maximum number of files (ie, max_fd+1) as determined by getdtablesize().
111 extern int cma__g_mx_file
;
114 * Number of submasks (ie "int" sized chunks) per file descriptor mask as
115 * determined by getdtablesize().
117 extern int cma__g_nspm
;
124 * Define a constant for the errno value which indicates that the requested
125 * operation was not performed because it would block the process.
127 # define cma__is_blocking(s) \
128 ((s == EAGAIN) || (s == EWOULDBLOCK) || (s == EINPROGRESS) || \
129 (s == EALREADY) || (s == EDEADLK))
132 * It is necessary to issue an I/O function, before calling cma__io_wait()
133 * in the following cases:
135 * * This file descriptor has been set non-blocking by CMA
136 * * This file descriptor has been set non-blocking by the user.
139 #define cma__issue_io_call(fd) \
140 ( (cma__g_file[fd]->non_blocking) || \
141 (cma__g_file[fd]->user_fl.user_non_blocking) )
144 #define cma__set_user_nonblocking(flags) \
147 * Determine if the file is open
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.
160 #define cma__fd_reserve(rfd) \
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__); \
171 * Unreserve a file descriptor
173 #define cma__fd_unreserve(ufd) cma__int_unlock (cma__g_file[ufd]->mutex)
176 * AND together two select file descriptor masks
178 #define cma__fdm_and(target,a,b) \
180 int __i__ = cma__g_nspm; \
182 (target)->fds_bits[__i__] = \
183 (a)->fds_bits[__i__] & (b)->fds_bits[__i__]; \
187 * Clear a bit in a select file descriptor mask
189 * FD_CLR(n, p) := ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
191 #define cma__fdm_clr_bit(n,p) FD_CLR (n, p)
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.
198 #define cma__fdm_copy(src,dst,nfds) { \
201 cma__t_mask *__s__ = (cma__t_mask *)(src); \
202 cma__t_mask *__d__ = (cma__t_mask *)(dst); \
204 for (__i__ = 0; __i__ < (nfds); __i__ += cma__c_nbpm) \
205 *__d__++ = *__s__++; \
208 cma__fdm_zero (dst); \
212 * To increment count for each bit set in fd - mask
214 #define cma__fdm_count_bits(map,count) \
216 int __i__ = cma__g_nspm; \
218 cma__t_mask __tm__; \
219 __tm__ = (map)->fds_bits[__i__]; \
222 __tm__ &= ~(__tm__ & (-__tm__)); /* Assumes 2's comp */ \
228 * Test if a bit is set in a select file descriptor mask
230 * FD_ISSET(n,p) := ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
232 #define cma__fdm_is_set(n,p) FD_ISSET (n, p)
235 * OR together two select file descriptor masks
237 #define cma__fdm_or(target,a,b) \
239 int __i__ = cma__g_nspm; \
241 (target)->fds_bits[__i__] = \
242 (a)->fds_bits[__i__] | (b)->fds_bits[__i__]; \
246 * Set a bit in a select file descriptor mask
248 * FD_SET(n,p) := ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
250 #define cma__fdm_set_bit(n,p) FD_SET (n, p)
253 * Clear a select file descriptor mask.
255 #define cma__fdm_zero(n) \
256 cma__memset ((char *) n, 0, cma__g_nspm * sizeof(cma__t_mask))
261 * CMA "thread-synchronous" I/O read/write operations
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.
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
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.
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.
305 * This macro is used for both read-type and write-type functions.
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.
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); \
324 __res__ = func arglist; \
327 cma__interrupt_enable (0); \
328 cma__fd_unreserve (fd); \
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; \
339 cma__fd_unreserve (fd); \
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; \
354 cma__io_epilog (type, fd); \
355 cma__fd_unreserve (fd); \
359 if (__res__ != -1) post_process; \
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.
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.
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).
382 * This macro is used for all "open"-type functions which return a single file
383 * desciptor by immediate value.
385 #define cma__ts_open(func,arglist,post_process) { \
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) \
397 cma__set_errno (EBADF); \
401 if (__fd__ >= cma__g_mx_file) \
402 cma__bugcheck ("cma__ts_open: fd is too large"); \
406 * This macro is used for all "open"-type functions which return a pair of file
407 * desciptors by reference parameter.
409 #define cma__ts_open2(func,fdpair,arglist,post_process) { \
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) \
422 cma__set_errno (EBADF); \
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"); \
432 * INTERNAL INTERFACES
434 extern void cma__close_general (int);
436 extern void cma__init_thread_io (void);
438 extern cma_t_boolean
cma__io_available (cma__t_io_type
,int,struct timeval
*);
440 extern void cma__io_epilog (cma__t_io_type
,int);
442 extern void cma__io_prolog (cma__t_io_type
,int);
444 extern void cma__io_wait (cma__t_io_type
,int);
446 extern void cma__open_general (int);
448 extern void cma__reinit_thread_io (int);
450 extern void cma__set_nonblocking (int);
452 extern void cma__set_user_nonblock_flags (int,int);
454 extern cma_t_boolean
cma__is_open (int);