From 4b5d04f475b8bc74c80eec515a2b7e385ed94883 Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Wed, 29 Feb 2012 08:11:01 +0000 Subject: [PATCH] Fixed tbb hang in Android apps --- 3rdparty/tbb/CMakeLists.txt | 8 +++--- 3rdparty/tbb/android_additional.h | 56 ++++++++++++++++++++++++--------------- cmake/OpenCVCompilerOptions.cmake | 2 +- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/3rdparty/tbb/CMakeLists.txt b/3rdparty/tbb/CMakeLists.txt index 20a0d80..adbee82 100644 --- a/3rdparty/tbb/CMakeLists.txt +++ b/3rdparty/tbb/CMakeLists.txt @@ -92,16 +92,16 @@ list(APPEND lib_srcs "${tbb_src_dir}/src/rml/client/rml_tbb.cpp") add_definitions(-D__TBB_DYNAMIC_LOAD_ENABLED=0 #required -D__TBB_BUILD=1 #required - -D__TBB_USE_GENERIC_DWORD_LOAD_STORE=1 #needed by TBB 4.0 update 1,2 - -D__TBB_TASK_CPP_DIRECTLY_INCLUDED=1 #needed by TBB 4.0 update 3 + -D__TBB_SURVIVE_THREAD_SWITCH=0 #no cilk on Android + -D__TBB_USE_GENERIC_DWORD_LOAD_STORE=1 #needed by TBB 4.0 update 1,2; fixed in TBB 4.0 update 3 but it has 2 new problems + -DUSE_PTHREAD #required -DTBB_USE_GCC_BUILTINS=1 #required -DTBB_USE_DEBUG=0 #just ot be sure -DTBB_NO_LEGACY=1 #don't need backward compatibility - -DUSE_PTHREAD #required -DDO_ITT_NOTIFY=0 #it seems that we don't need these notifications ) -add_library(tbb STATIC ${lib_srcs} ${lib_hdrs}) +add_library(tbb STATIC ${lib_srcs} ${lib_hdrs} "${CMAKE_CURRENT_SOURCE_DIR}/android_additional.h" "${CMAKE_CURRENT_SOURCE_DIR}/version_string.tmp") target_link_libraries(tbb c m dl) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w -include \"${CMAKE_CURRENT_SOURCE_DIR}/android_additional.h\"") diff --git a/3rdparty/tbb/android_additional.h b/3rdparty/tbb/android_additional.h index 4cfa7f0..a5c1a84 100644 --- a/3rdparty/tbb/android_additional.h +++ b/3rdparty/tbb/android_additional.h @@ -1,27 +1,41 @@ -#include -#include +#include -typedef unsigned long cpu_set_t; -#define __NCPUBITS (8 * sizeof (unsigned long)) - - -#define CPU_SET(cpu, cpusetp) \ - ((*(cpusetp)) |= (1UL << ((cpu) % __NCPUBITS))) +static inline int getPossibleCPUs() +{ + FILE* cpuPossible = fopen("/sys/devices/system/cpu/possible", "r"); + if(!cpuPossible) + return 1; -#define CPU_ISSET(cpu, cpusetp) \ - ((*(cpusetp)) & (1UL << ((cpu) % __NCPUBITS))) + char buf[2000]; //big enough for 1000 CPUs in worst possible configuration + char* pbuf = fgets(buf, sizeof(buf), cpuPossible); + fclose(cpuPossible); + if(!pbuf) + return 1; -#define CPU_ZERO(cpusetp) \ - memset((cpusetp), 0, sizeof(cpu_set_t)) + //parse string of form "0-1,3,5-7,10,13-15" + int cpusAvailable = 0; -inline static int -sched_setaffinity(pid_t pid, size_t len, cpu_set_t const * cpusetp) -{ - return syscall(__NR_sched_setaffinity, pid, len, cpusetp); + while(*pbuf) + { + const char* pos = pbuf; + bool range = false; + while(*pbuf && *pbuf != ',') + { + if(*pbuf == '-') range = true; + ++pbuf; + } + if(*pbuf) *pbuf++ = 0; + if(!range) + ++cpusAvailable; + else + { + int rstart = 0, rend = 0; + sscanf(pos, "%d-%d", &rstart, &rend); + cpusAvailable += rend - rstart + 1; + } + + } + return cpusAvailable ? cpusAvailable : 1; } -inline static int -sched_getaffinity(pid_t pid, size_t len, cpu_set_t const * cpusetp) -{ - return syscall(__NR_sched_getaffinity, pid, len, cpusetp); -} +#define __TBB_HardwareConcurrency() getPossibleCPUs() diff --git a/cmake/OpenCVCompilerOptions.cmake b/cmake/OpenCVCompilerOptions.cmake index 6836a20..98d7aca 100644 --- a/cmake/OpenCVCompilerOptions.cmake +++ b/cmake/OpenCVCompilerOptions.cmake @@ -108,7 +108,7 @@ if(CMAKE_COMPILER_IS_GNUCXX) # Profiling? if(ENABLE_PROFILING) set(OPENCV_EXTRA_C_FLAGS_RELEASE "${OPENCV_EXTRA_C_FLAGS_RELEASE} -pg -g") - elseif(NOT APPLE) + elseif(NOT APPLE AND NOT ANDROID) # Remove unreferenced functions: function level linking set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} -ffunction-sections") endif() -- 2.7.4