Blob
1 /*2 * Proc structure hash table indexed by proctabid() (usually getpid()).3 * No lock is necessary for lookups (important when called from signal4 * handlers).5 *6 * To be included from other files (e.g., Linux-clone.c).7 */9 #define T ((void*)-1)11 enum12 {13 PTABHASH = 1031,14 };16 static Lock ptablock;17 static Proc *proctab[PTABHASH];18 static Proc *theproc;19 static int multi;21 void22 _threadmultiproc(void)23 {24 if(multi == 0){25 multi = 1;26 _threadsetproc(theproc);27 }28 }30 void31 _threadsetproc(Proc *p)32 {33 int i, h;34 Proc **t;36 if(!multi){37 theproc = p;38 return;39 }40 lock(&ptablock);41 p->procid = procid();42 h = p->procid%PTABHASH;43 for(i=0; i<PTABHASH; i++){44 t = &proctab[(h+i)%PTABHASH];45 if(*t==nil || *t==T){46 *t = p;47 break;48 }49 }50 unlock(&ptablock);51 if(i == PTABHASH)52 sysfatal("too many procs - proctab is full");53 }55 static Proc**56 _threadfindproc(int id)57 {58 int i, h;59 Proc **t;61 if(!multi)62 return &theproc;64 h = id%PTABHASH;65 for(i=0; i<PTABHASH; i++){66 t = &proctab[(h+i)%PTABHASH];67 if(*t != nil && *t != T && (*t)->procid == id){68 unlock(&ptablock);69 return t;70 }71 }72 return nil;73 }75 Proc*76 _threadgetproc(void)77 {78 Proc **t;80 t = _threadfindproc(procid());81 if(t == nil)82 return nil;83 return *t;84 }86 Proc*87 _threaddelproc(void)88 {89 Proc **t, *p;91 t = _threadfindproc(procid());92 if(t == nil)93 return nil;94 p = *t;95 *t = T;96 return p;97 }