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>
27 extern char *calloc();
35 #include <afs/assert.h>
37 #define DEFAULT_READERS 5
39 #define STACK_SIZE (16*1024)
41 /* The shared queue */
42 typedef struct QUEUE {
43 struct QUEUE *prev, *next;
52 q = (queue *) malloc(sizeof(queue));
53 q -> prev = q -> next = q;
60 return (q->prev == q && q->next == q);
63 void insert(queue *q, char *s)
67 new = (queue *) malloc(sizeof(queue));
69 new -> prev = q -> prev;
70 q -> prev -> next = new;
82 printf("Remove from empty queue");
87 q -> next = old -> next;
88 q -> next -> prev = q;
96 int asleep; /* Number of processes sleeping -- used for
99 static int read_process(id)
102 printf("\t[Reader %d]\n", *id);
103 LWP_DispatchProcess(); /* Just relinquish control for now */
109 /* Wait until there is something in the queue */
111 ObtainReadLock(&q->lock);
113 ReleaseReadLock(&q->lock);
115 ObtainReadLock(&q->lock);
118 for (i=0; i<10000; i++) ;
120 printf("[%d: %s]\n", *id, Remove(q));
122 ReleaseReadLock(&q->lock);
123 LWP_DispatchProcess();
128 static int write_process()
130 static char *messages[] =
132 "Mary had a little lamb,",
133 "Its fleece was white as snow,",
134 "And everywhere that Mary went,",
135 "The lamb was sure to go",
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",
176 printf("\t[Writer]\n");
179 /* Now loop & write data */
180 for (mesg=messages; *mesg!=0; mesg++) {
181 ObtainWriteLock(&q->lock);
183 ReleaseWriteLock(&q->lock);
184 LWP_SignalProcess(q);
194 1: Number of readers to create (default is DEFAULT_READERS)
195 2: # msecs for interrupt (to satisfy Larry)
196 3: Present if lwp_debug to be set
199 #include "AFS_component_version_number.c"
202 int argc; char **argv;
206 afs_int32 interval; /* To satisfy Brad */
212 printf("\n*Readers & Writers*\n\n");
215 /* Determine # readers */
217 nreaders = DEFAULT_READERS;
219 sscanf(*++argv, "%d", &nreaders);
220 printf("[There will be %d readers]\n", nreaders);
222 interval = (argc >= 3 ? atoi(*++argv)*1000 : 50000);
224 if (argc == 4) lwp_debug = 1;
225 LWP_InitializeProcessSupport(0, &pid);
226 printf("[Support initialized]\n");
228 tv.tv_usec = interval;
229 PRE_InitPreempt(&tv);
231 /* Initialize queue */
234 /* Initialize lock */
238 /* Now create readers */
239 printf("[Creating Readers...\n");
240 readers = (PROCESS *) calloc(nreaders, sizeof(PROCESS));
241 readerid = (int *)calloc(nreaders, sizeof(i));
242 for (i=0; i<nreaders; i++)
243 LWP_CreateProcess(read_process, STACK_SIZE, 0, (void*)&readerid[i],
244 "Reader", &readers[i]);
247 printf("\t[Creating Writer...\n");
248 LWP_CreateProcess(write_process, STACK_SIZE, 1, 0, "Writer", &writer);
251 /* Now loop until everyone's done */
252 while (asleep != nreaders+1) LWP_DispatchProcess();
253 /* Destroy the readers */
254 for (i=nreaders-1; i>=0; i--) LWP_DestroyProcess(readers[i]);
255 printf("\n*Exiting*\n");