#include "func_test_lock.h"
#include <sys/lock.h>
__LOCK_INIT(static,lock1);
static int val1 = 0;
void test_lock1 ()
{
  __lock_init    (lock1);
  print((unsigned int)lock1.mutex);
    
  while (val1 < 100)
    {
      __lock_acquire (lock1);
      print(val1);
      val1 ++;
      __lock_release (lock1);
    }

  __lock_close   (lock1);
}

__LOCK_INIT(static,lock2);
static int val2 = 0;
void test_lock2 ()
{
  __lock_init    (lock2);
  print((unsigned int)lock2.mutex);
  while (val2 < 100)
    {
      if (__lock_try_acquire (lock2) == 1)
	{
	  print(val2);
	  val2 ++;
	  __lock_release (lock2);
	}
    }
  
  __lock_close   (lock2);
}

__LOCK_INIT_RECURSIVE(static,lock_recursive1);
static int val_recursive1 = 0;
void test_lock_recursive1 ()
{
  __lock_init_recursive    (lock_recursive1);
  print((unsigned int)lock_recursive1.mutex);
  
  while (val_recursive1 < 100)
    {
      __lock_acquire_recursive (lock_recursive1);
      __lock_acquire_recursive (lock_recursive1);
      print(val_recursive1);
      val_recursive1 ++;
      __lock_release_recursive (lock_recursive1);
      print(val_recursive1);
      val_recursive1 ++;
      __lock_release_recursive (lock_recursive1);
    }

  __lock_close_recursive   (lock_recursive1);
}

__LOCK_INIT_RECURSIVE(static,lock_recursive2);
static int val_recursive2 = 0;
void test_lock_recursive2 ()
{
  __lock_init_recursive    (lock_recursive2);
  print((unsigned int)lock_recursive2.mutex);  

  while (val_recursive2 < 100)
    {
      if (__lock_try_acquire_recursive (lock_recursive2) == 1)
	{
	  if (__lock_try_acquire_recursive (lock_recursive2) == 1)
	    {
	      print(val_recursive2);
	      val_recursive2 ++;
	      __lock_release_recursive (lock_recursive2);
	    }
	  print(val_recursive2);
	  val_recursive2 ++;
	  __lock_release_recursive (lock_recursive2);
	}
    }
  
  __lock_close_recursive   (lock_recursive2);
}
