Пример #1
0
int main(int argc, char *argv[])
{
	st_list *myList = NULL;
	st_list_item *item = NULL;
	en_llist_ret_code ret;

	myList = test();

	if (!myList) {
		fprintf(stderr, "Failed to operate with linke_list!\n");
		return -1;
	}

	// prints all elements from the list
	list_dump(myList);

	// prints the first item from the list
	item = llist_get_first(myList);
	if (!item) {
		fprintf(stderr, "Failed to retrieve an item from the list");
		llist_destroy(&myList);
		return -1;
	}
	list_dump_item(item);

	myStruct *mst = (myStruct *) item->data;
	free(mst->l);

	// prints the last item from the list
	item = llist_get_last(myList);
	if (!item) {
		fprintf(stderr, "Failed to retrieve an item from the list");
		llist_destroy(&myList);
		return -1;
	}
	list_dump_item(item);

	// get an item at the middle of the list
	item = llist_get_item(myList, 4);
	if (!item) {
		fprintf(stderr, "Failed to retrieve an item from the list");
		llist_destroy(&myList);
		return -1;
	}
	list_dump_item(item);

	// Remove test.
	printf("\nWill remove some items of the list!!\n");
	// First.
	ret = llist_rm_index(myList, 0);
	if (ret != LLIST_RET_SUCCESS) {
		fprintf(stderr, "Failed to remove first item.\n");
	}

	// Middle.
	ret = llist_rm_index(myList, 5);
	if (ret != LLIST_RET_SUCCESS) {
		fprintf(stderr, "Failed to remove middle item.\n");
	}

	// last.
	item = llist_get_last(myList);
	if (!item) {
		fprintf(stderr, "Failed to retrieve an item from the list");
		llist_destroy(&myList);
		return -1;
	}
	mst = (myStruct *) item->data;
	free(mst->l);

	ret = llist_rm_index(myList, item->index);
	if (ret != LLIST_RET_SUCCESS) {
		fprintf(stderr, "Failed to remove last item.\n");
	}

	list_dump(myList);

	// try to remove an invalid item.
	ret = llist_rm_index(myList, 9);
	if (ret == LLIST_RET_NOTFOUND) {
		fprintf(stderr, "[expected] Failed to remove last item. Item not found!\n");
	}
	else if (ret != LLIST_RET_SUCCESS) {
		fprintf(stderr, "Something went wrong while trying to remove last item.\n");
	}

	// free the list elements and erase the reference
	llist_destroy(&myList);

	return 0;
}
Пример #2
0
/**************************************************************************//**
*
* mss_timer_tick
*
* @brief      mss timer tick function - this function is to be called
*             periodically by timer ISR function in mss_hal.c
*
* @param      -
*
* @return     if true, there is a task activated, if false no task is activated
*
******************************************************************************/
bool mss_timer_tick(void)
{
  // local timer tick
  static mss_timer_tick_t timer_tick_cnt = 0;
  // flag indicating whether the timer tick function is already running
  static bool timer_tick_running = false;
  struct mss_timer_tbl_t *youngest_tmr;
  bool ret = false, loop;
  mss_int_flag_t int_flag;

  MSS_ENTER_CRITICAL_SECTION(int_flag);

  if(timer_tick_running == true)
  {
	// return false since this function is already running and now called
	// in different ISR context
    MSS_LEAVE_CRITICAL_SECTION(int_flag);
    return false;
  }

  // set flag to indicate timer tick is already running
  timer_tick_running = true;

  // copy hardware timer count tick to local timer tick
  timer_tick_cnt = mss_timer_tick_cnt;

  // loop in case hardware timer tick interrupt occurs between
  // long active timer list processing
  do
  {
	// if local timer tick is different than hardware timer tick,
	// it means that the hardware timer has fired while the timer tick
	// function is still processing the active timer list. Therefore
	// the timer tick function shall update the local timer and continue
	// processing the active timer list
	if(timer_tick_cnt != mss_timer_tick_cnt)
	{
      // increment timer tick
      timer_tick_cnt++;
	}

	do
	{
      // reset loop flag
      loop = false;

      // check for expired timer
      youngest_tmr = llist_touch_first(active_timer_llist);
	  if(youngest_tmr != NULL)
	  {
		if(youngest_tmr->expired_tick == timer_tick_cnt)
		{
		  // wake up task
		  mss_activate_task_int(youngest_tmr->task_id);

          // change timer state by shifting left one bit the state variable
          // which will change from running to expired in both one-shot and
          // periodic mode or from expired periodic to overflow
          youngest_tmr->state <<= 1;

          // remove timer object from active timer list
          llist_get_first(active_timer_llist);

          // if a periodic timer, returns to the active timer list
          if(youngest_tmr->reload_tick > 0)
          {
            // update timer tick first
            youngest_tmr->expired_tick = timer_tick_cnt +
                                         youngest_tmr->reload_tick;

            // put the timer into the active timer linked list
            llist_add_first(active_timer_llist, youngest_tmr);

            // sort the active timer list
            llist_sort(active_timer_llist, timer_cmp);
          }

          // return true
          ret = true;

          // do another loop for checking simultaneous timer ticks
          loop = true;

          // enable interrupt in between the process
          MSS_LEAVE_CRITICAL_SECTION(int_flag);
          MSS_ENTER_CRITICAL_SECTION(int_flag);
        }
      }
    }while(loop == true);
  }while(timer_tick_cnt != mss_timer_tick_cnt);


  // reset flag
  timer_tick_running = false;

  MSS_LEAVE_CRITICAL_SECTION(int_flag);

  return ret;
}