xref: /plan9/sys/src/libthread/kill.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include "threadimpl.h"
5 
6 static void tinterrupt(Proc*, Thread*);
7 
8 static void
threadxxxgrp(int grp,int dokill)9 threadxxxgrp(int grp, int dokill)
10 {
11 	Proc *p;
12 	Thread *t;
13 
14 	lock(&_threadpq.lock);
15 	for(p=_threadpq.head; p; p=p->next){
16 		lock(&p->lock);
17 		for(t=p->threads.head; t; t=t->nextt)
18 			if(t->grp == grp){
19 				if(dokill)
20 					t->moribund = 1;
21 				tinterrupt(p, t);
22 			}
23 		unlock(&p->lock);
24 	}
25 	unlock(&_threadpq.lock);
26 	_threadbreakrendez();
27 }
28 
29 static void
threadxxx(int id,int dokill)30 threadxxx(int id, int dokill)
31 {
32 	Proc *p;
33 	Thread *t;
34 
35 	lock(&_threadpq.lock);
36 	for(p=_threadpq.head; p; p=p->next){
37 		lock(&p->lock);
38 		for(t=p->threads.head; t; t=t->nextt)
39 			if(t->id == id){
40 				if(dokill)
41 					t->moribund = 1;
42 				tinterrupt(p, t);
43 				unlock(&p->lock);
44 				unlock(&_threadpq.lock);
45 				_threadbreakrendez();
46 				return;
47 			}
48 		unlock(&p->lock);
49 	}
50 	unlock(&_threadpq.lock);
51 	_threaddebug(DBGNOTE, "Can't find thread to kill");
52 	return;
53 }
54 
55 void
threadkillgrp(int grp)56 threadkillgrp(int grp)
57 {
58 	threadxxxgrp(grp, 1);
59 }
60 
61 void
threadkill(int id)62 threadkill(int id)
63 {
64 	threadxxx(id, 1);
65 }
66 
67 void
threadintgrp(int grp)68 threadintgrp(int grp)
69 {
70 	threadxxxgrp(grp, 0);
71 }
72 
73 void
threadint(int id)74 threadint(int id)
75 {
76 	threadxxx(id, 0);
77 }
78 
79 static void
tinterrupt(Proc * p,Thread * t)80 tinterrupt(Proc *p, Thread *t)
81 {
82 	switch(t->state){
83 	case Running:
84 		postnote(PNPROC, p->pid, "threadint");
85 		break;
86 	case Rendezvous:
87 		_threadflagrendez(t);
88 		break;
89 	}
90 }
91