Skip to content

Commit 8a52e4d

Browse files
committed
android support, hopefully
1 parent 23c89dd commit 8a52e4d

2 files changed

Lines changed: 14 additions & 37 deletions

File tree

jsrc/mt.c

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,47 +65,26 @@ I jfutex_waitn(UI4 *p,UI4 v,UI ns){I r;
6565
if(r==-ENOMEM)R EVWSFULL;
6666
R EVFACE;}
6767
#endif
68-
#elif defined(__linux__)&&!defined(ANDROID)
69-
//glibc 'syscall': stupid errno
70-
// ??? asm applicable to arm64
71-
// #if defined(__x86_64__)||defined(__i386__)||defined(_M_X64)||defined(_M_IX86)
72-
void jfutex_wake1(UI4 *p){
73-
__asm__ volatile("syscall" :: "a" (SYS_futex), //eax: syscall#
74-
"D" (p), //rdi: ptr
75-
"S" (FUTEX_WAKE_PRIVATE), //rsi: op
76-
"d" (1));} //rdx: count
77-
void jfutex_wakea(UI4 *p){
78-
__asm__ volatile("syscall" :: "a" (SYS_futex), //eax: syscall#
79-
"D" (p), //rdi: ptr
80-
"S" (FUTEX_WAKE_PRIVATE), //rsi: op
81-
"d" (0xffffffff));} //rdx: count
68+
#elif defined(__linux__)
69+
#include<unistd.h>
70+
#if defined(__NR_futex) && !defined(SYS_futex) //android seemingly defines the former, but not the latter
71+
#define SYS_futex __NR_futex
72+
#endif
73+
void jfutex_wake1(UI4 *p){syscall(SYS_futex,p,FUTEX_WAKE_PRIVATE,1);}
74+
void jfutex_wakea(UI4 *p){syscall(SYS_futex,p,FUTEX_WAKE_PRIVATE,0xffffffff);}
8275
C jfutex_wait(UI4 *p,UI4 v){
83-
register struct timespec *pts asm("r10") = 0;
84-
int r;__asm__ volatile("syscall" : "=a"(r) //result in rax
85-
: "a" (SYS_futex), //eax: syscall#
86-
"D" (p), //rdi: ptr
87-
"S" (FUTEX_WAIT_PRIVATE), //rsi: op
88-
"d" (v), //rdx: espected
89-
"r" (pts)); //r10: timeout (null=no timeout)
76+
struct timespec *pts = 0;
77+
int r=syscall(SYS_futex,p,FUTEX_WAIT_PRIVATE,v,pts);
9078
if(r>=0)R 0;
91-
if(r==-EAGAIN||r==-EINTR)R 0;
79+
if(errno==-EAGAIN||r==-EINTR)R 0;
9280
R EVFACE;}
9381
I jfutex_waitn(UI4 *p,UI4 v,UI ns){
9482
struct timespec ts={.tv_sec=ns/1000000000, .tv_nsec=ns%1000000000};
95-
register struct timespec *pts asm("r10") = &ts;
96-
int r;__asm__ volatile("syscall" : "=a"(r) //result in rax
97-
: "a" (SYS_futex), //eax: syscall#
98-
"D" (p), //rdi: ptr
99-
"S" (FUTEX_WAIT_PRIVATE), //rsi: op
100-
"d" (v), //rdx: espected
101-
"r" (pts), //r10: timeout (relative!)
102-
"m" (*pts)); //dummy memory clobber to keep from optimising out the initialisation of ts
83+
int r=syscall(SYS_futex,p,FUTEX_WAIT_PRIVATE,v,&ts);
10384
if(r>=0)R 0;
104-
if(r==-ETIMEDOUT)R -1;
105-
if(r==-EAGAIN||r==-EINTR)R 0;
85+
if(errno==-ETIMEDOUT)R -1;
86+
if(errno==-EAGAIN||r==-EINTR)R 0;
10687
R EVFACE;}
107-
#elif defined(ANDROID)
108-
// TODO
10988
#elif defined(_WIN32)
11089
// defined in cd.c to avoid name collisions between j.h and windows.h
11190
#endif

jsrc/mt.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ I jtpthread_mutex_trylock(jtpthread_mutex_t*,I self); //0=success -1=failure pos
2525
C jtpthread_mutex_unlock(jtpthread_mutex_t*,I self); //0 or error code
2626
//note: self must be non-zero
2727

28-
#if defined(__linux__)&&!defined(ANDROID)
28+
#if defined(__linux__)
2929
#include <linux/futex.h>
3030
#include <sys/syscall.h>
31-
#elif defined(ANDORID)
32-
#error no futex support for your platform
3331
#elif defined(__APPLE__)
3432
// ulock (~futex) junk from xnu. timeout=0 means wait forever
3533
extern int __ulock_wait(uint32_t operation, void *addr, uint64_t value, uint32_t timeout); // timeout in us

0 commit comments

Comments
 (0)