1#include <zircon/syscalls.h>
2#include <runtime/mutex.h>
3#include <threads.h>
4
5#include "time_conversion.h"
6
7int mtx_timedlock(mtx_t* restrict m, const struct timespec* restrict ts) {
8    zx_time_t deadline = ZX_TIME_INFINITE;
9    int ret = __timespec_to_deadline(ts, CLOCK_REALTIME, &deadline);
10    if (ret)
11        return ret == ETIMEDOUT ? thrd_timedout : thrd_error;
12
13    zx_status_t status = __zxr_mutex_timedlock((zxr_mutex_t*)&m->__i, deadline);
14    switch (status) {
15    default:
16        return thrd_error;
17    case 0:
18        return thrd_success;
19    case ZX_ERR_TIMED_OUT:
20        return thrd_timedout;
21    }
22}
23