Skip to content

Commit

Permalink
Use pthread's thread-specific storage on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Nov 28, 2023
1 parent a59b5ab commit 8b57249
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/crystal/system/unix/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ module Crystal::System::Thread
raise RuntimeError.from_errno("sched_yield") unless ret == 0
end

{% if flag?(:openbsd) %}
# no thread local storage (TLS) for OpenBSD,
# we use pthread's specific storage (TSS) instead:
# no thread local storage (TLS) for OpenBSD,
# we use pthread's specific storage (TSS) instead
#
# Android appears to support TLS to some degree, but executables fail with
# an underaligned TLS segment, see https://github.com/crystal-lang/crystal/issues/13951
{% if flag?(:openbsd) || flag?(:android) %}
@@current_key : LibC::PthreadKeyT

@@current_key = begin
Expand Down
4 changes: 4 additions & 0 deletions src/lib_c/aarch64-android/c/pthread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ lib LibC
fun pthread_detach(__pthread : PthreadT) : Int
fun pthread_getattr_np(__pthread : PthreadT, __attr : PthreadAttrT*) : Int
fun pthread_equal(__lhs : PthreadT, __rhs : PthreadT) : Int
fun pthread_getspecific(__key : PthreadKeyT) : Void*
fun pthread_join(__pthread : PthreadT, __return_value_ptr : Void**) : Int
fun pthread_key_create(__key_ptr : PthreadKeyT*, __key_destructor : Void* ->) : Int

fun pthread_mutexattr_destroy(__attr : PthreadMutexattrT*) : Int
fun pthread_mutexattr_init(__attr : PthreadMutexattrT*) : Int
Expand All @@ -36,4 +38,6 @@ lib LibC
fun pthread_mutex_unlock(__mutex : PthreadMutexT*) : Int

fun pthread_self : PthreadT

fun pthread_setspecific(__key : PthreadKeyT, __value : Void*) : Int
end
1 change: 1 addition & 0 deletions src/lib_c/aarch64-android/c/sys/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ lib LibC
end

alias PthreadCondattrT = Long
alias PthreadKeyT = Int

struct PthreadMutexT
__private : Int32[10]
Expand Down

0 comments on commit 8b57249

Please sign in to comment.