Skip to content

Commit

Permalink
ffi/pthread: fix pthread_attr_t and PTHREAD_CREATE_DETACHED decla…
Browse files Browse the repository at this point in the history
…rations

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 <int val> struct PrintConst;
PrintConst<sizeof (pthread_attr_t)> sizeof_pthread_attr_t;
PrintConst<PTHREAD_CREATE_DETACHED> valueof_PTHREAD_CREATE_DETACHED;
EOF
<stdin>:3:37: error: aggregate ‘PrintConst<56> sizeof_pthread_attr_t’ has incomplete type and cannot be defined
<stdin>:4:37: error: aggregate ‘PrintConst<1> valueof_PTHREAD_CREATE_DETACHED’ has incomplete type and cannot be defined
```
  • Loading branch information
benoit-pierre committed Oct 5, 2024
1 parent 8a86de0 commit f1070db
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 88 deletions.
22 changes: 0 additions & 22 deletions ffi-cdecl/pthread_cdecl.c

This file was deleted.

14 changes: 0 additions & 14 deletions ffi/pthread_64b_h.lua

This file was deleted.

14 changes: 0 additions & 14 deletions ffi/pthread_def_h.lua

This file was deleted.

74 changes: 64 additions & 10 deletions ffi/pthread_h.lua
Original file line number Diff line number Diff line change
@@ -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 <pthread.h>
template <int val> struct PrintConst;
PrintConst<sizeof (pthread_attr_t)> sizeof_pthread_attr_t;
PrintConst<PTHREAD_CREATE_DETACHED> valueof_PTHREAD_CREATE_DETACHED;
EOF
<stdin>:3:37: error: aggregate ‘PrintConst<56> sizeof_pthread_attr_t’ has incomplete type and cannot be defined
<stdin>: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
))
14 changes: 0 additions & 14 deletions ffi/pthread_x64_h.lua

This file was deleted.

14 changes: 0 additions & 14 deletions ffi/pthread_x86_h.lua

This file was deleted.

0 comments on commit f1070db

Please sign in to comment.