-
Notifications
You must be signed in to change notification settings - Fork 935
/
corecext.pyx
1418 lines (1121 loc) · 45.6 KB
/
corecext.pyx
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2009-2012 Denis Bilenko. See LICENSE for details.
# This first directive, supported in Cython 0.24+, causes sources
# files to be *much* smaller when it's false (139,027 LOC vs 35,000
# LOC) and thus cythonpp.py (and probably the compiler; also Visual C
# has limits on source file sizes) to be faster (73s vs 46s). But it does
# make debugging more difficult. Auto-pickling was added in 0.26, and
# that's a new feature that we don't need or want to allow in a gevent
# point release.
# cython: emit_code_comments=False, auto_pickle=False, language_level=3str
# NOTE: We generally cannot use the Cython IF directive as documented
# at
# http://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#conditional-compilation
# (e.g., IF UNAME_SYSNAME == "Windows") because when Cython says
# "compilation", it means when *Cython* compiles, not when the C
# compiler compiles. We distribute an sdist with a single pre-compiled
# C file for all platforms so that end users that don't use a binary
# wheel don't have to sit through cythonpp and other steps the Makefile does.
# See https://github.com/gevent/gevent/issues/1076
# We compile in 3str mode, which should mean we get absolute import
# by default.
from __future__ import absolute_import
cimport cython
cimport libev
from cpython.ref cimport Py_INCREF
from cpython.ref cimport Py_DECREF
from cpython.mem cimport PyMem_Malloc
from cpython.mem cimport PyMem_Free
from libc.errno cimport errno
cdef extern from "Python.h":
int Py_ReprEnter(object)
void Py_ReprLeave(object)
import sys
import os
import traceback
import signal as signalmodule
from gevent import getswitchinterval
from gevent.exceptions import HubDestroyed
__all__ = ['get_version',
'get_header_version',
'supported_backends',
'recommended_backends',
'embeddable_backends',
'time',
'loop']
cdef tuple integer_types
if sys.version_info[0] >= 3:
integer_types = int,
else:
integer_types = (int, long)
cdef extern from "callbacks.h":
void gevent_callback_io(libev.ev_loop, void*, int)
void gevent_callback_timer(libev.ev_loop, void*, int)
void gevent_callback_signal(libev.ev_loop, void*, int)
void gevent_callback_idle(libev.ev_loop, void*, int)
void gevent_callback_prepare(libev.ev_loop, void*, int)
void gevent_callback_check(libev.ev_loop, void*, int)
void gevent_callback_fork(libev.ev_loop, void*, int)
void gevent_callback_async(libev.ev_loop, void*, int)
void gevent_callback_child(libev.ev_loop, void*, int)
void gevent_callback_stat(libev.ev_loop, void*, int)
void gevent_run_callbacks(libev.ev_loop, void*, int)
void gevent_periodic_signal_check(libev.ev_loop, void*, int)
void gevent_call(loop, callback)
void gevent_noop(libev.ev_loop, void*, int)
void* gevent_realloc(void*, long size)
cdef extern from "stathelper.c":
object _pystat_fromstructstat(void*)
UNDEF = libev.EV_UNDEF
NONE = libev.EV_NONE
READ = libev.EV_READ
WRITE = libev.EV_WRITE
TIMER = libev.EV_TIMER
PERIODIC = libev.EV_PERIODIC
SIGNAL = libev.EV_SIGNAL
CHILD = libev.EV_CHILD
STAT = libev.EV_STAT
IDLE = libev.EV_IDLE
PREPARE = libev.EV_PREPARE
CHECK = libev.EV_CHECK
EMBED = libev.EV_EMBED
FORK = libev.EV_FORK
CLEANUP = libev.EV_CLEANUP
ASYNC = libev.EV_ASYNC
CUSTOM = libev.EV_CUSTOM
ERROR = libev.EV_ERROR
READWRITE = libev.EV_READ | libev.EV_WRITE
MINPRI = libev.EV_MINPRI
MAXPRI = libev.EV_MAXPRI
BACKEND_SELECT = libev.EVBACKEND_SELECT
BACKEND_POLL = libev.EVBACKEND_POLL
BACKEND_EPOLL = libev.EVBACKEND_EPOLL
BACKEND_KQUEUE = libev.EVBACKEND_KQUEUE
BACKEND_DEVPOLL = libev.EVBACKEND_DEVPOLL
BACKEND_PORT = libev.EVBACKEND_PORT
BACKEND_LINUXAIO = libev.EVBACKEND_LINUXAIO
BACKEND_IOURING = libev.EVBACKEND_IOURING
FORKCHECK = libev.EVFLAG_FORKCHECK
NOINOTIFY = libev.EVFLAG_NOINOTIFY
SIGNALFD = libev.EVFLAG_SIGNALFD
NOSIGMASK = libev.EVFLAG_NOSIGMASK
@cython.internal
cdef class _EVENTSType:
def __repr__(self):
return 'gevent.core.EVENTS'
cdef public object GEVENT_CORE_EVENTS = _EVENTSType()
EVENTS = GEVENT_CORE_EVENTS
def get_version():
return 'libev-%d.%02d' % (libev.ev_version_major(), libev.ev_version_minor())
def get_header_version():
return 'libev-%d.%02d' % (libev.EV_VERSION_MAJOR, libev.EV_VERSION_MINOR)
# This list backends in the order they are actually tried by libev,
# as defined in loop_init. The names must be lower case.
_flags = [
# IOCP
(libev.EVBACKEND_PORT, 'port'),
(libev.EVBACKEND_KQUEUE, 'kqueue'),
(libev.EVBACKEND_IOURING, 'linux_iouring'),
(libev.EVBACKEND_LINUXAIO, "linux_aio"),
(libev.EVBACKEND_EPOLL, 'epoll'),
(libev.EVBACKEND_POLL, 'poll'),
(libev.EVBACKEND_SELECT, 'select'),
(libev.EVFLAG_NOENV, 'noenv'),
(libev.EVFLAG_FORKCHECK, 'forkcheck'),
(libev.EVFLAG_NOINOTIFY, 'noinotify'),
(libev.EVFLAG_SIGNALFD, 'signalfd'),
(libev.EVFLAG_NOSIGMASK, 'nosigmask')
]
_flags_str2int = dict((string, flag) for (flag, string) in _flags)
_events = [(libev.EV_READ, 'READ'),
(libev.EV_WRITE, 'WRITE'),
(libev.EV__IOFDSET, '_IOFDSET'),
(libev.EV_PERIODIC, 'PERIODIC'),
(libev.EV_SIGNAL, 'SIGNAL'),
(libev.EV_CHILD, 'CHILD'),
(libev.EV_STAT, 'STAT'),
(libev.EV_IDLE, 'IDLE'),
(libev.EV_PREPARE, 'PREPARE'),
(libev.EV_CHECK, 'CHECK'),
(libev.EV_EMBED, 'EMBED'),
(libev.EV_FORK, 'FORK'),
(libev.EV_CLEANUP, 'CLEANUP'),
(libev.EV_ASYNC, 'ASYNC'),
(libev.EV_CUSTOM, 'CUSTOM'),
(libev.EV_ERROR, 'ERROR')]
cpdef _flags_to_list(unsigned int flags):
cdef list result = []
for code, value in _flags:
if flags & code:
result.append(value)
flags &= ~code
if not flags:
break
if flags:
result.append(flags)
return result
if sys.version_info[0] >= 3:
basestring = (bytes, str)
else:
basestring = __builtins__.basestring
cpdef unsigned int _flags_to_int(object flags) except? -1:
# Note, that order does not matter, libev has its own predefined order
if not flags:
return 0
if isinstance(flags, integer_types):
return flags
cdef unsigned int result = 0
try:
if isinstance(flags, basestring):
flags = flags.split(',')
for value in flags:
value = value.strip().lower()
if value:
result |= _flags_str2int[value]
except KeyError as ex:
raise ValueError('Invalid backend or flag: %s\nPossible values: %s' % (ex, ', '.join(sorted(_flags_str2int.keys()))))
return result
cdef str _str_hex(object flag):
if isinstance(flag, integer_types):
return hex(flag)
return str(flag)
cpdef _check_flags(unsigned int flags):
cdef list as_list
flags &= libev.EVBACKEND_MASK
if not flags:
return
if not (flags & libev.EVBACKEND_ALL):
raise ValueError('Invalid value for backend: 0x%x' % flags)
if not (flags & libev.ev_supported_backends()):
as_list = [_str_hex(x) for x in _flags_to_list(flags)]
raise ValueError('Unsupported backend: %s' % '|'.join(as_list))
cpdef _events_to_str(int events):
cdef list result = []
cdef int c_flag
for (flag, string) in _events:
c_flag = flag
if events & c_flag:
result.append(string)
events = events & (~c_flag)
if not events:
break
if events:
result.append(hex(events))
return '|'.join(result)
def supported_backends():
return _flags_to_list(libev.ev_supported_backends())
def recommended_backends():
return _flags_to_list(libev.ev_recommended_backends())
def embeddable_backends():
return _flags_to_list(libev.ev_embeddable_backends())
def time():
return libev.ev_time()
cdef bint _check_loop(loop loop) except -1:
if not loop._ptr:
raise ValueError('operation on destroyed loop')
return 1
cdef public class callback [object PyGeventCallbackObject, type PyGeventCallback_Type]:
cdef public object callback
cdef public tuple args
cdef callback next
def __init__(self, callback, args):
self.callback = callback
self.args = args
def stop(self):
self.callback = None
self.args = None
close = stop
# Note, that __nonzero__ and pending are different
# nonzero is used in contexts where we need to know whether to schedule another callback,
# so it's true if it's pending or currently running
# 'pending' has the same meaning as libev watchers: it is cleared before entering callback
def __nonzero__(self):
# it's nonzero if it's pending or currently executing
return self.args is not None
@property
def pending(self):
return self.callback is not None
def __repr__(self):
if Py_ReprEnter(self) != 0:
return "<...>"
try:
format = self._format()
result = "<%s at 0x%x%s" % (self.__class__.__name__, id(self), format)
if self.pending:
result += " pending"
if self.callback is not None:
result += " callback=%r" % (self.callback, )
if self.args is not None:
result += " args=%r" % (self.args, )
if self.callback is None and self.args is None:
result += " stopped"
return result + ">"
finally:
Py_ReprLeave(self)
def _format(self):
return ''
# See comments in cares.pyx about DEF constants and when to use
# what kind.
cdef extern from *:
"""
#define CALLBACK_CHECK_COUNT 50
"""
int CALLBACK_CHECK_COUNT
@cython.final
@cython.internal
cdef class CallbackFIFO(object):
cdef callback head
cdef callback tail
def __init__(self):
self.head = None
self.tail = None
cdef inline clear(self):
self.head = None
self.tail = None
cdef inline callback popleft(self):
cdef callback head = self.head
self.head = head.next
if self.head is self.tail or self.head is None:
self.tail = None
head.next = None
return head
cdef inline append(self, callback new_tail):
assert not new_tail.next
if self.tail is None:
if self.head is None:
# Completely empty, so this
# is now our head
self.head = new_tail
return
self.tail = self.head
assert self.head is not None
old_tail = self.tail
old_tail.next = new_tail
self.tail = new_tail
def __nonzero__(self):
return self.head is not None
def __len__(self):
cdef Py_ssize_t count = 0
head = self.head
while head is not None:
count += 1
head = head.next
return count
def __iter__(self):
cdef list objects = []
head = self.head
while head is not None:
objects.append(head)
head = head.next
return iter(objects)
cdef bint has_callbacks(self):
return self.head
def __repr__(self):
return "<callbacks@%r len=%d head=%r tail=%r>" % (id(self), len(self), self.head, self.tail)
cdef public class loop [object PyGeventLoopObject, type PyGeventLoop_Type]:
## embedded struct members
cdef libev.ev_prepare _prepare
cdef libev.ev_timer _timer0
cdef libev.ev_async _threadsafe_async
# We'll only actually start this timer if we're on Windows,
# but it doesn't hurt to compile it in on all platforms.
cdef libev.ev_timer _periodic_signal_checker
## pointer members
cdef public object error_handler
cdef libev.ev_loop* _ptr
cdef public CallbackFIFO _callbacks
## data members
cdef bint starting_timer_may_update_loop_time
# We must capture the 'default' state at initialiaztion
# time. Destroying the default loop in libev sets
# the libev internal pointer to 0, and ev_is_default_loop will
# no longer work.
cdef bint _default
cdef readonly double approx_timer_resolution
def __cinit__(self, object flags=None, object default=None, libev.intptr_t ptr=0):
self.starting_timer_may_update_loop_time = 0
self._default = 0
libev.ev_prepare_init(&self._prepare,
<void*>gevent_run_callbacks)
libev.ev_timer_init(&self._periodic_signal_checker,
<void*>gevent_periodic_signal_check,
0.3, 0.3)
libev.ev_timer_init(&self._timer0,
<void*>gevent_noop,
0.0, 0.0)
libev.ev_async_init(&self._threadsafe_async,
<void*>gevent_noop)
cdef unsigned int c_flags
if ptr:
self._ptr = <libev.ev_loop*>ptr
self._default = libev.ev_is_default_loop(self._ptr)
else:
c_flags = _flags_to_int(flags)
_check_flags(c_flags)
c_flags |= libev.EVFLAG_NOENV
c_flags |= libev.EVFLAG_FORKCHECK
if default is None:
default = True
if default:
self._default = 1
self._ptr = libev.gevent_ev_default_loop(c_flags)
if not self._ptr:
raise SystemError("ev_default_loop(%s) failed" % (c_flags, ))
if sys.platform == "win32":
libev.ev_timer_start(self._ptr, &self._periodic_signal_checker)
libev.ev_unref(self._ptr)
else:
self._ptr = libev.ev_loop_new(c_flags)
if not self._ptr:
raise SystemError("ev_loop_new(%s) failed" % (c_flags, ))
if default or SYSERR_CALLBACK is None:
set_syserr_cb(self._handle_syserr)
# Mark as not destroyed
libev.ev_set_userdata(self._ptr, self._ptr)
libev.ev_prepare_start(self._ptr, &self._prepare)
libev.ev_unref(self._ptr)
libev.ev_async_start(self._ptr, &self._threadsafe_async)
libev.ev_unref(self._ptr)
def __init__(self, object flags=None, object default=None, libev.intptr_t ptr=0):
self._callbacks = CallbackFIFO()
# See libev.corecffi for this attribute.
self.approx_timer_resolution = 0.00001
cdef _run_callbacks(self):
cdef callback cb
cdef int count = CALLBACK_CHECK_COUNT
self.starting_timer_may_update_loop_time = True
cdef libev.ev_tstamp now = libev.ev_now(self._ptr)
cdef libev.ev_tstamp expiration = now + <libev.ev_tstamp>getswitchinterval()
try:
libev.ev_timer_stop(self._ptr, &self._timer0)
while self._callbacks.head is not None:
cb = self._callbacks.popleft()
libev.ev_unref(self._ptr)
# On entry, this will set cb.callback to None,
# changing cb.pending from True to False; on exit,
# this will set cb.args to None, changing bool(cb)
# from True to False.
# XXX: Why is this a C callback, not cython?
gevent_call(self, cb)
count -= 1
if count == 0 and self._callbacks.head is not None:
# We still have more to run but we've reached
# the end of one check group
count = CALLBACK_CHECK_COUNT
libev.ev_now_update(self._ptr)
if libev.ev_now(self._ptr) >= expiration:
now = 0
break
if now != 0:
libev.ev_now_update(self._ptr)
if self._callbacks.head is not None:
libev.ev_timer_start(self._ptr, &self._timer0)
finally:
self.starting_timer_may_update_loop_time = False
cdef _stop_watchers(self, libev.ev_loop* ptr):
if not ptr:
return
if libev.ev_is_active(&self._prepare):
libev.ev_ref(ptr)
libev.ev_prepare_stop(ptr, &self._prepare)
if libev.ev_is_active(&self._periodic_signal_checker):
libev.ev_ref(ptr)
libev.ev_timer_stop(ptr, &self._periodic_signal_checker)
if libev.ev_is_active(&self._threadsafe_async):
libev.ev_ref(ptr)
libev.ev_async_stop(ptr, &self._threadsafe_async)
def destroy(self):
cdef libev.ev_loop* ptr = self._ptr
self._ptr = NULL
if ptr:
if not libev.ev_userdata(ptr):
# Whoops! Program error. They destroyed the loop,
# using a different loop object. Our _ptr is still
# valid, but the libev loop is gone. Doing anything
# else with it will likely cause a crash.
return
# Mark as destroyed
self._stop_watchers(ptr)
libev.ev_set_userdata(ptr, NULL)
if SYSERR_CALLBACK == self._handle_syserr:
set_syserr_cb(None)
libev.ev_loop_destroy(ptr)
def __dealloc__(self):
cdef libev.ev_loop* ptr = self._ptr
self._ptr = NULL
if ptr != NULL:
if not libev.ev_userdata(ptr):
# See destroy(). This is a bug in the caller.
return
self._stop_watchers(ptr)
if not self._default:
libev.ev_loop_destroy(ptr)
# Mark as destroyed
libev.ev_set_userdata(ptr, NULL)
@property
def ptr(self):
return <size_t>self._ptr
@property
def WatcherType(self):
return watcher
@property
def MAXPRI(self):
return libev.EV_MAXPRI
@property
def MINPRI(self):
return libev.EV_MINPRI
def _handle_syserr(self, message, errno):
if sys.version_info[0] >= 3:
message = message.decode()
self.handle_error(None, SystemError, SystemError(message + ': ' + os.strerror(errno)), None)
cpdef handle_error(self, context, type, value, tb):
cdef object handle_error
cdef object error_handler = self.error_handler
if type is HubDestroyed:
self._callbacks.clear()
self.break_()
return
if error_handler is not None:
# we do want to do getattr every time so that setting Hub.handle_error property just works
handle_error = getattr(error_handler, 'handle_error', error_handler)
handle_error(context, type, value, tb)
else:
self._default_handle_error(context, type, value, tb)
cpdef _default_handle_error(self, context, type, value, tb):
# note: Hub sets its own error handler so this is not used by gevent
# this is here to make core.loop usable without the rest of gevent
traceback.print_exception(type, value, tb)
if self._ptr:
libev.ev_break(self._ptr, libev.EVBREAK_ONE)
def run(self, nowait=False, once=False):
_check_loop(self)
cdef unsigned int flags = 0
if nowait:
flags |= libev.EVRUN_NOWAIT
if once:
flags |= libev.EVRUN_ONCE
with nogil:
libev.ev_run(self._ptr, flags)
def reinit(self):
if self._ptr:
libev.ev_loop_fork(self._ptr)
def ref(self):
_check_loop(self)
libev.ev_ref(self._ptr)
def unref(self):
_check_loop(self)
libev.ev_unref(self._ptr)
def break_(self, int how=libev.EVBREAK_ONE):
_check_loop(self)
libev.ev_break(self._ptr, how)
def verify(self):
_check_loop(self)
libev.ev_verify(self._ptr)
cpdef libev.ev_tstamp now(self) except *:
_check_loop(self)
return libev.ev_now(self._ptr)
cpdef void update_now(self) except *:
_check_loop(self)
libev.ev_now_update(self._ptr)
update = update_now # Old name, deprecated.
def __repr__(self):
return '<%s at 0x%x %s>' % (self.__class__.__name__, id(self), self._format())
@property
def default(self):
# If we're destroyed, we are not the default loop anymore,
# as far as Python is concerned.
return self._default if self._ptr else False
@property
def iteration(self):
_check_loop(self)
return libev.ev_iteration(self._ptr)
@property
def depth(self):
_check_loop(self)
return libev.ev_depth(self._ptr)
@property
def backend_int(self):
_check_loop(self)
return libev.ev_backend(self._ptr)
@property
def backend(self):
_check_loop(self)
cdef unsigned int backend = libev.ev_backend(self._ptr)
for key, value in _flags:
if key == backend:
return value
return backend
@property
def pendingcnt(self):
_check_loop(self)
return libev.ev_pending_count(self._ptr)
def io(self, libev.vfd_socket_t fd, int events, ref=True, priority=None):
return io(self, fd, events, ref, priority)
def closing_fd(self, libev.vfd_socket_t fd):
_check_loop(self)
cdef int pending_before = libev.ev_pending_count(self._ptr)
libev.ev_feed_fd_event(self._ptr, fd, 0xFFFF)
cdef int pending_after = libev.ev_pending_count(self._ptr)
return pending_after > pending_before
def timer(self, double after, double repeat=0.0, ref=True, priority=None):
return timer(self, after, repeat, ref, priority)
def signal(self, int signum, ref=True, priority=None):
return signal(self, signum, ref, priority)
def idle(self, ref=True, priority=None):
return idle(self, ref, priority)
def prepare(self, ref=True, priority=None):
return prepare(self, ref, priority)
def check(self, ref=True, priority=None):
return check(self, ref, priority)
def fork(self, ref=True, priority=None):
return fork(self, ref, priority)
def async_(self, ref=True, priority=None):
return async_(self, ref, priority)
# cython doesn't enforce async as a keyword
async = async_
def child(self, int pid, bint trace=0, ref=True):
if sys.platform == 'win32':
raise AttributeError("Child watchers are not supported on Windows")
return child(self, pid, trace, ref)
def install_sigchld(self):
libev.gevent_install_sigchld_handler()
def reset_sigchld(self):
libev.gevent_reset_sigchld_handler()
def stat(self, str path, float interval=0.0, ref=True, priority=None):
return stat(self, path, interval, ref, priority)
def run_callback(self, func, *args):
_check_loop(self)
cdef callback cb = callback(func, args)
self._callbacks.append(cb)
libev.ev_ref(self._ptr)
return cb
def run_callback_threadsafe(self, func, *args):
# We rely on the GIL to make this threadsafe.
cb = self.run_callback(func, *args)
libev.ev_async_send(self._ptr, &self._threadsafe_async)
return cb
def _format(self):
if not self._ptr:
return 'destroyed'
cdef object msg = self.backend
if self._default:
msg += ' default'
msg += ' pending=%s' % self.pendingcnt
msg += self._format_details()
return msg
def _format_details(self):
cdef str msg = ''
cdef object fileno = self.fileno()
cdef object activecnt = None
try:
activecnt = self.activecnt
except AttributeError:
pass
if activecnt is not None:
msg += ' ref=' + repr(activecnt)
if fileno is not None:
msg += ' fileno=' + repr(fileno)
return msg
def fileno(self):
cdef int fd
if self._ptr:
fd = libev.gevent_ev_loop_backend_fd(self._ptr)
if fd >= 0:
return fd
@property
def activecnt(self):
_check_loop(self)
return libev.gevent_ev_loop_activecnt(self._ptr)
@property
def sig_pending(self):
_check_loop(self)
return libev.gevent_ev_loop_sig_pending(self._ptr)
@property
def origflags(self):
return _flags_to_list(self.origflags_int)
@property
def origflags_int(self):
_check_loop(self)
return libev.gevent_ev_loop_origflags(self._ptr)
@property
def sigfd(self):
_check_loop(self)
fd = libev.gevent_ev_loop_sigfd(self._ptr)
if fd >= 0:
return fd
# Explicitly not EV_USE_SIGNALFD
raise AttributeError("sigfd")
from zope.interface import classImplements
# XXX: This invokes the side-table lookup, we would
# prefer to have it stored directly on the class. That means we
# need a class variable ``__implemented__``, but that's hard in
# Cython
from gevent._interfaces import ILoop
from gevent._interfaces import ICallback
classImplements(loop, ILoop)
classImplements(callback, ICallback)
cdef extern from *:
"""
#define FLAG_WATCHER_OWNS_PYREF (1 << 0) /* 0x1 */
#define FLAG_WATCHER_NEEDS_EVREF (1 << 1) /* 0x2 */
#define FLAG_WATCHER_UNREF_BEFORE_START (1 << 2) /* 0x4 */
#define FLAG_WATCHER_MASK_UNREF_NEEDS_REF 0x6
"""
# about readonly _flags attribute:
# bit #1 set if object owns Python reference to itself (Py_INCREF was
# called and we must call Py_DECREF later)
unsigned int FLAG_WATCHER_OWNS_PYREF
# bit #2 set if ev_unref() was called and we must call ev_ref() later
unsigned int FLAG_WATCHER_NEEDS_EVREF
# bit #3 set if user wants to call ev_unref() before start()
unsigned int FLAG_WATCHER_UNREF_BEFORE_START
# bits 2 and 3 are *both* set when we are active, but the user
# request us not to be ref'd anymore. We unref us (because going active will
# ref us) and then make a note of this in the future
unsigned int FLAG_WATCHER_MASK_UNREF_NEEDS_REF
cdef void _python_incref(watcher self):
if not self._flags & FLAG_WATCHER_OWNS_PYREF:
Py_INCREF(self)
self._flags |= FLAG_WATCHER_OWNS_PYREF
cdef void _python_decref(watcher self):
if self._flags & FLAG_WATCHER_OWNS_PYREF:
Py_DECREF(self)
self._flags &= ~FLAG_WATCHER_OWNS_PYREF
cdef void _libev_ref(watcher self):
if self._flags & FLAG_WATCHER_NEEDS_EVREF:
libev.ev_ref(self.loop._ptr)
self._flags &= ~FLAG_WATCHER_NEEDS_EVREF
cdef void _libev_unref(watcher self):
if self._flags & FLAG_WATCHER_MASK_UNREF_NEEDS_REF == FLAG_WATCHER_UNREF_BEFORE_START:
libev.ev_unref(self.loop._ptr)
self._flags |= FLAG_WATCHER_NEEDS_EVREF
ctypedef void (*start_stop_func)(libev.ev_loop*, void*) nogil
cdef struct start_and_stop:
start_stop_func start
start_stop_func stop
cdef start_and_stop make_ss(void* start, void* stop):
cdef start_and_stop result = start_and_stop(<start_stop_func>start, <start_stop_func>stop)
return result
cdef bint _watcher_start(watcher self, object callback, tuple args) except -1:
# This method should be called by subclasses of watcher, if they
# override the python-level `start` function: they've already paid
# for argument unpacking, and `start` cannot be cpdef since it
# uses varargs.
# We keep this as a function, not a cdef method of watcher.
# If it's a cdef method, it could potentially be overridden
# by a subclass, which means that the watcher gains a pointer to a
# function table (vtable), making each object 8 bytes larger.
_check_loop(self.loop)
if callback is None or not callable(callback):
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
self.args = args
_libev_unref(self)
_python_incref(self)
self._w_ss.start(self.loop._ptr, self._w_watcher)
return 1
cdef public class watcher [object PyGeventWatcherObject, type PyGeventWatcher_Type]:
"""Abstract base class for all the watchers"""
## pointer members
cdef public loop loop
cdef object _callback
cdef public tuple args
# By keeping a _w_watcher cached, the size of the io and timer
# structs becomes 152 bytes and child is 160 and stat is 512 (when
# the start_and_stop is inlined). On 64-bit macOS CPython 2.7. I
# hoped that using libev's data pointer and allocating the
# watchers directly and not as inline members would result in
# overall savings thanks to better padding, but it didn't. And it
# added lots of casts, making the code ugly.
# Table:
# gevent ver | 1.2 | This | +data
# Watcher Kind | | |
# Timer | 120 | 152 | 160
# IO | 120 | 152 | 160
# Child | 128 | 160 | 168
# Stat | 480 | 512 | 512
cdef libev.ev_watcher* _w_watcher
# By inlining the start_and_stop struct, instead of taking the address
# of a static struct or using the watcher's data pointer, we
# use an additional pointer of memory and incur an additional pointer copy
# on creation.
# But we use fewer pointer accesses for start/stop, and they have
# better cache locality. (Then again, we're bigger).
# Right now we're going for size, so we use the pointer. IO/Timer objects
# are then 144 bytes.
cdef start_and_stop* _w_ss
## Int members
# Our subclasses will declare the ev_X struct
# as an inline member. This is good for locality, but
# probably bad for alignment, as it will get tacked on
# immediately after our data.
# But all ev_watchers start with some ints, so maybe we can help that
# out by putting our ints here.
cdef readonly unsigned int _flags
def __init__(self, loop loop, ref=True, priority=None):
if not self._w_watcher or not self._w_ss.start or not self._w_ss.stop:
raise ValueError("Cannot construct a bare watcher")
self.loop = loop
self._flags = 0 if ref else FLAG_WATCHER_UNREF_BEFORE_START
if priority is not None:
libev.ev_set_priority(self._w_watcher, priority)
@property
def ref(self):
return False if self._flags & 4 else True
@ref.setter
def ref(self, object value):
_check_loop(self.loop)
if value:
# self.ref should be true after this.
if self.ref:
return # ref is already True
if self._flags & FLAG_WATCHER_NEEDS_EVREF: # ev_unref was called, undo
libev.ev_ref(self.loop._ptr)
# do not want unref, no outstanding unref
self._flags &= ~FLAG_WATCHER_MASK_UNREF_NEEDS_REF
else:
# self.ref must be false after this
if not self.ref:
return # ref is already False
self._flags |= FLAG_WATCHER_UNREF_BEFORE_START
if not self._flags & FLAG_WATCHER_NEEDS_EVREF and libev.ev_is_active(self._w_watcher):
libev.ev_unref(self.loop._ptr)
self._flags |= FLAG_WATCHER_NEEDS_EVREF
@property
def callback(self):
return self._callback
@callback.setter
def callback(self, object callback):
if callback is not None and not callable(callback):
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
@property
def priority(self):
return libev.ev_priority(self._w_watcher)
@priority.setter
def priority(self, int priority):
cdef libev.ev_watcher* w = self._w_watcher
if libev.ev_is_active(w):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(w, priority)
@property
def active(self):
return True if libev.ev_is_active(self._w_watcher) else False
@property
def pending(self):
return True if libev.ev_is_pending(self._w_watcher) else False
def start(self, object callback, *args):
_watcher_start(self, callback, args)
def stop(self):
_check_loop(self.loop)
_libev_ref(self)
# The callback cannot possibly fire while we are executing,
# so this is safe.