lwp: fix bug in rw with assigning reader id
[openafs.git] / src / lwp / rw.c
index 5232360..e060db7 100644 (file)
@@ -1,14 +1,12 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
  */
 
-#ifndef lint
-#endif
 /*
        (Multiple) readers & writers test of LWP stuff.
 
@@ -16,20 +14,13 @@ Created: 11/1/83, J. Rosenberg
 
 */
 
+#include <afsconfig.h>
 #include <afs/param.h>
-#ifdef AFS_NT40_ENV
-#include <malloc.h>
-#include <stdlib.h>
-#else
-#include <sys/time.h>
-extern char *calloc();
-#endif
-#include <stdio.h>
+
+#include <roken.h>
 
 #include "lwp.h"
 #include "lock.h"
-#include "preempt.h"
-#include <afs/assert.h>
 
 #define DEFAULT_READERS        5
 
@@ -37,71 +28,73 @@ extern char *calloc();
 
 /* The shared queue */
 typedef struct QUEUE {
-    struct QUEUE       *prev, *next;
-    char               *data;
-    struct Lock                lock;
+    struct QUEUE *prev, *next;
+    char *data;
+    struct Lock lock;
 } queue;
 
-queue *init()
+queue *
+init(void)
 {
     queue *q;
 
-    q = (queue *) malloc(sizeof(queue));
-    q -> prev = q -> next = q;
-    return(q);
+    q = malloc(sizeof(queue));
+    q->prev = q->next = q;
+    return (q);
 }
 
-char empty(q)
-    queue *q;
+char
+empty(queue *q)
 {
     return (q->prev == q && q->next == q);
 }
 
-void insert(queue *q, char *s)
+void
+insert(queue * q, char *s)
 {
     queue *new;
 
-    new = (queue *) malloc(sizeof(queue));
-    new -> data = s;
-    new -> prev = q -> prev;
-    q -> prev -> next = new;
-    q -> prev = new;
-    new -> next = q;
+    new = malloc(sizeof(queue));
+    new->data = s;
+    new->prev = q->prev;
+    q->prev->next = new;
+    q->prev = new;
+    new->next = q;
 }
 
-char *Remove(q)
-    queue *q;
+char *
+Remove(queue *q)
 {
     queue *old;
     char *s;
 
     if (empty(q)) {
        printf("Remove from empty queue");
-       assert(0);
+       exit(0);
     }
 
-    old = q -> next;
-    q -> next = old -> next;
-    q -> next -> prev = q;
-    s = old -> data;
+    old = q->next;
+    q->next = old->next;
+    q->next->prev = q;
+    s = old->data;
     free(old);
-    return(s);
+    return (s);
 }
-\f
+
 queue *q;
 
-int asleep;    /* Number of processes sleeping -- used for
-                  clean termination */
+int asleep;                    /* Number of processes sleeping -- used for
+                                * clean termination */
 
-static int read_process(id)
-    int id;
+static void *
+read_process(void *arg)
 {
-    printf("\t[Reader %d]\n", id);
-    LWP_DispatchProcess();             /* Just relinquish control for now */
+    int *id = (int *) arg;
+    printf("\t[Reader %d]\n", *id);
+    LWP_DispatchProcess();     /* Just relinquish control for now */
 
-    PRE_PreemptMe();
     for (;;) {
-        register int i;
+       int i;
 
        /* Wait until there is something in the queue */
        asleep++;
@@ -112,20 +105,18 @@ static int read_process(id)
            ObtainReadLock(&q->lock);
        }
        asleep--;
-       for (i=0; i<10000; i++) ;
-       PRE_BeginCritical();
-       printf("[%d: %s]\n", id, Remove(q));
-       PRE_EndCritical();
+       for (i = 0; i < 10000; i++);
+       printf("[%d: %s]\n", *id, Remove(q));
        ReleaseReadLock(&q->lock);
        LWP_DispatchProcess();
     }
     return 0;
 }
 
-static int write_process()
+static void *
+write_process(void *dummy)
 {
-    static char *messages[] =
-    {
+    static char *messages[] = {
        "Mary had a little lamb,",
        "Its fleece was white as snow,",
        "And everywhere that Mary went,",
@@ -171,10 +162,9 @@ static int write_process()
     char **mesg;
 
     printf("\t[Writer]\n");
-    PRE_PreemptMe();
 
     /* Now loop & write data */
-    for (mesg=messages; *mesg!=0; mesg++) {
+    for (mesg = messages; *mesg != 0; mesg++) {
        ObtainWriteLock(&q->lock);
        insert(q, *mesg);
        ReleaseWriteLock(&q->lock);
@@ -195,14 +185,14 @@ static int write_process()
 
 #include "AFS_component_version_number.c"
 
-main(argc, argv)
-   int argc; char **argv;
+int
+main(int argc, char **argv)
 {
     int nreaders, i;
-    afs_int32 interval;        /* To satisfy Brad */
+    PROCESS pid;
     PROCESS *readers;
+    int *readerid;
     PROCESS writer;
-    struct timeval tv;
 
     printf("\n*Readers & Writers*\n\n");
     setbuf(stdout, 0);
@@ -214,14 +204,10 @@ main(argc, argv)
        sscanf(*++argv, "%d", &nreaders);
     printf("[There will be %d readers]\n", nreaders);
 
-    interval = (argc >= 3 ? atoi(*++argv)*1000 : 50000);
-
-    if (argc == 4) lwp_debug = 1;
-    LWP_InitializeProcessSupport(0, (PROCESS*)&i);
+    if (argc == 4)
+       lwp_debug = 1;
+    LWP_InitializeProcessSupport(0, &pid);
     printf("[Support initialized]\n");
-    tv.tv_sec = 0;
-    tv.tv_usec = interval;
-    PRE_InitPreempt(&tv);
 
     /* Initialize queue */
     q = init();
@@ -233,9 +219,12 @@ main(argc, argv)
     /* Now create readers */
     printf("[Creating Readers...\n");
     readers = (PROCESS *) calloc(nreaders, sizeof(PROCESS));
-    for (i=0; i<nreaders; i++)
-       LWP_CreateProcess(read_process, STACK_SIZE, 0, (void*)i, "Reader",
-                         &readers[i]);
+    readerid = calloc(nreaders, sizeof(i));
+    for (i = 0; i < nreaders; i++) {
+       readerid[i] = i;
+       LWP_CreateProcess(read_process, STACK_SIZE, 0, (void *)&readerid[i],
+                         "Reader", &readers[i]);
+    }
     printf("done]\n");
 
     printf("\t[Creating Writer...\n");
@@ -243,9 +232,12 @@ main(argc, argv)
     printf("done]\n");
 
     /* Now loop until everyone's done */
-    while (asleep != nreaders+1) LWP_DispatchProcess();
+    while (asleep != nreaders + 1)
+       LWP_DispatchProcess();
     /* Destroy the readers */
-    for (i=nreaders-1; i>=0; i--) LWP_DestroyProcess(readers[i]);
+    for (i = nreaders - 1; i >= 0; i--)
+       LWP_DestroyProcess(readers[i]);
     printf("\n*Exiting*\n");
-}
 
+    exit(0);
+}