1 /* access whether we built embedded or not */ 2 3 #define LIBUV_EMBED ... 4 5 /* markers for the CFFI parser. Replaced when the string is read. */ 6 #define GEVENT_STRUCT_DONE int 7 #define GEVENT_ST_NLINK_T int 8 #define GEVENT_UV_OS_SOCK_T int 9 10 #define UV_EBUSY ... 11 12 #define UV_VERSION_MAJOR ... 13 #define UV_VERSION_MINOR ... 14 #define UV_VERSION_PATCH ... 15 16 typedef enum { 17 UV_RUN_DEFAULT = 0, 18 UV_RUN_ONCE, 19 UV_RUN_NOWAIT 20 } uv_run_mode; 21 22 typedef enum { 23 UV_UNKNOWN_HANDLE = 0, 24 UV_ASYNC, 25 UV_CHECK, 26 UV_FS_EVENT, 27 UV_FS_POLL, 28 UV_HANDLE, 29 UV_IDLE, 30 UV_NAMED_PIPE, 31 UV_POLL, 32 UV_PREPARE, 33 UV_PROCESS, 34 UV_STREAM, 35 UV_TCP, 36 UV_TIMER, 37 UV_TTY, 38 UV_UDP, 39 UV_SIGNAL, 40 UV_FILE, 41 UV_HANDLE_TYPE_MAX 42 } uv_handle_type; 43 44 enum uv_poll_event { 45 UV_READABLE = 1, 46 UV_WRITABLE = 2, 47 /* new in 1.9 */ 48 UV_DISCONNECT = 4, 49 /* new in 1.14.0 */ 50 UV_PRIORITIZED = 8, 51 }; 52 53 enum uv_fs_event { 54 UV_RENAME = 1, 55 UV_CHANGE = 2 56 }; 57 58 enum uv_fs_event_flags { 59 /* 60 * By default, if the fs event watcher is given a directory name, we will 61 * watch for all events in that directory. This flags overrides this behavior 62 * and makes fs_event report only changes to the directory entry itself. This 63 * flag does not affect individual files watched. 64 * This flag is currently not implemented yet on any backend. 65 */ 66 UV_FS_EVENT_WATCH_ENTRY = 1, 67 /* 68 * By default uv_fs_event will try to use a kernel interface such as inotify 69 * or kqueue to detect events. This may not work on remote filesystems such 70 * as NFS mounts. This flag makes fs_event fall back to calling stat() on a 71 * regular interval. 72 * This flag is currently not implemented yet on any backend. 73 */ 74 UV_FS_EVENT_STAT = 2, 75 /* 76 * By default, event watcher, when watching directory, is not registering 77 * (is ignoring) changes in it's subdirectories. 78 * This flag will override this behaviour on platforms that support it. 79 */ 80 UV_FS_EVENT_RECURSIVE = 4 81 }; 82 83 const char* uv_strerror(int); 84 const char* uv_err_name(int); 85 const char* uv_version_string(void); 86 const char* uv_handle_type_name(uv_handle_type type); 87 88 // handle structs and types 89 struct uv_loop_s { 90 void* data; 91 GEVENT_STRUCT_DONE _; 92 }; 93 struct uv_handle_s { 94 struct uv_loop_s* loop; 95 uv_handle_type type; 96 void *data; 97 GEVENT_STRUCT_DONE _; 98 }; 99 struct uv_idle_s { 100 struct uv_loop_s* loop; 101 uv_handle_type type; 102 void *data; 103 GEVENT_STRUCT_DONE _; 104 }; 105 struct uv_prepare_s { 106 struct uv_loop_s* loop; 107 uv_handle_type type; 108 void *data; 109 GEVENT_STRUCT_DONE _; 110 }; 111 struct uv_timer_s { 112 struct uv_loop_s* loop; 113 uv_handle_type type; 114 void *data; 115 GEVENT_STRUCT_DONE _; 116 }; 117 struct uv_signal_s { 118 struct uv_loop_s* loop; 119 uv_handle_type type; 120 void *data; 121 GEVENT_STRUCT_DONE _; 122 }; 123 struct uv_poll_s { 124 struct uv_loop_s* loop; 125 uv_handle_type type; 126 void *data; 127 GEVENT_STRUCT_DONE _; 128 }; 129 130 struct uv_check_s { 131 struct uv_loop_s* loop; 132 uv_handle_type type; 133 void *data; 134 GEVENT_STRUCT_DONE _; 135 }; 136 137 struct uv_async_s { 138 struct uv_loop_s* loop; 139 uv_handle_type type; 140 void *data; 141 GEVENT_STRUCT_DONE _; 142 }; 143 144 struct uv_fs_event_s { 145 struct uv_loop_s* loop; 146 uv_handle_type type; 147 void *data; 148 GEVENT_STRUCT_DONE _; 149 }; 150 151 struct uv_fs_poll_s { 152 struct uv_loop_s* loop; 153 uv_handle_type type; 154 void *data; 155 GEVENT_STRUCT_DONE _; 156 }; 157 158 typedef struct uv_loop_s uv_loop_t; 159 typedef struct uv_handle_s uv_handle_t; 160 typedef struct uv_idle_s uv_idle_t; 161 typedef struct uv_prepare_s uv_prepare_t; 162 typedef struct uv_timer_s uv_timer_t; 163 typedef struct uv_signal_s uv_signal_t; 164 typedef struct uv_poll_s uv_poll_t; 165 typedef struct uv_check_s uv_check_t; 166 typedef struct uv_async_s uv_async_t; 167 typedef struct uv_fs_event_s uv_fs_event_t; 168 typedef struct uv_fs_poll_s uv_fs_poll_t; 169 170 171 size_t uv_handle_size(uv_handle_type); 172 173 // callbacks with the same signature 174 typedef void (*uv_close_cb)(uv_handle_t *handle); 175 typedef void (*uv_idle_cb)(uv_idle_t *handle); 176 typedef void (*uv_timer_cb)(uv_timer_t *handle); 177 typedef void (*uv_check_cb)(uv_check_t* handle); 178 typedef void (*uv_async_cb)(uv_async_t* handle); 179 typedef void (*uv_prepare_cb)(uv_prepare_t *handle); 180 181 // callbacks with distinct sigs 182 typedef void (*uv_walk_cb)(uv_handle_t *handle, void *arg); 183 typedef void (*uv_poll_cb)(uv_poll_t *handle, int status, int events); 184 typedef void (*uv_signal_cb)(uv_signal_t *handle, int signum); 185 186 // Callback passed to uv_fs_event_start() which will be called 187 // repeatedly after the handle is started. If the handle was started 188 // with a directory the filename parameter will be a relative path to 189 // a file contained in the directory. The events parameter is an ORed 190 // mask of uv_fs_event elements. 191 typedef void (*uv_fs_event_cb)(uv_fs_event_t* handle, const char* filename, int events, int status); 192 193 typedef struct { 194 long tv_sec; 195 long tv_nsec; 196 } uv_timespec_t; 197 198 typedef struct { 199 uint64_t st_dev; 200 uint64_t st_mode; 201 uint64_t st_nlink; 202 uint64_t st_uid; 203 uint64_t st_gid; 204 uint64_t st_rdev; 205 uint64_t st_ino; 206 uint64_t st_size; 207 uint64_t st_blksize; 208 uint64_t st_blocks; 209 uint64_t st_flags; 210 uint64_t st_gen; 211 uv_timespec_t st_atim; 212 uv_timespec_t st_mtim; 213 uv_timespec_t st_ctim; 214 uv_timespec_t st_birthtim; 215 } uv_stat_t; 216 217 typedef void (*uv_fs_poll_cb)(uv_fs_poll_t* handle, int status, const uv_stat_t* prev, const uv_stat_t* curr); 218 219 // loop functions 220 uv_loop_t *uv_default_loop(); 221 uv_loop_t* uv_loop_new(); // not documented; neither is uv_loop_delete 222 int uv_loop_init(uv_loop_t* loop); 223 int uv_loop_fork(uv_loop_t* loop); 224 int uv_loop_alive(const uv_loop_t *loop); 225 int uv_loop_close(uv_loop_t* loop); 226 uint64_t uv_backend_timeout(uv_loop_t* loop); 227 int uv_run(uv_loop_t *, uv_run_mode mode); 228 int uv_backend_fd(const uv_loop_t* loop); 229 // The narrative docs for the two time functions say 'const', 230 // but the header does not. 231 void uv_update_time(uv_loop_t* loop); 232 uint64_t uv_now(uv_loop_t* loop); 233 void uv_stop(uv_loop_t *); 234 void uv_walk(uv_loop_t *loop, uv_walk_cb walk_cb, void *arg); 235 236 // handle functions 237 // uv_handle_t is the base type for all libuv handle types. 238 239 void uv_ref(void *); 240 void uv_unref(void *); 241 int uv_has_ref(void *); 242 void uv_close(void *handle, uv_close_cb close_cb); 243 int uv_is_active(void *handle); 244 int uv_is_closing(void *handle); 245 246 // idle functions 247 // Idle handles will run the given callback once per loop iteration, right 248 // before the uv_prepare_t handles. Note: The notable difference with prepare 249 // handles is that when there are active idle handles, the loop will perform a 250 // zero timeout poll instead of blocking for i/o. Warning: Despite the name, 251 // idle handles will get their callbacks called on every loop iteration, not 252 // when the loop is actually "idle". 253 int uv_idle_init(uv_loop_t *, uv_idle_t *idle); 254 int uv_idle_start(uv_idle_t *idle, uv_idle_cb cb); 255 int uv_idle_stop(uv_idle_t *idle); 256 257 // prepare functions 258 // Prepare handles will run the given callback once per loop iteration, right 259 // before polling for i/o. 260 int uv_prepare_init(uv_loop_t *, uv_prepare_t *prepare); 261 int uv_prepare_start(uv_prepare_t *prepare, uv_prepare_cb cb); 262 int uv_prepare_stop(uv_prepare_t *prepare); 263 264 // check functions 265 // Check handles will run the given callback once per loop iteration, right 266 int uv_check_init(uv_loop_t *, uv_check_t *check); 267 int uv_check_start(uv_check_t *check, uv_check_cb cb); 268 int uv_check_stop(uv_check_t *check); 269 270 // async functions 271 // Async handles allow the user to "wakeup" the event loop and get a callback called from another thread. 272 273 int uv_async_init(uv_loop_t *, uv_async_t*, uv_async_cb); 274 int uv_async_send(uv_async_t*); 275 276 // timer functions 277 // Timer handles are used to schedule callbacks to be called in the future. 278 int uv_timer_init(uv_loop_t *, uv_timer_t *handle); 279 int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat); 280 int uv_timer_stop(uv_timer_t *handle); 281 int uv_timer_again(uv_timer_t *handle); 282 void uv_timer_set_repeat(uv_timer_t *handle, uint64_t repeat); 283 uint64_t uv_timer_get_repeat(const uv_timer_t *handle); 284 285 // signal functions 286 // Signal handles implement Unix style signal handling on a per-event loop 287 // bases. 288 int uv_signal_init(uv_loop_t *loop, uv_signal_t *handle); 289 int uv_signal_start(uv_signal_t *handle, uv_signal_cb signal_cb, int signum); 290 int uv_signal_stop(uv_signal_t *handle); 291 292 // poll functions Poll handles are used to watch file descriptors for 293 // readability and writability, similar to the purpose of poll(2). It 294 // is not okay to have multiple active poll handles for the same 295 // socket, this can cause libuv to busyloop or otherwise malfunction. 296 // 297 // The purpose of poll handles is to enable integrating external 298 // libraries that rely on the event loop to signal it about the socket 299 // status changes, like c-ares or libssh2. Using uv_poll_t for any 300 // other purpose is not recommended; uv_tcp_t, uv_udp_t, etc. provide 301 // an implementation that is faster and more scalable than what can be 302 // achieved with uv_poll_t, especially on Windows. 303 // 304 // Note On windows only sockets can be polled with poll handles. On 305 // Unix any file descriptor that would be accepted by poll(2) can be 306 // used. 307 int uv_poll_init(uv_loop_t *loop, uv_poll_t *handle, int fd); 308 309 // Initialize the handle using a socket descriptor. On Unix this is 310 // identical to uv_poll_init(). On windows it takes a SOCKET handle; 311 // SOCKET handles are another name for HANDLE objects in win32, and 312 // those are defined as PVOID, even though they are not actually 313 // pointers (they're small integers). CPython and PyPy both return 314 // the SOCKET (as cast to an int) from the socket.fileno() method. 315 // libuv uses ``uv_os_sock_t`` for this type, which is defined as an 316 // int on unix. 317 int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle, GEVENT_UV_OS_SOCK_T socket); 318 int uv_poll_start(uv_poll_t *handle, int events, uv_poll_cb cb); 319 int uv_poll_stop(uv_poll_t *handle); 320 321 // FS Event handles allow the user to monitor a given path for 322 // changes, for example, if the file was renamed or there was a 323 // generic change in it. This handle uses the best backend for the job 324 // on each platform. 325 // 326 // Thereas also uv_fs_poll_t that uses stat for filesystems where 327 // the kernel event isn't available. 328 int uv_fs_event_init(uv_loop_t*, uv_fs_event_t*); 329 int uv_fs_event_start(uv_fs_event_t*, uv_fs_event_cb, const char* path, unsigned int flags); 330 int uv_fs_event_stop(uv_fs_event_t*); 331 int uv_fs_event_getpath(uv_fs_event_t*, char* buffer, size_t* size); 332 333 // FS Poll handles allow the user to monitor a given path for changes. 334 // Unlike uv_fs_event_t, fs poll handles use stat to detect when a 335 // file has changed so they can work on file systems where fs event 336 // handles can't. 337 // 338 // This is a closer match to libev. 339 int uv_fs_poll_init(void*, void*); 340 int uv_fs_poll_start(void*, uv_fs_poll_cb, const char* path, unsigned int); 341 int uv_fs_poll_stop(void*); 342 343 344 /* Standard library */ 345 void* memset(void *b, int c, size_t len); 346 347 348 /* gevent callbacks */ 349 // Implemented in Python code as 'def_extern'. In the case of poll callbacks and fs 350 // callbacks, if *status* is less than 0, it will be passed in the revents 351 // field. In cases of no extra arguments, revents will be 0. 352 // These will be created as static functions at the end of the 353 // _source.c and must be pre-declared at the top of that file if we 354 // call them 355 typedef void* GeventWatcherObject; 356 extern "Python" { 357 // Standard gevent._ffi.loop callbacks. 358 int python_callback(GeventWatcherObject handle, int revents); 359 void python_handle_error(GeventWatcherObject handle, int revents); 360 void python_stop(GeventWatcherObject handle); 361 362 void python_check_callback(uv_check_t* handle); 363 void python_prepare_callback(uv_prepare_t* handle); 364 void python_timer0_callback(uv_check_t* handle); 365 366 // libuv specific callback 367 void _uv_close_callback(uv_handle_t* handle); 368 void python_sigchld_callback(uv_signal_t* handle, int signum); 369 void python_queue_callback(uv_handle_t* handle, int revents); 370 } 371 372 static void _gevent_signal_callback1(uv_signal_t* handle, int arg); 373 static void _gevent_async_callback0(uv_async_t* handle); 374 static void _gevent_prepare_callback0(uv_prepare_t* handle); 375 static void _gevent_timer_callback0(uv_timer_t* handle); 376 static void _gevent_check_callback0(uv_check_t* handle); 377 static void _gevent_idle_callback0(uv_idle_t* handle); 378 static void _gevent_poll_callback2(uv_poll_t* handle, int status, int events); 379 static void _gevent_fs_event_callback3(uv_fs_event_t* handle, const char* filename, int events, int status); 380 381 typedef struct _gevent_fs_poll_s { 382 uv_fs_poll_t handle; 383 uv_stat_t curr; 384 uv_stat_t prev; 385 } gevent_fs_poll_t; 386 387 static void _gevent_fs_poll_callback3(uv_fs_poll_t* handle, int status, const uv_stat_t* prev, const uv_stat_t* curr); 388 389 static void gevent_uv_walk_callback_close(uv_handle_t* handle, void* arg); 390 static void gevent_close_all_handles(uv_loop_t* loop); 391 392 /* gevent utility functions */ 393 static void gevent_zero_timer(uv_timer_t* handle); 394 static void gevent_zero_prepare(uv_prepare_t* handle); 395 static void gevent_zero_check(uv_check_t* handle); 396 static void gevent_zero_loop(uv_loop_t* handle); 397 static void gevent_set_uv_alloc(); 398 static void gevent_test_setup(); 399