# HG changeset patch # User Erik Quanstrom # Date 1329518202 0 # Node ID d5c3a985ece44b36bd0bb86aeae6c0400e1666f4 # Parent cac66f146601bb39dfe9ec8f14f16cb02375f3a1 tsemacquire: uncoherent access to ticks cavet: i don't have any code for testing this. i noticed it by accident. i see two problems with the original code, 1. sleep can pop out on a different processor than it entered on, so i believe sys->ticks is correct. otherwise we require that ∀i, j in machnos, m(i)->ticks ≡ m(j)->ticks. 2. X->ticks - t is comuted twice, but it is not guarenteed to produce the same value each time. R=nixiedev, john, seed CC=nix-dev http://codereview.appspot.com/5677051 Committer: John Floren diff -r cac66f146601 -r d5c3a985ece4 sys/src/nix/port/sysproc.c --- a/sys/src/nix/port/sysproc.c Sat Feb 18 13:49:14 2012 +0000 +++ b/sys/src/nix/port/sysproc.c Fri Feb 17 22:36:42 2012 +0000 @@ -1182,12 +1182,11 @@ /* Acquire semaphore or time-out */ static int -tsemacquire(Segment* s, int* addr, ulong ms) +tsemacquire(Segment* s, int* addr, long ms) { int acquired; + ulong t; Sema phore; - int timedout; - ulong t; if(canacquire(addr)) return 1; @@ -1195,7 +1194,6 @@ return 0; acquired = 0; - timedout = 0; semqueue(s, addr, &phore); for(;;){ phore.waiting = 1; @@ -1206,21 +1204,18 @@ } if(waserror()) break; - t = m->ticks; + t = sys->ticks; tsleep(&phore, semawoke, &phore, ms); - if(TK2MS(m->ticks-t) >= ms) { - timedout = 1; - poperror(); + ms -= TK2MS(sys->ticks-t); + poperror(); + if(ms <= 0) break; - } - ms -= TK2MS(m->ticks-t); - poperror(); } semdequeue(s, &phore); coherence(); /* not strictly necessary due to lock in semdequeue */ if(!phore.waiting) semwakeup(s, addr, 1); - if(timedout) + if(ms <= 0) return 0; if(!acquired) nexterror();