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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include "gevent.h"
extern struct uwsgi_server uwsgi;
extern struct uwsgi_gevent ugevent;
int uwsgi_gevent_wait_write_hook(int fd, int timeout) {
PyObject *ret = NULL;
/// create a watcher for writes
PyObject *watcher = PyObject_CallMethod(ugevent.hub_loop, "io", "ii", fd, 2);
if (!watcher) return -1;
PyObject *timer = PyObject_CallMethod(ugevent.hub_loop, "timer", "i", timeout);
if (!timer) {
Py_DECREF(watcher);
return -1;
}
PyObject *current_greenlet = GET_CURRENT_GREENLET;
PyObject *current = PyObject_GetAttrString(current_greenlet, "switch");
ret = PyObject_CallMethod(watcher, "start", "OO", current, watcher);
if (!ret) {
stop_the_watchers_and_clear
return -1;
}
Py_DECREF(ret);
ret = PyObject_CallMethod(timer, "start", "OO", current, timer);
if (!ret) {
stop_the_watchers_and_clear
return -1;
}
Py_DECREF(ret);
ret = PyObject_CallMethod(ugevent.hub, "switch", NULL);
if (!ret) {
stop_the_watchers_and_clear
return -1;
}
Py_DECREF(ret);
if (ret == timer) {
stop_the_watchers_and_clear;
return 0;
}
stop_the_watchers_and_clear;
return 1;
}
int uwsgi_gevent_wait_read_hook(int fd, int timeout) {
PyObject *ret = NULL;
/// create a watcher for writes
PyObject *watcher = PyObject_CallMethod(ugevent.hub_loop, "io", "ii", fd, 1);
if (!watcher) return -1;
PyObject *timer = PyObject_CallMethod(ugevent.hub_loop, "timer", "i", timeout);
if (!timer) {
Py_DECREF(watcher);
return -1;
}
PyObject *current_greenlet = GET_CURRENT_GREENLET;
PyObject *current = PyObject_GetAttrString(current_greenlet, "switch");
ret = PyObject_CallMethod(watcher, "start", "OO", current, watcher);
if (!ret) {
stop_the_watchers_and_clear
return -1;
}
Py_DECREF(ret);
ret = PyObject_CallMethod(timer, "start", "OO", current, timer);
if (!ret) {
stop_the_watchers_and_clear
return -1;
}
Py_DECREF(ret);
ret = PyObject_CallMethod(ugevent.hub, "switch", NULL);
if (!ret) {
stop_the_watchers_and_clear
return -1;
}
Py_DECREF(ret);
if (ret == timer) {
stop_the_watchers_and_clear;
return 0;
}
stop_the_watchers_and_clear;
return 1;
}
int uwsgi_gevent_wait_milliseconds_hook(int timeout) {
PyObject *ret = NULL;
PyObject *timer = PyObject_CallMethod(ugevent.hub_loop, "timer", "f", ((double) timeout)/1000.0);
if (!timer) return -1;
PyObject *current_greenlet = GET_CURRENT_GREENLET;
PyObject *current = PyObject_GetAttrString(current_greenlet, "switch");
ret = PyObject_CallMethod(timer, "start", "OO", current, timer);
if (!ret) {
Py_DECREF(current); Py_DECREF(current_greenlet);
Py_DECREF(timer);
return -1;
}
Py_DECREF(ret);
ret = PyObject_CallMethod(ugevent.hub, "switch", NULL);
if (!ret) {
ret = PyObject_CallMethod(timer, "stop", NULL);
if (ret) { Py_DECREF(ret); }
Py_DECREF(current); Py_DECREF(current_greenlet);
Py_DECREF(timer);
return -1;
}
Py_DECREF(ret);
if (ret == timer) {
ret = PyObject_CallMethod(timer, "stop", NULL);
if (ret) { Py_DECREF(ret); }
Py_DECREF(current); Py_DECREF(current_greenlet);
Py_DECREF(timer);
return 0;
}
return -1;
}
|