Skip to content

[Memory64] pthread libc changes for 64-bit #15229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ var LibraryPThread = {
// Deduce which WebGL canvases (HTMLCanvasElements or OffscreenCanvases) should be passed over to the
// Worker that hosts the spawned pthread.
// Comma-delimited list of CSS selectors that must identify canvases by IDs: "#canvas1, #canvas2, ..."
var transferredCanvasNames = attr ? {{{ makeGetValue('attr', 36, 'i32') }}} : 0;
var transferredCanvasNames = attr ? {{{ makeGetValue('attr', 40, POINTER_TYPE) }}} : 0;
#if OFFSCREENCANVASES_TO_PTHREAD
// Proxied canvases string pointer -1 is used as a special token to fetch
// whatever canvases were passed to build in -s
Expand Down
17 changes: 11 additions & 6 deletions system/lib/libc/musl/arch/emscripten/bits/alltypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,18 @@ typedef long suseconds_t;


#if defined(__NEED_pthread_attr_t) && !defined(__DEFINED_pthread_attr_t)
typedef struct {
union {
int __i[10];
volatile int __vi[10];
unsigned __s[10];
} __u;
#ifdef __EMSCRIPTEN__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the 11 change to 10?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the 11 field is the pointer below.

// For canvas transfer implementation in Emscripten, use an extra 11th control field
// to pass a pointer to a string denoting the WebGL canvases to transfer.
typedef struct { union { int __i[11]; volatile int __vi[11]; unsigned __s[11]; } __u; } pthread_attr_t;
#else
typedef struct { union { int __i[10]; volatile int __vi[10]; unsigned __s[10]; } __u; } pthread_attr_t;
// For canvas transfer implementation in Emscripten, use an extra control field
// to pass a pointer to a string denoting the WebGL canvases to transfer.
const char *_a_transferredcanvases;
#endif
} pthread_attr_t;
#define __DEFINED_pthread_attr_t
#endif

Expand All @@ -114,7 +119,7 @@ typedef struct { union { int __i[12]; volatile int __vi[12]; void *__p[12]; } __
#endif

#if defined(__NEED_pthread_rwlock_t) && !defined(__DEFINED_pthread_rwlock_t)
typedef struct { union { int __i[8]; volatile int __vi[8]; void *__p[8]; } __u; } pthread_rwlock_t;
typedef struct { union { int __i[sizeof(long)==8?14:8]; volatile int __vi[sizeof(long)==8?14:8]; void *__p[sizeof(long)==8?7:8]; } __u; } pthread_rwlock_t;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the new constants 14 and 7? I can't see how doubling a pointer size leads to such things, so I'm missing something...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#define __DEFINED_pthread_rwlock_t
#endif

Expand Down
4 changes: 4 additions & 0 deletions system/lib/libc/musl/arch/emscripten/bits/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
#define LONG_BIT 32
#endif

#if __LP64__
#define LONG_MAX 0x7fffffffffffffffL
#else
#define LONG_MAX 0x7fffffffL
#endif
#define LLONG_MAX 0x7fffffffffffffffLL
13 changes: 13 additions & 0 deletions system/lib/libc/musl/arch/emscripten/bits/stdint.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@ typedef uint32_t uint_fast32_t;
#define UINT_FAST16_MAX UINT32_MAX
#define UINT_FAST32_MAX UINT32_MAX

#if __LP64__

#define INTPTR_MIN INT64_MIN
#define INTPTR_MAX INT64_MAX
#define UINTPTR_MAX UINT64_MAX
#define PTRDIFF_MIN INT64_MIN
#define PTRDIFF_MAX INT64_MAX
#define SIZE_MAX UINT64_MAX

#else

#define INTPTR_MIN INT32_MIN
#define INTPTR_MAX INT32_MAX
#define UINTPTR_MAX UINT32_MAX
#define PTRDIFF_MIN INT32_MIN
#define PTRDIFF_MAX INT32_MAX
#define SIZE_MAX UINT32_MAX

#endif
2 changes: 1 addition & 1 deletion system/lib/libc/musl/src/internal/pthread_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct __timer {
// XXX Emscripten: The spec allows detecting when multiple write locks would deadlock, so use an extra field
// _rw_wr_owner to record which thread owns the write lock in order to avoid hangs.
// Points to the pthread that currently has the write lock.
#define _rw_wr_owner __u.__vi[3]
#define _rw_wr_owner __u.__p[3]
#endif
#define _b_lock __u.__vi[0]
#define _b_waiters __u.__vi[1]
Expand Down
8 changes: 4 additions & 4 deletions system/lib/libc/musl/src/thread/pthread_rwlock_timedwrlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct times
#ifdef __EMSCRIPTEN__
/// XXX Emscripten: The spec allows detecting when multiple write locks would deadlock, which we do here to avoid hangs.
/// If attempting to lock the write lock that we already own, error out.
if (rw->_rw_wr_owner == (int)pthread_self()) return EDEADLK;
if (rw->_rw_wr_owner == (void *)pthread_self()) return EDEADLK;
#endif
int r, t;

r = pthread_rwlock_trywrlock(rw);
if (r != EBUSY) return r;

int spins = 100;
while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin();

Expand All @@ -27,7 +27,7 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct times
#ifdef __EMSCRIPTEN__
/// XXX Emscripten: The spec allows detecting when multiple write locks would deadlock, which we do here to avoid hangs.
/// Mark this thread as the owner of this write lock.
rw->_rw_wr_owner = (int)pthread_self();
rw->_rw_wr_owner = (void *)pthread_self();
#endif
return r;
}
2 changes: 1 addition & 1 deletion system/lib/libc/musl/src/thread/pthread_rwlock_trywrlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ int pthread_rwlock_trywrlock(pthread_rwlock_t *rw)
#ifdef __EMSCRIPTEN__
/// XXX Emscripten: The spec allows detecting when multiple write locks would deadlock, which we do here to avoid hangs.
/// Mark this thread to own the write lock, to ignore multiple attempts to lock.
rw->_rw_wr_owner = (int)pthread_self();
rw->_rw_wr_owner = (void *)pthread_self();
#endif
return 0;
}
2 changes: 1 addition & 1 deletion system/lib/libc/musl/src/thread/pthread_rwlock_unlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int pthread_rwlock_unlock(pthread_rwlock_t *rw)
#ifdef __EMSCRIPTEN__
/// XXX Emscripten: The spec allows detecting when multiple write locks would deadlock, which we do here to avoid hangs.
/// Mark this thread to not own the write lock anymore.
if (rw->_rw_wr_owner == (int)pthread_self()) rw->_rw_wr_owner = 0;
if (rw->_rw_wr_owner == (void *)pthread_self()) rw->_rw_wr_owner = 0;
#endif

