Exemple #1
0
//----------------------------------------------------------------------------
//  Structure's Field mutex->id_mutex have to be set to 0
//----------------------------------------------------------------------------
int tn_mutex_create(TN_MUTEX * mutex,
                    int attribute,
                    int ceil_priority)
{

#if TN_CHECK_PARAM
   if(mutex == NULL)
      return TERR_WRONG_PARAM;
   if(mutex->id_mutex != 0) //-- no recreation
      return TERR_WRONG_PARAM;
   if(attribute != TN_MUTEX_ATTR_CEILING && attribute != TN_MUTEX_ATTR_INHERIT)
      return TERR_WRONG_PARAM;
   if(attribute == TN_MUTEX_ATTR_CEILING &&
         (ceil_priority < 1 || ceil_priority > TN_NUM_PRIORITY - 2))
      return TERR_WRONG_PARAM;
#endif

   queue_reset(&(mutex->wait_queue));
   queue_reset(&(mutex->mutex_queue));
   queue_reset(&(mutex->lock_mutex_queue));

   mutex->attr          = attribute;
   mutex->holder        = NULL;
   mutex->ceil_priority = ceil_priority;
   mutex->cnt           = 0;
   mutex->id_mutex      = TN_ID_MUTEX;

   return TERR_NO_ERR;
}
Exemple #2
0
//-------------------------------------------------------------------------
// Structure's field dque->id_dque have to be set to 0
//-------------------------------------------------------------------------
int tn_queue_create(TN_DQUE * dque,     //-- Ptr to already existing TN_DQUE
                    void ** data_fifo,  //-- Ptr to already existing array of void * to store data queue entries.Can be NULL
                    int num_entries)    //-- Capacity of data queue(num entries).Can be 0

