PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/gdb-linaro-7.5-2012.12-1/gdb/osf-share/AT386/cma_thread_io.h

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