thread.cpp 2.61 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2010-2011 250bpm s.r.o.
3 4
    Copyright (c) 2007-2011 iMatix Corporation
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
5 6 7 8

    This file is part of 0MQ.

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

18
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
19 20 21 22 23 24 25
    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
26
#ifdef ZMQ_HAVE_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
27

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

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

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

#else

#include <signal.h>

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

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

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

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

#endif