Changeset 8:dba41f33ddce in xplcommon for src


Ignore:
Timestamp:
12/29/12 08:01:50 (11 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Clarified the blocking and non-blocking behaviour and extended the test program to test both

Location:
src/xplcommon
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/xplcommon/WaitableEvent.h

    r7 r8  
    4444 * An event on which one can wait with a waiter.
    4545 */
    46 class WaitableEvent : public Waitable
     46class WaitableEvent : public Waitable, public Failable
    4747{
    4848public:
    4949    /**
    5050     * Construct the event.
     51     *
     52     * @param waiter the waiter to be used for waiting. If given, the
     53     * check() function will always return immediately. If not given,
     54     * the check() function may block and returns only when the event
     55     * is fired.
    5156     */
    5257    WaitableEvent(Waiter* waiter = 0);
  • src/xplcommon/posix/WaitableEvent.cc

    r6 r8  
    4848
    4949WaitableEvent::WaitableEvent(Waiter* waiter) :
    50     Waitable(waiter, eventfd(0, EFD_NONBLOCK), POLLIN)
     50    Waitable(waiter, eventfd(0, (waiter==0) ? 0 : EFD_NONBLOCK), POLLIN),
     51    fired(false)
    5152{
    5253    assert(fd>=0);
     
    6768    if (write(fd, &one, sizeof(one))<0) {
    6869        if (errno!=EAGAIN) {
    69             perror("xplcommon::posix::WaitableEvent::fire: write");
    70             assert(0);
     70            setErrorCode(errno);
    7171        }
    7272    }
     
    7777bool WaitableEvent::check()
    7878{
    79     if (fired) {
     79    if (doCheck()) {
    8080        fired = false;
    8181        return true;
     
    9797{
    9898    if ((events&POLLIN)==POLLIN) {
    99         uint64_t value = 0;
    100         if (read(fd, &value, sizeof(value))<0) {
    101             if (errno!=EAGAIN) {
    102                 perror("xplcommon::posix::WaitableEvent::handleEvents: read");
    103                 assert(0);
    104             }
    105         } else {
    106             fired = value>0;
     99        doCheck();
     100    }
     101}
     102
     103//------------------------------------------------------------------------------
     104
     105bool WaitableEvent::doCheck(bool fromHandleEvents)
     106{
     107    if (failed()) return false;
     108    if (!fromHandleEvents && fired) return true;
     109
     110    uint64_t value = 0;
     111    if (read(fd, &value, sizeof(value))<0) {
     112        if (errno!=EAGAIN) {
     113            setErrorCode(errno);
    107114        }
     115    } else {
     116        fired = fired || value>0;
    108117    }
     118
     119    return fired;
    109120}
    110121
  • src/xplcommon/posix/WaitableEvent.h

    r6 r8  
    3434#include "Waitable.h"
    3535
     36#include "../Failable.h"
     37
    3638//------------------------------------------------------------------------------
    3739
     
    4345 * An event on which one can wait with a waiter.
    4446 */
    45 class WaitableEvent : public Waitable
     47class WaitableEvent : public Waitable, public Failable
    4648{
    4749private:
     
    8385     */
    8486    virtual void handleEvents(short events);
     87
     88private:
     89    /**
     90     * Perform the real check. If we already know that we are fired,
     91     * just return true. Otherwise try to read from the event fd.
     92     */
     93    bool doCheck(bool fromHandleEvents = false);
    8594};
    8695
Note: See TracChangeset for help on using the changeset viewer.