///////////////////////////////////////////////////////////////////////////////
//
//  bbuffer.cpp
//
//  Written by N. Howe and E. Altieri
//
//  This program demonstrates a race condition in a simple buffer
//  with one reader and one writer.  In order to exaggerate the
//  problem, a random delay is inserted between reading and writing
//  the critical variable.  This simulates conditions of heavy
//  processor load.
//
///////////////////////////////////////////////////////////////////////////////

// included header files
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
// FILL IN
using namespace std;

///////////////////////////////////////////////////////////////////////////////

// global preprocessor constants
#define BUFSIZE 10       // size of simulated buffer
#define N_ITER 100       // total number of reads and writes
#define MAX_DELAY 20000  // maximum length of sleep

///////////////////////////////////////////////////////////////////////////////

// Global static variables are shared between threads
static int buf[BUFSIZE];       // buffer
static int first;              // beginning of filled portion
static int len;                // length of filled portion

///////////////////////////////////////////////////////////////////////////////

// forward function declarations
static void * consumer(void* ptr);
static void * producer(void* ptr);

///////////////////////////////////////////////////////////////////////////////
//
//  main() spawns two threads, a reader and a writer.
//  It waits for them to finish, and then prints out the number
//  of items left in the buffer (which should be zero in the absence
//  of any race conditions).  Note that the reader thread may hang
//  if it runs out of items in the buffer.
//

int main() {
  // declare thread data structures here
  // FILL IN

  // spawn reader and writer threads
  // FILL IN

  // wait for them to finish
  // FILL IN

  // examine results
  cout << "Number of items left in buffer:  " << len << endl;
} // main()

///////////////////////////////////////////////////////////////////////////////
//
// rndsleep() sleeps for a random number of microseconds
// between 0 and MAXDELAY.  This simulates time spent on other processes.
//

static void rndsleep(void)
{
  unsigned long t;
  t = (unsigned long) ((1.0 * rand() / RAND_MAX) * MAX_DELAY);
  usleep(t);
} // rndsleep()

///////////////////////////////////////////////////////////////////////////////
//
//  read_item() reads one item from the buffer.
//

int read_item() {
  int result = buf[first];
  first = (first+1)%BUFSIZE;
  int tmp = len;
  rndsleep();  // make race conditions more common
  len = tmp-1;
  return result;
} // read_item()

///////////////////////////////////////////////////////////////////////////////
//
//  consumer() is the consumer thread.
//  It reads N_ITER items from the buffer, as long as it is not empty.
//
//  ptr is a required argument for thread startup, but is not used.
//

static void* consumer(void* ptr) {
  int item;

  for (int nread = 0; nread < N_ITER; nread++) {
    while (len == 0);  // delay if buffer is empty
    item = read_item();
    cout << "Read buffer item:  " << item << endl;
  }

  // clean up & exit thread
  // FILL IN
  return NULL;
} // consumer()

///////////////////////////////////////////////////////////////////////////////
//
//  write_item() writes one item to the buffer.
//
//  item is the value to write
//

void write_item(int item) {
  buf[(first+len)%BUFSIZE] = item;
  int tmp = len;
  rndsleep();  // make race conditions more common
  len = tmp+1;
} // write_item()

///////////////////////////////////////////////////////////////////////////////
//
// producer() is the producer thread.
// It writes N_ITER items to the buffer, as long as it is not full.
//
//  ptr is a required argument for thread startup, but is not used.
//

static void* producer(void* ptr) {
  // write N_ITER items to buffer
  for (int nwritten = 0; nwritten < N_ITER; nwritten++) {
    while (len == BUFSIZE);
    cout << "Writing buffer item:  " << nwritten << endl;
    write_item(nwritten);
  }

  // clean up & exit thread
  // FILL IN
  return NULL;
} // producer()

///////////////////////////////////////////////////////////////////////////////
