Skip to content

Commit 490dbba

Browse files
committed
fix tests, interfaces
1 parent ed42d9a commit 490dbba

4 files changed

Lines changed: 21 additions & 16 deletions

File tree

jsrc/ct.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,12 +751,12 @@ ASSERT(0,EVNONCE)
751751
C c=jtpthread_mutex_lock(jt,(jtpthread_mutex_t*)IAV0(mutex),1+THREADID(jt));ASSERT(!c,c); //1+ is to ensure nonzero id. TODO id should be unique per-task, not just per-thread
752752
}else if(timeout==0.0){
753753
I lockrc=jtpthread_mutex_trylock((jtpthread_mutex_t*)IAV0(mutex),1+THREADID(jt));
754-
lockfail=lockrc==-1; // -1 is a soft failure
755754
ASSERT(lockrc<=0,lockrc); // positive is a hard failure
755+
lockfail=lockrc==-1; // -1 is a soft failure
756756
}else{
757757
I lockrc=jtpthread_mutex_timedlock(jt,(jtpthread_mutex_t*)IAV0(mutex),1e9*timeout,1+THREADID(jt));
758-
lockfail=lockrc==-1; // -1 is a soft failure
759758
ASSERT(lockrc<=0,lockrc); // positive is a hard failure
759+
lockfail=lockrc==-1; // -1 is a soft failure
760760
}
761761
z=num(lockfail);
762762
#else

jsrc/mt.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ I jtpthread_mutex_timedlock(J jt,jtpthread_mutex_t *m,UI ns,I self){
6262
if(now.tv_nsec<=tgt.tv_nsec)ns+=tgt.tv_nsec-now.tv_nsec;
6363
else ns+=1000000000-(now.tv_nsec-tgt.tv_nsec);}
6464
success:m->ct+=m->recursive;m->owner=self; R 0;}
65-
B jtpthread_mutex_trylock(jtpthread_mutex_t *m,I self){
66-
if(uncommon(m->recursive)){if(m->owner!=self)R 0; m->ct++;R 1;}
67-
if(m->owner==self)R 0;
68-
if(casa(&m->v,&(UI4){FREE},LOCK)){m->owner=self;R 1;} R 0;}
65+
I jtpthread_mutex_trylock(jtpthread_mutex_t *m,I self){
66+
if(uncommon(m->recursive)&&m->owner){if(m->owner!=self)R -1; m->ct++;R 0;}
67+
if(m->owner==self)R EVCONCURRENCY;
68+
if(casa(&m->v,&(UI4){FREE},LOCK)){m->ct+=m->recursive;m->owner=self;R 0;} R -1;}
6969
C jtpthread_mutex_unlock(jtpthread_mutex_t *m,I self){
7070
if(unlikely(m->owner!=self))R EVCONCURRENCY;
7171
if(uncommon(m->recursive)){if(--m->ct)R 0;} //need to be released more times on this thread

jsrc/mt.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ static inline I jtpthread_mutex_timedlock(J jt,jtpthread_mutex_t *m,UI ns,I self
2323
if(r==ETIMEDOUT)R -1;
2424
if(r==EDEADLK)R EVCONCURRENCY;
2525
R EVFACE;}
26-
static inline B jtpthread_mutex_trylock(jtpthread_mutex_t *m,I self){R !pthread_mutex_trylock(m);}
27-
static inline B jtpthread_mutex_unlock(jtpthread_mutex_t *m,I self){
26+
static inline I jtpthread_mutex_trylock(jtpthread_mutex_t *m,I self){
27+
I4 r=pthread_mutex_timedlock(m);
28+
if(!r)R 0;
29+
if(r==ETIMEDOUT)R -1;
30+
if(r==EDEADLK||r==EOWNERDEAD)R EVCONCURRENCY;
31+
R EVFACE;}
32+
static inline C jtpthread_mutex_unlock(jtpthread_mutex_t *m,I self){
2833
I4 r=pthread_mutex_unlock(m);
2934
if(likely(!r))R 0;
3035
if(r==EPERM)R EVCONCURRENCY;
@@ -71,8 +76,8 @@ typedef struct {
7176

7277
void jtpthread_mutex_init(jtpthread_mutex_t*,B recursive);
7378
struct JTTstruct; C jtpthread_mutex_lock(struct JTTstruct *jt,jtpthread_mutex_t *m,I self);
74-
I jtpthread_mutex_timedlock(struct JTTstruct *jt,jtpthread_mutex_t*,UI ns,I self); //absolute timers suck; correct the interface. Negative result means failure; 0 is success; positive is error code
75-
B jtpthread_mutex_trylock(jtpthread_mutex_t*,I self); //0=failure 1=success, opposite posix
79+
I jtpthread_mutex_timedlock(struct JTTstruct *jt,jtpthread_mutex_t*,UI ns,I self); //absolute timers suck; correct the interface. -1=failure; 0=success; positive=error
80+
I jtpthread_mutex_trylock(jtpthread_mutex_t*,I self); //0=success -1=failure positive=error
7681
C jtpthread_mutex_unlock(jtpthread_mutex_t*,I self); //0 or error code
7782

7883
//note: self must be non-zero

test/gtdot.ijs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,23 +282,23 @@ assert. remote_stuff=2
282282
assert. remote_stuff=4
283283
NB. mutex
284284
mtx =. 10 T. 0
285-
0 = 11 T. mtx;0
285+
0 = >{{11 T. mtx;0}}t.''0
286286
1 = 11 T. mtx;0
287-
13 T. mtx
287+
mtx =. 10 T. 0
288288
0 = 11 T. mtx
289289
'domain error' -: 11 T. etx >: mtx
290290
13 T. mtx
291291
mtx =. 10 T. 0
292292
tod =. 6!:1''
293-
0 = 11 T. <mtx NB. boxed mtx OK
293+
0 = >{{11 T. <mtx}}t.''0 NB. boxed mtx OK
294294
1 = 11 T. mtx;2.0
295-
(2.005 > dly) *. 2 < dly =. tod-~6!:1'' NB. verify delay
295+
(2.005 > dly) *. 2 <: dly =. tod-~6!:1'' NB. verify delay
296296
tod =. 6!:1''
297297
1 = 11 T. mtx;0.1
298-
(0.105 > dly) *. 0.1 < dly =. tod-~6!:1'' NB. verify delay
298+
(0.105 > dly) *. 0.1 <: dly =. tod-~6!:1'' NB. verify delay
299299
tod =. 6!:1''
300300
1 = 11 T. mtx;0
301-
(0.005 > dly) *. 0 < dly =. tod-~6!:1'' NB. verify no delay
301+
(0.005 > dly) *. 0 <: dly =. tod-~6!:1'' NB. verify no delay
302302
13 T. mtx
303303
mtx=.10 T. 1 NB. recursive
304304
0 = 11 T. mtx

0 commit comments

Comments
 (0)