{

#if TN_CHECK_PARAM
   if(dque == NULL)
      return TERR_WRONG_PARAM;
   if(num_entries < 0 || dque->id_dque == TN_ID_DATAQUEUE)
      return TERR_WRONG_PARAM;
#endif

   queue_reset(&(dque->wait_send_list));
   queue_reset(&(dque->wait_receive_list));

   dque->data_fifo = data_fifo;
   dque->num_entries = num_entries;
   if(dque->data_fifo == NULL)
      dque->num_entries = 0;

   dque->tail_cnt   = 0;
   dque->header_cnt = 0;

   dque->id_dque = TN_ID_DATAQUEUE;

   return  TERR_NO_ERR;
}
Exemple #3
0
//----------------------------------------------------------------------------
void task_set_dormant_state(TN_TCB* task)
{
	     // v.2.7 - thanks to Alexander Gacov, Vyacheslav Ovsiyenko
   queue_reset(&(task->task_queue));
   queue_reset(&(task->timer_queue));

#ifdef USE_MUTEXES

   queue_reset(&(task->mutex_queue));

#endif

   task->pwait_queue = NULL;

   task->priority    = task->base_priority; //-- Task curr priority
   task->task_state  = TSK_STATE_DORMANT;   //-- Task state
   task->task_wait_reason = 0;              //-- Reason for waiting
   task->task_wait_rc = TERR_NO_ERR;

#ifdef USE_EVENTS

   task->ewait_pattern = 0;                 //-- Event wait pattern
   task->ewait_mode    = 0;                 //-- Event wait mode:  _AND or _OR

#endif

   task->data_elem     = NULL;              //-- Store data queue entry,if data queue is full

   task->tick_count    = TN_WAIT_INFINITE;  //-- Remaining time until timeout
   task->wakeup_count  = 0;                 //-- Wakeup request count
   task->suspend_count = 0;                 //-- Suspension count

   task->tslice_count  = 0;
}
Exemple #4
0
void pathfinder_propagate_attractivity (PathFinder *pf,
		Tile *tile,
		gint attractivity,
		gint step,
		gint8 depth)
{
	assert (pf != NULL);
	assert (tile != NULL);
	assert (depth >= 0);

	Queue *input  = queue_reset (pf->queue1);
	Queue *output = queue_reset (pf->queue2);
	Queue *tileset = queue_reset (pf->tileset);

	// Feed the input to start finding neigbours
	queue_push (input, tile);

	// The target tile needs to be part of the final tileset 
	// so the TILE_FLAG_BEING_PROCESSED flag can be removed
	queue_push (output, tile);

	// We set attractivity level when an input tile is consumed
	tile_set_flag (tile, TILE_FLAG_BEING_PROCESSED);
	tile_set_attractivity (tile, max (attractivity, tile_get_attractivity (tile)));

	while (! queue_is_empty (input) && --depth >= 0)
	{
		attractivity += step;
		pathfinder_select_tile_group_set_attractivity (pf,
				input,
				output,
				attractivity);

		// Avoids overflow
		queue_reset (input);

		// Memorize output
		queue_push_queue (tileset, output);

		// To get neighbours for next depth level, reuse by swapping input and
		// output queues
		Queue *tmp = input;
		input = output;
		output = tmp;
	}

	while (! queue_is_empty (tileset))
	{
		Tile *t = queue_pop (tileset);
		tile_unset_flag (t, TILE_FLAG_BEING_PROCESSED);
	}
}
Exemple #5
0
//----------------------------------------------------------------------------
//  Structure's field evf->id_event have to be set to 0
//----------------------------------------------------------------------------
int tn_event_create(TN_EVENT * evf,
                    int attr,              //-- Eventflag attribute
                    unsigned int pattern)  //-- Initial value of the eventflag bit pattern
{

#if TN_CHECK_PARAM
   if(evf == NULL)
      return TERR_WRONG_PARAM;
   if(evf->id_event == TN_ID_EVENT ||
        (((attr & TN_EVENT_ATTR_SINGLE) == 0)  &&
            ((attr & TN_EVENT_ATTR_MULTI) == 0)))
      return TERR_WRONG_PARAM;
#endif

   queue_reset(&(evf->wait_queue));
   evf->pattern = pattern;
   evf->attr = attr;
   if((attr & TN_EVENT_ATTR_CLR) && ((attr & TN_EVENT_ATTR_SINGLE)== 0))
   {
      evf->attr = TN_INVALID_VAL;
      return TERR_WRONG_PARAM;
   }
   evf->id_event = TN_ID_EVENT;

   return TERR_NO_ERR;
}
Exemple #6
0
//----------------------------------------------------------------------------
//   Structure's field sem->id_sem have to be set to 0
//----------------------------------------------------------------------------
int tn_sem_create(TN_SEM * sem,
                  int start_value,
                  int max_val)
{

#if TN_CHECK_PARAM
   if(sem == NULL) //-- Thanks to Michael Fisher
      return  TERR_WRONG_PARAM;
   if(max_val <= 0 || start_value < 0 ||
         start_value > max_val || sem->id_sem != 0) //-- no recreation
   {
      sem->max_count = 0;
      return  TERR_WRONG_PARAM;
   }
#endif

   TN_CHECK_NON_INT_CONTEXT

   queue_reset(&(sem->wait_queue));

   sem->count     = start_value;
   sem->max_count = max_val;
   sem->id_sem    = TN_ID_SEMAPHORE;

   return TERR_NO_ERR;
}
Exemple #7
0
/**
 *
 * @param sem
 * @param start_value
 * @param max_val
 *
 * @return TN_RETVAL
 */
