1 | /* |
2 | Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file |
3 | |
4 | This file is part of libzmq, the ZeroMQ core engine in C++. |
5 | |
6 | libzmq is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU Lesser General Public License (LGPL) as published |
8 | by the Free Software Foundation; either version 3 of the License, or |
9 | (at your option) any later version. |
10 | |
11 | As a special exception, the Contributors give you permission to link |
12 | this library with independent modules to produce an executable, |
13 | regardless of the license terms of these independent modules, and to |
14 | copy and distribute the resulting executable under terms of your choice, |
15 | provided that you also meet, for each linked independent module, the |
16 | terms and conditions of the license of that module. An independent |
17 | module is a module which is not derived from or based on this library. |
18 | If you modify this library, you must extend this exception to your |
19 | version of the library. |
20 | |
21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
24 | License for more details. |
25 | |
26 | You should have received a copy of the GNU Lesser General Public License |
27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
28 | */ |
29 | |
30 | #include "precompiled.hpp" |
31 | #include "mailbox_safe.hpp" |
32 | #include "clock.hpp" |
33 | #include "err.hpp" |
34 | |
35 | #include <algorithm> |
36 | |
37 | zmq::mailbox_safe_t::mailbox_safe_t (mutex_t *sync_) : _sync (sync_) |
38 | { |
39 | // Get the pipe into passive state. That way, if the users starts by |
40 | // polling on the associated file descriptor it will get woken up when |
41 | // new command is posted. |
42 | const bool ok = _cpipe.check_read (); |
43 | zmq_assert (!ok); |
44 | } |
45 | |
46 | zmq::mailbox_safe_t::~mailbox_safe_t () |
47 | { |
48 | // TODO: Retrieve and deallocate commands inside the cpipe. |
49 | |
50 | // Work around problem that other threads might still be in our |
51 | // send() method, by waiting on the mutex before disappearing. |
52 | _sync->lock (); |
53 | _sync->unlock (); |
54 | } |
55 | |
56 | void zmq::mailbox_safe_t::add_signaler (signaler_t *signaler_) |
57 | { |
58 | _signalers.push_back (signaler_); |
59 | } |
60 | |
61 | void zmq::mailbox_safe_t::remove_signaler (signaler_t *signaler_) |
62 | { |
63 | // TODO: make a copy of array and signal outside the lock |
64 | const std::vector<zmq::signaler_t *>::iterator end = _signalers.end (); |
65 | std::vector<signaler_t *>::iterator it = |
66 | std::find (_signalers.begin (), end, signaler_); |
67 | |
68 | if (it != end) |
69 | _signalers.erase (it); |
70 | } |
71 | |
72 | void zmq::mailbox_safe_t::clear_signalers () |
73 | { |
74 | _signalers.clear (); |
75 | } |
76 | |
77 | void zmq::mailbox_safe_t::send (const command_t &cmd_) |
78 | { |
79 | _sync->lock (); |
80 | _cpipe.write (cmd_, false); |
81 | const bool ok = _cpipe.flush (); |
82 | |
83 | if (!ok) { |
84 | _cond_var.broadcast (); |
85 | |
86 | for (std::vector<signaler_t *>::iterator it = _signalers.begin (), |
87 | end = _signalers.end (); |
88 | it != end; ++it) { |
89 | (*it)->send (); |
90 | } |
91 | } |
92 | |
93 | _sync->unlock (); |
94 | } |
95 | |
96 | int zmq::mailbox_safe_t::recv (command_t *cmd_, int timeout_) |
97 | { |
98 | // Try to get the command straight away. |
99 | if (_cpipe.read (cmd_)) |
100 | return 0; |
101 | |
102 | // If the timeout is zero, it will be quicker to release the lock, giving other a chance to send a command |
103 | // and immediately relock it. |
104 | if (timeout_ == 0) { |
105 | _sync->unlock (); |
106 | _sync->lock (); |
107 | } else { |
108 | // Wait for signal from the command sender. |
109 | int rc = _cond_var.wait (_sync, timeout_); |
110 | if (rc == -1) { |
111 | errno_assert (errno == EAGAIN || errno == EINTR); |
112 | return -1; |
113 | } |
114 | } |
115 | |
116 | // Another thread may already fetch the command |
117 | const bool ok = _cpipe.read (cmd_); |
118 | |
119 | if (!ok) { |
120 | errno = EAGAIN; |
121 | return -1; |
122 | } |
123 | |
124 | return 0; |
125 | } |
126 | |