Orace开源的异步IO编程库,特点是接口非常简单

简介: 官网:https://oss.oracle.com/projects/libaio-oracle/,正如标题所说,非常简单了,不用多解释,请直接看头文件,其中aio_poll类似于poll,重要的结构是aiocb64,类似于epoll_event。
官网: https://oss.oracle.com/projects/libaio-oracle/,正如标题所说,非常简单了,不用多解释,请直接看头文件,其中aio_poll类似于poll,重要的结构是aiocb64,类似于epoll_event。


  1. #ifndef _SKGAIO_H
  2. #define _SKGAIO_H

  3. #define IOCB_CMD_READ        0
  4. #define IOCB_CMD_WRITE        1
  5. #define IOCB_CMD_NOP        2
  6. #define IOCB_CMD_CANCEL        3
  7. #define IOCB_CMD_FSYNC        4
  8. #define IOCB_CMD_FDSYNC        5
  9. #define IOCB_CMD_RUNNING    6
  10. #define IOCB_CMD_DONE        7

  11. /* Maximum number of events to retrieve at once */
  12. #define MAX_EVENTS 512
  13. #define MAX_AIO_REAP MAX_EVENTS

  14. #include stdlib.h>
  15. #include asm/unistd.h>
  16. #include linux/types.h>
  17. #include signal.h>
  18. /*
  19.  * we always use a 64bit off_t when communicating
  20.  * with userland. its up to libraries to do the
  21.  * proper padding and aio_error abstraction
  22.  *
  23.  * FIXME: this must change from glibc's definition
  24.  * as we do *not* use the sigevent structure which
  25.  * is big and bloated.
  26.  */
  27. struct aiocb64 {
  28.   int aio_fildes; /* File desriptor. */
  29.   short aio_lio_opcode; /* Operation to be performed. */
  30.   short aio_reqprio; /* Request priority offset. */
  31.   void *aio_buf;             /* Location of buffer. */
  32.   size_t aio_nbytes; /* Length of transfer. */
  33.   loff_t aio_offset;        /* File offset. */
  34.   /* these are internal to the kernel/libc. */
  35.   long __aio_key; // kernel sets this to -1 if completed
  36.                      // otherwise >= 0 (the request#)
  37.   void * __aio_data; // pointer to be returned in event's data
  38.   int __error_code;
  39. };



  40. #ifdef DEBUG
  41. #define dbg_printf(fmt,arg...)\
  42.     printf(fmt, ##arg)
  43. #else
  44. #define dbg_printf(fmt,arg...)\
  45.     do { } while(0);
  46. #endif


  47. #define aiocb         aiocb64
  48. #define aio_read     aio_read64
  49. #define aio_write     aio_write64
  50. #define aio_poll     aio_poll64
  51. #define aio_error     aio_error64
  52. #define aio_return     aio_return64
  53. #define aio_cancel     aio_cancel64
  54. #define aio_suspend     aio_suspend64
  55. #define    lio_listio    lio_listio64
  56. #define aio_reap aio_reap64


  57. /* Initialize async i/o with the given maximum number of requests */
  58. int aio_init(int max_requests);

  59. /* Enqueue read request for given number of bytes and the given priority. */
  60. int aio_read64(struct aiocb64 *aiocbp);

  61. /* Enqueue write request for given number of bytes and the given priority. */
  62. int aio_write64(struct aiocb64 *aiocbp);

  63. /* Enqueue a poll request for a given fd. */
  64. int aio_poll64(struct aiocb64 *aiocbp);
  65.  
  66. /*
  67.  * Returns the status of the aiocb.
  68.  * If the operation is incomplete, the return value is undefined
  69.  * -1 is -errno for the call.
  70.  * >= -1 is the return code of the completed operation
  71.  */
  72. ssize_t aio_return64(struct aiocb64 *aiocbp);

  73. /*
  74.  * Returns the error status of the aiocb.
  75.  * 0 is -errno for the call.
  76.  * 0 is successfully complete
  77.  * EINPROGRESS is not complete at all.
  78.  * > 0 is errno for unsuccessful completion.
  79.  */
  80. int aio_error64(struct aiocb64 *aiocbp);

  81. /*
  82.  * Try to cancel asynchronous I/O requests outstanding against file
  83.  * descriptor FILDES.
  84.  */
  85. int aio_cancel64 ( int fildes, struct aiocb64 *aiocbp);
  86.  
  87. /*
  88.  * Suspend calling thread until at least one of the asynchronous I/O
  89.  * operations referenced by LIST has completed.
  90.  */
  91. int aio_suspend64(const struct aiocb64 * const list[],int nent,
  92.          const struct timespec *timeout);

  93. /*
  94.  * Suspend calling thread until waitfor asynchronouse I/O operations
  95.  * outstanding have completed.
  96.  */
  97. int aio_reap64(struct timespec *timeout, int waitfor,
  98.                struct aiocb *out_list[], int listsize,
  99.                int *completed_count);


  100. int lio_listio64(int mode, struct aiocb64 * const list[], int nent,
  101.                  struct sigevent *__restrict __sig);

  102. /* Operation codes for `aio_lio_opcode'. */
  103. enum
  104. {
  105.     LIO_READ,
  106. #define LIO_READ LIO_READ
  107.     LIO_WRITE,
  108. #define LIO_WRITE LIO_WRITE
  109.     LIO_NOP,
  110. #define LIO_NOP LIO_NOP
  111.     LIO_POLL,
  112. #define LIO_POLL LIO_POLL
  113. };
  114.  
  115. /* Return values of cancelation function. */
  116. enum
  117. {
  118.     AIO_CANCELED,
  119. #define AIO_CANCELED AIO_CANCELED
  120.     AIO_NOTCANCELED,
  121. #define AIO_NOTCANCELED AIO_NOTCANCELED
  122.     AIO_ALLDONE
  123. #define AIO_ALLDONE AIO_ALLDONE
  124. };

  125.  
  126. /* Synchronization options for `lio_listio' function. */
  127. enum
  128. {
  129.     LIO_WAIT,
  130. #define LIO_WAIT LIO_WAIT
  131.     LIO_NOWAIT
  132. #define LIO_NOWAIT LIO_NOWAIT
  133. };

  134. #endif /* _SKGAIO_H */




相关文章
|
1月前
|
数据采集 Java Python
python并发编程:Python异步IO实现并发爬虫
python并发编程:Python异步IO实现并发爬虫
26 1
|
1月前
|
算法 数据处理 Python
Python并发编程:解密异步IO与多线程
本文将深入探讨Python中的并发编程技术,重点介绍异步IO和多线程两种常见的并发模型。通过对比它们的特点、适用场景和实现方式,帮助读者更好地理解并发编程的核心概念,并掌握在不同场景下选择合适的并发模型的方法。
|
1月前
|
数据采集 异构计算
LabVIEW编程LabVIEW开发高级数据采集技术 操作数字IO 例程与相关资料
LabVIEW编程LabVIEW开发高级数据采集技术 操作数字IO 例程与相关资料
56 22
|
1月前
|
调度 数据库 Python
【专栏】异步IO在处理IO密集型任务中的高效性
【4月更文挑战第27天】本文介绍了Python并发编程和异步IO,包括并发的基本概念(多线程、多进程、协程),线程与进程的实现(threading和multiprocessing模块),协程的使用(asyncio模块),以及异步IO的原理和优势。强调了异步IO在处理IO密集型任务中的高效性,指出应根据任务类型选择合适的并发技术。
|
3天前
|
Python
并发编程,Python让你轻松驾驭多线程与异步IO!
【6月更文挑战第12天】本文探讨了Python中的并发编程,包括多线程和异步IO。通过`threading`模块展示了多线程编程,创建并运行多个线程以并发执行任务。同时,使用`asyncio`库演示了异步IO编程,允许在单线程中高效处理多个IO操作。两个示例代码详细解释了如何在Python中实现并发,展现了其在提升程序性能和响应速度方面的潜力。
|
6天前
|
调度 数据库 开发者
在Python编程中,并发编程和异步IO是两个重要的概念,它们对于提高程序性能和响应速度具有至关重要的作用
【6月更文挑战第10天】本文介绍了Python并发编程和异步IO,包括并发编程的基本概念如多线程、多进程和协程。线程和进程可通过threading及multiprocessing模块管理,但多线程受限于GIL。协程利用asyncio模块实现非阻塞IO,适合处理IO密集型任务。异步IO基于事件循环,能提高服务器并发处理能力,适用于网络编程和文件操作等场景。异步IO与多线程、多进程在不同任务中有各自优势,开发者应根据需求选择合适的技术。
18 0
|
15天前
|
存储 Java API
Java语言IO(输入/输出)编程技术深度解析
Java语言IO(输入/输出)编程技术深度解析
249 1
|
24天前
|
安全 Linux vr&ar
【Linux 系统】基础 IO(动静态库)-- 详解
【Linux 系统】基础 IO(动静态库)-- 详解
|
24天前
|
存储 编译器 vr&ar
【基础IO】谈谈动静态库(怒肝7000字)
【基础IO】谈谈动静态库(怒肝7000字)
|
1月前
|
数据采集 人工智能 异构计算
LabVIEW编程LabVIEW开发 PXI-6259多功能IO模块 例程与相关资料
LabVIEW编程LabVIEW开发 PXI-6259多功能IO模块 例程与相关资料
22 0