/***************************************************************************
 *   Copyright (C) 2006 by Brian Lauber   *
 *   bml8@case.edu   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#define REENTRANT

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>



struct shared
{
  int Cookies;
  int CookiesEatenByFatOne;
  sem_t Jar;     // Someone has their hand in the jar
  sem_t Refill;  // The Fonz needs to refill the jar
};


void TakeCookie(char *name, struct shared *s)
{
  while(s->Cookies == 0) // AHH!! No cookies!
  {
    sem_post( &(s->Refill) );
    sem_wait( &(s->Jar)    );
  }

  printf("%s takes a cookies (%i cookies left)\n", name, --(s->Cookies));
}


void* TheThinOne(struct shared *s)
{
  while(1)
  {
    sem_wait(&(s->Jar));
    if(s->CookiesEatenByFatOne >= 2)
    {
      TakeCookie("The Thin One", s);
      s->CookiesEatenByFatOne = 0;
    }
    sem_post(&(s->Jar));
    sleep(1);
  }
}

void* TheFatOne(struct shared *s)
{
  while(1)
  {
    sem_wait(&(s->Jar));
    TakeCookie("The Fat One", s);
    ++(s->CookiesEatenByFatOne);
    sem_post(&(s->Jar));
    // Munch munch munch
    sleep(1);
  }
}

void* TheFonz(struct shared *s)
{
  while(1)
  {
    sem_wait(&(s->Refill));
    printf("AAAyyyyy!!!  More cookies Mrs. C!!\n");
    s->Cookies = 10; // Yay!
    sem_post(&(s->Jar));
  }
}



int main(int argc, char *argv[])
{
  pthread_t ID[3];
  struct shared s;

  s.Cookies = 10;              // Put 10 cookies in the jar
  s.CookiesEatenByFatOne = 0;  // TheFatOne hasn't eaten any yet
  sem_init(&(s.Jar), 0, 1);    // Anyone can reach for jar
  sem_init(&(s.Refill), 0, 0); // Jar does not need to be refilled

  // Create 3 threads
  pthread_create(&ID[0], NULL, TheFatOne,  &s);
  pthread_create(&ID[1], NULL, TheThinOne, &s);
  pthread_create(&ID[2], NULL, TheFonz,    &s);

  sleep(60); // Let the threads do their thing
  return EXIT_SUCCESS;
}
