#include <time.h>
#include <pthread.h>
#include <rtl_ipc.h>
#include <semaphore.h>
#include <rtl_barrier.h>
#include <rtl_printf.h>
#include <rtl_conf.h>
#include <unistd.h>
#include <arch/cas.h>
#include "examples.h"

#if QUAJECT_EXAMPLE

#define WITHCAS   0

#if _RTL_POSIX_MUTEXS 
pthread_mutex_t withoutcas;
#endif



static unsigned long valor11=0x0;
pthread_t thread12;
pthread_t thread13;



unsigned long long mp_protected=0x0;
#if WITHCAS
void *task12(void *arg)
{
  pthread_make_periodic_np (pthread_self(), gethrtime(),3575ULL);
  while(1)
  {
    pthread_wait_np ();
    while(!cas2(&mp_protected,mp_protected,mp_protected+1));
  };
  return 0;
};

void *task13(void *arg)
{

  while(1)
  {
    while(!cas2(&mp_protected,mp_protected,mp_protected+1));
    if (mp_protected==0x5ffffff) {
    //     __asm("cli");
infinito:	 
	 goto infinito;
         };
  };
  return 0;
};
#else

void *task12(void *arg)
{
  pthread_make_periodic_np (pthread_self(), gethrtime(),3575ULL);
  while(1)
  {
    
   pthread_wait_np ();
   
#if _RTL_POSIX_MUTEXS 
   pthread_mutex_lock(&withoutcas);
   mp_protected++;
   pthread_mutex_unlock(&withoutcas);
#endif   
  };
  return 0;
};

void *task13(void *arg)
{
  while(1)
  {
#if _RTL_POSIX_MUTEXS 
   pthread_mutex_lock(&withoutcas);
   mp_protected++;
   pthread_mutex_unlock(&withoutcas);
#endif   
   if (mp_protected==0x5ffffff) {
//         __asm("cli");
infinito:	 
	 goto infinito;
         };
 
  };
  return 0;
};
#endif





#endif
