Skip to content

Commit

Permalink
windows
Browse files Browse the repository at this point in the history
  • Loading branch information
moon-chilled committed Jun 3, 2022
1 parent 4e9a0aa commit c6377af
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 55 deletions.
14 changes: 12 additions & 2 deletions jsrc/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdint.h> // portable: uint64_t MSVC: __int64
#include "jerr.h"

struct jtimespec { long long tv_sec, tv_nsec; };
struct jtimeval { long long tv_sec, tv_usec; };
Expand Down Expand Up @@ -36,8 +37,17 @@ int jgettimeofday(struct jtimeval *tp, struct jtimezone * tzp)
//monotonic clock
//alternative is QueryPerformanceCounter; it probably uses rdtsc, which is stable on recent processors, but it gives inconsistent results when the processor goes to sleep
struct jtimespec jmtclk(){
UI t=GetTickCount64();
R(struct jtimeval){.tv_sec=t/1000,.tv_nsec=1000000*(t%1000)};}
long long t=GetTickCount64();
return (struct jtimespec){.tv_sec=t/1000,.tv_nsec=1000000*(t%1000)};}

void jfutex_wake1(unsigned *p){WakeByAddressSingle(p);}
void jfutex_wakea(unsigned *p){WakeByAddressAll(p);}
unsigned char jfutex_wait(unsigned *p,unsigned v){return WaitOnAddress(p,&v,4,INFINITE)?0:EVFACE;}
long long jfutex_waitn(unsigned *p,unsigned v,unsigned long long ns){
if(WaitOnAddress(p,&v,4,ns/1000000))return 0;
if(GetLastError()==ERROR_TIMEOUT)return -1;
//is there EINTR on windows? Does it manifest as a spurious wake with no error?
return EVFACE;}
#else
#include"j.h"
struct jtimespec jmtclk(){struct timespec r; clock_gettime(CLOCK_MONOTONIC,&r);R r;}
Expand Down
11 changes: 1 addition & 10 deletions jsrc/mt.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,7 @@ I jfutex_waitn(UI4 *p,UI4 v,UI ns){
if(r==-EAGAIN||r==-EINTR)R 0;
R EVFACE;}
#elif defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
void jfutex_wake1(UI4 *p){WakeByAddressSingle(p);}
void jfutex_wakea(UI4 *p){WakeByAddressAll(p);}
C jfutex_wait(UI4 *p,UI4 v){R WaitOnAddress(p,&v,4,INFINITE)?0:EVFACE;}
I jfutex_waitn(UI4 *p,UI4 v,UI ns){
if(WaitOnAddress(p,&v,4,ns/1000000))R 0;
if(GetLastError()==ERROR_TIMEOUT)R -1;
//is there EINTR on windows? Does it manifest as a spurious wake with no error?
R EVFACE;}
// defined in cd.c to avoid name collisions between j.h and windows.h
#endif

#if defined(__APPLE__) || defined(__linux__)
Expand Down
43 changes: 0 additions & 43 deletions jsrc/mt.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,6 @@ __attribute__((cold)) I jfutex_waitn(UI4 *p,UI4 v,UI ns); //ditto, but wake up a
__attribute__((cold)) void jfutex_wake1(UI4 *p); //wake 1 thread waiting on p
__attribute__((cold)) void jfutex_wakea(UI4 *p); //wake all threads waiting on p

