/*
 * Implementation of automatic protothreads initialization and scheduling
 */

#include "pt_task.h"
#include "stdio.h"

static int task_start_sch_entry(pt_t  *p)
{
    PT_BEGIN(p);

    PT_END(p);
}
PT_TASK_START(task_start_sch_entry);


static int task_end_sch_entry(pt_t  *p)
{
    PT_BEGIN(p);

    

    PT_END(p);
}
PT_TASK_END(task_end_sch_entry);

bool pt_task_is_active(pt_task_t *task)
{
  if( ( task->status  < PT_WAITING ) && 
      (task->status< PT_EXITED) ){
  
    return 1;
      
  }else{
  
    return 0;
  }
}

/*
 * Schedule all registered task functions
 */
void pt_task_schedule(void)
{
    pt_task_t* start = (pt_task_t*)  PT_TASK_OBJ_START;
    pt_task_t* end = (pt_task_t*)    PT_TASK_OBJ_END;

    for (pt_task_t* task = start; task < end; task++)
    {
        if (task->fn_entry && task->pt && task->status < PT_ENDED)
        {
            task->status = task->fn_entry(task->pt);
        }
    }
}


bool pt_task_all_idle(void)
{
    bool is_idle = true; 
    pt_task_t* start = (pt_task_t*)PT_TASK_OBJ_START; 
    pt_task_t* end = (pt_task_t*)PT_TASK_OBJ_END; 
    for (pt_task_t* task = start; task < end; task++) { 
        if (task->pt && pt_task_is_active(task)) { 
            is_idle = false; 
            break; 
        } 
    } 
    return is_idle; 
}

