/*
 * tasks.c
 *
 * Written by Vicente Esteve LLoret <viesllo@inf.upv.es>
 * Copyright (C) Dec, 2002 OCERA Consortium.
 *
 * 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 version 2.
 *
 * Function init_tasks is called from init.c and it's used to create and
 * initializate tasks.
 */

#include <deblin/vhal.h>
#include <time.h>
#include <pthread.h>
#include <rtl_ipc.h>
#include <semaphore.h>
#include <rtl_barrier.h>
#include <rtl_conf.h>
#include <unistd.h>

extern pthread_t thread1;
extern pthread_t thread2;
extern pthread_t thread3;
extern pthread_t thread4;
extern pthread_t thread5;
extern pthread_t thread6;
extern pthread_t thread7;
extern pthread_t thread8;
extern pthread_t thread9;
extern pthread_t thread10;
extern pthread_t thread11;

#if CONFIG_OC_PBARRIERS 
extern pthread_barrier_t   barrier; // barrier synchronization object
extern pthread_barrierattr_t barrier_attr;
#endif

#if _RTL_POSIX_SEMS 
extern sem_t sem;
#endif

#if _RTL_POSIX_MUTEXS 
static pthread_mutex_t mutex;
#endif

void *task1(void *arg);
void *task2(void *arg);
void *task3(void *arg);
void *task4(void *arg);
void *task5(void *arg);
void *task6(void *arg);
void *task7(void *arg);
void *task8(void *arg);
void *task9(void *arg);
void *task10(void *arg);
void *task11(void *arg);

#define PERIODIC_EXAMPLE 0
#define BARRIERS_EXAMPLE 0
#define MUTEX_EXAMPLE    0
#define SEM_EXAMPLE      0
#define MPROT_EXAMPLE    0
#define POSIXIO_EXAMPLE  1
#define TERMINAL_EXAMPLE 1

unsigned long mprot_prueba;

void init_tasks(void)
{
#if CONFIG_OC_PBARRIERS 
 pthread_barrierattr_init(&barrier_attr);
 pthread_barrier_init (&barrier, &barrier_attr, 2);
 pthread_barrierattr_destroy(&barrier_attr);
#endif
#if _RTL_POSIX_SEMS
 sem_init (&sem, 1, 1);
#endif

#if _RTL_POSIX_MUTEXS
 pthread_mutex_init (&mutex, 0);
#endif
 
// rt_sem_init (&sem, RT_SEM_BINARY, 1);
//

  // PERIODIC THREAD EXAMPLE 
 if (PERIODIC_EXAMPLE) {
   pthread_create(&thread1,NULL,task1,0);
   pthread_create(&thread2,NULL,task2,0);
 };
  // BARRIERS EXAMPLE
 if (BARRIERS_EXAMPLE) {
   pthread_create(&thread3,NULL,task3,0);
   pthread_create(&thread4,NULL,task4,0);
 };
  // MUTEX EXAMPLE
 if (MUTEX_EXAMPLE) {
   pthread_create(&thread5,NULL,task5,0);
   pthread_create(&thread6,NULL,task6,0);
 };
  // SEMAPHORE EXAMPLE  
 if (SEM_EXAMPLE) {
   pthread_create(&thread7,NULL,task7,0);
   pthread_create(&thread8,NULL,task8,0);
 };
  // MPROTECT EXAMPLE
 if (MPROT_EXAMPLE) {
   pthread_create(&thread9,NULL,task9,0);
 };
  // POSIXIO EXAMPLE
 if (POSIXIO_EXAMPLE) {
   pthread_create(&thread10,NULL,task10,0); 
 };
  // TERMINAL EXAMPLE
 if (TERMINAL_EXAMPLE) {
   pthread_create(&thread11,NULL,task11,0); 
 };
 
}


