thread.cpp 4.11 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"
Martin Sustrik's avatar
Martin Sustrik committed
31 32 33 34
#include "thread.hpp"
#include "err.hpp"
#include "platform.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 76 77
void zmq::thread_t::setSchedulingParameters(int priority_, int schedulingPolicy_)
{
    // not implemented
}

Martin Sustrik's avatar
Martin Sustrik committed
78 79 80
#else

#include <signal.h>
81
#include <unistd.h>
Martin Sustrik's avatar
Martin Sustrik committed
82

83 84 85 86
extern "C"
{
    static void *thread_routine (void *arg_)
    {
87 88
#if !defined ZMQ_HAVE_OPENVMS && !defined ZMQ_HAVE_ANDROID
        //  Following code will guarantee more predictable latencies as it'll
89 90 91 92 93
        //  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);
94
        posix_assert (rc);
95
#endif
96

97
        zmq::thread_t *self = (zmq::thread_t*) arg_;
98 99 100 101 102
        self->tfn (self->arg);
        return NULL;
    }
}

Martin Sustrik's avatar
Martin Sustrik committed
103
void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
Martin Sustrik's avatar
Martin Sustrik committed
104 105
{
    tfn = tfn_;
Pieter Hintjens's avatar
Pieter Hintjens committed
106
    arg = arg_;
Martin Sustrik's avatar
Martin Sustrik committed
107
    int rc = pthread_create (&descriptor, NULL, thread_routine, this);
108
    posix_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
109 110
}

Martin Sustrik's avatar
Martin Sustrik committed
111
void zmq::thread_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
112 113
{
    int rc = pthread_join (descriptor, NULL);
114
    posix_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
115 116
}

117 118
void zmq::thread_t::setSchedulingParameters(int priority_, int schedulingPolicy_)
{
119
#if defined _POSIX_THREAD_PRIORITY_SCHEDULING && _POSIX_THREAD_PRIORITY_SCHEDULING >= 0
120 121 122
    int policy = 0;
    struct sched_param param;

123 124 125 126 127
#if _POSIX_THREAD_PRIORITY_SCHEDULING == 0 && defined _SC_THREAD_PRIORITY_SCHEDULING
    if (sysconf(_SC_THREAD_PRIORITY_SCHEDULING) < 0) {
        return;
    }
#endif
128 129 130 131 132 133 134 135 136 137 138 139 140
    int rc = pthread_getschedparam(descriptor, &policy, &param);
    posix_assert (rc);

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

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

141 142 143 144
#ifdef __NetBSD__
    if(policy == SCHED_OTHER) param.sched_priority = -1;
#endif

145 146
    rc = pthread_setschedparam(descriptor, policy, &param);
    posix_assert (rc);
147
#endif
148 149
}

Martin Sustrik's avatar
Martin Sustrik committed
150 151 152 153 154 155
#endif