summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/lib/thread
diff options
context:
space:
mode:
authorTom Rondeau <trondeau@vt.edu>2013-03-29 17:31:06 -0400
committerJohnathan Corgan <johnathan@corganlabs.com>2013-04-01 16:23:06 -0700
commitf74d3dae8f2ec423c61932b4ad0359f98b996a51 (patch)
tree1e16eb3537829248c177b04209acce3fe2bead96 /gnuradio-runtime/lib/thread
parenteea0e411411303ea94953c5cd8454c4139a474ff (diff)
gruel: moved gruel into subdirs of gnuradio-runtime.
PMTs are handled slightly different and are installed into their own module and include dir.
Diffstat (limited to 'gnuradio-runtime/lib/thread')
-rw-r--r--gnuradio-runtime/lib/thread/CMakeLists.txt28
-rw-r--r--gnuradio-runtime/lib/thread/thread.cc238
-rw-r--r--gnuradio-runtime/lib/thread/thread_body_wrapper.cc91
-rw-r--r--gnuradio-runtime/lib/thread/thread_group.cc98
4 files changed, 455 insertions, 0 deletions
diff --git a/gnuradio-runtime/lib/thread/CMakeLists.txt b/gnuradio-runtime/lib/thread/CMakeLists.txt
new file mode 100644
index 0000000000..de46dd8932
--- /dev/null
+++ b/gnuradio-runtime/lib/thread/CMakeLists.txt
@@ -0,0 +1,28 @@
+# Copyright 2010,2013 Free Software Foundation, Inc.
+#
+# This file is part of GNU Radio
+#
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GNU Radio 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
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING. If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+
+########################################################################
+# This file included, use CMake directory variables
+########################################################################
+
+list(APPEND gnuradio_runtime_sources
+ ${CMAKE_CURRENT_SOURCE_DIR}/thread.cc
+ ${CMAKE_CURRENT_SOURCE_DIR}/thread_body_wrapper.cc
+ ${CMAKE_CURRENT_SOURCE_DIR}/thread_group.cc
+)
diff --git a/gnuradio-runtime/lib/thread/thread.cc b/gnuradio-runtime/lib/thread/thread.cc
new file mode 100644
index 0000000000..af0822445d
--- /dev/null
+++ b/gnuradio-runtime/lib/thread/thread.cc
@@ -0,0 +1,238 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2012-2013 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio 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
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <thread/thread.h>
+
+#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
+
+#include <Windows.h>
+
+namespace gr {
+ namespace thread {
+
+ gr_thread_t
+ get_current_thread_id()
+ {
+ return GetCurrentThread();
+ }
+
+ void
+ thread_bind_to_processor(int n)
+ {
+ std::vector<int> mask(1, n);
+ thread_bind_to_processor(get_current_thread_id(), mask);
+ }
+
+ void
+ thread_bind_to_processor(const std::vector<int> &mask)
+ {
+ thread_bind_to_processor(get_current_thread_id(), mask);
+ }
+
+ void
+ thread_bind_to_processor(gr_thread_t thread, int n)
+ {
+ std::vector<int> mask(1, n);
+ thread_bind_to_processor(thread, mask);
+ }
+
+ void
+ thread_bind_to_processor(gr_thread_t thread, const std::vector<int> &mask)
+ {
+ //DWORD_PTR mask = (1 << n);
+ DWORD_PTR dword_mask = 0;
+
+ std::vector<int> _mask = mask;
+ std::vector<int>::iterator itr;
+ for(itr = _mask.begin(); itr != _mask.end(); itr++)
+ dword_mask |= (1 << (*itr));
+
+ DWORD_PTR ret = SetThreadAffinityMask(thread, dword_mask);
+ if(ret == 0) {
+ std::stringstream s;
+ s << "thread_bind_to_processor failed with error: "
+ << GetLastError() << std::endl;
+ throw std::runtime_error(s.str());
+ }
+ }
+
+ void
+ thread_unbind()
+ {
+ thread_unbind(get_current_thread_id());
+ }
+
+ void
+ thread_unbind(gr_thread_t thread)
+ {
+ DWORD_PTR dword_mask = sizeof(DWORD_PTR) - 1;
+ DWORD_PTR ret = SetThreadAffinityMask(thread, dword_mask);
+ if(ret == 0) {
+ std::stringstream s;
+ s << "thread_unbind failed with error: "
+ << GetLastError() << std::endl;
+ throw std::runtime_error(s.str());
+ }
+ }
+
+ } /* namespace thread */
+} /* namespace gr */
+
+
+#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
+
+namespace gr {
+ namespace thread {
+
+ gr_thread_t
+ get_current_thread_id()
+ {
+ // Not implemented on OSX
+ }
+
+ void
+ thread_bind_to_processor(int n)
+ {
+ // Not implemented on OSX
+ }
+
+ void
+ thread_bind_to_processor(gr_thread_t thread, int n)
+ {
+ // Not implemented on OSX
+ }
+
+ void
+ thread_bind_to_processor(const std::vector<int> &mask)
+ {
+ // Not implemented on OSX
+ }
+
+ void
+ thread_bind_to_processor(gr_thread_t thread, const std::vector<int> &mask)
+ {
+ // Not implemented on OSX
+ }
+
+ void
+ thread_unbind()
+ {
+ // Not implemented on OSX
+ }
+
+ void
+ thread_unbind(gr_thread_t thread)
+ {
+ // Not implemented on OSX
+ }
+
+ } /* namespace thread */
+} /* namespace gr */
+
+#else
+
+#include <sstream>
+#include <stdexcept>
+#include <pthread.h>
+
+namespace gr {
+ namespace thread {
+
+ gr_thread_t
+ get_current_thread_id()
+ {
+ return pthread_self();
+ }
+
+ void
+ thread_bind_to_processor(int n)
+ {
+ std::vector<int> mask(1, n);
+ thread_bind_to_processor(get_current_thread_id(), mask);
+ }
+
+ void
+ thread_bind_to_processor(const std::vector<int> &mask)
+ {
+ thread_bind_to_processor(get_current_thread_id(), mask);
+ }
+
+ void
+ thread_bind_to_processor(gr_thread_t thread, int n)
+ {
+ std::vector<int> mask(1, n);
+ thread_bind_to_processor(thread, mask);
+ }
+
+ void
+ thread_bind_to_processor(gr_thread_t thread, const std::vector<int> &mask)
+ {
+ cpu_set_t set;
+ size_t len = sizeof(cpu_set_t);
+ std::vector<int> _mask = mask;
+ std::vector<int>::iterator itr;
+
+ CPU_ZERO(&set);
+ for(itr = _mask.begin(); itr != _mask.end(); itr++)
+ CPU_SET(*itr, &set);
+
+ int ret = pthread_setaffinity_np(thread, len, &set);
+ if(ret != 0) {
+ std::stringstream s;
+ s << "thread_bind_to_processor failed with error: " << ret << std::endl;
+ throw std::runtime_error(s.str());
+ }
+ }
+
+ void
+ thread_unbind()
+ {
+ thread_unbind(get_current_thread_id());
+ }
+
+ void
+ thread_unbind(gr_thread_t thread)
+ {
+ cpu_set_t set;
+ size_t len = sizeof(cpu_set_t);
+
+ CPU_ZERO(&set);
+ long ncpus = sysconf(_SC_NPROCESSORS_ONLN);
+ for(long n = 0; n < ncpus; n++) {
+ CPU_SET(n, &set);
+ }
+
+ int ret = pthread_setaffinity_np(thread, len, &set);
+ if(ret != 0) {
+ std::stringstream s;
+ s << "thread_unbind failed with error: " << ret << std::endl;
+ throw std::runtime_error(s.str());
+ }
+ }
+
+ } /* namespace thread */
+} /* namespace gr */
+
+#endif
diff --git a/gnuradio-runtime/lib/thread/thread_body_wrapper.cc b/gnuradio-runtime/lib/thread/thread_body_wrapper.cc
new file mode 100644
index 0000000000..47feafdd42
--- /dev/null
+++ b/gnuradio-runtime/lib/thread/thread_body_wrapper.cc
@@ -0,0 +1,91 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008,2013 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * GNU Radio 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
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <thread/thread_body_wrapper.h>
+
+#ifdef HAVE_SIGNAL_H
+#include <signal.h>
+#endif
+
+#include <stdio.h>
+
+namespace gr {
+ namespace thread {
+
+#if defined(HAVE_PTHREAD_SIGMASK) && defined(HAVE_SIGNAL_H)
+
+ void mask_signals()
+ {
+ sigset_t new_set;
+ int r;
+
+ sigemptyset(&new_set);
+ sigaddset(&new_set, SIGHUP); // block these...
+ sigaddset(&new_set, SIGINT);
+ sigaddset(&new_set, SIGPIPE);
+ sigaddset(&new_set, SIGALRM);
+ sigaddset(&new_set, SIGTERM);
+ sigaddset(&new_set, SIGUSR1);
+ sigaddset(&new_set, SIGCHLD);
+#ifdef SIGPOLL
+ sigaddset(&new_set, SIGPOLL);
+#endif
+#ifdef SIGPROF
+ sigaddset(&new_set, SIGPROF);
+#endif
+#ifdef SIGSYS
+ sigaddset(&new_set, SIGSYS);
+#endif
+#ifdef SIGTRAP
+ sigaddset(&new_set, SIGTRAP);
+#endif
+#ifdef SIGURG
+ sigaddset(&new_set, SIGURG);
+#endif
+#ifdef SIGVTALRM
+ sigaddset(&new_set, SIGVTALRM);
+#endif
+#ifdef SIGXCPU
+ sigaddset(&new_set, SIGXCPU);
+#endif
+#ifdef SIGXFSZ
+ sigaddset(&new_set, SIGXFSZ);
+#endif
+ r = pthread_sigmask(SIG_BLOCK, &new_set, 0);
+ if(r != 0)
+ perror("pthread_sigmask");
+ }
+
+#else
+
+ void mask_signals()
+ {
+ }
+
+#endif
+
+ } /* namespace thread */
+} /* namespace gr */
+
diff --git a/gnuradio-runtime/lib/thread/thread_group.cc b/gnuradio-runtime/lib/thread/thread_group.cc
new file mode 100644
index 0000000000..034fe82e75
--- /dev/null
+++ b/gnuradio-runtime/lib/thread/thread_group.cc
@@ -0,0 +1,98 @@
+/* -*- c++ -*- */
+/*
+ * Copyright (C) 2001-2003 William E. Kempf
+ * Copyright (C) 2007 Anthony Williams
+ * Copyright 2008 Free Software Foundation, Inc.
+ *
+ * Distributed under the Boost Software License, Version 1.0. (See accompanying
+ * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+/*
+ * This was extracted from Boost 1.35.0 and fixed.
+ */
+
+#include <thread/thread_group.h>
+
+namespace gr {
+ namespace thread {
+
+ thread_group::thread_group()
+ {
+ }
+
+ thread_group::~thread_group()
+ {
+ // We shouldn't have to scoped_lock here, since referencing this
+ // object from another thread while we're deleting it in the
+ // current thread is going to lead to undefined behavior any
+ // way.
+ for(std::list<boost::thread*>::iterator it = m_threads.begin();
+ it != m_threads.end(); ++it) {
+ delete (*it);
+ }
+ }
+
+ boost::thread* thread_group::create_thread(const boost::function0<void>& threadfunc)
+ {
+ // No scoped_lock required here since the only "shared data" that's
+ // modified here occurs inside add_thread which does scoped_lock.
+ std::auto_ptr<boost::thread> thrd(new boost::thread(threadfunc));
+ add_thread(thrd.get());
+ return thrd.release();
+ }
+
+ void thread_group::add_thread(boost::thread* thrd)
+ {
+ boost::lock_guard<boost::shared_mutex> guard(m_mutex);
+
+ // For now we'll simply ignore requests to add a thread object
+ // multiple times. Should we consider this an error and either
+ // throw or return an error value?
+ std::list<boost::thread*>::iterator it = std::find(m_threads.begin(),
+ m_threads.end(), thrd);
+ BOOST_ASSERT(it == m_threads.end());
+ if(it == m_threads.end())
+ m_threads.push_back(thrd);
+ }
+
+ void thread_group::remove_thread(boost::thread* thrd)
+ {
+ boost::lock_guard<boost::shared_mutex> guard(m_mutex);
+
+ // For now we'll simply ignore requests to remove a thread
+ // object that's not in the group. Should we consider this an
+ // error and either throw or return an error value?
+ std::list<boost::thread*>::iterator it = std::find(m_threads.begin(),
+ m_threads.end(), thrd);
+ BOOST_ASSERT(it != m_threads.end());
+ if(it != m_threads.end())
+ m_threads.erase(it);
+ }
+
+ void thread_group::join_all()
+ {
+ boost::shared_lock<boost::shared_mutex> guard(m_mutex);
+ for(std::list<boost::thread*>::iterator it = m_threads.begin();
+ it != m_threads.end(); ++it) {
+ (*it)->join();
+ }
+ }
+
+ void thread_group::interrupt_all()
+ {
+ boost::shared_lock<boost::shared_mutex> guard(m_mutex);
+ for(std::list<boost::thread*>::iterator it=m_threads.begin(),end=m_threads.end();
+ it!=end; ++it) {
+ (*it)->interrupt();
+ }
+ }
+
+ size_t thread_group::size() const
+ {
+ boost::shared_lock<boost::shared_mutex> guard(m_mutex);
+ return m_threads.size();
+ }
+
+ } /* namespace thread */
+} /* namespace gr */