thread.cpp 2.41 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-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 30 31
extern "C"
{
    static unsigned int __stdcall thread_routine (void *arg_)
    {
32
        zmq::thread_t *self = (zmq::thread_t*) arg_;
33 34 35 36 37
        self->tfn (self->arg);
        return 0;
    }
}

Martin Sustrik's avatar
Martin Sustrik committed
38
void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
Martin Sustrik's avatar
Martin Sustrik committed
39 40 41 42
{
    tfn = tfn_;
    arg =arg_;
    descriptor = (HANDLE) _beginthreadex (NULL, 0,
43
        &::thread_routine, this, 0 , NULL);
Martin Sustrik's avatar
Martin Sustrik committed
44 45 46
    win_assert (descriptor != NULL);    
}

Martin Sustrik's avatar
Martin Sustrik committed
47
void zmq::thread_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
48 49 50
{
    DWORD rc = WaitForSingleObject (descriptor, INFINITE);
    win_assert (rc != WAIT_FAILED);
51
    BOOL rc2 = CloseHandle (descriptor);
52
    win_assert (rc2 != 0);
Martin Sustrik's avatar
Martin Sustrik committed
53 54 55 56 57 58
}

#else

#include <signal.h>

59 60 61 62
extern "C"
{
    static void *thread_routine (void *arg_)
    {
63 64
#if !defined ZMQ_HAVE_OPENVMS && !defined ZMQ_HAVE_ANDROID
        //  Following code will guarantee more predictable latencies as it'll
65 66 67 68 69
        //  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);
70
        posix_assert (rc);
71
#endif
72 73 74 75 76 77 78

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

Martin Sustrik's avatar
Martin Sustrik committed
79
void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
Martin Sustrik's avatar
Martin Sustrik committed
80 81 82 83
{
    tfn = tfn_;
    arg =arg_;
    int rc = pthread_create (&descriptor, NULL, thread_routine, this);
84
    posix_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
85 86
}

Martin Sustrik's avatar
Martin Sustrik committed
87
void zmq::thread_t::stop ()
Martin Sustrik's avatar
Martin Sustrik committed
88 89
{
    int rc = pthread_join (descriptor, NULL);
90
    posix_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
91 92 93 94 95 96 97 98
}

#endif