PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/clients/roscpp/include/ros/poll_set.h

https://gitlab.com/F34140r/ros_comm
C Header | 154 lines | 47 code | 17 blank | 90 comment | 0 complexity | c763b54889b86bea59fd7ba423a30b4f MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright (c) 2008, Willow Garage, Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. * * Neither the name of Willow Garage, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef ROSCPP_POLL_SET_H
  35. #define ROSCPP_POLL_SET_H
  36. #include <vector>
  37. #include "io.h"
  38. #include "common.h"
  39. #include <boost/shared_ptr.hpp>
  40. #include <boost/function.hpp>
  41. #include <boost/thread/mutex.hpp>
  42. namespace ros
  43. {
  44. class Transport;
  45. typedef boost::shared_ptr<Transport> TransportPtr;
  46. /**
  47. * \brief Manages a set of sockets being polled through the poll() function call.
  48. *
  49. * PollSet provides thread-safe ways of adding and deleting sockets, as well as adding
  50. * and deleting events.
  51. */
  52. class ROSCPP_DECL PollSet
  53. {
  54. public:
  55. PollSet();
  56. ~PollSet();
  57. typedef boost::function<void(int)> SocketUpdateFunc;
  58. /**
  59. * \brief Add a socket.
  60. *
  61. * addSocket() may be called from any thread.
  62. * \param sock The socket to add
  63. * \param update_func The function to call when a socket has events
  64. * \param transport The (optional) transport associated with this socket. Mainly
  65. * used to prevent the transport from being deleted while we're calling the update function
  66. */
  67. bool addSocket(int sock, const SocketUpdateFunc& update_func, const TransportPtr& transport = TransportPtr());
  68. /**
  69. * \brief Delete a socket
  70. *
  71. * delSocket() may be called from any thread.
  72. * \param sock The socket to delete
  73. */
  74. bool delSocket(int sock);
  75. /**
  76. * \brief Add events to be polled on a socket
  77. *
  78. * addEvents() may be called from any thread.
  79. * \param sock The socket to add events to
  80. * \param events The events to add
  81. */
  82. bool addEvents(int sock, int events);
  83. /**
  84. * \brief Delete events to be polled on a socket
  85. *
  86. * delEvents() may be called from any thread.
  87. * \param sock The socket to delete events from
  88. * \param events The events to delete
  89. */
  90. bool delEvents(int sock, int events);
  91. /**
  92. * \brief Process all socket events
  93. *
  94. * This function will actually call poll() on the available sockets, and allow
  95. * them to do their processing.
  96. *
  97. * update() may only be called from one thread at a time
  98. *
  99. * \param poll_timeout The time, in milliseconds, for the poll() call to timeout after
  100. * if there are no events. Note that this does not provide an upper bound for the entire
  101. * function, just the call to poll()
  102. */
  103. void update(int poll_timeout);
  104. /**
  105. * \brief Signal our poll() call to finish if it's blocked waiting (see the poll_timeout
  106. * option for update()).
  107. */
  108. void signal();
  109. private:
  110. /**
  111. * \brief Creates the native pollset for our sockets, if any have changed
  112. */
  113. void createNativePollset();
  114. /**
  115. * \brief Called when events have been triggered on our signal pipe
  116. */
  117. void onLocalPipeEvents(int events);
  118. struct SocketInfo
  119. {
  120. TransportPtr transport_;
  121. SocketUpdateFunc func_;
  122. int fd_;
  123. int events_;
  124. };
  125. typedef std::map<int, SocketInfo> M_SocketInfo;
  126. M_SocketInfo socket_info_;
  127. boost::mutex socket_info_mutex_;
  128. bool sockets_changed_;
  129. boost::mutex just_deleted_mutex_;
  130. typedef std::vector<int> V_int;
  131. V_int just_deleted_;
  132. std::vector<socket_pollfd> ufds_;
  133. boost::mutex signal_mutex_;
  134. signal_fd_t signal_pipe_[2];
  135. };
  136. }
  137. #endif // ROSCPP_POLL_SET_H