thread.cpp 5 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Martin Sustrik's avatar
Martin Sustrik committed
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
Martin Sustrik's avatar
Martin Sustrik committed
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq 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 GNU Lesser General Public
    License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "precompiled.hpp"
31
#include "macros.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
32 33 34
#include "thread.hpp"
#include "err.hpp"

Martin Sustrik's avatar
Martin Sustrik committed
35
#ifdef ZMQ_HAVE_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
36

37 38
extern "C"
{
boris@boressoft.ru's avatar
boris@boressoft.ru committed
39
#if defined _WIN32_WCE
40
    static DWORD thread_routine (LPVOID arg_)
boris@boressoft.ru's avatar
boris@boressoft.ru committed
41
#else
42
    static unsigned int __stdcall thread_routine (void *arg_)
boris@boressoft.ru's avatar
boris@boressoft.ru committed
43
#endif
44
    {
45
        zmq::thread_t *self = (zmq::thread_t*) arg_;
46 47 48 49 50
        self->tfn (self->arg);
        return 0;
    }
}

Martin Sustrik's avatar
Martin Sustrik committed
51
void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
Martin Sustrik's avatar
Martin Sustrik committed
52 53
{
    tfn = tfn_;
Pieter Hintjens's avatar
Pieter Hintjens committed
54
    arg = arg_;
55
#if defined _WIN32_WCE
boris@boressoft.ru's avatar
boris@boressoft.ru committed
56 57 58
    descriptor = (HANDLE) CreateThread (NULL, 0,
        &::thread_routine, this, 0 , NULL);
#else
Martin Sustrik's avatar
Martin Sustrik committed
59
    descriptor = (HANDLE) _beginthreadex (NULL, 0,
60
        &::thread_routine, this, 0 , NULL);
boris@boressoft.ru's avatar
boris@boressoft.ru committed
61
#endif
62
    win_assert (descriptor != NULL);
Martin Sustrik's avatar
Martin Sustrik committed
63 64
}

Martin Sustrik's avatar
Martin Sustrik committed
65
void zmq::thread_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
66 67 68
{
    DWORD rc = WaitForSingleObject (descriptor, INFINITE);
    win_assert (rc != WAIT_FAILED);
69
    BOOL rc2 = CloseHandle (descriptor);
70
    win_assert (rc2 != 0);
Martin Sustrik's avatar
Martin Sustrik committed
71 72
}

73 74 75
void zmq::thread_t::setSchedulingParameters(int priority_, int schedulingPolicy_)
{
    // not implemented
76 77
    LIBZMQ_UNUSED (priority_);
    LIBZMQ_UNUSED (schedulingPolicy_);
78 79
}

80 81 82 83 84 85
void zmq::thread_t::setThreadName(const char *name_)
{
    // not implemented
    LIBZMQ_UNUSED (name_);
}

Martin Sustrik's avatar
Martin Sustrik committed
86 87 88
#else

#include <signal.h>
89
#include <unistd.h>
Martin Sustrik's avatar
Martin Sustrik committed
90

91 92 93 94
extern "C"
{
    static void *thread_routine (void *arg_)
    {
95 96
#if !defined ZMQ_HAVE_OPENVMS && !defined ZMQ_HAVE_ANDROID
        //  Following code will guarantee more predictable latencies as it'll
97 98 99 100 101
        //  disallow any signal handling in the I/O thread.
        sigset_t signal_set;
        int rc = sigfillset (&signal_set);
        errno_assert (rc == 0);
        rc = pthread_sigmask (SIG_BLOCK, &signal_set, NULL);
102
        posix_assert (rc);
103
#endif
104

105
        zmq::thread_t *self = (zmq::thread_t*) arg_;
106 107 108 109 110
        self->tfn (self->arg);
        return NULL;
    }
}

Martin Sustrik's avatar
Martin Sustrik committed
111
void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
Martin Sustrik's avatar
Martin Sustrik committed
112 113
{
    tfn = tfn_;
Pieter Hintjens's avatar
Pieter Hintjens committed
114
    arg = arg_;
Martin Sustrik's avatar
Martin Sustrik committed
115
    int rc = pthread_create (&descriptor, NULL, thread_routine, this);
116
    posix_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
117 118
}

Martin Sustrik's avatar
Martin Sustrik committed
119
void zmq::thread_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
120 121
{
    int rc = pthread_join (descriptor, NULL);
122
    posix_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
123 124
}

125 126
void zmq::thread_t::setSchedulingParameters(int priority_, int schedulingPolicy_)
{
127
#if defined _POSIX_THREAD_PRIORITY_SCHEDULING && _POSIX_THREAD_PRIORITY_SCHEDULING >= 0
128 129 130
    int policy = 0;
    struct sched_param param;

131 132 133 134 135
#if _POSIX_THREAD_PRIORITY_SCHEDULING == 0 && defined _SC_THREAD_PRIORITY_SCHEDULING
    if (sysconf(_SC_THREAD_PRIORITY_SCHEDULING) < 0) {
        return;
    }
#endif
136 137 138 139 140 141 142 143 144 145 146 147 148
    int rc = pthread_getschedparam(descriptor, &policy, &param);
    posix_assert (rc);

    if(priority_ != -1)
    {
        param.sched_priority = priority_;
    }

    if(schedulingPolicy_ != -1)
    {
        policy = schedulingPolicy_;
    }

149 150 151 152
#ifdef __NetBSD__
    if(policy == SCHED_OTHER) param.sched_priority = -1;
#endif

153
    rc = pthread_setschedparam(descriptor, policy, &param);
154 155 156 157 158 159

#ifdef __FreeBSD_kernel__
    // If this feature is unavailable at run-time, don't abort.
    if(rc == ENOSYS) return;
#endif

160
    posix_assert (rc);
161 162 163 164
#else

    LIBZMQ_UNUSED (priority_);
    LIBZMQ_UNUSED (schedulingPolicy_);
165
#endif
166 167
}

168 169 170 171 172
void zmq::thread_t::setThreadName(const char *name_)
{
    if (!name_)
        return;

173
#if defined(ZMQ_HAVE_PTHREAD_SETNAME_1)
174 175
    int rc = pthread_setname_np(name_);
    posix_assert (rc);
176
#elif defined(ZMQ_HAVE_PTHREAD_SETNAME_2)
177 178
    int rc = pthread_setname_np(descriptor, name_);
    posix_assert (rc);
179
#elif defined(ZMQ_HAVE_PTHREAD_SETNAME_3)
180 181
    int rc = pthread_setname_np(descriptor, name_, NULL);
    posix_assert (rc);
182
#elif defined(ZMQ_HAVE_PTHREAD_SET_NAME)
183 184 185 186
    pthread_set_name_np(descriptor, name_);
#endif
}

Martin Sustrik's avatar
Martin Sustrik committed
187
#endif