change reference counting to use ainc and adec rather than locks. Reference: /n/atom/patch/applied/refainc Date: Thu May 15 01:29:18 CES 2014 Signed-off-by: quanstro@quanstro.net --- /sys/src/nix/port/syssem.c Thu May 15 01:29:17 2014 +++ /sys/src/nix/port/syssem.c Thu May 15 01:29:17 2014 @@ -15,9 +15,6 @@ * If n == 0, there are no tickets * If n < 0, there are |n| processes waiting for tickets * or preparing to wait for tickets. - * - * CAUTION: Do not use adec/ainc for semaphores, they would - * trap if the integer is negative, but that's ok for semaphores. */ static void @@ -71,7 +68,7 @@ * Make sure that no other process is waiting because we * made a temporary down. */ - semainc(s->np); + ainc(s->np); semwakeup(s, 1, 1); return; } @@ -106,7 +103,7 @@ * killed; we no longer want a ticket. */ lock(s); - semainc(s->np); /* we are no longer waiting; killed */ + ainc(s->np); /* we are no longer waiting; killed */ semwakeup(s, 1, 0); unlock(s); } @@ -169,7 +166,7 @@ * on it; it must be because someone gave us its * ticket in the mean while. We must put it back. */ - semainc(s->np); + ainc(s->np); semwakeup(s, 0, 0); }else{ s->nq--; @@ -192,7 +189,7 @@ r = -1; for(i = 0; i < n; i++){ s = ss[i]; - n = semadec(s->np); + n = adec(s->np); if(n >= 0){ r = i; goto Done; @@ -266,7 +263,7 @@ { int n; - n = semainc(s->np); + n = ainc(s->np); if(n <= 0) semwakeup(s, 1, 1); } @@ -280,7 +277,7 @@ yield(); if(*s->np <= 0 && dontblock) return -1; - n = semadec(s->np); + n = adec(s->np); if(n < 0) semsleep(s, dontblock); return 0; --- /sys/src/nix/port/portfns.h Thu May 15 01:29:17 2014 +++ /sys/src/nix/port/portfns.h Thu May 15 01:29:17 2014 @@ -330,8 +330,6 @@ Sem* segmksem(Segment*, int*); void segpage(Segment*, Page*); uintmem segppn(Segment*, uintmem); -int semadec(int*); -int semainc(int*); char* seprintpagestats(char*, char*); char* seprintphysstats(char*, char*); void setkernur(Ureg*, Proc*); --- /sys/src/nix/port/taslock.c Thu May 15 01:29:17 2014 +++ /sys/src/nix/port/taslock.c Thu May 15 01:29:17 2014 @@ -42,7 +42,7 @@ lockstats.locks++; if(up) - ainc(&up->nlocks); /* prevent being scheded */ + up->nlocks++; /* prevent being scheded */ if(tas(&l->key) == 0){ if(up) up->lastlock = l; @@ -55,7 +55,7 @@ return 0; } if(up) - adec(&up->nlocks); + up->nlocks--; cycles(&t0); lockstats.glare++; @@ -79,7 +79,7 @@ pause(); } if(up) - ainc(&up->nlocks); + up->nlocks++; if(tas(&l->key) == 0){ if(up) up->lastlock = l; @@ -93,7 +93,7 @@ return 1; } if(up) - adec(&up->nlocks); + up->nlocks--; } } @@ -146,10 +146,10 @@ canlock(Lock *l) { if(up) - ainc(&up->nlocks); + up->nlocks++; if(tas(&l->key)){ if(up) - adec(&up->nlocks); + up->nlocks--; return 0; } @@ -189,7 +189,7 @@ l->key = 0; coherence(); - if(up && adec(&up->nlocks) == 0 && up->delaysched && islo()){ + if(up && --up->nlocks == 0 && up->delaysched && islo()){ /* * Call sched if the need arose while locks were held * But, don't do it from interrupt routines, hence the islo() test --- /sys/src/nix/port/portdat.h Thu May 15 01:29:17 2014 +++ /sys/src/nix/port/portdat.h Thu May 15 01:29:17 2014 @@ -71,7 +71,6 @@ struct Ref { - Lock; int ref; }; @@ -214,7 +213,8 @@ struct Chan { - Ref; /* the Lock in this Ref is also Chan's lock */ + Lock; + Ref; Chan* next; /* allocation */ Chan* link; vlong offset; /* in fd */ @@ -383,6 +383,7 @@ struct Image { + Lock; Ref; Chan *c; /* channel to text file */ Qid qid; /* Qid for page cache coherence */ @@ -480,6 +481,7 @@ struct Segment { + Lock; Ref; QLock lk; ushort steal; /* Page stealer lock */ @@ -528,7 +530,8 @@ struct Rgrp { - Ref; /* the Ref's lock is also the Rgrp's lock */ + Lock; + Ref; Proc *rendhash[RENDHASH]; /* Rendezvous tag hash */ }; @@ -554,6 +557,7 @@ struct Fgrp { + Lock; Ref; Chan **fd; int nfd; /* number allocated */ --- /sys/src/nix/port/pgrp.c Thu May 15 01:29:17 2014 +++ /sys/src/nix/port/pgrp.c Thu May 15 01:29:17 2014 @@ -152,10 +152,8 @@ /* * Allocate mount ids in the same sequence as the parent group */ - lock(&mountid); for(mount = order; mount != nil; mount = mount->order) - mount->copy->mountid = mountid.ref++; - unlock(&mountid); + mount->copy->mountid = incref(&mountid); wunlock(&from->ns); } --- /sys/src/nix/port/devaoe.c Thu May 15 01:29:17 2014 +++ /sys/src/nix/port/devaoe.c Thu May 15 01:29:17 2014 @@ -239,9 +239,15 @@ Netlink nl[Nnetlink]; } netlinks; +typedef struct Count Count; +struct Count { + Lock; + int ref; +}; + extern Dev aoedevtab; -static Ref units; -static Ref drivevers; +static Count units; +static Count drivevers; static int debug; static int autodiscover = 1; static int rediscover; --- /sys/src/nix/port/ref.c Thu Jan 1 00:00:00 1970 +++ /sys/src/nix/port/ref.c Thu May 15 01:29:17 2014 @@ -0,0 +1,27 @@ +#include "u.h" +#include "../port/lib.h" +#include "mem.h" +#include "dat.h" +#include "fns.h" + +int +incref(Ref *r) +{ + int x; + + x = ainc(&r->ref); + if(x <= 0) + panic("incref pc=%#p", getcallerpc(&r)); + return x; +} + +int +decref(Ref *r) +{ + int x; + + x = adec(&r->ref); + if(x < 0) + panic("decref pc=%#p", getcallerpc(&r)); + return x; +} --- /sys/src/nix/k10/cpu Thu May 15 01:29:17 2014 +++ /sys/src/nix/k10/cpu Thu May 15 01:29:17 2014 @@ -81,7 +81,7 @@ il rootdir - bootcpu.out boot + boot$CONF.out boot /amd64/bin/auth/factotum factotum /amd64/bin/ip/ipconfig ipconfig /amd64/bin/usb/usbd usbd @@ -158,6 +158,7 @@ qio qlock rebootcmd + ref segment sysauth sysfile --- /sys/src/nix/k10/cpud Thu May 15 01:29:17 2014 +++ /sys/src/nix/k10/cpud Thu May 15 01:29:17 2014 @@ -81,7 +81,7 @@ local rootdir - bootcpud.out boot + boot$CONF.out boot /amd64/bin/auth/factotum factotum /amd64/bin/ip/ipconfig ipconfig /amd64/bin/usb/usbd usbd @@ -159,6 +159,7 @@ qio qlock rebootcmd + ref segment sysauth sysfile --- /sys/src/nix/k10/cpuf Thu May 15 01:29:17 2014 +++ /sys/src/nix/k10/cpuf Thu May 15 01:29:17 2014 @@ -82,7 +82,7 @@ local rootdir - bootcpuf.out boot + boot$CONF.out boot /amd64/bin/auth/factotum factotum /amd64/bin/ip/ipconfig ipconfig /amd64/bin/usb/usbd usbd @@ -161,6 +161,7 @@ qio qlock rebootcmd + ref segment sysauth sysfile --- /sys/src/nix/k10/term Thu May 15 01:29:17 2014 +++ /sys/src/nix/k10/term Thu May 15 01:29:17 2014 @@ -92,7 +92,7 @@ il rootdir - bootterm.out boot + boot$CONF.out boot /amd64/bin/auth/factotum factotum /amd64/bin/ip/ipconfig ipconfig /amd64/bin/usb/usbd usbd @@ -170,6 +170,7 @@ qio qlock rebootcmd + ref segment sysauth sysfile --- /sys/src/nix/k10/termd Thu May 15 01:29:17 2014 +++ /sys/src/nix/k10/termd Thu May 15 01:29:17 2014 @@ -92,7 +92,7 @@ local rootdir - boottermd.out boot + boot$CONF.out boot /amd64/bin/auth/factotum factotum /amd64/bin/ip/ipconfig ipconfig /amd64/bin/usb/usbd usbd @@ -171,6 +171,7 @@ qio qlock rebootcmd + ref segment sysauth sysfile --- /sys/src/nix/k10/termf Thu May 15 01:29:17 2014 +++ /sys/src/nix/k10/termf Thu May 15 01:29:17 2014 @@ -92,7 +92,7 @@ local rootdir - boottermf.out boot + boot$CONF.out boot /amd64/bin/auth/factotum factotum /amd64/bin/ip/ipconfig ipconfig /amd64/bin/usb/usbd usbd @@ -172,6 +172,7 @@ qio qlock rebootcmd + ref segment sysauth sysfile --- /sys/src/nix/k10/l64v.s Thu May 15 01:29:17 2014 +++ /sys/src/nix/k10/l64v.s Thu May 15 01:29:17 2014 @@ -274,37 +274,13 @@ /* BOTCH INCL AX */ RET -TEXT ainc(SB), 1, $-4 /* int ainc(int*); */ - MOVL $1, AX - LOCK; XADDL AX, (RARG) - ADDL $1, AX /* overflow if -ve or 0 */ - JGT _return -_trap: - XORQ BX, BX - MOVQ (BX), BX /* over under sideways down */ -_return: - RET - -TEXT adec(SB), 1, $-4 /* int adec(int*); */ - MOVL $-1, AX - LOCK; XADDL AX, (RARG) - SUBL $1, AX /* underflow if -ve */ - JLT _trap - - RET - -/* - * Semaphores rely on negative values for the counter, - * and don't have the same overflow/underflow conditions - * as ainc/adec. - */ -TEXT semainc(SB), 1, $-4 /* int semainc(int*); */ +TEXT ainc(SB), 1, $-4 /* int ainc(int*) */ MOVL $1, AX LOCK; XADDL AX, (RARG) ADDL $1, AX RET -TEXT semadec(SB), 1, $-4 /* int semadec(int*); */ +TEXT adec(SB), 1, $-4 /* int adec(int*) */ MOVL $-1, AX LOCK; XADDL AX, (RARG) SUBL $1, AX --- /sys/src/nix/k10/arch.c Thu May 15 01:29:17 2014 +++ /sys/src/nix/k10/arch.c Thu May 15 01:29:18 2014 @@ -11,31 +11,6 @@ #include "fns.h" #include "../port/error.h" -int -incref(Ref *r) -{ - int x; - - lock(r); - x = ++r->ref; - unlock(r); - return x; -} - -int -decref(Ref *r) -{ - int x; - - lock(r); - x = --r->ref; - unlock(r); - if(x < 0) - panic("decref pc=%#p", getcallerpc(&r)); - - return x; -} - void procrestore(Proc *p) {