zmq_poll.txt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. zmq_poll(3)
  2. ===========
  3. NAME
  4. ----
  5. zmq_poll - input/output multiplexing
  6. SYNOPSIS
  7. --------
  8. *int zmq_poll (zmq_pollitem_t '*items', int 'nitems', long 'timeout');*
  9. DESCRIPTION
  10. -----------
  11. The _zmq_poll()_ function provides a mechanism for applications to multiplex
  12. input/output events in a level-triggered fashion over a set of sockets. Each
  13. member of the array pointed to by the 'items' argument is a *zmq_pollitem_t*
  14. structure. The 'nitems' argument specifies the number of items in the 'items'
  15. array. The *zmq_pollitem_t* structure is defined as follows:
  16. ["literal", subs="quotes"]
  17. typedef struct
  18. {
  19. void '*socket';
  20. int 'fd';
  21. short 'events';
  22. short 'revents';
  23. } zmq_pollitem_t;
  24. For each *zmq_pollitem_t* item, _zmq_poll()_ shall examine either the 0MQ
  25. socket referenced by 'socket' *or* the standard socket specified by the file
  26. descriptor 'fd', for the event(s) specified in 'events'. If both 'socket' and
  27. 'fd' are set in a single *zmq_pollitem_t*, the 0MQ socket referenced by
  28. 'socket' shall take precedence and the value of 'fd' shall be ignored.
  29. For each *zmq_pollitem_t* item, _zmq_poll()_ shall first clear the 'revents'
  30. member, and then indicate any requested events that have occurred by setting the
  31. bit corresponding to the event condition in the 'revents' member.
  32. If none of the requested events have occurred on any *zmq_pollitem_t* item,
  33. _zmq_poll()_ shall wait 'timeout' milliseconds for an event to occur on
  34. any of the requested items. If the value of 'timeout' is `0`, _zmq_poll()_
  35. shall return immediately. If the value of 'timeout' is `-1`, _zmq_poll()_ shall
  36. block indefinitely until a requested event has occurred on at least one
  37. *zmq_pollitem_t*.
  38. The 'events' and 'revents' members of *zmq_pollitem_t* are bit masks constructed
  39. by OR'ing a combination of the following event flags:
  40. *ZMQ_POLLIN*::
  41. For 0MQ sockets, at least one message may be received from the 'socket' without
  42. blocking. For standard sockets this is equivalent to the 'POLLIN' flag of the
  43. _poll()_ system call and generally means that at least one byte of data may be
  44. read from 'fd' without blocking.
  45. *ZMQ_POLLOUT*::
  46. For 0MQ sockets, at least one message may be sent to the 'socket' without
  47. blocking. For standard sockets this is equivalent to the 'POLLOUT' flag of the
  48. _poll()_ system call and generally means that at least one byte of data may be
  49. written to 'fd' without blocking.
  50. *ZMQ_POLLERR*::
  51. For standard sockets, this flag is passed through _zmq_poll()_ to the
  52. underlying _poll()_ system call and generally means that some sort of error
  53. condition is present on the socket specified by 'fd'. For 0MQ sockets this flag
  54. has no effect if set in 'events', and shall never be returned in 'revents' by
  55. _zmq_poll()_.
  56. *ZMQ_POLLPRI*::
  57. For 0MQ sockets this flags is of no use. For standard sockets this means there
  58. is urgent data to read. Refer to the POLLPRI flag for more informations.
  59. For file descriptor, refer to your use case: as an example, GPIO interrupts
  60. are signaled through a POLLPRI event.
  61. This flag has no effect on Windows.
  62. NOTE: The _zmq_poll()_ function may be implemented or emulated using operating
  63. system interfaces other than _poll()_, and as such may be subject to the limits
  64. of those interfaces in ways not defined in this documentation.
  65. THREAD SAFETY
  66. -------------
  67. The *zmq_pollitem_t* array must only be used by the thread which
  68. will/is calling _zmq_poll_.
  69. If a socket is contained in multiple *zmq_pollitem_t* arrays, each owned by a
  70. different thread, the socket itself needs to be thead-safe (Server, Client, ...).
  71. Otherwise, behaviour is undefined.
  72. RETURN VALUE
  73. ------------
  74. Upon successful completion, the _zmq_poll()_ function shall return the number
  75. of *zmq_pollitem_t* structures with events signaled in 'revents' or `0` if no
  76. events have been signaled. Upon failure, _zmq_poll()_ shall return `-1` and set
  77. 'errno' to one of the values defined below.
  78. ERRORS
  79. ------
  80. *ETERM*::
  81. At least one of the members of the 'items' array refers to a 'socket' whose
  82. associated 0MQ 'context' was terminated.
  83. *EFAULT*::
  84. The provided 'items' was not valid (NULL).
  85. *EINTR*::
  86. The operation was interrupted by delivery of a signal before any events were
  87. available.
  88. EXAMPLE
  89. -------
  90. .Polling indefinitely for input events on both a 0MQ socket and a standard socket.
  91. ----
  92. zmq_pollitem_t items [2];
  93. /* First item refers to 0MQ socket 'socket' */
  94. items[0].socket = socket;
  95. items[0].events = ZMQ_POLLIN;
  96. /* Second item refers to standard socket 'fd' */
  97. items[1].socket = NULL;
  98. items[1].fd = fd;
  99. items[1].events = ZMQ_POLLIN;
  100. /* Poll for events indefinitely */
  101. int rc = zmq_poll (items, 2, -1);
  102. assert (rc >= 0);
  103. /* Returned events will be stored in items[].revents */
  104. ----
  105. SEE ALSO
  106. --------
  107. linkzmq:zmq_socket[3]
  108. linkzmq:zmq_send[3]
  109. linkzmq:zmq_recv[3]
  110. linkzmq:zmq[7]
  111. Your operating system documentation for the _poll()_ system call.
  112. AUTHORS
  113. -------
  114. This page was written by the 0MQ community. To make a change please
  115. read the 0MQ Contribution Policy at <http://www.zeromq.org/docs:contributing>.