#include <stdio.h>
#include "iLAbstractPlatform.h"
#include "iLThreads.h"
#include "iLSemaphore.h"
#include <unistd.h>


iLSemaphore g_sem1;
iLSemaphore g_sem2;


iLResult run1(void)
{
  iLSemaphore_Wait(&g_sem1);
  printf(" running 1  \n");
  sleep(2);
  printf(" running 3  \n");
  sleep(2);
  iLSemaphore_Signal(&g_sem1, 1);
  return iLR_OK;
}


iLResult run2(void)
{
  iLSemaphore_Wait(&g_sem2);
  printf(" running 2  \n");
  sleep(2);
  printf(" running 4  \n");
  sleep(2);
  iLSemaphore_Signal(&g_sem2, 1);
  return iLR_OK;
}


int main()
{
  iLSemaphore_Create(&g_sem1, 0, 100);
  iLSemaphore_Create(&g_sem2, 0, 100);

  iLThread thread1, thread2;
  iLThread_Create(&thread1, run1);
  iLThread_Create(&thread2, run2);

  printf("Detected the following:\n");
  printf("  Architecture:      " IL_ARCHITECTURE "\n");
  printf("  Compiler:          " IL_COMPILER "\n");
  printf("  Operating System:  " IL_OPERATING_SYSTEM "\n");
  printf("  Platform:          " IL_PLATFORM "\n");
  printf("  Version:           " IL_VERSION "\n");

  sleep(1);
  iLSemaphore_Signal(&g_sem1, 1);
  sleep(1);
  iLSemaphore_Signal(&g_sem2, 1);

  iLSemaphore_Wait(&g_sem1);
  printf(" running 5  \n");
  iLSemaphore_Wait(&g_sem2);
  printf(" running 6  \n");

  iLThread_Destroy(&thread1);
  iLThread_Destroy(&thread2);

  return 1;
}


