2011-11-22 21:24:44 +01:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// +build darwin nacl netbsd openbsd plan9 solaris windows
|
2012-01-13 06:11:45 +01:00
|
|
|
|
2011-11-22 21:24:44 +01:00
|
|
|
#include "runtime.h"
|
|
|
|
|
|
|
|
// This implementation depends on OS-specific implementations of
|
|
|
|
//
|
2012-01-13 06:11:45 +01:00
|
|
|
// uintptr runtime_semacreate(void)
|
2011-11-22 21:24:44 +01:00
|
|
|
// Create a semaphore, which will be assigned to m->waitsema.
|
|
|
|
// The zero value is treated as absence of any semaphore,
|
|
|
|
// so be sure to return a non-zero value.
|
|
|
|
//
|
2012-01-13 06:11:45 +01:00
|
|
|
// int32 runtime_semasleep(int64 ns)
|
2011-11-22 21:24:44 +01:00
|
|
|
// If ns < 0, acquire m->waitsema and return 0.
|
|
|
|
// If ns >= 0, try to acquire m->waitsema for at most ns nanoseconds.
|
|
|
|
// Return 0 if the semaphore was acquired, -1 if interrupted or timed out.
|
|
|
|
//
|
2012-01-13 06:11:45 +01:00
|
|
|
// int32 runtime_semawakeup(M *mp)
|
2011-11-22 21:24:44 +01:00
|
|
|
// Wake up mp, which is or will soon be sleeping on mp->waitsema.
|
|
|
|
//
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
LOCKED = 1,
|
|
|
|
|
|
|
|
ACTIVE_SPIN = 4,
|
|
|
|
ACTIVE_SPIN_CNT = 30,
|
|
|
|
PASSIVE_SPIN = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_lock(Lock *l)
|
|
|
|
{
|
2011-11-29 22:58:48 +01:00
|
|
|
M *m;
|
2011-11-22 21:24:44 +01:00
|
|
|
uintptr v;
|
|
|
|
uint32 i, spin;
|
|
|
|
|
2011-11-29 22:58:48 +01:00
|
|
|
m = runtime_m();
|
2011-11-22 21:24:44 +01:00
|
|
|
if(m->locks++ < 0)
|
|
|
|
runtime_throw("runtime_lock: lock count");
|
|
|
|
|
|
|
|
// Speculative grab for lock.
|
2013-07-16 08:54:42 +02:00
|
|
|
if(runtime_casp((void**)&l->key, nil, (void*)LOCKED))
|
2011-11-22 21:24:44 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(m->waitsema == 0)
|
|
|
|
m->waitsema = runtime_semacreate();
|
|
|
|
|
|
|
|
// On uniprocessor's, no point spinning.
|
|
|
|
// On multiprocessors, spin for ACTIVE_SPIN attempts.
|
|
|
|
spin = 0;
|
|
|
|
if(runtime_ncpu > 1)
|
|
|
|
spin = ACTIVE_SPIN;
|
|
|
|
|
|
|
|
for(i=0;; i++) {
|
2013-07-16 08:54:42 +02:00
|
|
|
v = (uintptr)runtime_atomicloadp((void**)&l->key);
|
2011-11-22 21:24:44 +01:00
|
|
|
if((v&LOCKED) == 0) {
|
|
|
|
unlocked:
|
2013-07-16 08:54:42 +02:00
|
|
|
if(runtime_casp((void**)&l->key, (void*)v, (void*)(v|LOCKED)))
|
2011-11-22 21:24:44 +01:00
|
|
|
return;
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
if(i<spin)
|
|
|
|
runtime_procyield(ACTIVE_SPIN_CNT);
|
|
|
|
else if(i<spin+PASSIVE_SPIN)
|
|
|
|
runtime_osyield();
|
|
|
|
else {
|
|
|
|
// Someone else has it.
|
|
|
|
// l->waitm points to a linked list of M's waiting
|
|
|
|
// for this lock, chained through m->nextwaitm.
|
|
|
|
// Queue this M.
|
|
|
|
for(;;) {
|
|
|
|
m->nextwaitm = (void*)(v&~LOCKED);
|
2013-07-16 08:54:42 +02:00
|
|
|
if(runtime_casp((void**)&l->key, (void*)v, (void*)((uintptr)m|LOCKED)))
|
2011-11-22 21:24:44 +01:00
|
|
|
break;
|
2013-07-16 08:54:42 +02:00
|
|
|
v = (uintptr)runtime_atomicloadp((void**)&l->key);
|
2011-11-22 21:24:44 +01:00
|
|
|
if((v&LOCKED) == 0)
|
|
|
|
goto unlocked;
|
|
|
|
}
|
|
|
|
if(v&LOCKED) {
|
|
|
|
// Queued. Wait.
|
|
|
|
runtime_semasleep(-1);
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_unlock(Lock *l)
|
|
|
|
{
|
|
|
|
uintptr v;
|
|
|
|
M *mp;
|
|
|
|
|
|
|
|
for(;;) {
|
2013-07-16 08:54:42 +02:00
|
|
|
v = (uintptr)runtime_atomicloadp((void**)&l->key);
|
2011-11-22 21:24:44 +01:00
|
|
|
if(v == LOCKED) {
|
2013-07-16 08:54:42 +02:00
|
|
|
if(runtime_casp((void**)&l->key, (void*)LOCKED, nil))
|
2011-11-22 21:24:44 +01:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Other M's are waiting for the lock.
|
|
|
|
// Dequeue an M.
|
|
|
|
mp = (void*)(v&~LOCKED);
|
2013-07-16 08:54:42 +02:00
|
|
|
if(runtime_casp((void**)&l->key, (void*)v, mp->nextwaitm)) {
|
2011-11-22 21:24:44 +01:00
|
|
|
// Dequeued an M. Wake it.
|
|
|
|
runtime_semawakeup(mp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
if(--runtime_m()->locks < 0)
|
|
|
|
runtime_throw("runtime_unlock: lock count");
|
2011-11-22 21:24:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// One-time notifications.
|
|
|
|
void
|
|
|
|
runtime_noteclear(Note *n)
|
|
|
|
{
|
2013-07-16 08:54:42 +02:00
|
|
|
n->key = 0;
|
2011-11-22 21:24:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_notewakeup(Note *n)
|
|
|
|
{
|
|
|
|
M *mp;
|
|
|
|
|
|
|
|
do
|
2013-07-16 08:54:42 +02:00
|
|
|
mp = runtime_atomicloadp((void**)&n->key);
|
|
|
|
while(!runtime_casp((void**)&n->key, mp, (void*)LOCKED));
|
2011-11-22 21:24:44 +01:00
|
|
|
|
|
|
|
// Successfully set waitm to LOCKED.
|
|
|
|
// What was it before?
|
|
|
|
if(mp == nil) {
|
|
|
|
// Nothing was waiting. Done.
|
|
|
|
} else if(mp == (M*)LOCKED) {
|
|
|
|
// Two notewakeups! Not allowed.
|
|
|
|
runtime_throw("notewakeup - double wakeup");
|
|
|
|
} else {
|
|
|
|
// Must be the waiting m. Wake it up.
|
|
|
|
runtime_semawakeup(mp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_notesleep(Note *n)
|
|
|
|
{
|
2011-11-29 22:58:48 +01:00
|
|
|
M *m;
|
|
|
|
|
|
|
|
m = runtime_m();
|
2013-11-06 20:49:01 +01:00
|
|
|
|
2013-12-06 19:26:27 +01:00
|
|
|
/* For gccgo it's OK to sleep in non-g0, and it happens in
|
|
|
|
stoptheworld because we have not implemented preemption.
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
if(runtime_g() != m->g0)
|
|
|
|
runtime_throw("notesleep not on g0");
|
2013-12-06 19:26:27 +01:00
|
|
|
*/
|
2013-11-06 20:49:01 +01:00
|
|
|
|
2011-11-22 21:24:44 +01:00
|
|
|
if(m->waitsema == 0)
|
|
|
|
m->waitsema = runtime_semacreate();
|
2013-07-16 08:54:42 +02:00
|
|
|
if(!runtime_casp((void**)&n->key, nil, m)) { // must be LOCKED (got wakeup)
|
|
|
|
if(n->key != LOCKED)
|
2011-11-22 21:24:44 +01:00
|
|
|
runtime_throw("notesleep - waitm out of sync");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Queued. Sleep.
|
2014-06-07 00:37:27 +02:00
|
|
|
m->blocked = true;
|
2011-11-22 21:24:44 +01:00
|
|
|
runtime_semasleep(-1);
|
2014-06-07 00:37:27 +02:00
|
|
|
m->blocked = false;
|
2011-11-22 21:24:44 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
static bool
|
|
|
|
notetsleep(Note *n, int64 ns, int64 deadline, M *mp)
|
2011-11-22 21:24:44 +01:00
|
|
|
{
|
2011-11-29 22:58:48 +01:00
|
|
|
M *m;
|
2011-11-22 21:24:44 +01:00
|
|
|
|
2011-11-29 22:58:48 +01:00
|
|
|
m = runtime_m();
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
// Conceptually, deadline and mp are local variables.
|
|
|
|
// They are passed as arguments so that the space for them
|
|
|
|
// does not count against our nosplit stack sequence.
|
2011-11-22 21:24:44 +01:00
|
|
|
|
|
|
|
// Register for wakeup on n->waitm.
|
2013-07-16 08:54:42 +02:00
|
|
|
if(!runtime_casp((void**)&n->key, nil, m)) { // must be LOCKED (got wakeup already)
|
|
|
|
if(n->key != LOCKED)
|
2011-11-22 21:24:44 +01:00
|
|
|
runtime_throw("notetsleep - waitm out of sync");
|
2013-11-06 20:49:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ns < 0) {
|
|
|
|
// Queued. Sleep.
|
2014-06-07 00:37:27 +02:00
|
|
|
m->blocked = true;
|
2013-11-06 20:49:01 +01:00
|
|
|
runtime_semasleep(-1);
|
2014-06-07 00:37:27 +02:00
|
|
|
m->blocked = false;
|
2013-11-06 20:49:01 +01:00
|
|
|
return true;
|
2011-11-22 21:24:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
deadline = runtime_nanotime() + ns;
|
|
|
|
for(;;) {
|
|
|
|
// Registered. Sleep.
|
2014-06-07 00:37:27 +02:00
|
|
|
m->blocked = true;
|
2011-11-22 21:24:44 +01:00
|
|
|
if(runtime_semasleep(ns) >= 0) {
|
2014-06-07 00:37:27 +02:00
|
|
|
m->blocked = false;
|
2011-11-22 21:24:44 +01:00
|
|
|
// Acquired semaphore, semawakeup unregistered us.
|
|
|
|
// Done.
|
2013-11-06 20:49:01 +01:00
|
|
|
return true;
|
2011-11-22 21:24:44 +01:00
|
|
|
}
|
2014-06-07 00:37:27 +02:00
|
|
|
m->blocked = false;
|
2011-11-22 21:24:44 +01:00
|
|
|
|
|
|
|
// Interrupted or timed out. Still registered. Semaphore not acquired.
|
2013-11-06 20:49:01 +01:00
|
|
|
ns = deadline - runtime_nanotime();
|
|
|
|
if(ns <= 0)
|
2011-11-22 21:24:44 +01:00
|
|
|
break;
|
|
|
|
// Deadline hasn't arrived. Keep sleeping.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deadline arrived. Still registered. Semaphore not acquired.
|
|
|
|
// Want to give up and return, but have to unregister first,
|
|
|
|
// so that any notewakeup racing with the return does not
|
|
|
|
// try to grant us the semaphore when we don't expect it.
|
|
|
|
for(;;) {
|
2013-07-16 08:54:42 +02:00
|
|
|
mp = runtime_atomicloadp((void**)&n->key);
|
2011-11-22 21:24:44 +01:00
|
|
|
if(mp == m) {
|
|
|
|
// No wakeup yet; unregister if possible.
|
2013-07-16 08:54:42 +02:00
|
|
|
if(runtime_casp((void**)&n->key, mp, nil))
|
2013-11-06 20:49:01 +01:00
|
|
|
return false;
|
2011-11-22 21:24:44 +01:00
|
|
|
} else if(mp == (M*)LOCKED) {
|
|
|
|
// Wakeup happened so semaphore is available.
|
|
|
|
// Grab it to avoid getting out of sync.
|
2014-06-07 00:37:27 +02:00
|
|
|
m->blocked = true;
|
2011-11-22 21:24:44 +01:00
|
|
|
if(runtime_semasleep(-1) < 0)
|
|
|
|
runtime_throw("runtime: unable to acquire - semaphore out of sync");
|
2014-06-07 00:37:27 +02:00
|
|
|
m->blocked = false;
|
2013-11-06 20:49:01 +01:00
|
|
|
return true;
|
|
|
|
} else
|
2011-11-22 21:24:44 +01:00
|
|
|
runtime_throw("runtime: unexpected waitm - semaphore out of sync");
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
runtime_notetsleep(Note *n, int64 ns)
|
|
|
|
{
|
|
|
|
M *m;
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
m = runtime_m();
|
|
|
|
|
|
|
|
if(runtime_g() != m->g0 && !m->gcing)
|
|
|
|
runtime_throw("notetsleep not on g0");
|
|
|
|
|
|
|
|
if(m->waitsema == 0)
|
|
|
|
m->waitsema = runtime_semacreate();
|
|
|
|
|
|
|
|
res = notetsleep(n, ns, 0, nil);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// same as runtime_notetsleep, but called on user g (not g0)
|
|
|
|
// calls only nosplit functions between entersyscallblock/exitsyscall
|
|
|
|
bool
|
|
|
|
runtime_notetsleepg(Note *n, int64 ns)
|
|
|
|
{
|
|
|
|
M *m;
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
m = runtime_m();
|
|
|
|
|
|
|
|
if(runtime_g() == m->g0)
|
|
|
|
runtime_throw("notetsleepg on g0");
|
|
|
|
|
|
|
|
if(m->waitsema == 0)
|
|
|
|
m->waitsema = runtime_semacreate();
|
|
|
|
|
|
|
|
runtime_entersyscallblock();
|
|
|
|
res = notetsleep(n, ns, 0, nil);
|
|
|
|
runtime_exitsyscall();
|
|
|
|
return res;
|
|
|
|
}
|