Coverage Report

Created: 2026-01-25 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzmq/src/mailbox.cpp
Line
Count
Source
1
/* SPDX-License-Identifier: MPL-2.0 */
2
3
#include "precompiled.hpp"
4
#include "mailbox.hpp"
5
#include "err.hpp"
6
7
zmq::mailbox_t::mailbox_t ()
8
0
{
9
    //  Get the pipe into passive state. That way, if the users starts by
10
    //  polling on the associated file descriptor it will get woken up when
11
    //  new command is posted.
12
0
    const bool ok = _cpipe.check_read ();
13
0
    zmq_assert (!ok);
14
0
    _active = false;
15
0
}
16
17
zmq::mailbox_t::~mailbox_t ()
18
0
{
19
    //  TODO: Retrieve and deallocate commands inside the _cpipe.
20
21
    // Work around problem that other threads might still be in our
22
    // send() method, by waiting on the mutex before disappearing.
23
0
    _sync.lock ();
24
0
    _sync.unlock ();
25
0
}
26
27
zmq::fd_t zmq::mailbox_t::get_fd () const
28
0
{
29
0
    return _signaler.get_fd ();
30
0
}
31
32
void zmq::mailbox_t::send (const command_t &cmd_)
33
0
{
34
0
    _sync.lock ();
35
0
    _cpipe.write (cmd_, false);
36
0
    const bool ok = _cpipe.flush ();
37
0
    _sync.unlock ();
38
0
    if (!ok)
39
0
        _signaler.send ();
40
0
}
41
42
int zmq::mailbox_t::recv (command_t *cmd_, int timeout_)
43
0
{
44
    //  Try to get the command straight away.
45
0
    if (_active) {
46
0
        if (_cpipe.read (cmd_))
47
0
            return 0;
48
49
        //  If there are no more commands available, switch into passive state.
50
0
        _active = false;
51
0
    }
52
53
    //  Wait for signal from the command sender.
54
0
    int rc = _signaler.wait (timeout_);
55
0
    if (rc == -1) {
56
0
        errno_assert (errno == EAGAIN || errno == EINTR);
57
0
        return -1;
58
0
    }
59
60
    //  Receive the signal.
61
0
    rc = _signaler.recv_failable ();
62
0
    if (rc == -1) {
63
0
        errno_assert (errno == EAGAIN);
64
0
        return -1;
65
0
    }
66
67
    //  Switch into active state.
68
0
    _active = true;
69
70
    //  Get a command.
71
0
    const bool ok = _cpipe.read (cmd_);
72
0
    zmq_assert (ok);
73
0
    return 0;
74
0
}
75
76
bool zmq::mailbox_t::valid () const
77
0
{
78
0
    return _signaler.valid ();
79
0
}