From ae9bab4379245769f42b9171c34821121a05ad03 Mon Sep 17 00:00:00 2001 From: Marcus Winter Date: Tue, 27 Jan 2015 14:51:01 +0100 Subject: [PATCH] portability: changed all checks for platform specific defines to checks for EMBB_* defines, fixed affinity implementation for FreeBSD --- base_c/CMakeLists.txt | 2 ++ base_c/include/embb/base/c/internal/cmake_config.h.in | 10 ++++++++++ base_c/src/core_set.c | 5 ++--- base_c/src/thread.c | 19 +++++++++++++------ containers_cpp/include/embb/containers/internal/lock_free_mpmc_queue-inl.h | 6 ++++-- containers_cpp/include/embb/containers/internal/lock_free_stack-inl.h | 6 ++++-- containers_cpp/test/hazard_pointer_test.cc | 6 ++++-- containers_cpp/test/queue_test-inl.h | 6 ++++-- 8 files changed, 43 insertions(+), 17 deletions(-) diff --git a/base_c/CMakeLists.txt b/base_c/CMakeLists.txt index f2ac385..8c3ba96 100644 --- a/base_c/CMakeLists.txt +++ b/base_c/CMakeLists.txt @@ -78,6 +78,8 @@ endif() include(CheckIncludeFiles) # Includes module to perform checks include(CheckSymbolExists) # Includes module to perform symbol checks check_include_files("sys/sysinfo.h" EMBB_HAS_HEADER_SYSINFO) +check_include_files("sys/types.h;sys/sysctl.h" EMBB_HAS_HEADER_SYSCTL) +check_include_files("sys/param.h;sys/cpuset.h" EMBB_HAS_HEADER_CPUSET) link_libraries(${link_libraries} ${gnu_libs}) set(CMAKE_EXTRA_INCLUDE_FILES sched.h) check_type_size(cpu_set_t EMBB_HAS_GLIB_CPU) diff --git a/base_c/include/embb/base/c/internal/cmake_config.h.in b/base_c/include/embb/base/c/internal/cmake_config.h.in index 89f2f4e..7b53f82 100644 --- a/base_c/include/embb/base/c/internal/cmake_config.h.in +++ b/base_c/include/embb/base/c/internal/cmake_config.h.in @@ -39,6 +39,16 @@ #cmakedefine EMBB_HAS_HEADER_SYSINFO /** + * Is used to get the number of cores on certain systems. + */ +#cmakedefine EMBB_HAS_HEADER_SYSCTL + +/** + * Is used to set thread affinities on certain systems. + */ +#cmakedefine EMBB_HAS_HEADER_CPUSET + +/** * Is used for Linux thread affinities. */ #cmakedefine EMBB_HAS_GLIB_CPU diff --git a/base_c/src/core_set.c b/base_c/src/core_set.c index f5a3225..dc19ad7 100644 --- a/base_c/src/core_set.c +++ b/base_c/src/core_set.c @@ -93,8 +93,7 @@ void embb_core_set_init(embb_core_set_t* core_set, int initializer) { #ifdef EMBB_HAS_HEADER_SYSINFO #include -#endif -#ifdef __FreeBSD__ +#elif defined EMBB_HAS_HEADER_SYSCTL #include #include #endif @@ -102,7 +101,7 @@ void embb_core_set_init(embb_core_set_t* core_set, int initializer) { unsigned int embb_core_count_available() { #ifdef EMBB_HAS_HEADER_SYSINFO return get_nprocs(); -#elif defined __FreeBSD__ +#elif defined EMBB_HAS_HEADER_SYSCTL const size_t kBufferSize = sizeof(unsigned int); char buf[kBufferSize]; size_t len = kBufferSize; diff --git a/base_c/src/thread.c b/base_c/src/thread.c index 6737f2b..dc62dce 100644 --- a/base_c/src/thread.c +++ b/base_c/src/thread.c @@ -157,6 +157,10 @@ int embb_thread_equal(const embb_thread_t* lhs, const embb_thread_t* rhs) { #ifdef EMBB_HAS_GLIB_CPU #include +#elif defined EMBB_HAS_HEADER_CPUSET +#include +#include +#include #endif #ifdef EMBB_HAS_HEADER_SYSINFO @@ -203,23 +207,26 @@ int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set, int status = pthread_attr_init(&attr); if (status != 0) return EMBB_ERROR; if (core_set != NULL) { -#ifdef EMBB_HAS_GLIB_CPU +#if defined(EMBB_HAS_GLIB_CPU) || defined(EMBB_HAS_HEADER_CPUSET) assert(embb_core_count_available() < CPU_SETSIZE && - "Core sets on Linux systems are only supported up to CPU_SETSIZE " - "processors!"); + "Core sets are only supported up to CPU_SETSIZE processors!"); +#ifdef EMBB_HAS_GLIB_CPU cpu_set_t cpuset; +#else + cpuset_t cpuset; +#endif CPU_ZERO(&cpuset); /* Disable all processors */ for (unsigned int i = 0; i < embb_core_count_available(); i++) { if (embb_core_set_contains(core_set, i)) { CPU_SET(i, &cpuset); } } - status = pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset); + status = pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset); if (status != 0) return EMBB_ERROR; -#else /* EMBB_HAS_GLIB_CPU */ +#else embb_log_write("base_c", EMBB_LOG_LEVEL_WARNING, "Could not set thread " "affinity, since no implementation available!\n"); -#endif /* else EMBB_HAS_GLIB_CPU */ +#endif } /* Dynamic allocation of thread arguments. Freed on call of join. */ diff --git a/containers_cpp/include/embb/containers/internal/lock_free_mpmc_queue-inl.h b/containers_cpp/include/embb/containers/internal/lock_free_mpmc_queue-inl.h index 5afd975..9c61388 100644 --- a/containers_cpp/include/embb/containers/internal/lock_free_mpmc_queue-inl.h +++ b/containers_cpp/include/embb/containers/internal/lock_free_mpmc_queue-inl.h @@ -27,6 +27,8 @@ #ifndef EMBB_CONTAINERS_INTERNAL_LOCK_FREE_MPMC_QUEUE_INL_H_ #define EMBB_CONTAINERS_INTERNAL_LOCK_FREE_MPMC_QUEUE_INL_H_ +#include + /* * The following algorithm uses hazard pointers and a lock-free value pool for * memory management. For a description of the algorithm, see @@ -78,13 +80,13 @@ LockFreeMPMCQueue::LockFreeMPMCQueue(size_t capacity) : capacity(capacity), // Disable "this is used in base member initializer" warning. // We explicitly want this. -#ifdef _MSC_VER +#ifdef EMBB_COMPILER_MSVC #pragma warning(push) #pragma warning(disable:4355) #endif delete_pointer_callback(*this, &LockFreeMPMCQueue::DeletePointerCallback), -#ifdef _MSC_VER +#ifdef EMBB_COMPILER_MSVC #pragma warning(pop) #endif hazardPointer(delete_pointer_callback, NULL, 2), diff --git a/containers_cpp/include/embb/containers/internal/lock_free_stack-inl.h b/containers_cpp/include/embb/containers/internal/lock_free_stack-inl.h index c03d7fb..7c6e9ab 100644 --- a/containers_cpp/include/embb/containers/internal/lock_free_stack-inl.h +++ b/containers_cpp/include/embb/containers/internal/lock_free_stack-inl.h @@ -27,6 +27,8 @@ #ifndef EMBB_CONTAINERS_INTERNAL_LOCK_FREE_STACK_INL_H_ #define EMBB_CONTAINERS_INTERNAL_LOCK_FREE_STACK_INL_H_ +#include + /* * The following algorithm uses hazard pointers and a lock-free value pool for * memory management. For a description of the algorithm, see @@ -70,13 +72,13 @@ LockFreeStack< Type, ValuePool >::LockFreeStack(size_t capacity) : capacity(capacity), // Disable "this is used in base member initializer" warning. // We explicitly want this. -#ifdef _MSC_VER +#ifdef EMBB_COMPILER_MSVC #pragma warning(push) #pragma warning(disable:4355) #endif delete_pointer_callback(*this, &LockFreeStack::DeletePointerCallback), -#ifdef _MSC_VER +#ifdef EMBB_COMPILER_MSVC #pragma warning(pop) #endif hazardPointer(delete_pointer_callback, NULL, 1), diff --git a/containers_cpp/test/hazard_pointer_test.cc b/containers_cpp/test/hazard_pointer_test.cc index 3c3883a..56f455b 100644 --- a/containers_cpp/test/hazard_pointer_test.cc +++ b/containers_cpp/test/hazard_pointer_test.cc @@ -26,16 +26,18 @@ #include "./hazard_pointer_test.h" +#include + namespace embb { namespace containers { namespace test { HazardPointerTest::HazardPointerTest() : -#ifdef _MSC_VER +#ifdef EMBB_COMPILER_MSVC #pragma warning(push) #pragma warning(disable:4355) #endif delete_pointer_callback(*this, &HazardPointerTest::DeletePointerCallback), -#ifdef _MSC_VER +#ifdef EMBB_COMPILER_MSVC #pragma warning(pop) #endif object_pool(NULL), diff --git a/containers_cpp/test/queue_test-inl.h b/containers_cpp/test/queue_test-inl.h index fba5d0b..4cc7a95 100644 --- a/containers_cpp/test/queue_test-inl.h +++ b/containers_cpp/test/queue_test-inl.h @@ -30,6 +30,8 @@ #include #include +#include + namespace embb { namespace containers { namespace test { @@ -54,13 +56,13 @@ n_threads(static_cast TOTAL_PRODUCE_CONSUME_COUNT). Post(&QueueTest::QueueTestSingleProducedSingleConsumer_Post, this); -#ifdef _MSC_VER +#ifdef EMBB_COMPILER_MSVC #pragma warning(push) #pragma warning(disable:4127) #endif if (MultipleProducers == true && MultipleConsumers == true) { -#ifdef _MSC_VER +#ifdef EMBB_COMPILER_MSVC #pragma warning(pop) #endif CreateUnit("QueueTestMultipleThreadsMultipleProducerMultipleConsumer"). -- libgit2 0.26.0