Notifying an event queue

x::eventqueuedestroynotify is a template class that defines a destructor callback which puts an object on an event queue. Instead of only notifying an event file descriptor, like x::eventdestroynotify does, x::eventqueuedestroynotify takes a reference to an event queue, and some arbitrary event queue object. x::eventqueuedestroynotify saves a copy of the event queue object. When the destructor callback gets triggered, the event queue object gets placed into the event queue.

#include <x/threads/job.H>
#include <x/eventqueuedestroynotify.H>
#include <x/threads/workerpool.H>

class myJobObj : public x::jobObj {

// ...

};

class myQueueObj;

typedef x::ptr<myJobObj> myJob;

class myServerObj;

x::workerpool<myServerObj>
    workerpool=x::workerpool<myServerObj>::create(4, 8);

x::eventfd eventfdNotify(x::eventfd::create());

x::eventqueue<myQueueObj> q=
    x::eventqueue<myQueueObj>::create(eventfdNotify);

x::eventqueuedestroynotify<myQueueObj> sendjob()
{
    myJob j(myJob::create());

    // ...

    workerpool->run(j);

    return x::eventqueuedestroynotify<myQueueObj>
        ::create(q, x::ref<myQueueObj>::create(), j);
}

auto jobCompleted=sendjob();

// ...

while (!jobCompleted->wasdestroyed())
{
   eventfdNotify->event();
}

myQueueObj res(q>pop());

x::eventqueuedestroynotify is a x::ref to a reference-counted object which saves a weak reference to the event queue, which gets installed as a destructor callback for a job object. After the job gets processed (or the job queue gets destroyed), the last reference to the job goes out of scope, destroying the job object and invoking the destructor callback.

The callback sets an internal flag, and inserts the saved event queue object into the event queue (unless the event queue went out of scope independently). wasdestroyed() retrieves the flag.