~kris/9p

9hist

ref: 9584dbf140aab3d221ec0028d0df1e422aa63a6d 9hist/port/service.c -rw-r--r-- 3.6 KiB
9584dbf1 — David du Colombier Plan 9 from Bell Labs 1991-06-14 35 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"errno.h"

#include	"fcall.h"

#define	BUFSIZE	(MAXFDATA+500) 	/* BUG */
typedef struct Buffer Buffer;
struct Buffer
{
	Buffer	*next;
	char	buf[BUFSIZE];
};

struct Servalloc {
	Lock;
	Service *all;
	Service *freel;
	Buffer	*ball;
	Buffer	*bfreel;
} servalloc;


/*
 *  allocate the taks force structures and put them
 *  on the free list
 */
void
serviceinit(void)
{
	Service *s;
	Buffer *b;

	servalloc.all = ialloc(conf.nservice * sizeof(Service), 0);
	servalloc.freel = servalloc.all;
	for(s = servalloc.all; s < &servalloc.all[conf.nservice-1]; s++)
		s->next = s+1;

	servalloc.ball = ialloc(2*conf.nservice * sizeof(Service), 0);
	servalloc.bfreel = servalloc.ball;
	for(b = servalloc.ball; b < &servalloc.ball[2*conf.nservice-1]; b++)
		b->next = b+1;
}

/*
 *  Add a process to a service
 */
static Proc *
reinforce(Service *s)
{
	int i;
	Proc *p;

	/*
	 *  don't start one up if we already have too many,
	 *  return our own process number
	 */
	lock(s);
	if(s->ref == NSTUB){
		unlock(s);
		return u->p;
	}
	s->ref++;
	unlock(s);

	/*
	 *  create it
	 */
	return kproc(s->name, 0, 0);
}

/*
 *  resign from a service
 */
static void
resign(Service *s)
{
	int i;

	print("%lux resigning\n", u->p);
	if(decref(s) == 0){
		print("freeing service\n");
		close(s->inc);
		close(s->outc);
		lock(&servalloc);
		s->next = servalloc.freel;
		servalloc.freel = s;
		unlock(&servalloc);
	}
	pexit(0, 1);
}

/*
 *  Create a service of kernel processes to handle file system requests.
 *  (*doit)() is a function that handles that event.  Service
 *  dynamicly creates new processes (up to MAXTASK) to ensure that 1 process
 *  is always awaiting while others are doing.
 */
void
service(char *name, Chan *cin, Chan *cout, void (*doit)(Chan*, char*, long))
{
	void *a;
	Service *s;
	Buffer *b;
	long n;

	b = 0;

	/*
	 *  allocate a service
	 */
	lock(&servalloc);
	s = servalloc.freel;
	if(s == 0)
		panic("no more services");
	servalloc.freel = s->next;
	unlock(&servalloc);
	memset(s, 0, sizeof(Service));
	if(name==0 || *name==0)
		strcpy(name, "filsys");
	else
		strncpy(s->name, name, sizeof(s->name));
	s->inc = cin;
	s->outc = cout;
	s->die = 0;
	incref(cin);
	incref(cout);

	if(waserror()){
		/*
		 *  tell everyone else to die
		 */
		s->die = 1;

		/*
		 *  wake up the next guy
		 */
		qunlock(&s->alock);

		/*
		 *  free the buffer if we have one
		 */
		if(b){
			lock(&servalloc);
			b->next = servalloc.bfreel;
			servalloc.bfreel = b;
			unlock(&servalloc);
		}
		resign(s);
	}

	/*
	 *  Create the first process in the service.  The
	 *  caller returns.
	 */
	if(reinforce(s)){
		poperror();
		return;
	}

	/*
	 *  first process enters the infinite loop
	 */
	for(;;){
		b = 0;
		qlock(&s->alock);

		/*
		 *  if there's more than 1 process waiting, we're
		 *  superfluous
		 */
		if(s->die || s->alock.head && s->alock.head!=s->alock.tail){
			qunlock(&s->alock);
			resign(s);
		}

		/*
		 *  get a buffer
		 */
		lock(&servalloc);
		b = servalloc.bfreel;
		if(b == 0){
			print("no serv buffers\n");
			unlock(&servalloc);
			continue;
		}
		servalloc.bfreel = b->next;
		unlock(&servalloc);

		/*
		 *  wait for something to happen
		 */
		n = (*devtab[cin->type].read)(cin, b->buf, sizeof(b->buf));
		if(n <= 0)
			error(Ehungup);

		/*
		 *  create a new process if none are waiting to
		 *  take over
		 */
		if(s->alock.head == 0)
			if(reinforce(s) == 0)
				continue;
		qunlock(&s->alock);

		/*
		 *  process an event
		 */
		(*doit)(cout, b->buf, n);

		/*
		 *  release the buffer
		 */
		lock(&servalloc);
		b->next = servalloc.bfreel;
		servalloc.bfreel = b;
		unlock(&servalloc);
	}
}