app_thread.cpp 5.51 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2010 iMatix Corporation
Martin Sustrik's avatar
Martin Sustrik committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the Lesser GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

20
#include <new>
21 22
#include <algorithm>

23
#include "../bindings/c/zmq.h"
Martin Sustrik's avatar
Martin Sustrik committed
24

unknown's avatar
unknown committed
25 26
#include "platform.hpp"

Martin Sustrik's avatar
Martin Sustrik committed
27
#if defined ZMQ_HAVE_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
28
#include "windows.hpp"
29 30 31
#if defined _MSC_VER
#include <intrin.h>
#endif
Martin Sustrik's avatar
Martin Sustrik committed
32 33 34 35 36
#else
#include <unistd.h>
#endif

#include "app_thread.hpp"
37
#include "dispatcher.hpp"
38 39
#include "fd_signaler.hpp"
#include "ypollset.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
40 41 42
#include "err.hpp"
#include "pipe.hpp"
#include "config.hpp"
43
#include "socket_base.hpp"
44
#include "p2p.hpp"
45
#include "pub.hpp"
46
#include "sub.hpp"
47 48
#include "req.hpp"
#include "rep.hpp"
49 50
#include "xreq.hpp"
#include "xrep.hpp"
51 52
#include "upstream.hpp"
#include "downstream.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
53 54 55 56 57 58

//  If the RDTSC is available we use it to prevent excessive
//  polling for commands. The nice thing here is that it will work on any
//  system with x86 architecture and gcc or MSVC compiler.
#if (defined __GNUC__ && (defined __i386__ || defined __x86_64__)) ||\
    (defined _MSC_VER && (defined _M_IX86 || defined _M_X64))
Martin Sustrik's avatar
Martin Sustrik committed
59
#define ZMQ_DELAY_COMMANDS
Martin Sustrik's avatar
Martin Sustrik committed
60 61
#endif

62 63
zmq::app_thread_t::app_thread_t (dispatcher_t *dispatcher_, int thread_slot_,
      int flags_) :
64
    object_t (dispatcher_, thread_slot_),
65
    associated (false),
Martin Sustrik's avatar
Martin Sustrik committed
66 67
    last_processing_time (0)
{
68
    if (flags_ & ZMQ_POLL) {
69
        signaler = new (std::nothrow) fd_signaler_t;
70 71 72
        zmq_assert (signaler);
    }
    else {
73
        signaler = new (std::nothrow) ypollset_t;
74 75
        zmq_assert (signaler);
    }
Martin Sustrik's avatar
Martin Sustrik committed
76 77
}

Martin Sustrik's avatar
Martin Sustrik committed
78
zmq::app_thread_t::~app_thread_t ()
Martin Sustrik's avatar
Martin Sustrik committed
79
{
80
    zmq_assert (sockets.empty ());
81 82
    zmq_assert (signaler);
    delete signaler;
Martin Sustrik's avatar
Martin Sustrik committed
83 84
}

Martin Sustrik's avatar
Martin Sustrik committed
85
zmq::i_signaler *zmq::app_thread_t::get_signaler ()
Martin Sustrik's avatar
Martin Sustrik committed
86
{
87
    return signaler;
Martin Sustrik's avatar
Martin Sustrik committed
88 89
}

Martin Sustrik's avatar
Martin Sustrik committed
90
bool zmq::app_thread_t::is_current ()
Martin Sustrik's avatar
Martin Sustrik committed
91
{
92 93
    return !sockets.empty () && associated &&
        thread_t::equal (tid, thread_t::id ());
Martin Sustrik's avatar
Martin Sustrik committed
94 95
}

Martin Sustrik's avatar
Martin Sustrik committed
96
bool zmq::app_thread_t::make_current ()
Martin Sustrik's avatar
Martin Sustrik committed
97 98 99
{
    //  If there are object managed by this slot we cannot assign the slot
    //  to a different thread.
100
    if (!sockets.empty ())
Martin Sustrik's avatar
Martin Sustrik committed
101 102
        return false;

103 104
    associated = true;
    tid = thread_t::id ();
Martin Sustrik's avatar
Martin Sustrik committed
105 106 107
    return true;
}

