From: Ralph Castain <rhc@open-mpi.org>
Content-Type: multipart/mixed;
	boundary="Apple-Mail=_03BC4BA6-4560-41E3-8D97-053BAB88446B"
X-Smtp-Server: smtp.gmail.com:rhc.openmpi@gmail.com
Subject: Event priorities
X-Universally-Unique-Identifier: 410cc399-29ea-4a5f-8318-aa599855b3f5
Date: Mon, 30 Apr 2012 13:53:03 -0600
Message-Id: <912123A6-4633-4462-AD88-1400FDF93895@open-mpi.org>
To: libevent-users@freehaven.net
Mime-Version: 1.0 (Apple Message framework v1257)


--Apple-Mail=_03BC4BA6-4560-41E3-8D97-053BAB88446B
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

Hi folks

I'm working with priorities in events using libevent 2.0.13. Since I'm =
not using the most current release, I thought I'd ask about a behavior =
we are seeing that might be a potential bug.

I have attached a simple reproducer of the problem. In short, suppose I =
have setup 8 priorities for my event base. I then execute an event at =
priority 4. In that callback, I setup and activate two new events - =
first one at priority 4, and then another at priority 0. We then cycle =
back through the event loop.

Given the prioritized event base, I would expect the event at priority 0 =
to be called back first. However, this isn't what happens. Instead, the =
first event to be defined (in this case, the one at priority 4) is =
called back first.

Is this the expected behavior? Can someone point me to the error in this =
reproducer that might be causing the problem?

FWIW: I found that if I adjust the initial event's priority to something =
different than 4 (i.e., the level of the next event to be defined), then =
I get the expected behavior. So it appears that the problem is that when =
we return to the loop, we trigger any pending event of the same priority =
as the one we were in, instead of looping around to check the highest =
priority first.

Thanks
Ralph

--Apple-Mail=_03BC4BA6-4560-41E3-8D97-053BAB88446B
Content-Disposition: attachment;
	filename=evpri-test2.c
Content-Type: application/octet-stream; x-unix-mode=0644; name="evpri-test2.c"
Content-Transfer-Encoding: 7bit


#include <event2/event.h>
#include <event2/event_struct.h>

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdbool.h>

#define SIGPRI 0
#define TERMPRI 1

static struct event_base *base;
static bool run=true;
static int loops=0;
static bool again=false;
static struct event ev2, ev3;


static void
die(const char *msg)
{
    fprintf(stderr, "%s\n", msg);
    fflush(stderr);
    exit(1);
}

static void
cbfunc2(evutil_socket_t fd, short what, void *arg)
{
    fprintf(stderr, "CAUGHT EVENT 2\n");
    fflush(stderr);
#if 0
    event_base_loopbreak(base);
#endif
    run = false;
}

static void
cbfunc1(evutil_socket_t fd, short what, void *arg)
{
    if (again) {
        fprintf(stderr, "CYCLING BACK THRU EVENT 1\n");
        return;
    }

    fprintf(stderr, "CAUGHT EVENT 1\n");
    fflush(stderr);
    again = true;

    if (event_assign(&ev2, base, -1, EV_WRITE, cbfunc1, NULL) < 0)
        die("event_assign_2");
    if (event_priority_set(&ev2, 4) < 0)
        die("event_priority_set2");
    if (event_add(&ev2, NULL) < 0)
        die("event_add2");
    event_active(&ev2, EV_WRITE, 1);
    fprintf(stderr, "CB1: FIRST EVENT DEFINED\n");
    fflush(stderr);

    if (event_assign(&ev3, base, -1, EV_WRITE, cbfunc2, NULL) < 0)
        die("event_assign_3");
    if (event_priority_set(&ev3, 0) < 0)
        die("event_priority_set3");
    if (event_add(&ev3, NULL) < 0)
        die("event_add3");
    event_active(&ev3, EV_WRITE, 1);
    fprintf(stderr, "CB2: SECOND EVENT DEFINED\n");
    fflush(stderr);
}

int
main(int argc, char **argv)
{
    struct event ev1;

    event_enable_debug_mode();

    fprintf(stderr, "Libevent %s\n", event_get_version());
    fflush(stderr);

    if (!(base = event_base_new()))
        die("event_base_new");
    if (event_base_priority_init(base, 8) < 0)
        die("event_base_priority_init");

    if (event_assign(&ev1, base, -1, EV_WRITE, cbfunc1, NULL) < 0)
        die("event_assign_1");
    if (event_priority_set(&ev1, 4) < 0)
        die("event_priority_set");
    if (event_add(&ev1, NULL) < 0)
        die("event_add");
    event_active(&ev1, EV_WRITE, 1);
    fprintf(stderr, "FIRST EVENT DEFINED\n");
    fflush(stderr);

    /*    event_dispatch(base); */

    while (run) {
        event_base_loop(base, EVLOOP_ONCE);
    }

    fprintf(stderr, "EXITED LOOP - FREEING BASE\n");
    fflush(stderr);
    event_base_free(base);
    return 0;
}

--Apple-Mail=_03BC4BA6-4560-41E3-8D97-053BAB88446B
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=us-ascii




--Apple-Mail=_03BC4BA6-4560-41E3-8D97-053BAB88446B--