do {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
.globaltype __tls_base, i32
#ifdef __wasm64__
#define PTR i64
#else
#define PTR i32
#endif

.globaltype __tls_base, PTR
.globaltype thread_id, PTR

.globaltype thread_id, i32
thread_id:

.globaltype is_main_thread, i32
Expand All @@ -11,13 +17,13 @@ is_runtime_thread:

.globl __pthread_self
__pthread_self:
.functype __pthread_self () -> (i32)
.functype __pthread_self () -> (PTR)
global.get thread_id
end_function

.globl _emscripten_thread_init
_emscripten_thread_init:
.functype _emscripten_thread_init (i32, i32, i32) -> ()
.functype _emscripten_thread_init (PTR, i32, i32) -> ()
local.get 0
global.set thread_id
local.get 1
Expand All @@ -30,7 +36,7 @@ _emscripten_thread_init:
# accessible from C/C++.
.globl _emscripten_tls_base
_emscripten_tls_base:
.functype _emscripten_tls_base () -> (i32)
.functype _emscripten_tls_base () -> (PTR)
global.get __tls_base
end_function

Expand Down
12 changes: 5 additions & 7 deletions system/lib/pthread/library_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,15 @@
#include <emscripten.h>
#include <emscripten/threading.h>

// Extra pthread_attr_t field:
#define _a_transferredcanvases __u.__s[9]

void __pthread_testcancel();

int emscripten_pthread_attr_gettransferredcanvases(const pthread_attr_t* a, const char** str) {
*str = (const char*)a->_a_transferredcanvases;
*str = a->_a_transferredcanvases;
return 0;
}

int emscripten_pthread_attr_settransferredcanvases(pthread_attr_t* a, const char* str) {
a->_a_transferredcanvases = (int)str;
a->_a_transferredcanvases = str;
return 0;
}

Expand Down Expand Up @@ -163,7 +160,7 @@ static void _do_call(em_queued_call* q) {
break;
case EM_PROXIED_JS_FUNCTION:
q->returnValue.d =
emscripten_receive_on_main_thread_js((int)q->functionPtr, q->args[0].i, &q->args[1].d);
emscripten_receive_on_main_thread_js((int)(size_t)q->functionPtr, q->args[0].i, &q->args[1].d);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intptr_t there to avoid the double cast?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 64-bit, intptr_t would cast from 64 to 64, so would still need a secondary cast?

break;
case EM_FUNC_SIG_V:
((em_func_v)q->functionPtr)();
Expand Down Expand Up @@ -654,7 +651,8 @@ double emscripten_run_in_main_runtime_thread_js(int index, int num_args, int64_t
}
c->calleeDelete = 1-sync;
c->functionEnum = EM_PROXIED_JS_FUNCTION;
c->functionPtr = (void*)index;
// Index not needed to ever be more than 32-bit.
c->functionPtr = (void*)(size_t)index;
assert(num_args+1 <= EM_QUEUED_JS_CALL_MAX_ARGS);
// The types are only known at runtime in these calls, so we store values that
// must be able to contain any valid JS value, including a 64-bit BigInt if
Expand Down
2 changes: 1 addition & 1 deletion tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ def get_files(self):
'pthread_join.c',
'pthread_testcancel.c',
'emscripten_proxy_main.c',
'emscripten_thread_state.s',
'emscripten_thread_state.S',
])
else:
ignore += ['thread']
Expand Down