TN_RETVAL tnec_sem_create (TN_SEM_S *sem, TN_UWORD start_value, TN_UWORD max_val)
{
    if (sem         == TN_NULL ||
        max_val     <= 0       ||
        start_value  > max_val
       )
    {
        sem->max_count = 0;
        return TERR_WRONG_PARAM;
    }

    if (sem->id_sem != 0)
        return TERR_EXS;

    if (tn_is_non_task_context())
    {
        return TERR_WCONTEXT;
    }

    queue_reset(&(sem->wait_queue));
    sem->count     = start_value;
    sem->max_count = max_val;
    sem->id_sem    = TN_ID_SEMAPHORE;

    return TERR_NO_ERR;
}
Exemple #8
0
//----------------------------------------------------------------------------
void task_curr_to_wait_action(CDLL_QUEUE * wait_que,
                              int wait_reason,
                              unsigned long timeout)
{
   task_to_non_runnable(tn_curr_run_task);

   tn_curr_run_task->task_state       = TSK_STATE_WAIT;
   tn_curr_run_task->task_wait_reason = wait_reason;
   tn_curr_run_task->tick_count       = timeout;

   //--- Add to the wait queue  - FIFO

   if(wait_que == NULL) //-- Thanks to Vavilov D.O.
   {
      queue_reset(&(tn_curr_run_task->task_queue));
   }
   else
   {
      queue_add_tail(wait_que, &(tn_curr_run_task->task_queue));
      tn_curr_run_task->pwait_queue = wait_que;
   }

   //--- Add to the timers queue

   if(timeout != TN_WAIT_INFINITE)
      queue_add_tail(&tn_wait_timeout_list, &(tn_curr_run_task->timer_queue));
}
Exemple #9
0
void uart_flush_receiver(uint8_t channel)
{
  uart_channel* uartChannel = look_up_uart_channel(channel);

  while (low_level_read_from_uart(uartChannel));

  queue_reset(&uartChannel->receiveQueue);
}
Exemple #10
0
//----------------------------------------------------------------------------
int tn_mutex_delete(TN_MUTEX * mutex)
{
   TN_INTSAVE_DATA

   CDLL_QUEUE * que;
   TN_TCB * task;

#if TN_CHECK_PARAM
   if(mutex == NULL)
      return TERR_WRONG_PARAM;
   if(mutex->id_mutex != TN_ID_MUTEX)
      return TERR_NOEXS;
#endif

   TN_CHECK_NON_INT_CONTEXT

   if(tn_curr_run_task != mutex->holder)
   {
      tn_enable_interrupt();
      return TERR_ILUSE;
   }

   //-- Remove all tasks(if any) from mutex's wait queue

   while(!is_queue_empty(&(mutex->wait_queue)))
   {
      tn_disable_interrupt();

      que  = queue_remove_head(&(mutex->wait_queue));
      task = get_task_by_tsk_queue(que);

    //-- If the task in system's blocked list, remove it

      if(task_wait_complete(task))
      {
         task->task_wait_rc = TERR_DLT;
         tn_enable_interrupt();
         tn_switch_context();
      }
   }

   if(tn_chk_irq_disabled() == 0)
      tn_disable_interrupt();

   if(mutex->holder != NULL)  //-- If the mutex is locked
   {
      do_unlock_mutex(mutex);
      queue_reset(&(mutex->mutex_queue));
   }
   mutex->id_mutex = 0; // Mutex not exists now

   tn_enable_interrupt();

   return TERR_NO_ERR;
}
Exemple #11
0
TN_RETVAL tnnc_mutex_create (TN_MUTEX_S *mutex, TN_UWORD attribute, TN_UWORD ceil_priority)
{
/*
    Not check parameter error

    if (mutex == TN_NULL)
        return TERR_WRONG_PARAM;

    if (attribute != TN_MUTEX_ATTR_CEILING && attribute != TN_MUTEX_ATTR_INHERIT)
        return TERR_WRONG_PARAM;

    if (attribute == TN_MUTEX_ATTR_CEILING &&
        (ceil_priority < 1 || ceil_priority > TN_NUM_PRIORITY-2)
       )
    {
        return TERR_WRONG_PARAM;
    }
*/

    if (mutex->id_mutex != 0)
        return TERR_EXS;

    /*
    if (tn_is_user_crit_sect())
    {
        return TERR_WCONTEXT;
    }
    */

    queue_reset(&(mutex->wait_queue));
    queue_reset(&(mutex->mutex_queue));
    queue_reset(&(mutex->lock_mutex_queue));

    mutex->attr          = attribute;
    mutex->holder        = TN_NULL;
    mutex->ceil_priority = ceil_priority;
    mutex->cnt           = 0;
    mutex->id_mutex      = TN_ID_MUTEX;

    return TERR_NO_ERR;
}
Exemple #12
0
Queue *pathfinder_select_in_range_sq (PathFinder *pf,
		Tile *tile,
		guint distance_sq)
{
	assert (pf != NULL);
	assert (tile != NULL);

	queue_reset (pf->tileset);	
	pathfinder_select_if_in_range (pf,
			pf->tileset,
			tile,
			tile,
			distance_sq);
	return pf->tileset;
}
Exemple #13
0
int main() {
  const int SIZE = 6;
  QUEUE queue;
  queue.size = SIZE;
  queue_init(&queue);
  char isRunning = 1;
  while (isRunning) {
    int choice;
    char * data;
    printf("\n1. 삽입\n2. 꺼내기\n3. 출력\n4. 전부 삭제\n5. 끝\n입력: ");
    scanf("%d", &choice);
    getchar();
    switch (choice) {
      case 1:
        if (queue_isFull(&queue)) {
          printf("큐가 꽉 찼습니다\n");
          break;
        }
        printf("데이터를 입력해주세요: ");
        data = (char *) malloc(256);
        gets(data);
        queue_add(&queue, data);
        break;
      case 2:
        if (queue_isEmpty(&queue)) {
          printf("큐가 비었습니다\n");
          break;
        }
        data = queue_remove(&queue);
        printf("읽은 데이터: %s\n", data);
        free(data);
        break;
      case 3:
        queue_print(&queue);
        break;
      case 4:
        // This doesn't free the queue, but whatever.
        queue_reset(&queue);
        break;
      case 5:
        isRunning = 0;
        break;
    }
  }
}
void test_push_pop(void)
{
  queue_t q;
  
  queue_init(&q);
  
  queue_push(&q, "your");
  queue_push(&q, "muffin");
  queue_push(&q, "stink");
  
  assert_str_equal("your", queue_pop(&q));
  assert_str_equal("muffin", queue_pop(&q));
  assert_str_equal("stink", queue_pop(&q));

  assert_equal(NULL, queue_pop(&q));
  
  queue_reset(&q);
}
Exemple #15
0
void queue_init(queue_t* q, uint8_t *buffer, uint16_t buffer_length) {
    q->buffer = buffer;
    q->capacity = buffer_length;
    queue_reset(q);
}
Exemple #16
0
/**
 *
 * @param timer_task_stack
 * @param timer_task_stack_size
 * @param idle_task_stack
 * @param idle_task_stack_size
 * @param app_in_cb
 * @param cpu_int_en
 * @param idle_user_cb
 */
