aboutsummaryrefslogtreecommitdiffstats
path: root/python/gevent/libev/_corecffi_source.c
blob: 63b216e5244120dad9971b7efee46837bec1825d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// passed to the real C compiler
#define LIBEV_EMBED 1

#ifdef _WIN32
#define EV_STANDALONE 1
#include "libev_vfd.h"
#endif


#include "libev.h"

static void
_gevent_noop(struct ev_loop *_loop, struct ev_timer *w, int revents) { }

void (*gevent_noop)(struct ev_loop *, struct ev_timer *, int) = &_gevent_noop;

static int python_callback(void* handle, int revents);
static void python_handle_error(void* handle, int revents);
static void python_stop(void* handle);

static void _gevent_generic_callback(struct ev_loop* loop,
									 struct ev_watcher* watcher,
									 int revents)
{
    void* handle = watcher->data;
    int cb_result = python_callback(handle, revents);
    switch(cb_result) {
        case -1:
            // in case of exception, call self.loop.handle_error;
            // this function is also responsible for stopping the watcher
            // and allowing memory to be freed
            python_handle_error(handle, revents);
        break;
        case 1:
            // Code to stop the event. Note that if python_callback
            // has disposed of the last reference to the handle,
            // `watcher` could now be invalid/disposed memory!
            if (!ev_is_active(watcher)) {
                python_stop(handle);
            }
        break;
        case 2:
            // watcher is already stopped and dead, nothing to do.
        break;
        default:
            fprintf(stderr,
                    "WARNING: gevent: Unexpected return value %d from Python callback "
                    "for watcher %p and handle %d\n",
                    cb_result,
                    watcher, handle);
            // XXX: Possible leaking of resources here? Should we be
            // closing the watcher?
    }
}

static void gevent_zero_timer(struct ev_timer* handle)
{
	memset(handle, 0, sizeof(struct ev_timer));
}

static void gevent_zero_check(struct ev_check* handle)
{
	memset(handle, 0, sizeof(struct ev_check));
}

static void gevent_zero_prepare(struct ev_prepare* handle)
{
	memset(handle, 0, sizeof(struct ev_prepare));
}