#if !defined(__APPLE__) && !defined(__linux__)
#include <pthread.h>
typedef pthread_mutex_t jtpthread_mutex_t;
static inline void jtpthread_mutex_init(jtpthread_mutex_t *m,B recursive){
if(likely(!recursive)){pthread_mutex_init(m,0);}
else{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(m,&attr);}}
static inline C jtpthread_mutex_lock(J jt,jtpthread_mutex_t *m,I self){
I4 r=pthread_mutex_lock(m);
if(likely(r==0))R 0;
if(r==EDEADLK)R EVCONCURRENCY;
R EVFACE;}
static inline I jtpthread_mutex_timedlock(J jt,jtpthread_mutex_t *m,UI ns,I self){
#if SY_WIN32
struct jtimeval now;jgettimeofday(&now,0);
struct timespec t;
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;}
#else
struct timespec t;clock_gettime(CLOCK_REALTIME,&t);
t.tv_sec+=ns/1000000000;t.tv_nsec+=ns%1000000000;if(t.tv_nsec>=1000000000){t.tv_sec++;t.tv_nsec-=1000000000;}
#endif
I4 r=pthread_mutex_timedlock(m,&t);
if(r==0)R 0;
if(r==ETIMEDOUT)R -1;
if(r==EDEADLK)R EVCONCURRENCY;
R EVFACE;}
static inline I jtpthread_mutex_trylock(jtpthread_mutex_t *m,I self){
I4 r=pthread_mutex_trylock(m);
if(!r)R 0;
if(r==EBUSY)R -1;
if(r==EAGAIN)R EVLIMIT; //'max recursive locks exceeded'
if(r==EDEADLK||r==EOWNERDEAD)R EVCONCURRENCY;
R EVFACE;}
static inline C jtpthread_mutex_unlock(jtpthread_mutex_t *m,I self){
I4 r=pthread_mutex_unlock(m);
if(likely(!r))R 0;
if(r==EPERM)R EVCONCURRENCY;
R EVFACE;}
#else
typedef struct {
B recursive;
I owner; //user-provided; task id
Expand Down Expand Up @@ -107,5 +65,4 @@ extern int __ulock_wake(uint32_t operation, void *addr, uint64_t wake_value);
// untested windows path; make henry test it when he gets back from vacation
// don't pollute everybody with windows.h. win api is fairly basic anyway, so there is not much to take advantage of
#endif //_WIN32
#endif //__APPLE__ || __linux__
#endif //PYXES
1 change: 1 addition & 0 deletions makemsvc/jdll/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ OBJS= \
../../jsrc/xu.o \
../../jsrc/crc32c.o \
../../jsrc/str.o \
../../jsrc/mt.o \
../../jsrc/openssl/sha/keccak1600.o \
../../jsrc/openssl/sha/md4_dgst.o \
../../jsrc/openssl/sha/md4_one.o \
Expand Down
1 change: 1 addition & 0 deletions makemsvc/jdll/makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ OBJS= \
../../jsrc/xu.o \
../../jsrc/crc32c.o \
../../jsrc/str.o \
../../jsrc/mt.o \
../../jsrc/openssl/sha/keccak1600.o \
../../jsrc/openssl/sha/md4_dgst.o \
../../jsrc/openssl/sha/md4_one.o \
Expand Down
1 change: 1 addition & 0 deletions makevs/jdll/template/jdll_vs2013.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@
<ClCompile Include="..\..\jsrc\xu.c" />
<ClCompile Include="..\..\jsrc\crc32c.c" />
<ClCompile Include="..\..\jsrc\str.c" />
<ClCompile Include="..\..\jsrc\mt.c" />
<ClCompile Include="..\..\jsrc\openssl\sha\keccak1600.c" />
<ClCompile Include="..\..\jsrc\openssl\sha\md4_dgst.c" />
<ClCompile Include="..\..\jsrc\openssl\sha\md4_one.c" />
Expand Down
1 change: 1 addition & 0 deletions makevs/jdll/template/jdll_vs2019.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@
<ClCompile Include="..\..\jsrc\xu.c" />
<ClCompile Include="..\..\jsrc\crc32c.c" />
<ClCompile Include="..\..\jsrc\str.c" />
<ClCompile Include="..\..\jsrc\mt.c" />
<ClCompile Include="..\..\jsrc\openssl\sha\keccak1600.c" />
<ClCompile Include="..\..\jsrc\openssl\sha\md4_dgst.c" />
<ClCompile Include="..\..\jsrc\openssl\sha\md4_one.c" />
Expand Down
1 change: 1 addition & 0 deletions makevs/jdll/template/jdll_vs2019_clang.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,7 @@ copy $(outdir)$(targetname).dll $(userprofile)\jbld\j64\bin\javx2.dll</Command>
<ClCompile Include="..\..\jsrc\xu.c" />
<ClCompile Include="..\..\jsrc\crc32c.c" />
<ClCompile Include="..\..\jsrc\str.c" />
<ClCompile Include="..\..\jsrc\mt.c" />
<ClCompile Include="..\..\jsrc\openssl\sha\keccak1600.c" />
<ClCompile Include="..\..\jsrc\openssl\sha\md4_dgst.c" />
<ClCompile Include="..\..\jsrc\openssl\sha\md4_one.c" />
Expand Down

0 comments on commit c6377af

Please sign in to comment.