int32 runtime·semasleep(int64 ns) { int32 ret; int32 ms; if(ns >= 0) { // TODO: Plan 9 needs a new system call, tsemacquire. // The kernel implementation is the same as semacquire // except with a tsleep and check for timeout. // It would be great if the implementation returned the // value that was added to the semaphore, so that on // timeout the return value would be 0, on success 1. // Then the error string does not have to be parsed // to detect timeout. // // If a negative time indicates no timeout, then // semacquire can be implemented (in the kernel) // as tsemacquire(p, v, -1). runtime·throw("semasleep: timed sleep not implemented on Plan 9"); /* if(ns < 0) ms = -1; else if(ns/1000 > 0x7fffffffll) ms = 0x7fffffff; else ms = ns/1000; ret = runtime·plan9_tsemacquire(&m->waitsemacount, 1, ms); if(ret == 1) return 0; // success return -1; // timeout or interrupted */ } while(runtime·plan9_semacquire(&m->waitsemacount, 1) < 0) { /* interrupted; try again */ } return 0; // success }
int32 runtime·semasleep(int64 ns) { int32 ret; int32 ms; if(ns >= 0) { if(ns/1000000 > 0x7fffffffll) ms = 0x7fffffff; else ms = ns/1000000; ret = runtime·plan9_tsemacquire(&m->waitsemacount, ms); if(ret == 1) return 0; // success return -1; // timeout or interrupted } while(runtime·plan9_semacquire(&m->waitsemacount, 1) < 0) { /* interrupted; try again (c.f. lock_sema.c) */ } return 0; // success }