From f1070db1577c1933ea4c774b11c30e58392e5d94 Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Sat, 5 Oct 2024 19:29:43 +0200 Subject: [PATCH] ffi/pthread: fix `pthread_attr_t` and `PTHREAD_CREATE_DETACHED` declarations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The declarations of `pthread_attr_t` and `PTHREAD_CREATE_DETACHED` depend on the arch / OS: | Target machine | `sizeof (pthread_attr_t)` | `PTHREAD_CREATE_DETACHED` | |---------------------------------|--------------------------:|--------------------------:| | aarch64-linux-gnu-g++ | 64 | 1 | | aarch64-unknown-linux-android21 | 56 | 1 | | arm-kindlepw2-linux-gnueabi | 36 | 1 | | arm-linux-gnueabihf | 36 | 1 | | arm64-apple-darwin23.6.0 | 64 | 2 | | armv7a-unknown-linux-android18 | 24 | 1 | | i386-pc-linux-gnu | 36 | 1 | | i686-unknown-linux-android18 | 24 | 1 | | x86_64-apple-darwin22.6.0 | 64 | 2 | | x86_64-pc-linux-gnu | 56 | 1 | The following C++ compiler command can be used to print those values: ```bash ▸ g++ -x c++ -c -o /dev/null - <<\EOF template struct PrintConst; PrintConst sizeof_pthread_attr_t; PrintConst valueof_PTHREAD_CREATE_DETACHED; EOF :3:37: error: aggregate ‘PrintConst<56> sizeof_pthread_attr_t’ has incomplete type and cannot be defined :4:37: error: aggregate ‘PrintConst<1> valueof_PTHREAD_CREATE_DETACHED’ has incomplete type and cannot be defined ``` --- ffi-cdecl/pthread_cdecl.c | 22 ------------ ffi/pthread_64b_h.lua | 14 -------- ffi/pthread_def_h.lua | 14 -------- ffi/pthread_h.lua | 74 +++++++++++++++++++++++++++++++++------ ffi/pthread_x64_h.lua | 14 -------- ffi/pthread_x86_h.lua | 14 -------- 6 files changed, 64 insertions(+), 88 deletions(-) delete mode 100644 ffi-cdecl/pthread_cdecl.c delete mode 100644 ffi/pthread_64b_h.lua delete mode 100644 ffi/pthread_def_h.lua delete mode 100644 ffi/pthread_x64_h.lua delete mode 100644 ffi/pthread_x86_h.lua diff --git a/ffi-cdecl/pthread_cdecl.c b/ffi-cdecl/pthread_cdecl.c deleted file mode 100644 index 0b2b5225d..000000000 --- a/ffi-cdecl/pthread_cdecl.c +++ /dev/null @@ -1,22 +0,0 @@ -#include - -#include "ffi-cdecl.h" - -cdecl_type(pthread_t) - -// NOTE: This is... annoying. The array's size depends on the arch (c.f., __SIZEOF_PTHREAD_ATTR_T). -// We dispatch loading the right one with a bit of trickery in ffi/pthread_h.lua -// c.f., https://github.com/koreader/koreader/pull/4016#issuecomment-399692355 for the full story -//cdecl_const(__SIZEOF_PTHREAD_ATTR_T) - -//cdecl_union(pthread_attr_t) -cdecl_type(pthread_attr_t) - -cdecl_func(pthread_attr_init) -cdecl_func(pthread_attr_setdetachstate) -cdecl_func(pthread_attr_destroy) - -cdecl_const(PTHREAD_CREATE_DETACHED) - -cdecl_func(pthread_create) - diff --git a/ffi/pthread_64b_h.lua b/ffi/pthread_64b_h.lua deleted file mode 100644 index 5eda53437..000000000 --- a/ffi/pthread_64b_h.lua +++ /dev/null @@ -1,14 +0,0 @@ -local ffi = require("ffi") - -ffi.cdef[[ -typedef long unsigned int pthread_t; -typedef union { - char __size[64]; - long int __align; -} pthread_attr_t; -int pthread_attr_init(pthread_attr_t *) __attribute__((nothrow, leaf)); -int pthread_attr_setdetachstate(pthread_attr_t *, int) __attribute__((nothrow, leaf)); -int pthread_attr_destroy(pthread_attr_t *) __attribute__((nothrow, leaf)); -static const int PTHREAD_CREATE_DETACHED = 1; -int pthread_create(pthread_t *restrict, const pthread_attr_t *restrict, void *(*)(void *), void *restrict) __attribute__((nothrow)); -]] diff --git a/ffi/pthread_def_h.lua b/ffi/pthread_def_h.lua deleted file mode 100644 index 23667b271..000000000 --- a/ffi/pthread_def_h.lua +++ /dev/null @@ -1,14 +0,0 @@ -local ffi = require("ffi") - -ffi.cdef[[ -typedef long unsigned int pthread_t; -typedef union { - char __size[36]; - long int __align; -} pthread_attr_t; -int pthread_attr_init(pthread_attr_t *) __attribute__((__nothrow__, __leaf__)); -int pthread_attr_setdetachstate(pthread_attr_t *, int) __attribute__((__nothrow__, __leaf__)); -int pthread_attr_destroy(pthread_attr_t *) __attribute__((__nothrow__, __leaf__)); -static const int PTHREAD_CREATE_DETACHED = 1; -int pthread_create(pthread_t *restrict, const pthread_attr_t *restrict, void *(*)(void *), void *restrict) __attribute__((__nothrow__)); -]] diff --git a/ffi/pthread_h.lua b/ffi/pthread_h.lua index 9799cd0fb..d7c083869 100644 --- a/ffi/pthread_h.lua +++ b/ffi/pthread_h.lua @@ -1,14 +1,68 @@ local ffi = require("ffi") --- The declaration vary depending on the arch, load the right one... - -if ffi.arch == "x64" then - require("ffi/pthread_x64_h") -elseif ffi.arch == "x86" then - require("ffi/pthread_x86_h") -elseif ffi.abi("64bit") then - require("ffi/pthread_64b_h") -else - require("ffi/pthread_def_h") +--[[-- +The declarations of `pthread_attr_t` and `PTHREAD_CREATE_DETACHED` depend on the arch / OS: + +| Target machine | `sizeof (pthread_attr_t)` | `PTHREAD_CREATE_DETACHED` | +|---------------------------------|--------------------------:|--------------------------:| +| aarch64-linux-gnu-g++ | 64 | 1 | +| aarch64-unknown-linux-android21 | 56 | 1 | +| arm-kindlepw2-linux-gnueabi | 36 | 1 | +| arm-linux-gnueabihf | 36 | 1 | +| arm64-apple-darwin23.6.0 | 64 | 2 | +| armv7a-unknown-linux-android18 | 24 | 1 | +| i386-pc-linux-gnu | 36 | 1 | +| i686-unknown-linux-android18 | 24 | 1 | +| x86_64-apple-darwin22.6.0 | 64 | 2 | +| x86_64-pc-linux-gnu | 56 | 1 | + +The following C++ compiler command can be used to print those values: + +```bash +▸ g++ -x c++ -c -o /dev/null - <<\EOF +#include +template struct PrintConst; +PrintConst sizeof_pthread_attr_t; +PrintConst valueof_PTHREAD_CREATE_DETACHED; +EOF +:3:37: error: aggregate ‘PrintConst<56> sizeof_pthread_attr_t’ has incomplete type and cannot be defined +:4:37: error: aggregate ‘PrintConst<1> valueof_PTHREAD_CREATE_DETACHED’ has incomplete type and cannot be defined +``` +--]] +local sizeof_pthread_attr_t +local valueof_PTHREAD_CREATE_DETACHED +if ffi.os == "OSX" then + sizeof_pthread_attr_t = 64 + valueof_PTHREAD_CREATE_DETACHED = 2 +elseif ffi.os == "Linux" then + if os.getenv("IS_ANDROID") then + sizeof_pthread_attr_t = ffi.abi("32bit") and 24 or 56 + elseif ffi.arch == "arm" or ffi.arch == "x86" then + sizeof_pthread_attr_t = 36 + elseif ffi.arch == "x64" then + sizeof_pthread_attr_t = 56 + elseif ffi.arch == "arm64" or ffi.arch == "arm64be" then + sizeof_pthread_attr_t = 64 + end + valueof_PTHREAD_CREATE_DETACHED = 1 +end + +if not sizeof_pthread_attr_t or not valueof_PTHREAD_CREATE_DETACHED then + error("unsupported arch / OS") end +ffi.cdef(string.format( + [[ + typedef union { + char __size[%u]; + long int __align; + } pthread_attr_t; + typedef long unsigned int pthread_t; + static const int PTHREAD_CREATE_DETACHED = %u; + int pthread_attr_init(pthread_attr_t *); + int pthread_attr_setdetachstate(pthread_attr_t *, int); + int pthread_attr_destroy(pthread_attr_t *); + int pthread_create(pthread_t *restrict, const pthread_attr_t *restrict, void *(*)(void *), void *restrict); + ]] + , sizeof_pthread_attr_t, valueof_PTHREAD_CREATE_DETACHED +)) diff --git a/ffi/pthread_x64_h.lua b/ffi/pthread_x64_h.lua deleted file mode 100644 index 7c42697c8..000000000 --- a/ffi/pthread_x64_h.lua +++ /dev/null @@ -1,14 +0,0 @@ -local ffi = require("ffi") - -ffi.cdef[[ -typedef long unsigned int pthread_t; -typedef union { - char __size[56]; - long int __align; -} pthread_attr_t; -int pthread_attr_init(pthread_attr_t *) __attribute__((__nothrow__, __leaf__)); -int pthread_attr_setdetachstate(pthread_attr_t *, int) __attribute__((__nothrow__, __leaf__)); -int pthread_attr_destroy(pthread_attr_t *) __attribute__((__nothrow__, __leaf__)); -static const int PTHREAD_CREATE_DETACHED = 1; -int pthread_create(pthread_t *restrict, const pthread_attr_t *restrict, void *(*)(void *), void *restrict) __attribute__((__nothrow__)); -]] diff --git a/ffi/pthread_x86_h.lua b/ffi/pthread_x86_h.lua deleted file mode 100644 index a194f67a1..000000000 --- a/ffi/pthread_x86_h.lua +++ /dev/null @@ -1,14 +0,0 @@ -local ffi = require("ffi") - -ffi.cdef[[ -typedef long unsigned int pthread_t; -typedef union { - char __size[32]; - long int __align; -} pthread_attr_t; -int pthread_attr_init(pthread_attr_t *) __attribute__((__nothrow__, __leaf__)); -int pthread_attr_setdetachstate(pthread_attr_t *, int) __attribute__((__nothrow__, __leaf__)); -int pthread_attr_destroy(pthread_attr_t *) __attribute__((__nothrow__, __leaf__)); -static const int PTHREAD_CREATE_DETACHED = 1; -int pthread_create(pthread_t *restrict, const pthread_attr_t *restrict, void *(*)(void *), void *restrict) __attribute__((__nothrow__)); -]]