Skip to content
Advertisement

LINUX LISH.H list_for_each infinite loop

I’m trying to implement a list of task_struct and use it as a FIFO. In addition I need a function to go through the list and search for a particular task.

My problem is that the list_for_each macro gets stuck in an infinite loop.

Here below there is a snippet from my code:

typedef struct task_list_node {
   struct list_head next;
   struct task_struct *task;
} task_list_node;

task_list_node * find(struct semaphore *sem) 
{
   struct list_head * next;
   struct list_head task_list = sem->task_list;

   list_for_each(next, &task_list) {
      task_list_node * elem;
      // Get a pointer to the element on the list
      elem = list_entry(next, task_list_node, next);

      // [...]

   }
   return(NULL);   
}

Thanks in advance for any tips!

Advertisement

Answer

list_for_each() itself is fine.

#define list_for_each(pos, head) 
    for (pos = (head)->next; pos != (head); pos = pos->next)

The problem is: struct list_head task_list = sem->task_list;

You are creating a structure like:

    |--------------------------------------------------------
   /                                                        |
  head #a "sem->task_list"  ---element 1 .... element #N -----
                                   ^
  head #b "task_list" -------------|

list_for_each() should stop when "pos" == head #a, but you are not using head #a "sem->task_list" for iteration, but head #b "task_list", so the stop condition is "pos" == "task_list" is not reached.

Should fix like:

list_for_each(next, &sem->task_list) {
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement