Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

after QUEUE_ADD(h, n) , then call QUEUE_FOREACH will infinitely loop #754

Closed
paulran opened this issue Mar 8, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@paulran
Copy link

commented Mar 8, 2016

wq_test.c

#include "queue.h"
#include <stdio.h>

struct uv__work {
  int id;
  void* wq[2];
};

#define QUEUE_DATA(ptr, type, field) \
  ((type *) ((char *) (ptr) - offsetof(type, field)))


static QUEUE wq;

int main()
{
    struct uv__work *pw;
    QUEUE* q;

    QUEUE_INIT(&wq);

    struct uv__work  w1;
    w1.id = 1;
    QUEUE_INSERT_TAIL(&wq, &w1.wq);

    struct uv__work w2;
    w2.id = 2;
    QUEUE_INSERT_TAIL(&wq, &w2.wq);

    struct uv__work w3;
    w3.id = 3;
    QUEUE_INSERT_TAIL(&wq, &w3.wq);


    QUEUE_FOREACH(q, &wq)
    {
        pw = QUEUE_DATA(q, uv__work, wq);
        printf("queue wq data is %d.\n", pw->id);
    }
    printf("-----------------------------\n");

    QUEUE wq2;
    QUEUE_INIT(&wq2);

    struct uv__work w4;
    w4.id = 4;
    QUEUE_INSERT_TAIL(&wq2, &w4.wq);

    QUEUE_FOREACH(q, &wq2)
    {
        pw = QUEUE_DATA(q, uv__work, wq);
        printf("queue wq2 data is %d.\n", pw->id);
    }
    printf("-----------------------------\n");


    QUEUE_ADD(&wq, &wq2); // add

    QUEUE_FOREACH(q, &wq)
    {
        pw = QUEUE_DATA(q, uv__work, wq);
        printf("queue wq data is %d.\n", pw->id);
    }
    printf("-----------------------------\n");

    QUEUE_FOREACH(q, &wq2)
    {
        pw = QUEUE_DATA(q, uv__work, wq);
        printf("queue wq2 data is %d.\n", pw->id);
    }
    printf("-----------------------------\n");

    return 0;
}

The program output:

queue wq data is 1.
queue wq data is 2.
queue wq data is 3.
-----------------------------
queue wq2 data is 4.
-----------------------------
queue wq data is 1.
queue wq data is 2.
queue wq data is 3.
queue wq data is 4.
-----------------------------
queue wq2 data is 4.
queue wq2 data is 0.
queue wq2 data is 1.
queue wq2 data is 2.
queue wq2 data is 3.
queue wq2 data is 4.
queue wq2 data is 0.
queue wq2 data is 1.
queue wq2 data is 2.
........
@saghul

This comment has been minimized.

Copy link
Member

commented Mar 8, 2016

You have to initialize the queues inside your uv__work structure. Please use the mailing list for questions.

@saghul saghul closed this Mar 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.