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