Mailbox.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. Copyright (c) 2010-2011 250bpm s.r.o.
  3. Copyright (c) 2007-2009 iMatix Corporation
  4. Copyright (c) 2007-2015 Other contributors as noted in the AUTHORS file
  5. This file is part of 0MQ.
  6. 0MQ is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. 0MQ is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. using System;
  18. using System.Diagnostics;
  19. using System.Net.Sockets;
  20. using NetMQ.Core.Utils;
  21. namespace NetMQ.Core
  22. {
  23. internal interface IMailboxEvent
  24. {
  25. void Ready();
  26. }
  27. internal class IOThreadMailbox : IMailbox
  28. {
  29. private readonly Proactor m_proactor;
  30. private readonly IMailboxEvent m_mailboxEvent;
  31. private readonly YPipe<Command> m_commandPipe = new YPipe<Command>(Config.CommandPipeGranularity, "mailbox");
  32. /// <summary>
  33. /// There's only one thread receiving from the mailbox, but there
  34. /// is arbitrary number of threads sending. Given that ypipe requires
  35. /// synchronised access on both of its endpoints, we have to synchronize
  36. /// the sending side.
  37. /// </summary>
  38. private readonly object m_sync = new object();
  39. #if DEBUG
  40. /// <summary>Mailbox name. Only used for debugging.</summary>
  41. private readonly string m_name;
  42. #endif
  43. private bool m_disposed;
  44. public IOThreadMailbox(string name, Proactor proactor, IMailboxEvent mailboxEvent)
  45. {
  46. m_proactor = proactor;
  47. m_mailboxEvent = mailboxEvent;
  48. // Get the pipe into passive state. That way, if the users starts by
  49. // polling on the associated file descriptor it will get woken up when
  50. // new command is posted.
  51. bool ok = m_commandPipe.TryRead(out Command cmd);
  52. Debug.Assert(!ok);
  53. #if DEBUG
  54. m_name = name;
  55. #endif
  56. }
  57. public void Send(Command command)
  58. {
  59. bool ok;
  60. lock (m_sync)
  61. {
  62. m_commandPipe.Write(ref command, false);
  63. ok = m_commandPipe.Flush();
  64. }
  65. if (!ok)
  66. {
  67. m_proactor.SignalMailbox(this);
  68. }
  69. }
  70. public bool TryRecv(int timeout, out Command command)
  71. {
  72. throw new System.NotImplementedException();
  73. }
  74. public bool TryRecv(out Command command)
  75. {
  76. return m_commandPipe.TryRead(out command);
  77. }
  78. public void RaiseEvent()
  79. {
  80. if (!m_disposed)
  81. {
  82. m_mailboxEvent.Ready();
  83. }
  84. }
  85. public void Close()
  86. {
  87. m_disposed = true;
  88. }
  89. #if DEBUG
  90. public override string ToString()
  91. {
  92. return base.ToString() + "[" + m_name + "]";
  93. }
  94. #endif
  95. }
  96. internal class Mailbox : IMailbox
  97. {
  98. /// <summary>
  99. /// The pipe to store actual commands.
  100. /// </summary>
  101. private readonly YPipe<Command> m_commandPipe = new YPipe<Command>(Config.CommandPipeGranularity, "mailbox");
  102. /// <summary>
  103. /// Signaler to pass signals from writer thread to reader thread.
  104. /// </summary>
  105. private readonly Signaler m_signaler = new Signaler();
  106. /// <summary>
  107. /// There's only one thread receiving from the mailbox, but there
  108. /// is an arbitrary number of threads sending. Given that <see cref="YPipe{T}"/> requires
  109. /// synchronised access on both of its endpoints, we have to synchronize
  110. /// the sending side.
  111. /// </summary>
  112. private readonly object m_sync = new object();
  113. /// <summary>
  114. /// True if the underlying pipe is active, ie. when we are allowed to
  115. /// read commands from it.
  116. /// </summary>
  117. private bool m_active;
  118. #if DEBUG
  119. /// <summary>Mailbox name. Only used for debugging.</summary>
  120. private readonly string m_name;
  121. #endif
  122. /// <summary>
  123. /// Create a new Mailbox with the given name.
  124. /// </summary>
  125. /// <param name="name">the name to give this new Mailbox</param>
  126. public Mailbox(string name)
  127. {
  128. // Get the pipe into passive state. That way, if the users starts by
  129. // polling on the associated file descriptor it will get woken up when
  130. // new command is posted.
  131. bool ok = m_commandPipe.TryRead(out Command cmd);
  132. //Debug.Assert(!ok);
  133. m_active = false;
  134. #if DEBUG
  135. m_name = name;
  136. #endif
  137. }
  138. /// <summary>
  139. /// Get the socket-handle contained by the Signaler.
  140. /// </summary>
  141. public Socket Handle => m_signaler.Handle;
  142. /// <summary>
  143. /// Send the given Command out across the command-pipe.
  144. /// </summary>
  145. /// <param name="cmd">the Command to send</param>
  146. public void Send(Command cmd)
  147. {
  148. bool ok;
  149. lock (m_sync)
  150. {
  151. m_commandPipe.Write(ref cmd, false);
  152. ok = m_commandPipe.Flush();
  153. }
  154. //if (LOG.isDebugEnabled())
  155. // LOG.debug( "{} -> {} / {} {}", new Object[] { Thread.currentThread().getName(), cmd_, cmd_.arg , !ok});
  156. if (!ok)
  157. {
  158. m_signaler.Send();
  159. }
  160. }
  161. /// <summary>
  162. /// Receive and return a Command from the command-pipe.
  163. /// </summary>
  164. /// <param name="timeout">how long to wait for a command (in milliseconds) before returning</param>
  165. /// <param name="command"></param>
  166. public bool TryRecv(int timeout, out Command command)
  167. {
  168. // Try to get the command straight away.
  169. if (m_active)
  170. {
  171. if (m_commandPipe.TryRead(out command))
  172. return true;
  173. // If there are no more commands available, switch into passive state.
  174. try
  175. {
  176. m_active = false;
  177. m_signaler.Recv();
  178. }
  179. catch
  180. {
  181. m_active = true;
  182. command = default(Command);
  183. return false;
  184. }
  185. }
  186. // Wait for signal from the command sender.
  187. if (!m_signaler.WaitEvent(timeout))
  188. {
  189. command = default(Command);
  190. return false;
  191. }
  192. // We've got the signal. Now we can switch into active state.
  193. m_active = true;
  194. // Get a command.
  195. var ok = m_commandPipe.TryRead(out command);
  196. // Debug.Assert(ok);
  197. return ok;
  198. }
  199. /// <summary>
  200. /// Close the contained Signaler.
  201. /// </summary>
  202. public void Close()
  203. {
  204. m_signaler.Close();
  205. }
  206. #if DEBUG
  207. /// <summary>
  208. /// Override ToString to provide the type-name, plus the Mailbox name within brackets.
  209. /// </summary>
  210. /// <returns>a string of the form Mailbox[name]</returns>
  211. public override string ToString()
  212. {
  213. return base.ToString() + "[" + m_name + "]";
  214. }
  215. #endif
  216. }
  217. }