zmq_poller.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. zmq_poller(3)
  2. ===========
  3. NAME
  4. ----
  5. zmq_poller - input/output multiplexing
  6. SYNOPSIS
  7. --------
  8. *void *zmq_poller_new (void);*
  9. *int zmq_poller_destroy (void '*poller_p');*
  10. *int zmq_poller_size (void '*poller');*
  11. *int zmq_poller_add (void '*poller', void '*socket', void '*user_data', short 'events');*
  12. *int zmq_poller_modify (void '*poller', void '*socket', short 'events');*
  13. *int zmq_poller_remove (void '*poller', void '*socket');*
  14. *int zmq_poller_add_fd (void '*poller', int 'fd', void '*user_data', short 'events');*
  15. *int zmq_poller_modify_fd (void '*poller', int 'fd', short 'events');*
  16. *int zmq_poller_remove_fd (void '*poller', int 'fd');*
  17. *int zmq_poller_wait (void '*poller',
  18. zmq_poller_event_t '*event',
  19. long 'timeout');*
  20. *int zmq_poller_wait_all (void '*poller',
  21. zmq_poller_event_t '*events',
  22. int 'n_events',
  23. long 'timeout');*
  24. *int zmq_poller_fd (void '*poller', zmq_fd_t '*fd');*
  25. DESCRIPTION
  26. -----------
  27. The _zmq_poller_*_ functions provide a mechanism for applications to multiplex
  28. input/output events in a level-triggered fashion over a set of sockets.
  29. _zmq_poller_new_ and _zmq_poller_destroy_ manage the lifetime of a poller
  30. instance. _zmq_poller_new_ creates and returns a new poller instance, while
  31. _zmq_poller_destroy_ destroys it. A pointer to a valid poller must be passed
  32. as the _poller_p_ argument of _zmq_poller_destroy_. In particular,
  33. _zmq_poller_destroy_ may not be called multiple times for the same poller
  34. instance. _zmq_poller_destroy_ sets the passed pointer to NULL in case of a
  35. successful execution. _zmq_poller_destroy_ implicitly unregisters all
  36. registered sockets and file descriptors.
  37. _zmq_poller_size_ queries the number of sockets or file descriptors registered
  38. with a poller. The initial size of a poller is 0, a successful add operation
  39. increases the size by 1 and a successful remove operation decreases the size
  40. by 1. The size is unaffected by the events specified.
  41. _zmq_poller_add_, _zmq_poller_modify_ and _zmq_poller_remove_ manage the 0MQ
  42. sockets registered with a poller.
  43. _zmq_poller_add_ registers a new _socket_ with a given _poller_. Both _poller_
  44. and _socket_ must point to valid 0MQ objects. The _events_ parameter specifies
  45. which event types the client wants to subscribe to. It is legal to specify no
  46. events (i.e. 0), and activate them later with _zmq_poller_modify_.
  47. In addition, _user_data_ may be specified, which is not used by the poller, but
  48. passed back to the caller when an event was signalled in a call to
  49. _zmq_poller_wait_ or _zmq_poller_wait_all_. _user_data_ may be NULL. If it is
  50. not NULL, it must be a valid pointer. Otherwise, behaviour is undefined.
  51. You must only add a socket to a single poller instance once (unless
  52. _zmq_poller_remove_ has been called for that socket before). You may
  53. add a socket to multiple poller instances, if the socket itself
  54. is explicitly thread-safe (Server, Client, ...). If the socket is not,
  55. you may invoke undefined behavior.
  56. _zmq_poller_modify_ modifies the subscribed events for a socket. It is
  57. legal to specify no events (i.e. 0) to disable events temporarily, and
  58. reactivate them later with another call to _zmq_poller_modify_.
  59. _zmq_poller_remove_ removes a socket registration completely.
  60. _zmq_poller_remove_ must be called before a socket is closed with _zmq_close_.
  61. Note that it is not necessary to call _zmq_poller_remove_ for any socket
  62. before calling _zmq_poller_destroy_.
  63. Also note that calling _zmq_poller_remove_ is not equivalent to calling
  64. _zmq_poller_modify_ with no events. _zmq_poller_modify_ does not free resources
  65. associated with the socket registration, and requires that the _socket_
  66. remains valid.
  67. _zmq_poller_add_fd_, _zmq_poller_modify_fd_ and _zmq_poller_remove_fd_ are
  68. analogous to the previous functions but manage regular file descriptiors
  69. registered with a poller. On Windows, these functions can only be used with
  70. WinSock sockets.
  71. In the following, 0MQ sockets added with _zmq_poller_add_ and file descriptors
  72. added with _zmq_poller_add_fd_ are referred to as 'registered objects'.
  73. The *zmq_poller_event_t* structure is defined as follows:
  74. ["literal", subs="quotes"]
  75. typedef struct
  76. {
  77. void *socket;
  78. zmq_fd_t fd;
  79. void *user_data;
  80. short events;
  81. } zmq_poller_event_t;
  82. For each registered object, _zmq_poller_wait_all()_ shall examine the
  83. registered objects for the event(s) currently registered.
  84. If none of the registered events have occurred, _zmq_poller_wait_all_ shall
  85. wait 'timeout' milliseconds for an event to occur on any of the registered
  86. objects. If the value of 'timeout' is `0`, _zmq_poller_wait_all_ shall
  87. return immediately. If the value of 'timeout' is `-1`, _zmq_poller_wait_all_
  88. shall block indefinitely until one event has occurred on any of the
  89. registered objects.
  90. The 'events' argument _zmq_poller_wait_all_ must be a pointer to an array of
  91. at least 'n_events' elements. Behaviour is undefined if 'events' does not point
  92. to an array of at least 'n_events' elements.
  93. _zmq_poller_wait_all_ returns at most 'n_events' events. If more than
  94. 'n_events' events were signalled, only an unspecified subset of the signalled
  95. events is returned through 'events'.
  96. A caller is advised to ensure that 'n_events' is equal to the number of
  97. registered objects. Otherwise, a livelock situation may result: If more than
  98. 'n_events' registered objects have an active event on each call to
  99. _zmq_poller_wait_all_, it might happen that the same subset of registered
  100. objects is always returned, and the caller never notices the events on the
  101. others. The number of objects registered can be queried with
  102. _zmq_poller_size_.
  103. _zmq_poller_wait_all_ returns the number of valid elements. The valid elements
  104. are placed in positions '0' to 'n_events - 1' in the 'events' array. All
  105. members of a valid element are set to valid values by _zmq_poller_wait_all_.
  106. For socket events 'socket' is non-null and 'fd' is an operating system
  107. specific value for an invalid socket (-1 or INVALID_SOCKET). For fd events
  108. 'socket' is NULL and 'fd' is a valid file descriptor.
  109. The client does therefore not need to initialize the contents of the events
  110. array before a call to _zmq_poller_wait_all_. It is unspecified whether the
  111. the remaining elements of 'events' are written to by _zmq_poller_wait_all_.
  112. _zmq_poller_fd_ queries the file descriptor associated with the zmq_poller,
  113. and stores it in the address pointer to by 'fd'.
  114. The zmq_poller is only guaranteed to have a file descriptor if
  115. at least one thread-safe socket is currently registered.
  116. Note that closing a socket that is registered in a poller leads to undefined
  117. behavior. The socket must be unregistered first.
  118. EVENT TYPES
  119. -----------
  120. The 'events' parameter of _zmq_poller_add_ and _zmq_poller_modify_, and the
  121. 'events' member of the zmq_poller_event_t structure are bit masks constructed
  122. by OR'ing a combination of the following event flags:
  123. *ZMQ_POLLIN*::
  124. For 0MQ sockets, at least one message may be received from the 'socket' without
  125. blocking. For standard sockets this is equivalent to the 'POLLIN' flag of the
  126. _poll()_ system call and generally means that at least one byte of data may be
  127. read from 'fd' without blocking.
  128. *ZMQ_POLLOUT*::
  129. For 0MQ sockets, at least one message may be sent to the 'socket' without
  130. blocking. For standard sockets this is equivalent to the 'POLLOUT' flag of the
  131. _poll()_ system call and generally means that at least one byte of data may be
  132. written to 'fd' without blocking.
  133. *ZMQ_POLLERR*::
  134. For 0MQ sockets this flag has no effect on the _zmq_poller_add_ and
  135. _zmq_poller_modify_ functions, and is never set in the
  136. 'events' member of the zmq_poller_event_t structure.
  137. For standard sockets, this flag is passed through _zmq_poller_wait_all_ to the
  138. underlying _poll()_ system call and generally means that some sort of error
  139. condition is present on the socket specified by 'fd'.
  140. *ZMQ_POLLPRI*::
  141. For 0MQ sockets this flag has no effect on the _zmq_poller_add_ and
  142. _zmq_poller_modify_ functions, and is never set in the
  143. 'events' member of the zmq_poller_event_t structure.
  144. For standard sockets this means there
  145. is urgent data to read. Refer to the POLLPRI flag for more informations.
  146. For a file descriptor, refer to your OS documentation: as an example, GPIO
  147. interrupts are signaled through a POLLPRI event.
  148. This flag has no effect on Windows.
  149. NOTE: The _zmq_poller_*_ functions may be implemented or emulated using operating
  150. system interfaces other than _poll()_, and as such may be subject to the limits
  151. of those interfaces in ways not defined in this documentation.
  152. THREAD SAFETY
  153. -------------
  154. Like most other 0MQ objects, a poller is not thread-safe. All operations must
  155. be called from the same thread. Otherwise, behaviour is undefined.
  156. In addition to that, if you want to add a socket to multiple existing poller
  157. instances, the socket itself needs to be thread-safe (Server, Client, ...).
  158. Otherwise, behaviour is undefined.
  159. RETURN VALUE
  160. ------------
  161. _zmq_poller_new_ returns a valid pointer to a poller, or NULL in case of a failure.
  162. All functions that return an int, return -1 in case of a failure. In that case,
  163. zmq_errno() can be used to query the type of the error as described below.
  164. _zmq_poller_wait_all_ returns the number of events signalled and returned in
  165. the events array. It never returns 0.
  166. All other functions return 0 in case of a successful execution.
  167. ERRORS
  168. ------
  169. On _zmq_poller_new_:
  170. *ENOMEM*::
  171. A new poller could not be allocated successfully.
  172. On _zmq_poller_destroy_:
  173. *EFAULT*::
  174. _poller_p_ did not point to a valid poller. Note that passing an invalid pointer (e.g.
  175. pointer to deallocated memory) may cause undefined behaviour (e.g. an access violation).
  176. On _zmq_poller_size_:
  177. *EFAULT*::
  178. _poller_ did not point to a valid poller. Note that passing an
  179. invalid pointer (e.g. pointer to deallocated memory) may cause undefined
  180. behaviour (e.g. an access violation).
  181. On _zmq_poller_add_, _zmq_poller_modify_ and _zmq_poller_remove_:
  182. *EFAULT*::
  183. _poller_ did not point to a valid poller. Note that passing an
  184. invalid pointer (e.g. pointer to deallocated memory) may cause undefined
  185. behaviour (e.g. an access violation).
  186. *ENOTSOCK*::
  187. _socket_ did not point to a valid socket. Note that passing an
  188. invalid pointer (e.g. pointer to deallocated memory) may cause undefined
  189. behaviour (e.g. an access violation).
  190. On _zmq_poller_add_:
  191. *EMFILE*::
  192. TODO
  193. On _zmq_poller_add_ or _zmq_poller_add_fd_:
  194. *ENOMEM*::
  195. Necessary resources could not be allocated.
  196. *EINVAL*::
  197. _socket_ resp. _fd_ was already registered with the poller.
  198. On _zmq_poller_modify_, _zmq_poller_modify_fd_, _zmq_poller_remove_ or
  199. _zmq_poller_remove_fd_:
  200. *EINVAL*::
  201. _socket_ resp. _fd_ was not registered with the poller.
  202. On _zmq_poller_add_fd_, _zmq_poller_modify_fd_ and _zmq_poller_remove_fd_:
  203. *EBADF**:
  204. The _fd_ specified was the retired fd.
  205. On _zmq_poller_wait_ and _zmq_poller_wait_all_:
  206. *ENOMEM*::
  207. Necessary resources could not be allocated.
  208. *ETERM*::
  209. At least one of the registered objects is a 'socket' whose associated 0MQ
  210. 'context' was terminated.
  211. *EFAULT*::
  212. The provided 'events' was NULL, or 'poller' did not point to a valid poller,
  213. or there are no registered objects or all event subscriptions are disabled
  214. and 'timeout' was negative.
  215. *EINTR*::
  216. The operation was interrupted by delivery of a signal before any events were
  217. available.
  218. *EAGAIN*::
  219. No registered event was signalled before the timeout was reached.
  220. On _zmq_poller_fd_:
  221. *EINVAL*::
  222. The poller has no associated file descriptor.
  223. *EFAULT*::
  224. The provided 'poller' did not point to a valid poller.
  225. EXAMPLE
  226. -------
  227. .Polling indefinitely for input events on both a 0MQ socket and a standard socket.
  228. ----
  229. void *poller = zmq_poller_new ();
  230. zmq_poller_event_t events [2];
  231. /* First item refers to 0MQ socket 'socket' */
  232. zmq_poller_add (poller, socket, NULL, ZMQ_POLLIN);
  233. /* Second item refers to standard socket 'fd' */
  234. zmq_poller_add_fd (poller, fd, NULL, ZMQ_POLLIN);
  235. /* Poll for events indefinitely */
  236. int rc = zmq_poller_wait_all (poller, events, 2, -1);
  237. assert (rc >= 0);
  238. /* Returned events will be stored in 'events' */
  239. zmq_poller_destroy (poller);
  240. ----
  241. SEE ALSO
  242. --------
  243. linkzmq:zmq_socket[3]
  244. linkzmq:zmq_send[3]
  245. linkzmq:zmq_recv[3]
  246. linkzmq:zmq[7]
  247. AUTHORS
  248. -------
  249. This page was written by the 0MQ community. To make a change please
  250. read the 0MQ Contribution Policy at <http://www.zeromq.org/docs:contributing>.