X-Git-Url: https://pd.if.org/git/?p=pdclib.old;a=blobdiff_plain;f=opt%2Fpthreads%2Fmtx_timedlock.c;fp=opt%2Fpthreads%2Fmtx_timedlock.c;h=ed490a874cf836346d24ecb29badd5896201d5b8;hp=0000000000000000000000000000000000000000;hb=d293fb98fe80a472ad4d11b4d8a1413d2142064a;hpb=42ca148113de4800d41cbd0491c508160bb1cf36 diff --git a/opt/pthreads/mtx_timedlock.c b/opt/pthreads/mtx_timedlock.c new file mode 100644 index 0000000..ed490a8 --- /dev/null +++ b/opt/pthreads/mtx_timedlock.c @@ -0,0 +1,38 @@ +#ifndef REGTEST +#include +// On Mac OS X, supress system definition of struct timespec +#ifdef __APPLE__ + #define _STRUCT_TIMESPEC struct timespec +#endif +#include +#include + +/* Can only implement if timeouts are supported. + * + * Namely, Mac OS X does not implement timeouts + */ +#if defined(_POSIX_TIMEOUTS) && (_POSIX_TIMEOUTS - 200112L) >= 0L +int mtx_timedlock(mtx_t *restrict mtx, const struct timespec *restrict ts) +{ + switch(pthread_mutex_timedlock(mtx, ts)) { + case 0: + return thrd_success; + case ETIMEDOUT: + return thrd_timeout; + default: + return thrd_error; + } +} +#endif + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + return TEST_RESULTS; +} + +#endif