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>
26 extern char *calloc();
34 #include <afs/afs_assert.h>
36 #define DEFAULT_READERS 5
38 #define STACK_SIZE (16*1024)
40 /* The shared queue */
41 typedef struct QUEUE {
42 struct QUEUE *prev, *next;
52 q = (queue *) malloc(sizeof(queue));
53 q->prev = q->next = q;
61 return (q->prev == q && q->next == q);
65 insert(queue * q, char *s)
69 new = (queue *) malloc(sizeof(queue));
85 printf("Remove from empty queue");
99 int asleep; /* Number of processes sleeping -- used for
100 * clean termination */
106 printf("\t[Reader %d]\n", *id);
107 LWP_DispatchProcess(); /* Just relinquish control for now */
113 /* Wait until there is something in the queue */
115 ObtainReadLock(&q->lock);
117 ReleaseReadLock(&q->lock);
119 ObtainReadLock(&q->lock);
122 for (i = 0; i < 10000; i++);
124 printf("[%d: %s]\n", *id, Remove(q));
126 ReleaseReadLock(&q->lock);
127 LWP_DispatchProcess();
135 static char *messages[] = {
136 "Mary had a little lamb,",
137 "Its fleece was white as snow,",
138 "And everywhere that Mary went,",
139 "The lamb was sure to go",
140 "Mary had a little lamb,",
141 "Its fleece was white as snow,",
142 "And everywhere that Mary went,",
143 "The lamb was sure to go",
144 "Mary had a little lamb,",
145 "Its fleece was white as snow,",
146 "And everywhere that Mary went,",
147 "The lamb was sure to go",
148 "Mary had a little lamb,",
149 "Its fleece was white as snow,",
150 "And everywhere that Mary went,",
151 "The lamb was sure to go",
152 "Mary had a little lamb,",
153 "Its fleece was white as snow,",
154 "And everywhere that Mary went,",
155 "The lamb was sure to go",
156 "Mary had a little lamb,",
157 "Its fleece was white as snow,",
158 "And everywhere that Mary went,",
159 "The lamb was sure to go",
160 "Mary had a little lamb,",
161 "Its fleece was white as snow,",
162 "And everywhere that Mary went,",
163 "The lamb was sure to go",
164 "Mary had a little lamb,",
165 "Its fleece was white as snow,",
166 "And everywhere that Mary went,",
167 "The lamb was sure to go",
168 "Mary had a little lamb,",
169 "Its fleece was white as snow,",
170 "And everywhere that Mary went,",
171 "The lamb was sure to go",
172 "Mary had a little lamb,",
173 "Its fleece was white as snow,",
174 "And everywhere that Mary went,",
175 "The lamb was sure to go",
180 printf("\t[Writer]\n");
183 /* Now loop & write data */
184 for (mesg = messages; *mesg != 0; mesg++) {
185 ObtainWriteLock(&q->lock);
187 ReleaseWriteLock(&q->lock);
188 LWP_SignalProcess(q);
198 1: Number of readers to create (default is DEFAULT_READERS)
199 2: # msecs for interrupt (to satisfy Larry)
200 3: Present if lwp_debug to be set
203 #include "AFS_component_version_number.c"
211 afs_int32 interval; /* To satisfy Brad */
217 printf("\n*Readers & Writers*\n\n");
220 /* Determine # readers */
222 nreaders = DEFAULT_READERS;
224 sscanf(*++argv, "%d", &nreaders);
225 printf("[There will be %d readers]\n", nreaders);
227 interval = (argc >= 3 ? atoi(*++argv) * 1000 : 50000);
231 LWP_InitializeProcessSupport(0, &pid);
232 printf("[Support initialized]\n");
234 tv.tv_usec = interval;
235 PRE_InitPreempt(&tv);
237 /* Initialize queue */
240 /* Initialize lock */
244 /* Now create readers */
245 printf("[Creating Readers...\n");
246 readers = (PROCESS *) calloc(nreaders, sizeof(PROCESS));
247 readerid = (int *)calloc(nreaders, sizeof(i));
248 for (i = 0; i < nreaders; i++)
249 LWP_CreateProcess(read_process, STACK_SIZE, 0, (void *)&readerid[i],
250 "Reader", &readers[i]);
253 printf("\t[Creating Writer...\n");
254 LWP_CreateProcess(write_process, STACK_SIZE, 1, 0, "Writer", &writer);
257 /* Now loop until everyone's done */
258 while (asleep != nreaders + 1)
259 LWP_DispatchProcess();
260 /* Destroy the readers */
261 for (i = nreaders - 1; i >= 0; i--)
262 LWP_DestroyProcess(readers[i]);
263 printf("\n*Exiting*\n");