qmsk/net/lib/event2/base.pxd
author Tero Marttila <terom@fixme.fi>
Sat, 26 Sep 2009 23:38:02 +0300
changeset 56 07ed878c847b
parent 52 722fc70a197a
permissions -rw-r--r--
doctweaks and some skeleton tests for event-aliveness
39
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Core `struct event_base` type
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
cimport qmsk.net.lib.event2.lib as lib
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
cdef class event_base :
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
    """
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
        An event_base stores the "global" state associated with events - it holds a set of events (pending/active/scheduled),
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
        and uses some specific multiplexing method (select/poll/epoll/kqueue/etc) to drive these events.
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
        
56
07ed878c847b doctweaks and some skeleton tests for event-aliveness
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
    12
        
07ed878c847b doctweaks and some skeleton tests for event-aliveness
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
    13
        *** POSIX Signal Handling ***
39
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
52
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    15
        Libevent has its own asynchronous signal-handling mechanism that is incompatible with Python's signal-handling
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    16
        mechanism.
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    17
        
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    18
        Technical details:
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    19
            Python's signal handlers will simply set a flag corresponding to the signal, and wait for
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    20
            PyErr_CheckSignals to be run. Following this, libevent's epoll/select/etc() call will return EINTER - but
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    21
            this will only cause libevent to check its own signal state, and continue running.
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    22
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    23
        To solve this, we register a "wakeup fd" in Python's signal handling system, whereby a byte is written to a
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    24
        socket pair whenever python's signal handler runs. We then register an (internal) event for the other end of
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    25
        this socketpair in the event_base, which then runs the python signal handlers from "inside" of .loop(),
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    26
        propagating exceptions out of .loop() - this solution is more or less equivalent to how libevent's signal
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    27
        handling works.
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    28
        
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    29
        This means that python's default signals and any other signals added using the signal module continue to work
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    30
        at the cost of some additional complexity, but any libevent signal-events also work as well :)
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    31
        
56
07ed878c847b doctweaks and some skeleton tests for event-aliveness
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
    32
        *** TODO ***
07ed878c847b doctweaks and some skeleton tests for event-aliveness
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
    33
07ed878c847b doctweaks and some skeleton tests for event-aliveness
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
    34
        Currently, you can only have one event_base at a time - there should be separate types used for the main
52
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    35
        thread and sub-threads, since the python signal handling mechanism only works in the main thread.
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    36
56
07ed878c847b doctweaks and some skeleton tests for event-aliveness
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
    37
        The propagate-exceptions doesn't actually work either, it just aborts the event loop :)
52
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    38
56
07ed878c847b doctweaks and some skeleton tests for event-aliveness
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
    39
        Support for event_config!
07ed878c847b doctweaks and some skeleton tests for event-aliveness
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
    40
        Support for event_base_priority_init!
39
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
    """
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
    # the underlying ev_base object
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
    cdef lib.event_base *ev_base
075eaafa80a7 initial lib.event2 code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
52
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    46
    # internal event for PySignal wakeups
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    47
    cdef lib.event *ev_pysignal
722fc70a197a change CallbackEvent to replace fd with ev, event2 doc/style tweaks
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    48