44#if PYXES
55#ifndef __APPLE__
66#include <pthread.h>
7+ #if SY_WIN32
8+ struct timezone {
9+ int tz_minuteswest ;
10+ int tz_dsttime ;
11+ };
12+
13+ // Tip o'hat to Michaelangel007 on StackOverflow
14+ // MSVC defines this in winsock2.h!?
15+ typedef struct timeval {
16+ long tv_sec ;
17+ long tv_usec ;
18+ } timeval ;
19+ extern int gettimeofday (struct timeval * tp , struct timezone * tzp );
20+ #endif
721typedef pthread_mutex_t jtpthread_mutex_t ;
822static inline void jtpthread_mutex_init (jtpthread_mutex_t * m ,B recursive ){
923 if (likely (!recursive )){pthread_mutex_init (m ,0 );}
@@ -18,15 +32,21 @@ static inline C jtpthread_mutex_lock(J jt,jtpthread_mutex_t *m,I self){
1832 if (r == EDEADLK )R EVCONCURRENCY ;
1933 R EVFACE ;}
2034static inline I jtpthread_mutex_timedlock (J jt ,jtpthread_mutex_t * m ,UI ns ,I self ){
35+ #if _WIN32
36+ struct timeval now ;gettimeofday (& now ,0 );
37+ struct timespec t ;
38+ t .tv_sec = now .tv_sec + ns /1000000000 ;t .tv_nsec = 1000 * now .tv_usec + ns %1000000000 ;if (t .tv_nsec >=1000000000 ){t .tv_sec ++ ;t .tv_nsec -= 1000000000 ;}
39+ #else
2140 struct timespec t ;clock_gettime (CLOCK_REALTIME ,& t );
2241 t .tv_sec += ns /1000000000 ;t .tv_nsec += ns %1000000000 ;if (t .tv_nsec >=1000000000 ){t .tv_sec ++ ;t .tv_nsec -= 1000000000 ;}
42+ #endif
2343 I4 r = pthread_mutex_timedlock (m ,& t );
2444 if (r == 0 )R 0 ;
2545 if (r == ETIMEDOUT )R - 1 ;
2646 if (r == EDEADLK )R EVCONCURRENCY ;
2747 R EVFACE ;}
2848static inline I jtpthread_mutex_trylock (jtpthread_mutex_t * m ,I self ){
29- I4 r = pthread_mutex_timedlock (m );
49+ I4 r = pthread_mutex_trylock (m );
3050 if (!r )R 0 ;
3151 if (r == ETIMEDOUT )R - 1 ;
3252 if (r == EDEADLK || r == EOWNERDEAD )R EVCONCURRENCY ;
0 commit comments