replace RefCountBase with SimpleRefCount<> to avoid duplicate refcounting implementations.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2008 INRIA
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author: Mathieu Lacage <mathieu.lacage.inria.fr>
24 #include "fatal-error.h"
25 #include "system-thread.h"
28 NS_LOG_COMPONENT_DEFINE ("SystemThread");
33 // Private implementation class for the SystemThread class. The deal is
34 // that we export the SystemThread class to the user. The header just
35 // declares a class and its members. There is a forward declaration for
36 // a private implementation class there and a member declaration. Thus
37 // there is no knowledge of the implementation in the exported header.
39 // We provide an implementation class for each operating system. This is
40 // the Unix implementation of the SystemThread.
42 // In order to use the SystemThread, you will include "system-thread.h" and
43 // get the implementation by linking unix-system-thread.cc (if you are running
46 class SystemThreadImpl
49 SystemThreadImpl (Callback<void> callback);
57 static void *DoRun (void *arg);
58 Callback<void> m_callback;
64 SystemThreadImpl::SystemThreadImpl (Callback<void> callback)
65 : m_callback (callback)
67 NS_LOG_FUNCTION_NOARGS ();
68 // Make sure we have a SIGALRM handler which does not terminate
72 sigemptyset (&act.sa_mask);
73 act.sa_handler = SIG_IGN;
74 sigaction (SIGALRM, &act, 0);
78 SystemThreadImpl::Start (void)
80 NS_LOG_FUNCTION_NOARGS ();
82 int rc = pthread_create (&m_thread, NULL, &SystemThreadImpl::DoRun,
87 NS_FATAL_ERROR ("pthread_create failed: " << rc << "=\"" <<
88 strerror(rc) << "\".");
93 SystemThreadImpl::Join (void)
95 NS_LOG_FUNCTION_NOARGS ();
98 int rc = pthread_join (m_thread, &thread_return);
101 NS_FATAL_ERROR ("pthread_join failed: " << rc << "=\"" <<
102 strerror(rc) << "\".");
107 SystemThreadImpl::Shutdown (void)
109 NS_LOG_FUNCTION_NOARGS ();
113 // send a SIGALRM signal on the target thread to make sure that it
115 pthread_kill (m_thread, SIGALRM);
119 SystemThreadImpl::Break (void)
121 NS_LOG_FUNCTION_NOARGS ();
127 SystemThreadImpl::DoRun (void *arg)
129 NS_LOG_FUNCTION_NOARGS ();
131 SystemThreadImpl *self = static_cast<SystemThreadImpl *> (arg);
138 // Remember that we just export the delcaration of the SystemThread class to
139 // the user. There is no code to implement the SystemThread methods. We
140 // have to do that here. We just vector the calls to our implementation
143 SystemThread::SystemThread (Callback<void> callback)
144 : m_impl (new SystemThreadImpl (callback))
146 NS_LOG_FUNCTION_NOARGS ();
149 SystemThread::~SystemThread()
151 NS_LOG_FUNCTION_NOARGS ();
156 SystemThread::Start (void)
158 NS_LOG_FUNCTION_NOARGS ();
163 SystemThread::Join (void)
165 NS_LOG_FUNCTION_NOARGS ();
170 SystemThread::Shutdown (void)
172 NS_LOG_FUNCTION_NOARGS ();
177 SystemThread::Break (void)
179 NS_LOG_FUNCTION_NOARGS ();
180 return m_impl->Break ();