Martin Sustrik's avatar
Martin Sustrik committed
108
void zmq::app_thread_t::process_commands (bool block_, bool throttle_)
Martin Sustrik's avatar
Martin Sustrik committed
109
{
110
    uint64_t signals;
Martin Sustrik's avatar
Martin Sustrik committed
111
    if (block_)
112
        signals = signaler->poll ();
Martin Sustrik's avatar
Martin Sustrik committed
113 114
    else {

Martin Sustrik's avatar
Martin Sustrik committed
115
#if defined ZMQ_DELAY_COMMANDS
Martin Sustrik's avatar
Martin Sustrik committed
116 117 118 119 120 121
        //  Optimised version of command processing - it doesn't have to check
        //  for incoming commands each time. It does so only if certain time
        //  elapsed since last command processing. Command delay varies
        //  depending on CPU speed: It's ~1ms on 3GHz CPU, ~2ms on 1.5GHz CPU
        //  etc. The optimisation makes sense only on platforms where getting
        //  a timestamp is a very cheap operation (tens of nanoseconds).
Martin Sustrik's avatar
Martin Sustrik committed
122
        if (throttle_) {
Martin Sustrik's avatar
Martin Sustrik committed
123

Martin Sustrik's avatar
Martin Sustrik committed
124
            //  Get timestamp counter.
Martin Sustrik's avatar
Martin Sustrik committed
125
#if defined __GNUC__
Martin Sustrik's avatar
Martin Sustrik committed
126 127 128 129
            uint32_t low;
            uint32_t high;
            __asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
            uint64_t current_time = (uint64_t) high << 32 | low;
Martin Sustrik's avatar
Martin Sustrik committed
130
#elif defined _MSC_VER
Martin Sustrik's avatar
Martin Sustrik committed
131
            uint64_t current_time = __rdtsc ();
Martin Sustrik's avatar
Martin Sustrik committed
132 133 134 135
#else
#error
#endif

Martin Sustrik's avatar
Martin Sustrik committed
136 137 138 139 140 141
            //  Check whether certain time have elapsed since last command
            //  processing.
            if (current_time - last_processing_time <= max_command_delay)
                return;
            last_processing_time = current_time;
        }
Martin Sustrik's avatar
Martin Sustrik committed
142 143 144
#endif

        //  Check whether there are any commands pending for this thread.
145
        signals = signaler->check ();
Martin Sustrik's avatar
Martin Sustrik committed
146 147 148 149 150 151 152
    }

    if (signals) {

        //  Traverse all the possible sources of commands and process
        //  all the commands from all of them.
        for (int i = 0; i != thread_slot_count (); i++) {
153
            if (signals & (uint64_t (1) << i)) {
Martin Sustrik's avatar
Martin Sustrik committed
154
                command_t cmd;
155
                while (dispatcher->read (i, get_thread_slot (), &cmd))
Martin Sustrik's avatar
Martin Sustrik committed
156 157 158 159 160
                    cmd.destination->process_command (cmd);
            }
        }
    }
}
161

162
zmq::socket_base_t *zmq::app_thread_t::create_socket (int type_)
163
{
164 165
    socket_base_t *s = NULL;
    switch (type_) {
166
    case ZMQ_P2P:
167
        s = new (std::nothrow) p2p_t (this);
168
        break;
169
    case ZMQ_PUB:
170
        s = new (std::nothrow) pub_t (this);
171
        break;
172
    case ZMQ_SUB:
173
        s = new (std::nothrow) sub_t (this);
174 175
        break;
    case ZMQ_REQ:
176
        s = new (std::nothrow) req_t (this);
177
        break;
178
    case ZMQ_REP:
179
        s = new (std::nothrow) rep_t (this);
180
        break;
181
    case ZMQ_XREQ:
182
        s = new (std::nothrow) xreq_t (this);
183 184
        break;
    case ZMQ_XREP:
185
        s = new (std::nothrow) xrep_t (this);
186
        break;       
187
    case ZMQ_UPSTREAM:
188
        s = new (std::nothrow) upstream_t (this);
189 190
        break;
    case ZMQ_DOWNSTREAM:
191
        s = new (std::nothrow) downstream_t (this);
192 193
        break;
    default:
Martin Sustrik's avatar
Martin Sustrik committed
194 195
        errno = EINVAL;
        return NULL;
196
    }
197
    zmq_assert (s);
198

199
    sockets.push_back (s);
200

201 202 203
    return s;
}

204
void zmq::app_thread_t::remove_socket (socket_base_t *socket_)
205
{
206
    sockets.erase (socket_);
207
}