Locks in UPC

Phil Merkey

Introduction

<locks1.c>= [D->]
// Lock Example 1   (No Locks)
#include<stdio.h>
#include<upc.h>

main()
{
   char sleepcmd[80];

   // simulate random amount of work
   sprintf(sleepcmd, "sleep %d", (THREADS-MYTHREAD) % 3 );
   system(sleepcmd);

   printf("Thread %2d: ", MYTHREAD);
   system("date");
}  

<locks1.c>+= [<-D->]
// Lock Example (Serializing the Critical Section)
#include<stdio.h>
#include<upc.h>

main()
{
   int t;
   char sleepcmd[80];

   // simulate random amount of work
   sprintf(sleepcmd, "sleep %d", (THREADS-MYTHREAD) % 3 );
   system(sleepcmd);
  
   for(t=0; t<THREADS; t++) {
     upc_barrier;
     if( t != MYTHREAD ) continue;
      printf("Thread %2d: ", MYTHREAD);
      system("date");
   }
}

<locks1.c>+= [<-D]
// Lock Example (Locking Critical Section)
#include<stdio.h>
#include<upc.h>

upc_lock_t *instr_lock;

main()
{
   char sleepcmd[80];

   // allocate and initialize lock
   instr_lock = upc_all_lock_alloc();
   upc_lock_init( instr_lock );

   // simulate random amount of work
   sprintf(sleepcmd, "sleep %d", (THREADS-MYTHREAD) % 3 );
   system(sleepcmd);

   upc_lock (instr_lock );
      printf("Thread %2d: ", MYTHREAD);
      system("date");
   upc_unlock (instr_lock );
}