2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 (Multiple) readers & writers test of LWP stuff.
13 Created: 11/1/83, J. Rosenberg
17 #include <afsconfig.h>
18 #include <afs/param.h>
28 extern char *calloc();
36 #include <afs/assert.h>
38 #define DEFAULT_READERS 5
40 #define STACK_SIZE (16*1024)
42 /* The shared queue */
43 typedef struct QUEUE {
44 struct QUEUE *prev, *next;
54 q = (queue *) malloc(sizeof(queue));
55 q->prev = q->next = q;
63 return (q->prev == q && q->next == q);
67 insert(queue * q, char *s)
71 new = (queue *) malloc(sizeof(queue));
87 printf("Remove from empty queue");
101 int asleep; /* Number of processes sleeping -- used for
102 * clean termination */
108 printf("\t[Reader %d]\n", *id);
109 LWP_DispatchProcess(); /* Just relinquish control for now */
115 /* Wait until there is something in the queue */
117 ObtainReadLock(&q->lock);
119 ReleaseReadLock(&q->lock);
121 ObtainReadLock(&q->lock);
124 for (i = 0; i < 10000; i++);
126 printf("[%d: %s]\n", *id, Remove(q));
128 ReleaseReadLock(&q->lock);
129 LWP_DispatchProcess();
137 static char *messages[] = {
138 "Mary had a little lamb,",
139 "Its fleece was white as snow,",
140 "And everywhere that Mary went,",
141 "The lamb was sure to go",
142 "Mary had a little lamb,",
143 "Its fleece was white as snow,",
144 "And everywhere that Mary went,",
145 "The lamb was sure to go",
146 "Mary had a little lamb,",
147 "Its fleece was white as snow,",
148 "And everywhere that Mary went,",
149 "The lamb was sure to go",
150 "Mary had a little lamb,",
151 "Its fleece was white as snow,",
152 "And everywhere that Mary went,",
153 "The lamb was sure to go",
154 "Mary had a little lamb,",
155 "Its fleece was white as snow,",
156 "And everywhere that Mary went,",
157 "The lamb was sure to go",
158 "Mary had a little lamb,",
159 "Its fleece was white as snow,",
160 "And everywhere that Mary went,",
161 "The lamb was sure to go",
162 "Mary had a little lamb,",
163 "Its fleece was white as snow,",
164 "And everywhere that Mary went,",
165 "The lamb was sure to go",
166 "Mary had a little lamb,",
167 "Its fleece was white as snow,",
168 "And everywhere that Mary went,",
169 "The lamb was sure to go",
170 "Mary had a little lamb,",
171 "Its fleece was white as snow,",
172 "And everywhere that Mary went,",
173 "The lamb was sure to go",
174 "Mary had a little lamb,",
175 "Its fleece was white as snow,",
176 "And everywhere that Mary went,",
177 "The lamb was sure to go",
182 printf("\t[Writer]\n");
185 /* Now loop & write data */
186 for (mesg = messages; *mesg != 0; mesg++) {
187 ObtainWriteLock(&q->lock);
189 ReleaseWriteLock(&q->lock);
190 LWP_SignalProcess(q);
200 1: Number of readers to create (default is DEFAULT_READERS)
201 2: # msecs for interrupt (to satisfy Larry)
202 3: Present if lwp_debug to be set
205 #include "AFS_component_version_number.c"
213 afs_int32 interval; /* To satisfy Brad */
219 printf("\n*Readers & Writers*\n\n");
222 /* Determine # readers */
224 nreaders = DEFAULT_READERS;
226 sscanf(*++argv, "%d", &nreaders);
227 printf("[There will be %d readers]\n", nreaders);
229 interval = (argc >= 3 ? atoi(*++argv) * 1000 : 50000);
233 LWP_InitializeProcessSupport(0, &pid);
234 printf("[Support initialized]\n");
236 tv.tv_usec = interval;
237 PRE_InitPreempt(&tv);
239 /* Initialize queue */
242 /* Initialize lock */
246 /* Now create readers */
247 printf("[Creating Readers...\n");
248 readers = (PROCESS *) calloc(nreaders, sizeof(PROCESS));
249 readerid = (int *)calloc(nreaders, sizeof(i));
250 for (i = 0; i < nreaders; i++)
251 LWP_CreateProcess(read_process, STACK_SIZE, 0, (void *)&readerid[i],
252 "Reader", &readers[i]);
255 printf("\t[Creating Writer...\n");
256 LWP_CreateProcess(write_process, STACK_SIZE, 1, 0, "Writer", &writer);
259 /* Now loop until everyone's done */
260 while (asleep != nreaders + 1)
261 LWP_DispatchProcess();
262 /* Destroy the readers */
263 for (i = nreaders - 1; i >= 0; i--)
264 LWP_DestroyProcess(readers[i]);
265 printf("\n*Exiting*\n");