| plan 9 kernel history: overview | file list | diff list |
2002/0414/port/taslock.c (diff list | history)
| 2002/0414/sys/src/9/port/taslock.c:1,187 – 2002/0416/sys/src/9/port/taslock.c:1,192 (short | long | prev | next) | ||
| 1992/0222 | #include "u.h" | |
| 1992/0321 | #include "../port/lib.h" | |
| 1992/0222 | #include "mem.h" #include "dat.h" #include "fns.h" #include "../port/error.h" | |
| 2002/0403 | struct { | |
| 1998/0606 | ulong locks; ulong glare; ulong inglare; } lockstats; | |
| 2000/1106 | static void dumplockmem(char *tag, Lock *l) { uchar *cp; int i; iprint("%s: ", tag); cp = (uchar*)l; for(i = 0; i < 64; i++) iprint("%2.2ux ", cp[i]); iprint("\n"); } | |
| 1992/0222 | void | |
| 1995/1030 | lockloop(Lock *l, ulong pc) { | |
| 1998/0606 | Proc *p; p = l->p; | |
| 2001/0527 | print("lock 0x%lux loop key 0x%lux pc 0x%lux held by pc 0x%lux proc %lud\n", l, l->key, pc, l->pc, p ? p->pid : 0); | |
| 1995/1030 | dumpaproc(up); | |
| 1998/0606 | if(p != nil) dumpaproc(p); | |
| 1996/0523 | ||
| 1998/0604 | if(up && up->state == Running && islo()) | |
| 1996/0523 | sched(); | |
| 1995/1030 | } | |
| 2002/0403 | int | |
| 1992/0222 | lock(Lock *l) { | |
| 2002/0403 | int i; ulong pc; | |
| 1995/1009 | ||
| 1999/0501 | pc = getcallerpc(&l); | |
| 1995/1009 | ||
| 2002/0403 | lockstats.locks++; if(up) up->nlocks++; /* prevent being scheded */ | |
| 1996/0522 | if(tas(&l->key) == 0){ | |
| 2002/0404 | if(up) up->lastlock = l; | |
| 1996/0522 | l->pc = pc; | |
| 1998/0604 | l->p = up; l->isilock = 0; | |
| 2002/0403 | return 0; | |
| 1995/1009 | } | |
| 2002/0403 | if(up) up->nlocks--; /* didn't get the lock, allow scheding */ | |
| 1995/0108 | ||
| 1999/0811 | lockstats.glare++; | |
| 1996/0522 | for(;;){ | |
| 1999/0811 | lockstats.inglare++; | |
| 1996/0522 | i = 0; | |
| 1998/0604 | while(l->key){ | |
| 2002/0403 | if(i++ > 100000000){ i = 0; lockloop(l, pc); | |
| 1996/0522 | } | |
| 1998/0604 | } | |
| 2002/0403 | if(up) up->nlocks++; | |
| 1995/1009 | if(tas(&l->key) == 0){ | |
| 2002/0403 | if(up) up->lastlock = l; | |
| 1995/1009 | l->pc = pc; | |
| 1998/0604 | l->p = up; l->isilock = 0; | |
| 2002/0403 | return 1; | |
| 1995/1009 | } | |
| 2002/0403 | if(up) up->nlocks--; | |
| 1995/0108 | } | |
| 2002/0404 | return 0; /* For the compiler */ | |
| 1995/0110 | } | |
| 1995/0108 | ||
| 1995/0110 | void ilock(Lock *l) { ulong x; | |
| 2002/0403 | ulong pc; | |
| 1995/0110 | ||
| 1999/0501 | pc = getcallerpc(&l); | |
| 1999/0811 | lockstats.locks++; | |
| 1995/1009 | ||
| 1995/0110 | x = splhi(); if(tas(&l->key) == 0){ | |
| 2002/0403 | if(up) up->lastlock = l; | |
| 1995/0110 | l->sr = x; | |
| 1995/1009 | l->pc = pc; | |
| 1998/0604 | l->p = up; l->isilock = 1; | |
| 1993/1204 | return; | |
| 1995/0110 | } | |
| 1999/0811 | lockstats.glare++; | |
| 2002/0402 | if(conf.nmach < 2){ dumplockmem("ilock:", l); | |
| 2002/0114 | panic("ilock: no way out: pc %luX\n", pc); | |
| 2002/0402 | } | |
| 1998/0522 | ||
| 1993/0830 | for(;;){ | |
| 1999/0811 | lockstats.inglare++; | |
| 1998/0228 | splx(x); | |
| 1994/0808 | while(l->key) ; | |
| 1998/0228 | x = splhi(); | |
| 1995/0110 | if(tas(&l->key) == 0){ | |
| 2002/0404 | if(up) up->lastlock = l; | |
| 1995/0110 | l->sr = x; | |
| 1995/1009 | l->pc = pc; | |
| 1998/0604 | l->p = up; l->isilock = 1; | |
| 1992/0222 | return; | |
| 1995/0110 | } | |
| 1992/0222 | } } | |
| 1995/0109 | int canlock(Lock *l) | |
| 1994/0322 | { | |
| 2002/0403 | if(up) up->nlocks++; | |
| 2002/0402 | if(tas(&l->key)){ | |
| 2002/0403 | if(up) up->nlocks--; | |
| 1996/0511 | return 0; | |
| 2002/0402 | } | |
| 1996/0522 | ||
| 2002/0403 | if(up) up->lastlock = l; | |
| 1999/0501 | l->pc = getcallerpc(&l); | |
| 1998/0604 | l->p = up; l->isilock = 0; | |
| 1992/0222 | return 1; } void unlock(Lock *l) { | |
| 1998/0522 | if(l->key == 0) | |
| 1999/0501 | print("unlock: not locked: pc %luX\n", getcallerpc(&l)); | |
| 1998/0604 | if(l->isilock) | |
| 1999/0710 | print("unlock of ilock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc); | |
| 2002/0402 | if(l->p != up) print("unlock: up changed: pc %lux, acquired at pc %lux, lock p 0x%p, unlock up 0x%p\n", getcallerpc(&l), l->pc, l->p, up); | |
| 1997/0327 | l->key = 0; | |
| 2002/0406 | coherence(); | |
| 2002/0414 |
| |
| 2002/0416 | /* give up the processor if ... */ if(up && --up->nlocks == 0 /* we've closed the last nexted lock */ && up->delaysched /* we delayed scheduling because of the lock */ && up->state == Running){ /* we're in running state */ up->delaysched = 0; sched(); } | |
| 1994/0322 | } void iunlock(Lock *l) { ulong sr; | |
| 1998/0603 | if(l->key == 0) | |
| 1999/0501 | print("iunlock: not locked: pc %luX\n", getcallerpc(&l)); | |
| 1998/0604 | if(!l->isilock) | |
| 1999/0710 | print("iunlock of lock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc); | |
| 1998/0603 | ||
| 1994/0322 | sr = l->sr; | |
| 1997/0327 | l->key = 0; | |
| 2001/0314 | coherence(); | |
| 1998/0606 | ||
| 1999/0501 | m->splpc = getcallerpc(&l); | |
| 1998/0606 | splxpc(sr); | |
| 1992/0222 | } | |