]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libthread/kill.c
authsrv: get rid of needreply parameter by changing vnc protocol handler
[plan9front.git] / sys / src / libthread / kill.c
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
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
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
56 threadkillgrp(int grp)
57 {
58         threadxxxgrp(grp, 1);
59 }
60
61 void
62 threadkill(int id)
63 {
64         threadxxx(id, 1);
65 }
66
67 void
68 threadintgrp(int grp)
69 {
70         threadxxxgrp(grp, 0);
71 }
72
73 void
74 threadint(int id)
75 {
76         threadxxx(id, 0);
77 }
78
79 static void
80 tinterrupt(Proc *p, Thread *t)
81 {
82         char buf[64];
83         int fd;
84
85         switch(t->state){
86         case Running:
87                 snprint(buf, sizeof(buf), "/proc/%d/ctl", p->pid);
88                 fd = open(buf, OWRITE|OCEXEC);
89                 if(fd >= 0){
90                         if(write(fd, "interrupt", 9) == 9){
91                                 close(fd);
92                                 break;
93                         }
94                         close(fd);
95                 }
96                 postnote(PNPROC, p->pid, "threadint");
97                 break;
98         case Rendezvous:
99                 _threadflagrendez(t);
100                 break;
101         }
102 }