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 <afs/param.h>
18 #include <afsconfig.h>
27 extern char *calloc();
34 #include <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;
51 q = (queue *) malloc(sizeof(queue));
52 q -> prev = q -> next = q;
59 return (q->prev == q && q->next == q);
62 void insert(queue *q, char *s)
66 new = (queue *) malloc(sizeof(queue));
68 new -> prev = q -> prev;
69 q -> prev -> next = new;
81 printf("Remove from empty queue");
86 q -> next = old -> next;
87 q -> next -> prev = q;
95 int asleep; /* Number of processes sleeping -- used for
98 static int read_process(id)
101 printf("\t[Reader %d]\n", id);
102 LWP_DispatchProcess(); /* Just relinquish control for now */
108 /* Wait until there is something in the queue */
110 ObtainReadLock(&q->lock);
112 ReleaseReadLock(&q->lock);
114 ObtainReadLock(&q->lock);
117 for (i=0; i<10000; i++) ;
119 printf("[%d: %s]\n", id, Remove(q));
121 ReleaseReadLock(&q->lock);
122 LWP_DispatchProcess();
127 static int write_process()
129 static char *messages[] =
131 "Mary had a little lamb,",
132 "Its fleece was white as snow,",
133 "And everywhere that Mary went,",
134 "The lamb was sure to go",
135 "Mary had a little lamb,",
136 "Its fleece was white as snow,",
137 "And everywhere that Mary went,",
138 "The lamb was sure to go",
139 "Mary had a little lamb,",
140 "Its fleece was white as snow,",
141 "And everywhere that Mary went,",
142 "The lamb was sure to go",
143 "Mary had a little lamb,",
144 "Its fleece was white as snow,",
145 "And everywhere that Mary went,",
146 "The lamb was sure to go",
147 "Mary had a little lamb,",
148 "Its fleece was white as snow,",
149 "And everywhere that Mary went,",
150 "The lamb was sure to go",
151 "Mary had a little lamb,",
152 "Its fleece was white as snow,",
153 "And everywhere that Mary went,",
154 "The lamb was sure to go",
155 "Mary had a little lamb,",
156 "Its fleece was white as snow,",
157 "And everywhere that Mary went,",
158 "The lamb was sure to go",
159 "Mary had a little lamb,",
160 "Its fleece was white as snow,",
161 "And everywhere that Mary went,",
162 "The lamb was sure to go",
163 "Mary had a little lamb,",
164 "Its fleece was white as snow,",
165 "And everywhere that Mary went,",
166 "The lamb was sure to go",
167 "Mary had a little lamb,",
168 "Its fleece was white as snow,",
169 "And everywhere that Mary went,",
170 "The lamb was sure to go",
175 printf("\t[Writer]\n");
178 /* Now loop & write data */
179 for (mesg=messages; *mesg!=0; mesg++) {
180 ObtainWriteLock(&q->lock);
182 ReleaseWriteLock(&q->lock);
183 LWP_SignalProcess(q);
193 1: Number of readers to create (default is DEFAULT_READERS)
194 2: # msecs for interrupt (to satisfy Larry)
195 3: Present if lwp_debug to be set
198 #include "AFS_component_version_number.c"
201 int argc; char **argv;
204 afs_int32 interval; /* To satisfy Brad */
209 printf("\n*Readers & Writers*\n\n");
212 /* Determine # readers */
214 nreaders = DEFAULT_READERS;
216 sscanf(*++argv, "%d", &nreaders);
217 printf("[There will be %d readers]\n", nreaders);
219 interval = (argc >= 3 ? atoi(*++argv)*1000 : 50000);
221 if (argc == 4) lwp_debug = 1;
222 LWP_InitializeProcessSupport(0, (PROCESS*)&i);
223 printf("[Support initialized]\n");
225 tv.tv_usec = interval;
226 PRE_InitPreempt(&tv);
228 /* Initialize queue */
231 /* Initialize lock */
235 /* Now create readers */
236 printf("[Creating Readers...\n");
237 readers = (PROCESS *) calloc(nreaders, sizeof(PROCESS));
238 for (i=0; i<nreaders; i++)
239 LWP_CreateProcess(read_process, STACK_SIZE, 0, (void*)i, "Reader",
243 printf("\t[Creating Writer...\n");
244 LWP_CreateProcess(write_process, STACK_SIZE, 1, 0, "Writer", &writer);
247 /* Now loop until everyone's done */
248 while (asleep != nreaders+1) LWP_DispatchProcess();
249 /* Destroy the readers */
250 for (i=nreaders-1; i>=0; i--) LWP_DestroyProcess(readers[i]);
251 printf("\n*Exiting*\n");