thread.cpp 2.52 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2013 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 43
{
    tfn = tfn_;
    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
}

#else

#include <signal.h>

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

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

Martin Sustrik's avatar
Martin Sustrik committed
86
void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
Martin Sustrik's avatar
Martin Sustrik committed
87 88 89 90
{
    tfn = tfn_;
    arg =arg_;
    int rc = pthread_create (&descriptor, NULL, thread_routine, this);
91
    posix_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
92 93
}

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

#endif