void TN_NORETURN tnec_start_system(TN_UWORD  *timer_task_stack,
                                   TN_UWORD   timer_task_stack_size,
                                   TN_UWORD  *idle_task_stack,
                                   TN_UWORD   idle_task_stack_size,
                                   void     (*app_in_cb)(void),
                                   void     (*cpu_int_en)(void),
                                   void     (*idle_user_cb)(void)
                                  )
{
    TN_WORD i;

    /* ToDo - initialize the sys log (if enabled) */

    for (i = 0; i < TN_NUM_PRIORITY; i++)
    {
        queue_reset(&(tn_ready_list[i]));
        tn_tslice_ticks[i] = NO_TIME_SLICE;
    }

    queue_reset(&tn_create_queue);
    tn_created_tasks_qty      = 0;

    tn_system_state           = TN_ST_STATE_NOT_RUN;

    tn_enable_switch_context  = TN_TRUE;
    tn_ready_to_run_bmp       = 0;

    tn_context_switch_request = TN_FALSE;
    tn_sys_context            = TN_CONTEXT_TASK;

    tn_next_task_to_run       = TN_NULL;
    tn_curr_run_task          = TN_NULL;

    queue_reset(&tn_locked_mutexes_list);
    queue_reset(&tn_blocked_tasks_list);
    queue_reset(&tn_wait_timeout_list);

    tnec_task_create((TN_TCB_S*)&tn_timer_task,
                     tn_timer_task_func,
                     0,
                     timer_task_stack,
                     timer_task_stack_size,
                     TN_NULL,
                     TN_TASK_TIMER
                    );

    tnec_task_create((TN_TCB_S*)&tn_idle_task,
                     tn_idle_task_func,
                     (TN_NUM_PRIORITY - 1),
                     idle_task_stack,
                     idle_task_stack_size,
                     TN_NULL,
                     TN_TASK_IDLE
                    );

    task_to_runnable(&tn_idle_task);
    task_to_runnable(&tn_timer_task);

    tn_curr_run_task            = &tn_idle_task;

    appl_init_callback          = app_in_cb;
    cpu_interrupt_enbl_callback = cpu_int_en;
    idle_user_func_callback     = idle_user_cb;

    tn_start_exe();

    for(;;);

/*    for (;;);*/
}
Exemple #17
0
//----------------------------------------------------------------------------
//  Structure's field fmp->id_id_fmp have to be set to 0
//----------------------------------------------------------------------------
int tn_fmem_create(TN_FMP * fmp,
                     void * start_addr,
                     unsigned int block_size,
                     int num_blocks)
{
   void ** p_tmp;
   unsigned char * p_block;
   unsigned long i,j;

#if TN_CHECK_PARAM
   if(fmp == NULL)
      return TERR_WRONG_PARAM;
   if(fmp->id_fmp == TN_ID_FSMEMORYPOOL)
      return TERR_WRONG_PARAM;
#endif

   if(start_addr == NULL || num_blocks < 2 || block_size < sizeof(int))
   {
      fmp->fblkcnt = 0;
      fmp->num_blocks = 0;
      fmp->id_fmp = 0;
      fmp->free_list = NULL;
      return TERR_WRONG_PARAM;
   }

   queue_reset(&(fmp->wait_queue));

  //-- Prepare addr/block aligment

   i = ((unsigned long)start_addr + (TN_ALIG -1)) & (~(TN_ALIG-1));
   fmp->start_addr  = (void*)i;
   fmp->block_size = (block_size + (TN_ALIG -1)) & (~(TN_ALIG-1));

   i = (unsigned long)start_addr + block_size * num_blocks;
   j = (unsigned long)fmp->start_addr + fmp->block_size * num_blocks;

   fmp->num_blocks = num_blocks;

   while(j > i)  //-- Get actual num_blocks
   {
      j -= fmp->block_size;
      fmp->num_blocks--;
   }

   if(fmp->num_blocks < 2)
   {
      fmp->fblkcnt    = 0;
      fmp->num_blocks = 0;
      fmp->free_list  = NULL;
      return TERR_WRONG_PARAM;
   }

  //-- Set blocks ptrs for allocation -------

   p_tmp = (void **)fmp->start_addr;
   p_block  = (unsigned char *)fmp->start_addr + fmp->block_size;
   for(i = 0; i < (fmp->num_blocks - 1); i++)
   {
      *p_tmp  = (void *)p_block;  //-- contents of cell = addr of next block
      p_tmp   = (void **)p_block;
      p_block += fmp->block_size;
   }
   *p_tmp = NULL;          //-- Last memory block first cell contents -  NULL

   fmp->free_list = fmp->start_addr;
   fmp->fblkcnt   = fmp->num_blocks;

   fmp->id_fmp = TN_ID_FSMEMORYPOOL;

  //-----------------------------------------

   return TERR_NO_ERR;
}
Exemple #18
0
void queue_init(QUEUE * queue) {
  queue_reset(queue);
  queue->data = (char **) malloc(sizeof(char *) * queue->size);
}
Exemple #19
0
/**
 *
 * @param fmp
 * @param start_addr
 * @param block_size
 * @param num_blocks
 *
 * @return TN_RETVAL
 */
