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

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
7
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
8 9 10 11 12 13
    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
14
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
15

16
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
17 18 19 20 21 22 23
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "thread.hpp"
#include "err.hpp"
#include "platform.hpp"

Martin Sustrik's avatar
Martin Sustrik committed
24
#ifdef ZMQ_HAVE_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
25

26 27
extern "C"
{
boris@boressoft.ru's avatar
boris@boressoft.ru committed
28 29 30
#if defined _WIN32_WCE
	static DWORD thread_routine (LPVOID arg_)
#else
31
    static unsigned int __stdcall thread_routine (void *arg_)
boris@boressoft.ru's avatar
boris@boressoft.ru committed
32
#endif
33
    {
34
        zmq::thread_t *self = (zmq::thread_t*) arg_;
35 36 37 38 39
        self->tfn (self->arg);
        return 0;
    }
}

Martin Sustrik's avatar
Martin Sustrik committed
40
void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
Martin Sustrik's avatar
Martin Sustrik committed
41 42
{
    tfn = tfn_;
Pieter Hintjens's avatar
Pieter Hintjens committed
43
    arg = arg_;
44
#if defined _WIN32_WCE
boris@boressoft.ru's avatar
boris@boressoft.ru committed
45 46 47
    descriptor = (HANDLE) CreateThread (NULL, 0,
        &::thread_routine, this, 0 , NULL);
#else
Martin Sustrik's avatar
Martin Sustrik committed
48
    descriptor = (HANDLE) _beginthreadex (NULL, 0,
49
        &::thread_routine, this, 0 , NULL);
boris@boressoft.ru's avatar
boris@boressoft.ru committed
50
#endif
Martin Sustrik's avatar
Martin Sustrik committed
51 52 53
    win_assert (descriptor != NULL);    
}

Martin Sustrik's avatar
Martin Sustrik committed
54
void zmq::thread_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
55 56 57
{
    DWORD rc = WaitForSingleObject (descriptor, INFINITE);
    win_assert (rc != WAIT_FAILED);
58
    BOOL rc2 = CloseHandle (descriptor);
59
    win_assert (rc2 != 0);
Martin Sustrik's avatar
Martin Sustrik committed
60 61
}

62 63 64 65 66
void zmq::thread_t::setSchedulingParameters(int priority_, int schedulingPolicy_)
{
    // not implemented
}

Martin Sustrik's avatar
Martin Sustrik committed
67 68 69 70
#else

#include <signal.h>

71 72 73 74
extern "C"
{
    static void *thread_routine (void *arg_)
    {
75 76
#if !defined ZMQ_HAVE_OPENVMS && !defined ZMQ_HAVE_ANDROID
        //  Following code will guarantee more predictable latencies as it'll
77 78 79 80 81
        //  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);
82
        posix_assert (rc);
83
#endif
84 85 86 87 88 89 90

        zmq::thread_t *self = (zmq::thread_t*) arg_;   
        self->tfn (self->arg);
        return NULL;
    }
}

Martin Sustrik's avatar
Martin Sustrik committed
91
void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
Martin Sustrik's avatar
Martin Sustrik committed
92 93
{
    tfn = tfn_;
Pieter Hintjens's avatar
Pieter Hintjens committed
94
    arg = arg_;
Martin Sustrik's avatar
Martin Sustrik committed
95
    int rc = pthread_create (&descriptor, NULL, thread_routine, this);
96
    posix_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
97 98
}

Martin Sustrik's avatar
Martin Sustrik committed
99
void zmq::thread_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
100 101
{
    int rc = pthread_join (descriptor, NULL);
102
    posix_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
103 104
}

105 106
void zmq::thread_t::setSchedulingParameters(int priority_, int schedulingPolicy_)
{
107
#if !defined ZMQ_HAVE_ZOS
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    int policy = 0;
    struct sched_param param;

    int rc = pthread_getschedparam(descriptor, &policy, &param);
    posix_assert (rc);

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

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

    rc = pthread_setschedparam(descriptor, policy, &param);
    posix_assert (rc);
126
#endif
127 128
}

Martin Sustrik's avatar
Martin Sustrik committed
129 130 131 132 133 134
#endif