aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/runtime/linux_sync.c (24209B)


      1 #define _GNU_SOURCE
      2 #include "linux_sync.h"
      3 #include <errno.h>
      4 #include <fcntl.h>
      5 #ifdef __EMSCRIPTEN__
      6 #include <emscripten/threading.h>
      7 #else
      8 #include <linux/futex.h>
      9 #endif
     10 #include <limits.h>
     11 #include <stdatomic.h>
     12 #include <pthread.h>
     13 #include <sys/file.h>
     14 #include <sys/mman.h>
     15 #include <sys/stat.h>
     16 #include <sys/syscall.h>
     17 #include <time.h>
     18 #include <unistd.h>
     19 
     20 /* Shared state contains no pointers. Each open handle holds a shared file lock,
     21  * so the kernel releases the last reference even after an abnormal process exit. */
     22 struct semaphore_state {
     23     _Atomic uint32_t count;
     24     uint32_t maximum;
     25     char name[260];
     26 #ifdef __EMSCRIPTEN__
     27     unsigned references;
     28 #endif
     29 };
     30 _Static_assert(sizeof(_Atomic uint32_t) == 4 && ATOMIC_INT_LOCK_FREE == 2,
     31                "Linux futexes require lock-free 32-bit counters");
     32 
     33 struct semaphore_handle {
     34     uint32_t id, access;
     35     int fd, kind; /* 0: semaphore, 1: auto-reset event, 2: mutex, 3: manual-reset event */
     36     pthread_mutex_t ownership;
     37     int abandoned;
     38     unsigned references;
     39     struct semaphore_state *state;
     40     struct semaphore_handle *next;
     41 };
     42 static struct semaphore_handle *handles;
     43 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     44 static pthread_cond_t changed = PTHREAD_COND_INITIALIZER;
     45 static uint32_t next_handle = 0x100;
     46 
     47 static void system_failure(const char *operation)
     48 {
     49     perror(operation);
     50     abort();
     51 }
     52 
     53 static struct semaphore_handle *find_handle_locked(uint32_t id)
     54 {
     55     for (struct semaphore_handle *handle = handles; handle; handle = handle->next)
     56         if (handle->id == id) return handle;
     57     MEM32(g_fs_base + 0x34) = 6; /* ERROR_INVALID_HANDLE */
     58     return NULL;
     59 }
     60 
     61 static uint32_t open_semaphore_locked(const char *name, int create, int32_t initial,
     62                                int32_t maximum, uint32_t access)
     63 {
     64     int fd = -1, existing = 0;
     65     struct semaphore_state *state = NULL;
     66 #ifdef __EMSCRIPTEN__
     67     /* Browser workers share one process and one named-object namespace. */
     68     if (name) {
     69         for (struct semaphore_handle *other = handles; other; other = other->next)
     70             if (!strcmp(other->state->name, name)) { state = other->state; break; }
     71     }
     72     existing = state != NULL;
     73     if (!existing && !create) { MEM32(g_fs_base + 0x34) = 2; return 0; }
     74     if (!existing && (maximum <= 0 || initial < 0 || initial > maximum)) {
     75         MEM32(g_fs_base + 0x34) = 87;
     76         return 0;
     77     }
     78     if (!existing) {
     79         if (name && strlen(name) >= 260) abort();
     80         state = calloc(1, sizeof(*state));
     81         if (!state) abort();
     82         atomic_init(&state->count, initial);
     83         state->maximum = maximum;
     84         strcpy(state->name, name ? name : "");
     85     }
     86     ++state->references;
     87 #else
     88     int directory = -1;
     89     if (name) {
     90         if (!*name || strlen(name) >= 260 || strchr(name, '\\')) {
     91             fprintf(stderr, "Unsupported semaphore namespace/name: %s\n", name);
     92             abort();
     93         }
     94         /* Serialize namespace changes independently of the object's lifetime locks. */
     95         char path[80];
     96         snprintf(path, sizeof(path), "/tmp/ao-native-sync-%u", (unsigned)getuid());
     97         if (mkdir(path, 0700) && errno != EEXIST) system_failure("semaphore namespace mkdir");
     98         directory = open(path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
     99         if (directory < 0) system_failure("semaphore namespace open");
    100         struct stat info;
    101         if (fstat(directory, &info)) system_failure("semaphore namespace stat");
    102         if (info.st_uid != getuid() || (info.st_mode & 077)) {
    103             fprintf(stderr, "Semaphore namespace must be owned by this user with mode 0700\n");
    104             abort();
    105         }
    106         if (flock(directory, LOCK_EX)) system_failure("semaphore namespace lock");
    107 
    108         /* Hash only the filename; verify the full case-sensitive name before sharing. */
    109         uint64_t hash = 14695981039346656037ull;
    110         for (const unsigned char *p = (const unsigned char *)name; *p; ++p)
    111             hash = (hash ^ *p) * 1099511628211ull;
    112         snprintf(path, sizeof(path), "%016llx", (unsigned long long)hash);
    113         fd = openat(directory, path, O_RDWR | O_NOFOLLOW | O_CLOEXEC | (create ? O_CREAT : 0), 0600);
    114         if (fd < 0) {
    115             if (errno != ENOENT) system_failure("semaphore object open");
    116             close(directory);
    117             MEM32(g_fs_base + 0x34) = 2; /* ERROR_FILE_NOT_FOUND */
    118             return 0;
    119         }
    120         if (flock(fd, LOCK_EX | LOCK_NB)) {
    121             if (errno != EWOULDBLOCK) system_failure("semaphore lifetime lock");
    122             existing = 1;
    123         }
    124         if (!existing && !create) {
    125             close(fd);
    126             close(directory);
    127             MEM32(g_fs_base + 0x34) = 2;
    128             return 0;
    129         }
    130     }
    131 
    132     if (!existing && (maximum <= 0 || initial < 0 || initial > maximum)) {
    133         if (fd >= 0) close(fd);
    134         if (directory >= 0) close(directory);
    135         MEM32(g_fs_base + 0x34) = 87; /* ERROR_INVALID_PARAMETER */
    136         return 0;
    137     }
    138     if (fd >= 0) {
    139         if (!existing && ftruncate(fd, sizeof(*state))) system_failure("semaphore object resize");
    140         struct stat info;
    141         if (fstat(fd, &info)) system_failure("semaphore object stat");
    142         if (info.st_size != sizeof(*state)) {
    143             fprintf(stderr, "Incompatible live semaphore storage\n");
    144             abort();
    145         }
    146     }
    147     state = mmap(NULL, sizeof(*state), PROT_READ | PROT_WRITE,
    148                  MAP_SHARED | (fd < 0 ? MAP_ANONYMOUS : 0), fd, 0);
    149     if (state == MAP_FAILED) system_failure("semaphore mapping");
    150     if (!existing) {
    151         atomic_init(&state->count, initial);
    152         state->maximum = maximum;
    153         strcpy(state->name, name ? name : "");
    154     } else if (strcmp(state->name, name)) {
    155         fprintf(stderr, "Semaphore name hash collision\n");
    156         abort();
    157     }
    158     if (fd >= 0 && flock(fd, LOCK_SH)) system_failure("semaphore shared lifetime lock");
    159     if (directory >= 0) close(directory);
    160 #endif
    161 
    162     struct semaphore_handle *handle = malloc(sizeof(*handle));
    163     if (!handle) system_failure("semaphore handle allocation");
    164     handle->id = next_handle;
    165     next_handle += 4;
    166     handle->access = access;
    167     handle->fd = fd;
    168     handle->kind = 0;
    169     handle->abandoned = 0;
    170     handle->references = 1;
    171     handle->state = state;
    172     handle->next = handles;
    173     handles = handle;
    174     if (create) MEM32(g_fs_base + 0x34) = existing ? 183 : 0; /* ERROR_ALREADY_EXISTS */
    175     return handle->id;
    176 }
    177 
    178 static void create_semaphore_locked(void)
    179 {
    180     if (MEM32(g_esp + 4)) {
    181         fprintf(stderr, "Semaphore security attributes/inheritance are not implemented\n");
    182         abort();
    183     }
    184     uint32_t name = MEM32(g_esp + 16);
    185     g_eax = open_semaphore_locked(name ? (const char *)ADDR(name) : NULL, 1,
    186                            MEM32(g_esp + 8), MEM32(g_esp + 12), 0x1f0003);
    187     g_esp += 20;
    188 }
    189 
    190 static void open_named_semaphore_locked(void)
    191 {
    192     uint32_t access = MEM32(g_esp + 4);
    193     if (MEM32(g_esp + 8) || (access & ~0x1f0003u)) {
    194         fprintf(stderr, "Semaphore inheritance/generic access masks are not implemented\n");
    195         abort();
    196     }
    197     uint32_t name = MEM32(g_esp + 12);
    198     g_eax = 0;
    199     if (name) g_eax = open_semaphore_locked((const char *)ADDR(name), 0, 0, 0, access);
    200     else MEM32(g_fs_base + 0x34) = 87;
    201     g_esp += 16;
    202 }
    203 
    204 static void create_event_locked(void)
    205 {
    206     if (MEM32(g_esp + 4) || MEM32(g_esp + 16)) {
    207         fprintf(stderr, "Event security attributes and names are not implemented\n");
    208         abort();
    209     }
    210     g_eax = open_semaphore_locked(NULL, 1, MEM32(g_esp + 12) != 0, 1, 0x1f0003);
    211     handles->kind = MEM32(g_esp + 8) ? 3 : 1;
    212     g_esp += 20;
    213 }
    214 
    215 static void change_event_object_locked(struct semaphore_handle *handle, int signaled)
    216 {
    217     /* Auto-reset wakes one waiter; manual-reset stays signaled for all waiters. */
    218     atomic_store(&handle->state->count, signaled);
    219     if (signaled && pthread_cond_broadcast(&changed)) abort();
    220     if (signaled &&
    221 #ifdef __EMSCRIPTEN__
    222         emscripten_futex_wake(&handle->state->count, handle->kind == 3 ? INT_MAX : 1) < 0)
    223 #else
    224         syscall(SYS_futex, &handle->state->count, FUTEX_WAKE,
    225                 handle->kind == 3 ? INT_MAX : 1, NULL, NULL, 0) < 0)
    226 #endif
    227         system_failure("event wake");
    228 }
    229 
    230 static void change_event_locked(int signaled)
    231 {
    232     struct semaphore_handle *handle = find_handle_locked(MEM32(g_esp + 4));
    233     g_eax = 0;
    234     if (handle && handle->kind != 1 && handle->kind != 3) MEM32(g_fs_base + 0x34) = 6;
    235     else if (handle) {
    236         change_event_object_locked(handle, signaled);
    237         g_eax = 1;
    238     }
    239     g_esp += 8;
    240 }
    241 
    242 static void set_event_locked(void) { change_event_locked(1); }
    243 static void reset_event_locked(void) { change_event_locked(0); }
    244 
    245 static void create_mutex_locked(void)
    246 {
    247     if (MEM32(g_esp + 4) || MEM32(g_esp + 12)) {
    248         fprintf(stderr, "Mutex security attributes and names are not implemented\n");
    249         abort();
    250     }
    251     g_eax = open_semaphore_locked(NULL, 1, 0, 1, 0x1f0001);
    252     handles->kind = 2;
    253     pthread_mutexattr_t attributes;
    254     if (pthread_mutexattr_init(&attributes) ||
    255         pthread_mutexattr_settype(&attributes, PTHREAD_MUTEX_RECURSIVE) ||
    256 #ifndef __EMSCRIPTEN__
    257         pthread_mutexattr_setrobust(&attributes, PTHREAD_MUTEX_ROBUST) ||
    258 #endif
    259         pthread_mutex_init(&handles->ownership, &attributes) ||
    260         pthread_mutexattr_destroy(&attributes)) abort();
    261     if (MEM32(g_esp + 8) && pthread_mutex_lock(&handles->ownership)) abort();
    262     g_esp += 16;
    263 }
    264 
    265 static void release_mutex_locked(void)
    266 {
    267     struct semaphore_handle *handle = find_handle_locked(MEM32(g_esp + 4));
    268     g_eax = 0;
    269     if (handle && handle->kind != 2) MEM32(g_fs_base + 0x34) = 6;
    270     else if (handle) {
    271         int error = pthread_mutex_unlock(&handle->ownership);
    272         if (error == EPERM) MEM32(g_fs_base + 0x34) = 288; /* ERROR_NOT_OWNER */
    273         else if (error) abort();
    274         else {
    275             g_eax = 1;
    276             if (pthread_cond_broadcast(&changed)) abort();
    277         }
    278     }
    279     g_esp += 8;
    280 }
    281 
    282 static void release_handle_locked(struct semaphore_handle *handle)
    283 {
    284     if (--handle->references) return;
    285     if (handle->kind == 2 && pthread_mutex_destroy(&handle->ownership)) {
    286         fprintf(stderr, "Closing an owned mutex is not implemented\n");
    287         abort();
    288     }
    289 #ifdef __EMSCRIPTEN__
    290     if (!--handle->state->references) free(handle->state);
    291 #else
    292     if (munmap(handle->state, sizeof(*handle->state))) system_failure("semaphore unmap");
    293 #endif
    294     if (handle->fd >= 0 && close(handle->fd)) system_failure("semaphore handle close");
    295     free(handle);
    296 }
    297 
    298 /* Native I/O workers retain event objects without entering a guest CPU context. */
    299 struct semaphore_handle *linux_sync_event_acquire(uint32_t id)
    300 {
    301     if (pthread_mutex_lock(&mutex)) abort();
    302     struct semaphore_handle *event = find_handle_locked(id);
    303     if (event && event->kind != 1 && event->kind != 3) event = NULL;
    304     if (event) ++event->references;
    305     if (pthread_mutex_unlock(&mutex)) abort();
    306     return event;
    307 }
    308 
    309 void linux_sync_event_release(struct semaphore_handle *event)
    310 {
    311     if (pthread_mutex_lock(&mutex)) abort();
    312     release_handle_locked(event);
    313     if (pthread_mutex_unlock(&mutex)) abort();
    314 }
    315 
    316 void linux_sync_event_change(struct semaphore_handle *event, int signaled)
    317 {
    318     if (pthread_mutex_lock(&mutex)) abort();
    319     change_event_object_locked(event, signaled);
    320     if (pthread_mutex_unlock(&mutex)) abort();
    321 }
    322 
    323 /* Browser callbacks retry rather than blocking the UI on a worker. */
    324 int linux_sync_event_try_change(struct semaphore_handle *event, int signaled)
    325 {
    326     int error = pthread_mutex_trylock(&mutex);
    327     if (error == EBUSY) return 0;
    328     if (error) abort();
    329     change_event_object_locked(event, signaled);
    330     if (pthread_mutex_unlock(&mutex)) abort();
    331     return 1;
    332 }
    333 
    334 static void close_handle_locked(void)
    335 {
    336     struct semaphore_handle *handle = find_handle_locked(MEM32(g_esp + 4));
    337     g_eax = 0;
    338     if (handle) {
    339         struct semaphore_handle **link = &handles;
    340         while (*link != handle) link = &(*link)->next;
    341         *link = handle->next;
    342         release_handle_locked(handle);
    343         g_eax = 1;
    344     }
    345     g_esp += 8;
    346 }
    347 
    348 static void release_semaphore_locked(void)
    349 {
    350     struct semaphore_handle *handle = find_handle_locked(MEM32(g_esp + 4));
    351     int32_t increment = MEM32(g_esp + 8);
    352     g_eax = 0;
    353     if (handle && handle->kind != 0) MEM32(g_fs_base + 0x34) = 6;
    354     else if (handle && !(handle->access & 2)) MEM32(g_fs_base + 0x34) = 5;
    355     else if (handle && increment <= 0) MEM32(g_fs_base + 0x34) = 87;
    356     else if (handle) {
    357         uint32_t count = atomic_load(&handle->state->count);
    358         for (;;) {
    359             if ((uint32_t)increment > handle->state->maximum - count) {
    360                 MEM32(g_fs_base + 0x34) = 298; /* ERROR_TOO_MANY_POSTS */
    361                 break;
    362             }
    363             if (atomic_compare_exchange_weak(&handle->state->count, &count, count + increment)) {
    364                 uint32_t previous = MEM32(g_esp + 12);
    365                 if (previous) MEM32(previous) = count;
    366                 if (pthread_cond_broadcast(&changed)) abort();
    367 #ifdef __EMSCRIPTEN__
    368                 if (emscripten_futex_wake(&handle->state->count, increment) < 0)
    369 #else
    370                 if (syscall(SYS_futex, &handle->state->count, FUTEX_WAKE, increment, NULL, NULL, 0) < 0)
    371 #endif
    372                     system_failure("semaphore wake");
    373                 g_eax = 1;
    374                 break;
    375             }
    376         }
    377     }
    378     g_esp += 16;
    379 }
    380 
    381 static void wait_for_single_object_locked(void)
    382 {
    383     struct semaphore_handle *handle = find_handle_locked(MEM32(g_esp + 4));
    384     uint32_t milliseconds = MEM32(g_esp + 8);
    385     g_eax = UINT32_MAX; /* WAIT_FAILED */
    386     if (handle && !(handle->access & 0x100000)) MEM32(g_fs_base + 0x34) = 5;
    387     else if (handle) {
    388         ++handle->references;
    389         struct timespec deadline;
    390         if (milliseconds != UINT32_MAX) {
    391             if (clock_gettime(CLOCK_MONOTONIC, &deadline)) system_failure("semaphore clock");
    392             deadline.tv_sec += milliseconds / 1000;
    393             deadline.tv_nsec += (milliseconds % 1000) * 1000000;
    394             if (deadline.tv_nsec >= 1000000000) { ++deadline.tv_sec; deadline.tv_nsec -= 1000000000; }
    395         }
    396         if (handle->kind == 2) {
    397             /* Native robust mutexes preserve recursion and report owner death. */
    398             if (pthread_mutex_unlock(&mutex)) abort();
    399             int error = !milliseconds ? pthread_mutex_trylock(&handle->ownership) :
    400                 milliseconds == UINT32_MAX ? pthread_mutex_lock(&handle->ownership) :
    401 #ifdef __EMSCRIPTEN__
    402                 pthread_mutex_trylock(&handle->ownership);
    403             while (milliseconds && milliseconds != UINT32_MAX && error == EBUSY) {
    404                 struct timespec now;
    405                 clock_gettime(CLOCK_MONOTONIC, &now);
    406                 if (now.tv_sec > deadline.tv_sec ||
    407                     (now.tv_sec == deadline.tv_sec && now.tv_nsec >= deadline.tv_nsec)) break;
    408                 usleep(1000);
    409                 error = pthread_mutex_trylock(&handle->ownership);
    410             }
    411 #else
    412                 pthread_mutex_clocklock(&handle->ownership, CLOCK_MONOTONIC, &deadline);
    413 #endif
    414             if (error == EOWNERDEAD && pthread_mutex_consistent(&handle->ownership)) abort();
    415             if (pthread_mutex_lock(&mutex)) abort();
    416             if (!error || error == EOWNERDEAD) {
    417                 g_eax = error == EOWNERDEAD || handle->abandoned ? 128 : 0;
    418                 handle->abandoned = 0;
    419             }
    420             else if (error == ETIMEDOUT || error == EBUSY) g_eax = 258;
    421             else abort();
    422         } else for (;;) {
    423             uint32_t count = atomic_load(&handle->state->count);
    424             if (count) {
    425                 if (handle->kind == 3 ||
    426                     atomic_compare_exchange_weak(&handle->state->count, &count, count - 1)) {
    427                     g_eax = 0; /* WAIT_OBJECT_0 */
    428                     break;
    429                 }
    430                 continue;
    431             }
    432             if (!milliseconds) { g_eax = 258; break; }
    433             if (pthread_mutex_unlock(&mutex)) abort();
    434 #ifdef __EMSCRIPTEN__
    435             double timeout = INFINITY;
    436             if (milliseconds != UINT32_MAX) {
    437                 struct timespec now;
    438                 clock_gettime(CLOCK_MONOTONIC, &now);
    439                 timeout = (deadline.tv_sec - now.tv_sec) * 1000.0 + (deadline.tv_nsec - now.tv_nsec) / 1000000.0;
    440                 if (timeout < 0) timeout = 0;
    441             }
    442             int result = emscripten_futex_wait(&handle->state->count, 0, timeout);
    443             int error = -result;
    444 #else
    445             int result = syscall(SYS_futex, &handle->state->count, FUTEX_WAIT_BITSET, 0,
    446                                  milliseconds == UINT32_MAX ? NULL : &deadline, NULL, FUTEX_BITSET_MATCH_ANY);
    447             int error = errno;
    448 #endif
    449             if (pthread_mutex_lock(&mutex)) abort();
    450             if (!result || error == EAGAIN || error == EINTR) continue;
    451             if (error == ETIMEDOUT) { g_eax = 258; break; }
    452             errno = error;
    453             system_failure("semaphore wait");
    454         }
    455         release_handle_locked(handle);
    456     }
    457     g_esp += 12;
    458 }
    459 
    460 static uint32_t try_multiple_locked(struct semaphore_handle **objects, unsigned count, int all)
    461 {
    462     unsigned acquired = 0;
    463     for (; acquired < count; ++acquired) {
    464         struct semaphore_handle *object = objects[acquired];
    465         int ready;
    466         if (object->kind == 2) {
    467             int error = pthread_mutex_trylock(&object->ownership);
    468             if (error == EOWNERDEAD) {
    469                 if (pthread_mutex_consistent(&object->ownership)) abort();
    470                 object->abandoned = 1;
    471             } else if (error && error != EBUSY) abort();
    472             ready = error != EBUSY;
    473         } else ready = atomic_load(&object->state->count) != 0;
    474         if (!ready) {
    475             if (all) break;
    476             continue;
    477         }
    478         if (!all) {
    479             uint32_t result = acquired + (object->abandoned ? 128 : 0);
    480             object->abandoned = 0;
    481             if (object->kind == 0 || object->kind == 1) atomic_fetch_sub(&object->state->count, 1);
    482             return result;
    483         }
    484     }
    485 
    486     /* Commit all signal consumption together, or undo only tentative mutex locks. */
    487     if (all && acquired == count) {
    488         uint32_t result = 0;
    489         for (unsigned i = 0; i < count; ++i) {
    490             if (objects[i]->abandoned) result = 128;
    491             objects[i]->abandoned = 0;
    492             if (objects[i]->kind == 0 || objects[i]->kind == 1)
    493                 atomic_fetch_sub(&objects[i]->state->count, 1);
    494         }
    495         return result;
    496     }
    497     if (all) for (unsigned i = 0; i < acquired; ++i)
    498         if (objects[i]->kind == 2 && pthread_mutex_unlock(&objects[i]->ownership)) abort();
    499     return 258;
    500 }
    501 
    502 static void wait_for_multiple_objects_locked(void)
    503 {
    504     uint32_t count = MEM32(g_esp + 4), array = MEM32(g_esp + 8);
    505     uint32_t all = MEM32(g_esp + 12), milliseconds = MEM32(g_esp + 16);
    506     g_esp += 20;
    507     g_eax = UINT32_MAX;
    508     if (!count || count > 64) { MEM32(g_fs_base + 0x34) = 87; return; }
    509     struct semaphore_handle *objects[64];
    510     for (unsigned i = 0; i < count; ++i) {
    511         objects[i] = find_handle_locked(MEM32(array + 4*i));
    512         if (!objects[i]) return;
    513         if (!(objects[i]->access & 0x100000)) { MEM32(g_fs_base + 0x34) = 5; return; }
    514         if (objects[i]->fd >= 0) {
    515             fprintf(stderr, "Cross-process multiple-object waits are not implemented\n");
    516             abort();
    517         }
    518         if (all) for (unsigned j = 0; j < i; ++j)
    519             if (objects[i] == objects[j]) { MEM32(g_fs_base + 0x34) = 87; return; }
    520     }
    521 
    522     /* Retain handles while sleeping; the registry lock makes local waits atomic. */
    523     int has_mutex = 0;
    524     for (unsigned i = 0; i < count; ++i) {
    525         ++objects[i]->references;
    526         has_mutex |= objects[i]->kind == 2;
    527     }
    528     struct timespec now, deadline;
    529     if (clock_gettime(CLOCK_MONOTONIC, &now)) abort();
    530     deadline.tv_sec = now.tv_sec + milliseconds / 1000;
    531     deadline.tv_nsec = now.tv_nsec + (milliseconds % 1000) * 1000000;
    532     if (deadline.tv_nsec >= 1000000000) { ++deadline.tv_sec; deadline.tv_nsec -= 1000000000; }
    533     for (;;) {
    534         g_eax = try_multiple_locked(objects, count, all);
    535         if (g_eax != 258 || !milliseconds) break;
    536         if (clock_gettime(CLOCK_MONOTONIC, &now)) abort();
    537         if (milliseconds != UINT32_MAX && (now.tv_sec > deadline.tv_sec ||
    538             (now.tv_sec == deadline.tv_sec && now.tv_nsec >= deadline.tv_nsec))) break;
    539         struct timespec wake = deadline;
    540         if (has_mutex) {
    541             /* Owner death has no condition notification; recheck robust mutexes. */
    542             now.tv_nsec += 10000000;
    543             if (now.tv_nsec >= 1000000000) { ++now.tv_sec; now.tv_nsec -= 1000000000; }
    544             if (milliseconds == UINT32_MAX || now.tv_sec < wake.tv_sec ||
    545                 (now.tv_sec == wake.tv_sec && now.tv_nsec < wake.tv_nsec)) wake = now;
    546         }
    547 #ifdef __EMSCRIPTEN__
    548         /* The browser libc exposes timed waits on the condition's realtime clock. */
    549         struct timespec realtime;
    550         clock_gettime(CLOCK_MONOTONIC, &now);
    551         clock_gettime(CLOCK_REALTIME, &realtime);
    552         realtime.tv_sec += wake.tv_sec - now.tv_sec;
    553         realtime.tv_nsec += wake.tv_nsec - now.tv_nsec;
    554         if (realtime.tv_nsec < 0) { --realtime.tv_sec; realtime.tv_nsec += 1000000000; }
    555         if (realtime.tv_nsec >= 1000000000) { ++realtime.tv_sec; realtime.tv_nsec -= 1000000000; }
    556         int error = !has_mutex && milliseconds == UINT32_MAX ? pthread_cond_wait(&changed, &mutex) :
    557             pthread_cond_timedwait(&changed, &mutex, &realtime);
    558 #else
    559         int error = !has_mutex && milliseconds == UINT32_MAX ? pthread_cond_wait(&changed, &mutex) :
    560             pthread_cond_clockwait(&changed, &mutex, CLOCK_MONOTONIC, &wake);
    561 #endif
    562         if (error && error != ETIMEDOUT) abort();
    563     }
    564     for (unsigned i = 0; i < count; ++i) release_handle_locked(objects[i]);
    565 }
    566 
    567 static void call_serialized(recomp_func_t function)
    568 {
    569     if (pthread_mutex_lock(&mutex)) abort();
    570     function();
    571     if (pthread_mutex_unlock(&mutex)) abort();
    572 }
    573 
    574 static void create_semaphore(void) { call_serialized(create_semaphore_locked); }
    575 static void open_named_semaphore(void) { call_serialized(open_named_semaphore_locked); }
    576 static void close_handle(void) { call_serialized(close_handle_locked); }
    577 static void release_semaphore(void) { call_serialized(release_semaphore_locked); }
    578 static void wait_for_single_object(void) { call_serialized(wait_for_single_object_locked); }
    579 static void create_event(void) { call_serialized(create_event_locked); }
    580 static void set_event(void) { call_serialized(set_event_locked); }
    581 static void reset_event(void) { call_serialized(reset_event_locked); }
    582 static void create_mutex(void) { call_serialized(create_mutex_locked); }
    583 static void release_mutex(void) { call_serialized(release_mutex_locked); }
    584 static void wait_for_multiple_objects(void) { call_serialized(wait_for_multiple_objects_locked); }
    585 
    586 static void wait_for_multiple_objects_ex(void)
    587 {
    588     if (MEM32(g_esp + 20)) {
    589         fprintf(stderr, "Alertable multiple-object waits are not implemented\n");
    590         abort();
    591     }
    592     wait_for_multiple_objects();
    593     g_esp += 4;
    594 }
    595 
    596 recomp_func_t linux_sync_lookup(const char *name)
    597 {
    598     static const struct { const char *name; recomp_func_t function; } functions[] = {
    599         {"CreateSemaphoreA", create_semaphore}, {"OpenSemaphoreA", open_named_semaphore},
    600         {"CloseHandle", close_handle}, {"ReleaseSemaphore", release_semaphore},
    601         {"WaitForSingleObject", wait_for_single_object},
    602         {"CreateEventA", create_event}, {"SetEvent", set_event}, {"ResetEvent", reset_event},
    603         {"CreateMutexA", create_mutex}, {"ReleaseMutex", release_mutex},
    604         {"WaitForMultipleObjects", wait_for_multiple_objects},
    605         {"WaitForMultipleObjectsEx", wait_for_multiple_objects_ex},
    606     };
    607     for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i)
    608         if (!strcmp(name, functions[i].name)) return functions[i].function;
    609     return NULL;
    610 }