TN_RETVAL tnnc_fmem_create (TN_FMP_S *fmp, void *start_addr, TN_UWORD block_size, TN_UWORD num_blocks)
{
    void          **p_tmp;
    TN_UCHAR    *p_block;
    TN_UWORD    i, j;
/*
    Not check parameter error

    if (fmp == TN_NULL)
        return TERR_WRONG_PARAM;

    if (start_addr == TN_NULL || num_blocks < 2 || block_size < sizeof(TN_WORD))
    {
        fmp->fblkcnt    = 0;
        fmp->num_blocks = 0;
        fmp->id_fmp     = 0;
        fmp->free_list  = TN_NULL;
        return TERR_WRONG_PARAM;
    }
*/

    if (fmp->id_fmp != 0)
        return TERR_EXS;

    queue_reset(&(fmp->wait_queue));

    /* Prepare addr/block aligment */

    i = (((TN_UWORD)start_addr + (TN_ALIG -1))/TN_ALIG) * TN_ALIG;

    fmp->start_addr = (void*)i;
    fmp->block_size = ((block_size + (TN_ALIG -1))/TN_ALIG) * TN_ALIG;

    i = (TN_UWORD)start_addr      + block_size * num_blocks;
    j = (TN_UWORD)fmp->start_addr + fmp->block_size * num_blocks;

    fmp->num_blocks = num_blocks;

    while (j > i)
    {
        /* Get actual num_blocks */
        j -= fmp->block_size;
        fmp->num_blocks--;
    }

    /* TODO - pass this error */

    if (fmp->num_blocks < 2)
    {
        fmp->fblkcnt    = 0;
        fmp->num_blocks = 0;
        fmp->free_list  = TN_NULL;
        return TERR_WRONG_PARAM;
    }

    /* Set blocks ptrs for allocation */

    p_tmp    = (void **)fmp->start_addr;
    p_block  = (TN_UCHAR *)fmp->start_addr + fmp->block_size;

    for (i = 0; i < (fmp->num_blocks - 1); i++)
    {
        *p_tmp   = (void *)p_block;  /* contents of cell = addr of next block */
        p_tmp    = (void **)p_block;
        p_block += fmp->block_size;
    }

    *p_tmp = TN_NULL;          /* Last memory block first cell contents -  NULL */

    fmp->free_list = fmp->start_addr;
    fmp->fblkcnt   = fmp->num_blocks;
    fmp->id_fmp    = TN_ID_FSMEMORYPOOL;

    return TERR_NO_ERR;
}
Exemple #20
0
static void queue_init(struct tsp_queue *q_ptr, char *name)
{
	strcpy(q_ptr->name, name);
	pthread_mutex_init(&q_ptr->mutex, 0);
	queue_reset(q_ptr);
}
Exemple #21
0
Fichier : tn.c Projet : ADTL/AFGUI
//----------------------------------------------------------------------------
// TN main function (never return)
//----------------------------------------------------------------------------
void tn_start_system(void)
{
   int i;

   //-- Clear/set all globals (vars, lists, etc)

   for(i=0;i < TN_NUM_PRIORITY;i++)
   {
      queue_reset(&(tn_ready_list[i]));
      tn_tslice_ticks[i] = NO_TIME_SLICE;
   }

   queue_reset(&tn_create_queue);
   tn_created_tasks_qty = 0;

   tn_system_state = TN_ST_STATE_NOT_RUN;

   tn_ready_to_run_bmp = 0;

   tn_idle_count       = 0;
   tn_curr_performance = 0;

   tn_next_task_to_run = NULL;
   tn_curr_run_task    = NULL;

  //-- System tasks

   queue_reset(&tn_wait_timeout_list);

   //--- Timer task

   tn_task_create((TN_TCB*)&tn_timer_task,        //-- task TCB
                  tn_timer_task_func,             //-- task function
                  0,                              //-- task priority
                  &(tn_timer_task_stack           //-- task stack first addr in memory
                      [TN_TIMER_STACK_SIZE-1]),
                  TN_TIMER_STACK_SIZE,            //-- task stack size (in int,not bytes)
                  NULL,                           //-- task function parameter
                  TN_TASK_TIMER);                 //-- Creation option

   //--- Idle task

   tn_task_create((TN_TCB*)&tn_idle_task,         //-- task TCB
                  tn_idle_task_func,              //-- task function
                  TN_NUM_PRIORITY-1,              //-- task priority
                  &(tn_idle_task_stack            //-- task stack first addr in memory
                      [TN_IDLE_STACK_SIZE-1]),
                  TN_IDLE_STACK_SIZE,             //-- task stack size (in int,not bytes)
                  NULL,                           //-- task function parameter
                  TN_TASK_IDLE);                  //-- Creation option

    //-- Activate timer & idle tasks

   tn_next_task_to_run = &tn_idle_task; //-- Just for the task_to_runnable() proper op

   task_to_runnable(&tn_idle_task);
   task_to_runnable(&tn_timer_task);

   tn_curr_run_task = &tn_idle_task;  //-- otherwise it is NULL

   //-- Run OS - first context switch

   tn_start_exe();
}