libzmq master
The Intelligent Transport Layer

thread.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2010-2011 250bpm s.r.o.
00003     Copyright (c) 2007-2011 iMatix Corporation
00004     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
00005 
00006     This file is part of 0MQ.
00007 
00008     0MQ is free software; you can redistribute it and/or modify it under
00009     the terms of the GNU Lesser General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012 
00013     0MQ is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #include "thread.hpp"
00023 #include "err.hpp"
00024 #include "platform.hpp"
00025 
00026 #ifdef ZMQ_HAVE_WINDOWS
00027 
00028 extern "C"
00029 {
00030     static unsigned int __stdcall thread_routine (void *arg_)
00031     {
00032         zmq::thread_t *self = (zmq::thread_t*) arg_;
00033         self->tfn (self->arg);
00034         return 0;
00035     }
00036 }
00037 
00038 void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
00039 {
00040     tfn = tfn_;
00041     arg =arg_;
00042     descriptor = (HANDLE) _beginthreadex (NULL, 0,
00043         &::thread_routine, this, 0 , NULL);
00044     win_assert (descriptor != NULL);    
00045 }
00046 
00047 void zmq::thread_t::stop ()
00048 {
00049     DWORD rc = WaitForSingleObject (descriptor, INFINITE);
00050     win_assert (rc != WAIT_FAILED);
00051     BOOL rc2 = CloseHandle (descriptor);
00052     win_assert (rc2 != 0);
00053 }
00054 
00055 #else
00056 
00057 #include <signal.h>
00058 
00059 extern "C"
00060 {
00061     static void *thread_routine (void *arg_)
00062     {
00063 #if !defined ZMQ_HAVE_OPENVMS && !defined ZMQ_HAVE_ANDROID
00064         //  Following code will guarantee more predictable latencies as it'll
00065         //  disallow any signal handling in the I/O thread.
00066         sigset_t signal_set;
00067         int rc = sigfillset (&signal_set);
00068         errno_assert (rc == 0);
00069         rc = pthread_sigmask (SIG_BLOCK, &signal_set, NULL);
00070         posix_assert (rc);
00071 #endif
00072 
00073         zmq::thread_t *self = (zmq::thread_t*) arg_;   
00074         self->tfn (self->arg);
00075         return NULL;
00076     }
00077 }
00078 
00079 void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
00080 {
00081     tfn = tfn_;
00082     arg =arg_;
00083     int rc = pthread_create (&descriptor, NULL, thread_routine, this);
00084     posix_assert (rc);
00085 }
00086 
00087 void zmq::thread_t::stop ()
00088 {
00089     int rc = pthread_join (descriptor, NULL);
00090     posix_assert (rc);
00091 }
00092 
00093 #endif
00094 
00095 
00096 
00097 
00098 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines