android linux pthread_cancel,Android NDK缺失pthread_cancel和pthread_setcancelstate
Android NDK在v5版本后不再提供全部的POSIX線程庫的API(比如pthread_cancel和pthread_setcancelstate)。原因之一是線程被標記結(jié)束后不一定會把自己擁有的資源釋放掉,甚至不一定會結(jié)束,因此很可能造成內(nèi)存泄露或死鎖等問題,而這些問題在移動設(shè)備上更加突出[1]。
比較安全的方法是使用更安全pthread_kill函數(shù)代替,有關(guān)pthread_kill的功能講解和替換方式可以參見這篇博客。
此處僅記錄ffmpeg編譯android版本時對應(yīng)的問題及解決。使用ndk-r14b編譯arm64時報錯:
libavformat/udp.c: In function 'circular_buffer_task_rx':
libavformat/udp.c:499:5: error: implicit declaration of function 'pthread_setcancelstate' [-Werror=implicit-function-declaration]
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
^
libavformat/udp.c:499:28: error: 'PTHREAD_CANCEL_DISABLE' undeclared (first use in this function)
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
^
libavformat/udp.c:499:28: note: each undeclared identifier is reported only once for each function it appears in
libavformat/udp.c:513:32: error: 'PTHREAD_CANCEL_ENABLE' undeclared (first use in this function)
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
^
libavformat/udp.c: In function 'circular_buffer_task_tx':
libavformat/udp.c:561:28: error: 'PTHREAD_CANCEL_DISABLE' undeclared (first use in this function)
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
^
libavformat/udp.c:596:32: error: 'PTHREAD_CANCEL_ENABLE' undeclared (first use in this function)
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
^
libavformat/udp.c: In function 'udp_close':
libavformat/udp.c:1142:13: error: implicit declaration of function 'pthread_cancel' [-Werror=implicit-function-declaration]
pthread_cancel(s->circular_buffer_thread);
解決方案:修改ffmpeg/libavformat/udp.c文件函數(shù)開始部分,添加:
#define SIG_CANCEL_SIGNAL SIGUSR1
#define PTHREAD_CANCEL_ENABLE 1
#define PTHREAD_CANCEL_DISABLE 0
typedef long pthread_t;
static int pthread_setcancelstate(int state, int *oldstate) {
sigset_t new, old;
int ret;
sigemptyset (&new);
sigaddset (&new, SIG_CANCEL_SIGNAL);
ret = pthread_sigmask(state == PTHREAD_CANCEL_ENABLE ? SIG_BLOCK : SIG_UNBLOCK, &new , &old);
if(oldstate != NULL)
{
*oldstate =sigismember(&old,SIG_CANCEL_SIGNAL) == 0 ? PTHREAD_CANCEL_DISABLE : PTHREAD_CANCEL_ENABLE;
}
return ret;
}
static inline int pthread_cancel(pthread_t thread) {
return pthread_kill(thread, SIG_CANCEL_SIGNAL);
}
另:在Android NDK中是沒有l(wèi)ibpthread.a的,有關(guān)的功能實現(xiàn)已經(jīng)被集成到了libc.a中,所以使用時不需要使用-lpthread,而應(yīng)該使用-lc鏈接參數(shù)。
附:ffmpeg添加libdavs2模塊并合并成一個.so文件輸出的arm64版編譯腳本:
#!/bin/sh
build_dir=$(pwd)
AVS2SRC=$build_dir/cavs2dec_lib # path to libdavs2 install dictionary
ARCH=arm64
SDK_VERSION=21
NDK="/Android/android-ndk-r14b"
export PKG_CONFIG_PATH=$AVS2SRC/lib/pkgconfig
if [ "$ARCH" = "arm64" ]
then
PLATFORM_PREFIX="aarch64-linux-android-"
HOST="aarch64"
PLATFORM_VERSION=4.9
EXTRA_CFLAGS="-march=armv8-a -O3 -D__ARM_ARCH_7__ -D__ARM_ARCH_7A__ -DPIC \
-flax-vector-conversions -Wunused-variable -fPIE -pie -pthread"
else
PLATFORM_PREFIX="arm-linux-androideabi-"
HOST="arm"
PLATFORM_VERSION=4.9
EXTRA_CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon -marm -mtune=cortex-a8 -D__ARM_ARCH_7__ -D__ARM_ARCH_7A__ \
-flax-vector-conversions -Wunused-variable -fPIE -pie"
fi
NDKROOT=$NDK/platforms/android-${SDK_VERSION}/arch-${ARCH}
TOOLCHAIN=$NDK/toolchains/${PLATFORM_PREFIX}${PLATFORM_VERSION}/prebuilt/linux-x86_64
CROSS_PREFIX=$TOOLCHAIN/bin/${PLATFORM_PREFIX}
EXTRA_LDFLAGS="-fPIE -pie -llog -landroid -shared"
# cp $NDKROOT/usr/include/linux/sysctl.h $NDKROOT/usr/include/sys/sysctl.h
build_one(){
./configure \
--target-os=linux \
--prefix=$build_dir/avs2_lib \
--enable-cross-compile \
--extra-libs="-lgcc -lstdc++ -lc" \
--arch=$ARCH \
--cross-prefix=$CROSS_PREFIX \
--cc=${CROSS_PREFIX}gcc \
--nm=${CROSS_PREFIX}nm \
--sysroot=$NDKROOT \
--extra-cflags="-I$NDKROOT/usr/include -I$AVS2SRC/include -O3 -fpic \
-DANDROID -DHAVE_SYS_UIO_H=1 -Dipv6mr_interface=ipv6mr_ifindex -fasm \
-Wno-psabi -fno-short-enums -fno-strict-aliasing -finline-limit=300 \
-DHAVE_CLOSESOCKET=0 -fvisibility=hidden -fdata-sections -ffunction-sections $EXTRA_CFLAGS" \
--extra-ldflags="-Wl,-rpath-link=$NDKROOT/usr/lib -L$NDKROOT/usr/lib -lc \
-lm -ldl -llog -lstdc++ $AVS2SRC/lib/libdavs2.a -L$AVS2SRC/lib -ldavs2 \
$EXTRA_LDFLAGS" \
--enable-gpl \
--enable-libdavs2 \
--disable-shared \
--enable-static \
--disable-linux-perf \
--enable-pic \
--disable-armv5te \
--disable-armv6 \
--disable-armv6t2 \
--pkg-config=pkg-config
}
cd FFmpegAVS2
make distclean
build_one
make -j16 && make install
# use to package all part into single .so lib
${CROSS_PREFIX}ld -z muldefs -rpath-link=$NDKROOT/usr/lib \
-L$NDKROOT/usr/lib -L$PREFIX/lib -soname libffmpeg.so -shared -nostdlib -Bsymbolic \
--whole-archive --no-undefined -o $AVS2SRC/libffmpeg.so $AVS2SRC/lib/libdavs2.a \
libavcodec/libavcodec.a libavfilter/libavfilter.a libswresample/libswresample.a \
libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a \
libpostproc/libpostproc.a libavdevice/libavdevice.a -fPIC -lc -lm -lz -ldl -llog \
$NDK/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/libgnustl_shared.so -lstdc++ \
--dynamic-linker=/system/bin/linker $TOOLCHAIN/lib/gcc/aarch64-linux-android/4.9.x/libgcc.a
總結(jié)
以上是生活随笔為你收集整理的android linux pthread_cancel,Android NDK缺失pthread_cancel和pthread_setcancelstate的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: python中的index函数
- 下一篇: 查话费100861
