diff options
author | Marcus Müller <mmueller@gnuradio.org> | 2019-08-07 21:45:12 +0200 |
---|---|---|
committer | Marcus Müller <marcus@hostalia.de> | 2019-08-09 23:04:28 +0200 |
commit | f7bbf2c1d8d780294f3e016aff239ca35eb6516e (patch) | |
tree | e09ab6112e02b2215b2d59ac24d3d6ea2edac745 /gnuradio-runtime/lib/thread | |
parent | 78431dc6941e3acc67c858277dfe4a0ed583643c (diff) |
Tree: clang-format without the include sorting
Diffstat (limited to 'gnuradio-runtime/lib/thread')
-rw-r--r-- | gnuradio-runtime/lib/thread/thread.cc | 529 | ||||
-rw-r--r-- | gnuradio-runtime/lib/thread/thread_body_wrapper.cc | 55 | ||||
-rw-r--r-- | gnuradio-runtime/lib/thread/thread_group.cc | 128 |
3 files changed, 333 insertions, 379 deletions
diff --git a/gnuradio-runtime/lib/thread/thread.cc b/gnuradio-runtime/lib/thread/thread.cc index 2a5f3f7c02..85240cbee1 100644 --- a/gnuradio-runtime/lib/thread/thread.cc +++ b/gnuradio-runtime/lib/thread/thread.cc @@ -31,219 +31,189 @@ #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++) +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) { + DWORD_PTR ret = SetThreadAffinityMask(thread, dword_mask); + if (ret == 0) { std::stringstream s; - s << "thread_bind_to_processor failed with error: " - << GetLastError() << std::endl; + 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() { 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) { +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; + s << "thread_unbind failed with error: " << GetLastError() << std::endl; throw std::runtime_error(s.str()); - } } +} - int - thread_priority(gr_thread_t thread) - { - // Not implemented on Windows - return -1; - } +int thread_priority(gr_thread_t thread) +{ + // Not implemented on Windows + return -1; +} - int - set_thread_priority(gr_thread_t thread, int priority) - { - // Not implemented on Windows - return -1; - } +int set_thread_priority(gr_thread_t thread, int priority) +{ + // Not implemented on Windows + return -1; +} #ifndef __MINGW32__ -#pragma pack(push,8) - typedef struct tagTHREADNAME_INFO - { - DWORD dwType; // Must be 0x1000 - LPCSTR szName; // Pointer to name (in user addr space) - DWORD dwThreadID; // Thread ID (-1 = caller thread) - DWORD dwFlags; // Reserved for future use, must be zero - } THREADNAME_INFO; +#pragma pack(push, 8) +typedef struct tagTHREADNAME_INFO { + DWORD dwType; // Must be 0x1000 + LPCSTR szName; // Pointer to name (in user addr space) + DWORD dwThreadID; // Thread ID (-1 = caller thread) + DWORD dwFlags; // Reserved for future use, must be zero +} THREADNAME_INFO; #pragma pack(pop) - static void - _set_thread_name(gr_thread_t thread, const char* name, DWORD dwThreadId) - { - const DWORD SET_THREAD_NAME_EXCEPTION = 0x406D1388; - - THREADNAME_INFO info; - info.dwType = 0x1000; - info.szName = name; - info.dwThreadID = dwThreadId; - info.dwFlags = 0; - - __try - { - RaiseException(SET_THREAD_NAME_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info); - } - __except(EXCEPTION_EXECUTE_HANDLER) - { - } - } - - void - set_thread_name(gr_thread_t thread, std::string name) - { - DWORD dwThreadId = GetThreadId(thread); - if (dwThreadId == 0) +static void _set_thread_name(gr_thread_t thread, const char* name, DWORD dwThreadId) +{ + const DWORD SET_THREAD_NAME_EXCEPTION = 0x406D1388; + + THREADNAME_INFO info; + info.dwType = 0x1000; + info.szName = name; + info.dwThreadID = dwThreadId; + info.dwFlags = 0; + + __try { + RaiseException(SET_THREAD_NAME_EXCEPTION, + 0, + sizeof(info) / sizeof(ULONG_PTR), + (ULONG_PTR*)&info); + } __except (EXCEPTION_EXECUTE_HANDLER) { + } +} + +void set_thread_name(gr_thread_t thread, std::string name) +{ + DWORD dwThreadId = GetThreadId(thread); + if (dwThreadId == 0) return; - if (name.empty()) + if (name.empty()) name = boost::str(boost::format("thread %lu") % dwThreadId); - _set_thread_name(thread, name.c_str(), dwThreadId); - } + _set_thread_name(thread, name.c_str(), dwThreadId); +} #else - void - set_thread_name(gr_thread_t thread, std::string name) - { - /* Not implemented on mingw-w64 */ - } +void set_thread_name(gr_thread_t thread, std::string name) +{ + /* Not implemented on mingw-w64 */ +} #endif /* !__MINGW32__ */ - } /* namespace thread */ +} /* namespace thread */ } /* namespace gr */ -#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) || \ - defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__GNU__) || \ - defined(__NetBSD__) +#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) || \ + defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__GNU__) || \ + defined(__NetBSD__) namespace gr { - namespace thread { - - gr_thread_t - get_current_thread_id() - { - // Not implemented on OSX - return NULL; - } - - 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 - } - - int - thread_priority(gr_thread_t thread) - { - sched_param param; - int priority; - int policy; - int ret; - ret = pthread_getschedparam (thread, &policy, ¶m); - priority = param.sched_priority; - return (ret==0)?priority:ret; - } - - int - set_thread_priority(gr_thread_t thread, int priority) - { - int policy; - struct sched_param param; - pthread_getschedparam (thread, &policy, ¶m); - param.sched_priority = priority; - return pthread_setschedparam(thread, policy, ¶m); - } - - void - set_thread_name(gr_thread_t thread, std::string name) - { - // Not implemented on OSX - } - - } /* namespace thread */ +namespace thread { + +gr_thread_t get_current_thread_id() +{ + // Not implemented on OSX + return NULL; +} + +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 +} + +int thread_priority(gr_thread_t thread) +{ + sched_param param; + int priority; + int policy; + int ret; + ret = pthread_getschedparam(thread, &policy, ¶m); + priority = param.sched_priority; + return (ret == 0) ? priority : ret; +} + +int set_thread_priority(gr_thread_t thread, int priority) +{ + int policy; + struct sched_param param; + pthread_getschedparam(thread, &policy, ¶m); + param.sched_priority = priority; + return pthread_setschedparam(thread, policy, ¶m); +} + +void set_thread_name(gr_thread_t thread, std::string name) +{ + // Not implemented on OSX +} + +} /* namespace thread */ } /* namespace gr */ #else @@ -254,131 +224,116 @@ namespace gr { #include <sys/prctl.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++) +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) { + 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() { thread_unbind(get_current_thread_id()); } - void - thread_unbind(gr_thread_t thread) - { - cpu_set_t set; - size_t len = sizeof(cpu_set_t); +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_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) { + 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()); - } } - - int - thread_priority(gr_thread_t thread) - { - sched_param param; - int priority; - int policy; - int ret; - ret = pthread_getschedparam (thread, &policy, ¶m); - priority = param.sched_priority; - return (ret==0)?priority:ret; - } - - int - set_thread_priority(gr_thread_t thread, int priority) - { - int policy; - struct sched_param param; - pthread_getschedparam (thread, &policy, ¶m); - param.sched_priority = priority; - return pthread_setschedparam(thread, policy, ¶m); - } - - void - set_thread_name(gr_thread_t thread, std::string name) - { - if (thread != pthread_self()) // Naming another thread is not supported +} + +int thread_priority(gr_thread_t thread) +{ + sched_param param; + int priority; + int policy; + int ret; + ret = pthread_getschedparam(thread, &policy, ¶m); + priority = param.sched_priority; + return (ret == 0) ? priority : ret; +} + +int set_thread_priority(gr_thread_t thread, int priority) +{ + int policy; + struct sched_param param; + pthread_getschedparam(thread, &policy, ¶m); + param.sched_priority = priority; + return pthread_setschedparam(thread, policy, ¶m); +} + +void set_thread_name(gr_thread_t thread, std::string name) +{ + if (thread != pthread_self()) // Naming another thread is not supported return; - if (name.empty()) + if (name.empty()) name = boost::str(boost::format("thread %llu") % ((unsigned long long)thread)); - const int max_len = 16; // Maximum accepted by PR_SET_NAME + const int max_len = 16; // Maximum accepted by PR_SET_NAME - if ((int)name.size() > max_len) // Shorten the name if necessary by taking as many characters from the front - { // so that the unique_id can still fit on the end + if ((int)name.size() > max_len) // Shorten the name if necessary by taking as many + // characters from the front + { // so that the unique_id can still fit on the end int i = name.size() - 1; - for (; i >= 0; --i) - { - std::string s = name.substr(i, 1); - int n = atoi(s.c_str()); - if ((n == 0) && (s != "0")) - break; + for (; i >= 0; --i) { + std::string s = name.substr(i, 1); + int n = atoi(s.c_str()); + if ((n == 0) && (s != "0")) + break; } - name = name.substr(0, std::max(0, max_len - ((int)name.size() - (i + 1)))) + name.substr(i + 1); - } - - prctl(PR_SET_NAME, name.c_str(), 0, 0, 0); + name = name.substr(0, std::max(0, max_len - ((int)name.size() - (i + 1)))) + + name.substr(i + 1); } - } /* namespace thread */ + prctl(PR_SET_NAME, name.c_str(), 0, 0, 0); +} + +} /* 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 index fffa7e4c44..c820414853 100644 --- a/gnuradio-runtime/lib/thread/thread_body_wrapper.cc +++ b/gnuradio-runtime/lib/thread/thread_body_wrapper.cc @@ -32,60 +32,57 @@ #include <stdio.h> namespace gr { - namespace thread { +namespace thread { #if defined(HAVE_PTHREAD_SIGMASK) && defined(HAVE_SIGNAL_H) && !defined(__MINGW32__) - void mask_signals() - { - sigset_t new_set; - int r; +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); + 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); + sigaddset(&new_set, SIGPOLL); #endif #ifdef SIGPROF - sigaddset(&new_set, SIGPROF); + sigaddset(&new_set, SIGPROF); #endif #ifdef SIGSYS - sigaddset(&new_set, SIGSYS); + sigaddset(&new_set, SIGSYS); #endif #ifdef SIGTRAP - sigaddset(&new_set, SIGTRAP); + sigaddset(&new_set, SIGTRAP); #endif #ifdef SIGURG - sigaddset(&new_set, SIGURG); + sigaddset(&new_set, SIGURG); #endif #ifdef SIGVTALRM - sigaddset(&new_set, SIGVTALRM); + sigaddset(&new_set, SIGVTALRM); #endif #ifdef SIGXCPU - sigaddset(&new_set, SIGXCPU); + sigaddset(&new_set, SIGXCPU); #endif #ifdef SIGXFSZ - sigaddset(&new_set, SIGXFSZ); + sigaddset(&new_set, SIGXFSZ); #endif - r = pthread_sigmask(SIG_BLOCK, &new_set, 0); - if(r != 0) + r = pthread_sigmask(SIG_BLOCK, &new_set, 0); + if (r != 0) perror("pthread_sigmask"); - } +} #else - void mask_signals() - { - } +void mask_signals() {} #endif - } /* namespace thread */ +} /* namespace thread */ } /* namespace gr */ - diff --git a/gnuradio-runtime/lib/thread/thread_group.cc b/gnuradio-runtime/lib/thread/thread_group.cc index 637525e19e..1d9b8c846c 100644 --- a/gnuradio-runtime/lib/thread/thread_group.cc +++ b/gnuradio-runtime/lib/thread/thread_group.cc @@ -15,84 +15,86 @@ #include <gnuradio/thread/thread_group.h> namespace gr { - namespace thread { +namespace thread { - thread_group::thread_group() - { - } +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) { +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::unique_ptr<boost::thread> thrd(new boost::thread(threadfunc)); - add_thread(thrd.get()); - return thrd.release(); - } +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::unique_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); +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()) + // 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); +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()) + // 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::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(); - } +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(); - } +size_t thread_group::size() const +{ + boost::shared_lock<boost::shared_mutex> guard(m_mutex); + return m_threads.size(); +} - } /* namespace thread */ +} /* namespace thread */ } /* namespace gr */ |