Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SystemLayerImplSelect: add libev support: CHIP_SYSTEM_CONFIG_USE_LIBEV #22043

Closed
wants to merge 2 commits into from

Conversation

plan44
Copy link
Contributor

@plan44 plan44 commented Aug 19, 2022

When CHIP_SYSTEM_CONFIG_USE_LIBEV is set, SystemLayerImplSelect expects a libev [1] mainloop to be present to schedule timers and socket watches (similar to CHIP_SYSTEM_CONFIG_USE_DISPATCH for Darwin).

A libev mainloop must be passed to SystemLayer using SetLibEvLoop() before any timers or socket watches are used - otherwise, chipDie() is invoked.

Usage

The entire project needs to be build with CHIP_SYSTEM_CONFIG_USE_LIBEV=1 (this can be done via invoking a project-specific extra config via the default_configs_extra argument in args.gni)

Setting up the libev mainloop and handing it over to SystemLayer must be done in application specific code, outside the code provided by chip examples. Also adding libev as a dependency must be done in the application's BUILD.gn.

Background

libev is a multi-platform event library often used in embedded linux context to handle events, and builds the basis for various libraries with non-blocking APIs. This changeset allows using the connectedhomeip stack with libev based applications.

Testing

  • verified with regular chip-tool build that change does not affect builds with CHIP_SYSTEM_CONFIG_USE_LIBEV not set.
  • tested with an application which bases on libev and sets CHIP_SYSTEM_CONFIG_USE_LIBEV=1: chip timer and socket watches work the same way as with select or dispatch based applications.

@github-actions
Copy link

github-actions bot commented Aug 19, 2022

PR #22043: Size comparison from 67d6821 to 6a0d64c

Increases (10 builds for bl602, cc13x2_26x2, esp32, nrfconnect, psoc6, telink)
platform target config section 67d6821 6a0d64c change % change
bl602 lighting-app bl602 .text 1051040 1051044 4 0.0
cc13x2_26x2 lock-mtd LP_CC2652R7 (read/write) 180552 180560 8 0.0
esp32 all-clusters-app c3devkit (read only) 1031268 1031270 2 0.0
.flash.text 1031268 1031270 2 0.0
nrfconnect all-clusters-minimal-app nrf52840dk_nrf52840 text 803068 803072 4 0.0
psoc6 all-clusters cy8ckit_062s2_43012 .debug_line 3646638 3646639 1 0.0
all-clusters-minimal cy8ckit_062s2_43012 .debug_line 3667042 3667043 1 0.0
light cy8ckit_062s2_43012 .debug_line 3238220 3238221 1 0.0
lock cy8ckit_062s2_43012 .debug_info 22199614 22199616 2 0.0
.debug_line 3246906 3246907 1 0.0
telink light-switch-app tlsr9518adk80d (read/write) 808736 808744 8 0.0
text 571314 571318 4 0.0
lighting-app tlsr9518adk80d (read/write) 830672 830680 8 0.0
text 589404 589406 2 0.0
Decreases (2 builds for cc13x2_26x2, psoc6)
platform target config section 67d6821 6a0d64c change % change
cc13x2_26x2 lock-mtd LP_CC2652R7 (read only) 656831 656823 -8 -0.0
.text 554592 554584 -8 -0.0
psoc6 light cy8ckit_062s2_43012 .debug_info 21844447 21844446 -1 -0.0
Full report (43 builds for bl602, cc13x2_26x2, cyw30739, efr32, esp32, k32w, linux, mbed, nrfconnect, psoc6, telink)
platform target config section 67d6821 6a0d64c change % change
bl602 lighting-app bl602 (read/write) 1383858 1383858 0 0.0
.bss 120258 120258 0 0.0
.data 4480 4480 0 0.0
.text 1051040 1051044 4 0.0
bl602+rpc (read/write) 1429370 1429370 0 0.0
.bss 127698 127698 0 0.0
.data 4600 4600 0 0.0
.text 1082800 1082800 0 0.0
cc13x2_26x2 all-clusters-app LP_CC2652R7 (read only) 673027 673027 0 0.0
(read/write) 178468 178468 0 0.0
.bss 74388 74388 0 0.0
.data 3372 3372 0 0.0
.rodata 88835 88835 0 0.0
.text 583876 583876 0 0.0
all-clusters-minimal-app LP_CC2652R7 (read only) 637747 637747 0 0.0
(read/write) 157948 157948 0 0.0
.bss 73660 73660 0 0.0
.data 3372 3372 0 0.0
.rodata 77979 77979 0 0.0
.text 559444 559444 0 0.0
lock-ftd LP_CC2652R7 (read only) 674079 674079 0 0.0
(read/write) 167616 167616 0 0.0
.bss 71476 71476 0 0.0
.data 3296 3296 0 0.0
.rodata 76671 76671 0 0.0
.text 596928 596928 0 0.0
lock-mtd LP_CC2652R7 (read only) 656831 656823 -8 -0.0
(read/write) 180552 180560 8 0.0
.bss 67164 67164 0 0.0
.data 3296 3296 0 0.0
.rodata 101759 101759 0 0.0
.text 554592 554584 -8 -0.0
pump-app LP_CC2652R7 (read only) 684751 684751 0 0.0
(read/write) 157752 157752 0 0.0
.bss 71516 71516 0 0.0
.data 3296 3296 0 0.0
.rodata 89959 89959 0 0.0
.text 594308 594308 0 0.0
pump-controller-app LP_CC2652R7 (read only) 669243 669243 0 0.0
(read/write) 173380 173380 0 0.0
.bss 71636 71636 0 0.0
.data 3292 3292 0 0.0
.rodata 85515 85515 0 0.0
.text 583248 583248 0 0.0
shell LP_CC2652R7 (read only) 665710 665710 0 0.0
(read/write) 181304 181304 0 0.0
.bss 76708 76708 0 0.0
.data 3376 3376 0 0.0
.rodata 85782 85782 0 0.0
.text 579612 579612 0 0.0
cyw30739 light cyw930739m2evb_01 (read/write) 586618 586618 0 0.0
.app_xip_area 463284 463284 0 0.0
.bss 65768 65768 0 0.0
.data 744 744 0 0.0
.rodata 0 0 0 0.0
.text 112 112 0 0.0
lock cyw930739m2evb_01 (read/write) 592418 592418 0 0.0
.app_xip_area 464300 464300 0 0.0
.bss 70552 70552 0 0.0
.data 748 748 0 0.0
.rodata 0 0 0 0.0
.text 112 112 0 0.0
ota-requestor-no-progress-logging cyw930739m2evb_01 (read/write) 599538 599538 0 0.0
.app_xip_area 476924 476924 0 0.0
.bss 65080 65080 0 0.0
.data 716 716 0 0.0
.rodata 0 0 0 0.0
.text 112 112 0 0.0
efr32 lighting-app BRD4161A (read/write) 1100980 1100980 0 0.0
.bss 133372 133372 0 0.0
.data 2068 2068 0 0.0
.text 965516 965516 0 0.0
BRD4161A+rpc (read/write) 1155232 1155232 0 0.0
.bss 150060 150060 0 0.0
.data 2280 2280 0 0.0
.text 1002872 1002872 0 0.0
BRD4161A+rs911x (read/write) 990152 990152 0 0.0
.bss 162696 162696 0 0.0
.data 2056 2056 0 0.0
.text 825380 825380 0 0.0
lock-app BRD4161A+wf200 (read/write) 1139636 1139636 0 0.0
.bss 145872 145872 0 0.0
.data 2064 2064 0 0.0
.text 991680 991680 0 0.0
window-app BRD4161A (read/write) 1092420 1092420 0 0.0
.bss 134812 134812 0 0.0
.data 2096 2096 0 0.0
.text 955492 955492 0 0.0
esp32 all-clusters-app c3devkit (read only) 1031268 1031270 2 0.0
(read/write) 1489610 1489610 0 0.0
.dram0.bss 71136 71136 0 0.0
.dram0.data 14600 14600 0 0.0
.flash.rodata 218440 218440 0 0.0
.flash.text 1031268 1031270 2 0.0
.iram0.text 62902 62902 0 0.0
m5stack (read only) 1084371 1084371 0 0.0
(read/write) 491544 491544 0 0.0
.dram0.bss 76640 76640 0 0.0
.dram0.data 34144 34144 0 0.0
.flash.rodata 248764 248764 0 0.0
.flash.text 1078987 1078987 0 0.0
.iram0.text 123267 123267 0 0.0
k32w light k32w0+release (read/write) 646676 646676 0 0.0
.bss 70400 70400 0 0.0
.data 2068 2068 0 0.0
.text 571480 571480 0 0.0
lock k32w0+release (read/write) 704196 704196 0 0.0
.bss 70864 70864 0 0.0
.data 2076 2076 0 0.0
.text 628528 628528 0 0.0
linux all-clusters-app debug (read only) 3038505 3038505 0 0.0
(read/write) 156024 156024 0 0.0
.bss 61920 61920 0 0.0
.data 2096 2096 0 0.0
.data.rel.ro 85624 85624 0 0.0
.dynamic 608 608 0 0.0
.got 4568 4568 0 0.0
.init 27 27 0 0.0
.init_array 1168 1168 0 0.0
.rodata 274667 274667 0 0.0
.text 2584690 2584690 0 0.0
all-clusters-minimal-app debug (read only) 2874353 2874353 0 0.0
(read/write) 147624 147624 0 0.0
.bss 61152 61152 0 0.0
.data 2064 2064 0 0.0
.data.rel.ro 78120 78120 0 0.0
.dynamic 608 608 0 0.0
.got 4488 4488 0 0.0
.init 27 27 0 0.0
.init_array 1152 1152 0 0.0
.rodata 274859 274859 0 0.0
.text 2423122 2423122 0 0.0
bridge-app debug+rpc (read only) 2373001 2373001 0 0.0
(read/write) 127584 127584 0 0.0
.bss 50656 50656 0 0.0
.data 3600 3600 0 0.0
.data.rel.ro 67464 67464 0 0.0
.dynamic 608 608 0 0.0
.got 4392 4392 0 0.0
.init 27 27 0 0.0
.init_array 824 824 0 0.0
.rodata 203496 203496 0 0.0
.text 2006882 2006882 0 0.0
chip-tool debug (read only) 10868673 10868673 0 0.0
(read/write) 651296 651296 0 0.0
.bss 25240 25240 0 0.0
.data 3266 3266 0 0.0
.data.rel.ro 616280 616280 0 0.0
.dynamic 608 608 0 0.0
.got 5096 5096 0 0.0
.init 27 27 0 0.0
.init_array 768 768 0 0.0
.rodata 562677 562677 0 0.0
.text 8799364 8799364 0 0.0
chip-tool-ipv6only arm64 (read only) 10255980 10255980 0 0.0
(read/write) 699105 699105 0 0.0
.bss 33297 33297 0 0.0
.data 3272 3272 0 0.0
.data.rel.ro 643792 643792 0 0.0
.dynamic 560 560 0 0.0
.got 13784 13784 0 0.0
.init 24 24 0 0.0
.init_array 192 192 0 0.0
.rodata 493340 493340 0 0.0
.text 8124596 8124596 0 0.0
lighting-app debug+rpc (read only) 2597033 2597033 0 0.0
(read/write) 130176 130176 0 0.0
.bss 49760 49760 0 0.0
.data 2096 2096 0 0.0
.data.rel.ro 72360 72360 0 0.0
.dynamic 608 608 0 0.0
.got 4392 4392 0 0.0
.init 27 27 0 0.0
.init_array 920 920 0 0.0
.rodata 220304 220304 0 0.0
.text 2206018 2206018 0 0.0
lock-app debug (read only) 2580929 2580929 0 0.0
(read/write) 125512 125512 0 0.0
.bss 48288 48288 0 0.0
.data 1712 1712 0 0.0
.data.rel.ro 69512 69512 0 0.0
.dynamic 608 608 0 0.0
.got 4464 4464 0 0.0
.init 27 27 0 0.0
.init_array 896 896 0 0.0
.rodata 237360 237360 0 0.0
.text 2176914 2176914 0 0.0
ota-provider-app debug (read only) 2358105 2358105 0 0.0
(read/write) 118976 118976 0 0.0
.bss 47808 47808 0 0.0
.data 1936 1936 0 0.0
.data.rel.ro 63336 63336 0 0.0
.dynamic 608 608 0 0.0
.got 4488 4488 0 0.0
.init 27 27 0 0.0
.init_array 760 760 0 0.0
.rodata 209336 209336 0 0.0
.text 1985298 1985298 0 0.0
ota-requestor-app debug (read only) 2523369 2523369 0 0.0
(read/write) 127320 127320 0 0.0
.bss 50336 50336 0 0.0
.data 2304 2304 0 0.0
.data.rel.ro 68728 68728 0 0.0
.dynamic 608 608 0 0.0
.got 4480 4480 0 0.0
.init 27 27 0 0.0
.init_array 848 848 0 0.0
.rodata 216160 216160 0 0.0
.text 2134642 2134642 0 0.0
shell debug (read only) 2606793 2606793 0 0.0
(read/write) 142144 142144 0 0.0
.bss 57832 57832 0 0.0
.data 1264 1264 0 0.0
.data.rel.ro 77224 77224 0 0.0
.dynamic 608 608 0 0.0
.got 4136 4136 0 0.0
.init 27 27 0 0.0
.init_array 1040 1040 0 0.0
.rodata 234770 234770 0 0.0
.text 2213602 2213602 0 0.0
thermostat-no-ble arm64 (read only) 2357300 2357300 0 0.0
(read/write) 141825 141825 0 0.0
.bss 55345 55345 0 0.0
.data 1672 1672 0 0.0
.data.rel.ro 75984 75984 0 0.0
.dynamic 560 560 0 0.0
.got 5048 5048 0 0.0
.init 24 24 0 0.0
.init_array 408 408 0 0.0
.rodata 140620 140620 0 0.0
.text 1978960 1978960 0 0.0
tv-app debug (read only) 3173993 3173993 0 0.0
(read/write) 257968 257968 0 0.0
.bss 167480 167480 0 0.0
.data 4736 4736 0 0.0
.data.rel.ro 79184 79184 0 0.0
.dynamic 608 608 0 0.0
.got 4856 4856 0 0.0
.init 27 27 0 0.0
.init_array 1072 1072 0 0.0
.rodata 258952 258952 0 0.0
.text 2725298 2725298 0 0.0
tv-casting-app debug (read only) 5466769 5466769 0 0.0
(read/write) 160400 160400 0 0.0
.bss 51448 51448 0 0.0
.data 2432 2432 0 0.0
.data.rel.ro 100088 100088 0 0.0
.dynamic 608 608 0 0.0
.got 4776 4776 0 0.0
.init 27 27 0 0.0
.init_array 1040 1040 0 0.0
.rodata 343729 343729 0 0.0
.text 4851522 4851522 0 0.0
mbed lock-app CY8CPROTO_062_4343W+release (read only) 6224 6224 0 0.0
(read/write) 2454328 2454328 0 0.0
.bss 215044 215044 0 0.0
.data 5872 5872 0 0.0
.text 1416972 1416972 0 0.0
nrfconnect all-clusters-app nrf52840dk_nrf52840 (read/write) 1180603 1180603 0 0.0
bss 143737 143737 0 0.0
rodata 143356 143356 0 0.0
text 814664 814664 0 0.0
all-clusters-minimal-app nrf52840dk_nrf52840 (read/write) 1159799 1159799 0 0.0
bss 142964 142964 0 0.0
rodata 134944 134944 0 0.0
text 803068 803072 4 0.0
psoc6 all-clusters cy8ckit_062s2_43012 (read only) 881000 881000 0 0.0
(read/write) 1700996 1700996 0 0.0
.ARM.attributes 46 46 0 0.0
.ARM.exidx 8 8 0 0.0
.bss 149688 149688 0 0.0
.comment 204 204 0 0.0
.copy.table 24 24 0 0.0
.cy_m0p_image 6216 6216 0 0.0
.cy_sharedmem 8 8 0 0.0
.data 2656 2656 0 0.0
.debug_abbrev 1217459 1217459 0 0.0
.debug_aranges 111528 111528 0 0.0
.debug_frame 372352 372352 0 0.0
.debug_info 26643196 26643196 0 0.0
.debug_line 3646638 3646639 1 0.0
.debug_loc 3562036 3562036 0 0.0
.debug_ranges 336576 336576 0 0.0
.debug_str 3393852 3393852 0 0.0
.heap 881000 881000 0 0.0
.noinit 148 148 0 0.0
.ramVectors 736 736 0 0.0
.shstrtab 288 288 0 0.0
.stab 156 156 0 0.0
.stabstr 335 335 0 0.0
.stack_dummy 4096 4096 0 0.0
.strtab 569346 569346 0 0.0
.symtab 420416 420416 0 0.0
.text 1540264 1540264 0 0.0
.zero.table 8 8 0 0.0
text 0 0 0 0.0
all-clusters-minimal cy8ckit_062s2_43012 (read only) 881736 881736 0 0.0
(read/write) 1644204 1644204 0 0.0
.ARM.attributes 46 46 0 0.0
.ARM.exidx 8 8 0 0.0
.bss 148952 148952 0 0.0
.comment 204 204 0 0.0
.copy.table 24 24 0 0.0
.cy_m0p_image 6216 6216 0 0.0
.cy_sharedmem 8 8 0 0.0
.data 2656 2656 0 0.0
.debug_abbrev 1209298 1209298 0 0.0
.debug_aranges 111000 111000 0 0.0
.debug_frame 375432 375432 0 0.0
.debug_info 26379890 26379890 0 0.0
.debug_line 3667042 3667043 1 0.0
.debug_loc 3549673 3549673 0 0.0
.debug_ranges 335192 335192 0 0.0
.debug_str 3382841 3382841 0 0.0
.heap 881736 881736 0 0.0
.noinit 148 148 0 0.0
.ramVectors 736 736 0 0.0
.shstrtab 288 288 0 0.0
.stab 156 156 0 0.0
.stabstr 335 335 0 0.0
.stack_dummy 4096 4096 0 0.0
.strtab 533820 533820 0 0.0
.symtab 407008 407008 0 0.0
.text 1484208 1484208 0 0.0
.zero.table 0 0 0 0.0
8 8 0 0.0
light cy8ckit_062s2_43012 (read only) 890080 890080 0 0.0
(read/write) 1561436 1561436 0 0.0
.ARM.attributes 46 46 0 0.0
.ARM.exidx 8 8 0 0.0
.bss 140816 140816 0 0.0
.comment 204 204 0 0.0
.copy.table 24 24 0 0.0
.cy_m0p_image 6216 6216 0 0.0
.cy_sharedmem 8 8 0 0.0
.data 2448 2448 0 0.0
.debug_abbrev 1043971 1043971 0 0.0
.debug_aranges 103168 103168 0 0.0
.debug_frame 345676 345676 0 0.0
.debug_info 21844447 21844446 -1 -0.0
.debug_line 3238220 3238221 1 0.0
.debug_loc 3249205 3249205 0 0.0
.debug_ranges 301032 301032 0 0.0
.debug_str 3188935 3188935 0 0.0
.heap 890080 890080 0 0.0
.noinit 148 148 0 0.0
.ramVectors 736 736 0 0.0
.shstrtab 288 288 0 0.0
.stab 156 156 0 0.0
.stabstr 335 335 0 0.0
.stack_dummy 4096 4096 0 0.0
.strtab 467101 467101 0 0.0
.symtab 374064 374064 0 0.0
.text 1409784 1409784 0 0.0
.zero.table 0 0 0 0.0
8 8 0 0.0
lock cy8ckit_062s2_43012 (read only) 885584 885584 0 0.0
(read/write) 1598708 1598708 0 0.0
.ARM.attributes 46 46 0 0.0
.ARM.exidx 8 8 0 0.0
.bss 145296 145296 0 0.0
.comment 204 204 0 0.0
.copy.table 24 24 0 0.0
.cy_m0p_image 6216 6216 0 0.0
.cy_sharedmem 8 8 0 0.0
.data 2464 2464 0 0.0
.debug_abbrev 1051147 1051147 0 0.0
.debug_aranges 103840 103840 0 0.0
.debug_frame 348500 348500 0 0.0
.debug_info 22199614 22199616 2 0.0
.debug_line 3246906 3246907 1 0.0
.debug_loc 3289325 3289325 0 0.0
.debug_ranges 304448 304448 0 0.0
.debug_str 3216366 3216366 0 0.0
.heap 885584 885584 0 0.0
.noinit 148 148 0 0.0
.ramVectors 736 736 0 0.0
.shstrtab 288 288 0 0.0
.stab 156 156 0 0.0
.stabstr 335 335 0 0.0
.stack_dummy 4096 4096 0 0.0
.strtab 473342 473342 0 0.0
.symtab 377248 377248 0 0.0
.text 1442560 1442560 0 0.0
.zero.table 0 0 0 0.0
8 8 0 0.0
telink light-switch-app tlsr9518adk80d (read/write) 808736 808744 8 0.0
bss 71448 71448 0 0.0
noinit 43488 43488 0 0.0
text 571314 571318 4 0.0
lighting-app tlsr9518adk80d (read/write) 830672 830680 8 0.0
bss 72304 72304 0 0.0
noinit 43488 43488 0 0.0
text 589404 589406 2 0.0

@@ -283,6 +325,24 @@ CHIP_ERROR LayerImplSelect::RequestCallbackOnPendingRead(SocketWatchToken token)
dispatch_activate(watch->mRdSource);
}
}
#elif CHIP_SYSTEM_CONFIG_USE_LIBEV
if (mLibEvLoopP == nullptr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes CHIP_SYSTEM_CONFIG_USE_LIBEV mutually exclusive with CHIP_SYSTEM_CONFIG_USE_DISPATCH. But in the Darwin platform manager code the ifdefs are assuming both could be defined at the same time, no?

I think we should stick to them being mutually exclusive and static_asserting that as needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I started with #if/#endif because this gives clearer blocks in diffs and does not touch the existing code. But it was not possible to keep it that way in many places, so sticking with #if/#elif in all places is probably better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See new commit, now changed to #if/#elif throughout.

@@ -322,10 +382,27 @@ CHIP_ERROR LayerImplSelect::RequestCallbackOnPendingWrite(SocketWatchToken token
}
});
// only now we are sure the source exists and can become active
watch->mPendingIO.Set(SocketEventFlags::kWrite);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that was a functionally irrelevant leftover I found after #21349. Does not belong here, but was not worth a separate PR, and then I forgot. It originated in a then abandoned version of the code that tried to suspend/resume the source watching, and needed the flag to set late to avoid race conditions. But in the version accepted the flag is being set already at the top of the method. Same in RequestCallbackOnPendingRead but there I deleted that line in #21349 already.

Comment on lines 502 to 595
#endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be an editing accident, possibly because this is one of the cases that should become #elif as per discussion above, but I was still trying to keep the separate #if blocks.

#if CHIP_SYSTEM_CONFIG_USE_LIBEV
#include <ev.h>
#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
#error "CHIP_SYSTEM_CONFIG_USE_LIBEV and CHIP_SYSTEM_CONFIG_USE_DISPATCH are mutually exclusive"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so they are mutually exclusive, so why the platform manager changes on Darwin?

The Darwin platform manager always uses dispatch, period.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Darwin platform manager always uses dispatch, period.

Not necessarily ;-) Using dispatch for I/O and timers is a decision orthogonal to using the Darwin platform manager. In fact, for development of my libev based app (on the mac, in XCode, just because it's so much more comfortable to use…), I use the Darwin platform, but just swap dispatch for libev.
Of course, this is never going to be a production app configuration, so I'd understand you wouldn't want it in mainline. On the other hand, these types of unorthodox setups tend to reveal otherwise hard to find platform dependencies and edge cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use the Darwin platform, but just swap dispatch for libev.

How do you guarantee that I/O processing is happening on the right dispatch queue in that case, and in particular that it does not race with other Matter things that do run on that dispatch queue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you guarantee that I/O processing is happening on the right dispatch queue in that case

Without CHIP_SYSTEM_CONFIG_USE_DISPATCH, nothing in the non-Darwin-specific code can run anywhere else than under libev control (timer and IO callbacks) on the same thread/queue.

Only Darwin specific code could indirectly make use of APIs that would call back from other queue/thread contexts. Of those, my already-on-network app only uses dns-sd and only for publishing. I cannot guarantee anything here, I just tried it works in my context, which is, as said, not a setup for a productive build, but just a development convenience. For that, the possible dangers are isolated enough.

Bottom line - I'd understand if you didn't want #ifs in the platform manager that only support a non-100% safe development setup. You decide 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without CHIP_SYSTEM_CONFIG_USE_DISPATCH, nothing in the non-Darwin-specific code can run anywhere else than under libev control (timer and IO callbacks) on the same thread/queue.

Does this assume that something other than src/platform/darwin/PlatformManagerImpl.cpp is being used as the platform manager? There's a not-entirely-obvious dependency here where ScheduleLambda on the System::Layer will run it via PostEvent on the platform manager...

And of course various things directly PostEvent and whatnot.

If you're not using the platform manager's event loop at all (so not calling RunEventLoop or StartEventLoopTask), then I'd like to understand the setup and what things are running. If you are, then the assumption is that PostEvent and whatever System::Layer is doing run things on the same thread, or at least interlocked sanely so they can't run at the same time.

Mostly I feel that event loops and the stuff around them are complex enough people get confused easily, so I would rather not have code around that makes them more confused.

plan44 added a commit to plan44/connectedhomeip that referenced this pull request Aug 29, 2022
- as discussed in project-chip#22043
- also add conditional definition for `ev_io_modify()` required for older libev versions (taken from current libev source, blown up to multiline by clang-format)
plan44 added a commit to plan44/connectedhomeip that referenced this pull request Aug 29, 2022
- as discussed in project-chip#22043
- also add conditional definition for `ev_io_modify()` required for older libev versions (taken from current libev source, blown up to multiline by clang-format)
@plan44 plan44 force-pushed the PR/add-support-for-libev branch from 5d66185 to c4bf883 Compare August 29, 2022 21:19
plan44 added 2 commits August 29, 2022 23:28
…LIBEV=1

When CHIP_SYSTEM_CONFIG_USE_LIBEV is set, SystemLayerImplSelect expects a *libev* mainloop to be present to schedule timers and socket watches (similar to CHIP_SYSTEM_CONFIG_USE_DISPATCH for Darwin).

A libev mainloop must be passed to SystemLayer using `SetLibEvLoop()` before any timers or socket watches are used - otherwise, `chipDie()` is invoked.

# Usage
The entire project needs to be build with `CHIP_SYSTEM_CONFIG_USE_LIBEV=1` (this can be done via invoking a project-specific extra config via the `default_configs_extra` argument in args.gni)

Setting up the libev mainloop and handing it over to SystemLayer must be done in application specific code, outside the code provided by chip examples. Also adding libev as a dependency must be done in the application's BUILD.gn.

# Background
*libev* is a multi-platform event library often used in embedded linux context to handle events, and builds the basis for various libraries with non-blocking APIs. This changeset allows using the *connectedhomeip* stack with libev based applications.
- as discussed in project-chip#22043
- also add conditional definition for `ev_io_modify()` required for older libev versions (taken from current libev source, blown up to multiline by clang-format)
@plan44 plan44 force-pushed the PR/add-support-for-libev branch from 6918036 to 36e7aa5 Compare August 29, 2022 21:29
@github-actions
Copy link

github-actions bot commented Aug 29, 2022

PR #22043: Size comparison from 6329339 to 36e7aa5

Increases (5 builds for cc13x2_26x2, psoc6)
platform target config section 6329339 36e7aa5 change % change
cc13x2_26x2 lock-ftd LP_CC2652R7 (read/write) 167680 167688 8 0.0
psoc6 all-clusters cy8ckit_062s2_43012 .debug_info 26696028 26696030 2 0.0
.debug_line 3655093 3655094 1 0.0
all-clusters-minimal cy8ckit_062s2_43012 .debug_info 26432723 26432724 1 0.0
.debug_line 3675609 3675610 1 0.0
light cy8ckit_062s2_43012 .debug_line 3246078 3246079 1 0.0
lock cy8ckit_062s2_43012 .debug_info 22275869 22275870 1 0.0
.debug_line 3254895 3254896 1 0.0
Decreases (3 builds for cc13x2_26x2, esp32)
platform target config section 6329339 36e7aa5 change % change
cc13x2_26x2 lock-ftd LP_CC2652R7 (read only) 674023 674015 -8 -0.0
.text 596872 596864 -8 -0.0
esp32 all-clusters-app c3devkit (read only) 1033498 1033494 -4 -0.0
.flash.text 1033498 1033494 -4 -0.0
m5stack (read/write) 490780 490772 -8 -0.0
.flash.rodata 247352 247344 -8 -0.0
Full report (43 builds for bl602, cc13x2_26x2, cyw30739, efr32, esp32, k32w, linux, mbed, nrfconnect, psoc6, telink)
platform target config section 6329339 36e7aa5 change % change
bl602 lighting-app bl602 (read/write) 1385062 1385062 0 0.0
.bss 120274 120274 0 0.0
.data 4488 4488 0 0.0
.text 1051464 1051464 0 0.0
bl602+rpc (read/write) 1430958 1430958 0 0.0
.bss 127706 127706 0 0.0
.data 4600 4600 0 0.0
.text 1083480 1083480 0 0.0
cc13x2_26x2 all-clusters-app LP_CC2652R7 (read only) 672979 672979 0 0.0
(read/write) 178412 178412 0 0.0
.bss 74284 74284 0 0.0
.data 3372 3372 0 0.0
.rodata 88835 88835 0 0.0
.text 583828 583828 0 0.0
all-clusters-minimal-app LP_CC2652R7 (read only) 637683 637683 0 0.0
(read/write) 157844 157844 0 0.0
.bss 73556 73556 0 0.0
.data 3372 3372 0 0.0
.rodata 77979 77979 0 0.0
.text 559380 559380 0 0.0
lock-ftd LP_CC2652R7 (read only) 674023 674015 -8 -0.0
(read/write) 167680 167688 8 0.0
.bss 71484 71484 0 0.0
.data 3296 3296 0 0.0
.rodata 76671 76671 0 0.0
.text 596872 596864 -8 -0.0
lock-mtd LP_CC2652R7 (read only) 656955 656955 0 0.0
(read/write) 180436 180436 0 0.0
.bss 67172 67172 0 0.0
.data 3296 3296 0 0.0
.rodata 101883 101883 0 0.0
.text 554592 554592 0 0.0
pump-app LP_CC2652R7 (read only) 684739 684739 0 0.0
(read/write) 157668 157668 0 0.0
.bss 71420 71420 0 0.0
.data 3296 3296 0 0.0
.rodata 89947 89947 0 0.0
.text 594308 594308 0 0.0
pump-controller-app LP_CC2652R7 (read only) 669239 669239 0 0.0
(read/write) 173280 173280 0 0.0
.bss 71532 71532 0 0.0
.data 3292 3292 0 0.0
.rodata 85503 85503 0 0.0
.text 583256 583256 0 0.0
shell LP_CC2652R7 (read only) 665686 665686 0 0.0
(read/write) 181224 181224 0 0.0
.bss 76604 76604 0 0.0
.data 3376 3376 0 0.0
.rodata 85782 85782 0 0.0
.text 579588 579588 0 0.0
cyw30739 light cyw930739m2evb_01 (read/write) 586690 586690 0 0.0
.app_xip_area 463348 463348 0 0.0
.bss 65776 65776 0 0.0
.data 744 744 0 0.0
.rodata 0 0 0 0.0
.text 112 112 0 0.0
lock cyw930739m2evb_01 (read/write) 592442 592442 0 0.0
.app_xip_area 464316 464316 0 0.0
.bss 70560 70560 0 0.0
.data 748 748 0 0.0
.rodata 0 0 0 0.0
.text 112 112 0 0.0
ota-requestor-no-progress-logging cyw930739m2evb_01 (read/write) 599434 599434 0 0.0
.app_xip_area 476812 476812 0 0.0
.bss 65088 65088 0 0.0
.data 716 716 0 0.0
.rodata 0 0 0 0.0
.text 112 112 0 0.0
efr32 lighting-app BRD4161A (read/write) 1107504 1107504 0 0.0
.bss 136332 136332 0 0.0
.data 2072 2072 0 0.0
.text 969080 969080 0 0.0
BRD4161A+rpc (read/write) 971556 971556 0 0.0
.bss 150844 150844 0 0.0
.data 2252 2252 0 0.0
.text 818440 818440 0 0.0
BRD4161A+rs911x (read/write) 1001224 1001224 0 0.0
.bss 169088 169088 0 0.0
.data 2064 2064 0 0.0
.text 830052 830052 0 0.0
lock-app BRD4161A+wf200 (read/write) 1149716 1149716 0 0.0
.bss 152168 152168 0 0.0
.data 2072 2072 0 0.0
.text 995456 995456 0 0.0
window-app BRD4161A (read/write) 1098744 1098744 0 0.0
.bss 137772 137772 0 0.0
.data 2096 2096 0 0.0
.text 958856 958856 0 0.0
esp32 all-clusters-app c3devkit (read only) 1033498 1033494 -4 -0.0
(read/write) 1493486 1493486 0 0.0
.dram0.bss 71088 71088 0 0.0
.dram0.data 13696 13696 0 0.0
.flash.rodata 218032 218032 0 0.0
.flash.text 1033498 1033494 -4 -0.0
.iram0.text 65160 65160 0 0.0
m5stack (read only) 1085827 1085827 0 0.0
(read/write) 490780 490772 -8 -0.0
.dram0.bss 76608 76608 0 0.0
.dram0.data 34152 34152 0 0.0
.flash.rodata 247352 247344 -8 -0.0
.flash.text 1080443 1080443 0 0.0
.iram0.text 123939 123939 0 0.0
k32w light k32w0+release (read/write) 647292 647292 0 0.0
.bss 70424 70424 0 0.0
.data 2068 2068 0 0.0
.text 572072 572072 0 0.0
lock k32w0+release (read/write) 704288 704288 0 0.0
.bss 70864 70864 0 0.0
.data 2076 2076 0 0.0
.text 628620 628620 0 0.0
linux all-clusters-app debug (read only) 3043401 3043401 0 0.0
(read/write) 156032 156032 0 0.0
.bss 61792 61792 0 0.0
.data 2096 2096 0 0.0
.data.rel.ro 85768 85768 0 0.0
.dynamic 608 608 0 0.0
.got 4568 4568 0 0.0
.init 27 27 0 0.0
.init_array 1176 1176 0 0.0
.rodata 275307 275307 0 0.0
.text 2588658 2588658 0 0.0
all-clusters-minimal-app debug (read only) 2879185 2879185 0 0.0
(read/write) 147632 147632 0 0.0
.bss 61024 61024 0 0.0
.data 2064 2064 0 0.0
.data.rel.ro 78264 78264 0 0.0
.dynamic 608 608 0 0.0
.got 4488 4488 0 0.0
.init 27 27 0 0.0
.init_array 1160 1160 0 0.0
.rodata 275467 275467 0 0.0
.text 2427058 2427058 0 0.0
bridge-app debug+rpc (read only) 2377449 2377449 0 0.0
(read/write) 127752 127752 0 0.0
.bss 50656 50656 0 0.0
.data 3600 3600 0 0.0
.data.rel.ro 67640 67640 0 0.0
.dynamic 608 608 0 0.0
.got 4392 4392 0 0.0
.init 27 27 0 0.0
.init_array 832 832 0 0.0
.rodata 204168 204168 0 0.0
.text 2010370 2010370 0 0.0
chip-tool debug (read only) 10902865 10902865 0 0.0
(read/write) 657384 657384 0 0.0
.bss 25240 25240 0 0.0
.data 3266 3266 0 0.0
.data.rel.ro 622352 622352 0 0.0
.dynamic 608 608 0 0.0
.got 5096 5096 0 0.0
.init 27 27 0 0.0
.init_array 776 776 0 0.0
.rodata 563541 563541 0 0.0
.text 8818484 8818484 0 0.0
chip-tool-ipv6only arm64 (read only) 10284372 10284372 0 0.0
(read/write) 705233 705233 0 0.0
.bss 33297 33297 0 0.0
.data 3280 3280 0 0.0
.data.rel.ro 649832 649832 0 0.0
.dynamic 560 560 0 0.0
.got 13848 13848 0 0.0
.init 24 24 0 0.0
.init_array 200 200 0 0.0
.rodata 494036 494036 0 0.0
.text 8137876 8137876 0 0.0
lighting-app debug+rpc (read only) 2602377 2602377 0 0.0
(read/write) 130536 130536 0 0.0
.bss 49792 49792 0 0.0
.data 2096 2096 0 0.0
.data.rel.ro 72680 72680 0 0.0
.dynamic 608 608 0 0.0
.got 4392 4392 0 0.0
.init 27 27 0 0.0
.init_array 928 928 0 0.0
.rodata 221008 221008 0 0.0
.text 2210178 2210178 0 0.0
lock-app debug (read only) 2585361 2585361 0 0.0
(read/write) 125712 125712 0 0.0
.bss 48288 48288 0 0.0
.data 1712 1712 0 0.0
.data.rel.ro 69688 69688 0 0.0
.dynamic 608 608 0 0.0
.got 4464 4464 0 0.0
.init 27 27 0 0.0
.init_array 904 904 0 0.0
.rodata 238000 238000 0 0.0
.text 2180418 2180418 0 0.0
ota-provider-app debug (read only) 2362601 2362601 0 0.0
(read/write) 119144 119144 0 0.0
.bss 47808 47808 0 0.0
.data 1936 1936 0 0.0
.data.rel.ro 63512 63512 0 0.0
.dynamic 608 608 0 0.0
.got 4488 4488 0 0.0
.init 27 27 0 0.0
.init_array 768 768 0 0.0
.rodata 209976 209976 0 0.0
.text 1988866 1988866 0 0.0
ota-requestor-app debug (read only) 2527929 2527929 0 0.0
(read/write) 127552 127552 0 0.0
.bss 50368 50368 0 0.0
.data 2304 2304 0 0.0
.data.rel.ro 68920 68920 0 0.0
.dynamic 608 608 0 0.0
.got 4480 4480 0 0.0
.init 27 27 0 0.0
.init_array 856 856 0 0.0
.rodata 216704 216704 0 0.0
.text 2138322 2138322 0 0.0
shell debug (read only) 2611689 2611689 0 0.0
(read/write) 142184 142184 0 0.0
.bss 57704 57704 0 0.0
.data 1264 1264 0 0.0
.data.rel.ro 77376 77376 0 0.0
.dynamic 608 608 0 0.0
.got 4136 4136 0 0.0
.init 27 27 0 0.0
.init_array 1048 1048 0 0.0
.rodata 235410 235410 0 0.0
.text 2217570 2217570 0 0.0
thermostat-no-ble arm64 (read only) 2361556 2361556 0 0.0
(read/write) 141857 141857 0 0.0
.bss 55233 55233 0 0.0
.data 1680 1680 0 0.0
.data.rel.ro 76112 76112 0 0.0
.dynamic 560 560 0 0.0
.got 5056 5056 0 0.0
.init 24 24 0 0.0
.init_array 416 416 0 0.0
.rodata 141276 141276 0 0.0
.text 1982240 1982240 0 0.0
tv-app debug (read only) 3188281 3188281 0 0.0
(read/write) 258040 258040 0 0.0
.bss 167352 167352 0 0.0
.data 4752 4752 0 0.0
.data.rel.ro 79368 79368 0 0.0
.dynamic 608 608 0 0.0
.got 4856 4856 0 0.0
.init 27 27 0 0.0
.init_array 1080 1080 0 0.0
.rodata 259784 259784 0 0.0
.text 2738466 2738466 0 0.0
tv-casting-app debug (read only) 5508945 5508945 0 0.0
(read/write) 160536 160536 0 0.0
.bss 51352 51352 0 0.0
.data 2432 2432 0 0.0
.data.rel.ro 100304 100304 0 0.0
.dynamic 608 608 0 0.0
.got 4776 4776 0 0.0
.init 27 27 0 0.0
.init_array 1048 1048 0 0.0
.rodata 344945 344945 0 0.0
.text 4892098 4892098 0 0.0
mbed lock-app CY8CPROTO_062_4343W+release (read only) 6224 6224 0 0.0
(read/write) 2454872 2454872 0 0.0
.bss 215044 215044 0 0.0
.data 5872 5872 0 0.0
.text 1417516 1417516 0 0.0
nrfconnect all-clusters-app nrf52840dk_nrf52840 (read/write) 1180691 1180691 0 0.0
bss 143641 143641 0 0.0
rodata 143380 143380 0 0.0
text 814724 814724 0 0.0
all-clusters-minimal-app nrf52840dk_nrf52840 (read/write) 1159871 1159871 0 0.0
bss 142868 142868 0 0.0
rodata 134968 134968 0 0.0
text 803116 803116 0 0.0
psoc6 all-clusters cy8ckit_062s2_43012 (read only) 842216 842216 0 0.0
(read/write) 1741700 1741700 0 0.0
.ARM.attributes 46 46 0 0.0
.ARM.exidx 8 8 0 0.0
.bss 188464 188464 0 0.0
.comment 204 204 0 0.0
.copy.table 24 24 0 0.0
.cy_m0p_image 6216 6216 0 0.0
.cy_sharedmem 8 8 0 0.0
.data 2664 2664 0 0.0
.debug_abbrev 1221325 1221325 0 0.0
.debug_aranges 111696 111696 0 0.0
.debug_frame 372812 372812 0 0.0
.debug_info 26696028 26696030 2 0.0
.debug_line 3655093 3655094 1 0.0
.debug_loc 3568177 3568177 0 0.0
.debug_ranges 337560 337560 0 0.0
.debug_str 3426632 3426632 0 0.0
.heap 842216 842216 0 0.0
.noinit 148 148 0 0.0
.ramVectors 736 736 0 0.0
.shstrtab 288 288 0 0.0
.stab 156 156 0 0.0
.stabstr 335 335 0 0.0
.stack_dummy 4096 4096 0 0.0
.strtab 570457 570457 0 0.0
.symtab 421424 421424 0 0.0
.text 1542184 1542184 0 0.0
.zero.table 8 8 0 0.0
text 0 0 0 0.0
all-clusters-minimal cy8ckit_062s2_43012 (read only) 842952 842952 0 0.0
(read/write) 1684868 1684868 0 0.0
.ARM.attributes 46 46 0 0.0
.ARM.exidx 8 8 0 0.0
.bss 187728 187728 0 0.0
.comment 204 204 0 0.0
.copy.table 24 24 0 0.0
.cy_m0p_image 6216 6216 0 0.0
.cy_sharedmem 8 8 0 0.0
.data 2664 2664 0 0.0
.debug_abbrev 1213164 1213164 0 0.0
.debug_aranges 111168 111168 0 0.0
.debug_frame 375892 375892 0 0.0
.debug_info 26432723 26432724 1 0.0
.debug_line 3675609 3675610 1 0.0
.debug_loc 3555814 3555814 0 0.0
.debug_ranges 336176 336176 0 0.0
.debug_str 3415637 3415637 0 0.0
.heap 842952 842952 0 0.0
.noinit 148 148 0 0.0
.ramVectors 736 736 0 0.0
.shstrtab 288 288 0 0.0
.stab 156 156 0 0.0
.stabstr 335 335 0 0.0
.stack_dummy 4096 4096 0 0.0
.strtab 534931 534931 0 0.0
.symtab 408016 408016 0 0.0
.text 1486088 1486088 0 0.0
.zero.table 0 0 0 0.0
8 8 0 0.0
light cy8ckit_062s2_43012 (read only) 851184 851184 0 0.0
(read/write) 1602172 1602172 0 0.0
.ARM.attributes 46 46 0 0.0
.ARM.exidx 8 8 0 0.0
.bss 179704 179704 0 0.0
.comment 204 204 0 0.0
.copy.table 24 24 0 0.0
.cy_m0p_image 6216 6216 0 0.0
.cy_sharedmem 8 8 0 0.0
.data 2456 2456 0 0.0
.debug_abbrev 1047983 1047983 0 0.0
.debug_aranges 103344 103344 0 0.0
.debug_frame 346160 346160 0 0.0
.debug_info 21896230 21896230 0 0.0
.debug_line 3246078 3246079 1 0.0
.debug_loc 3254196 3254196 0 0.0
.debug_ranges 301648 301648 0 0.0
.debug_str 3220857 3220857 0 0.0
.heap 851184 851184 0 0.0
.noinit 148 148 0 0.0
.ramVectors 736 736 0 0.0
.shstrtab 288 288 0 0.0
.stab 156 156 0 0.0
.stabstr 335 335 0 0.0
.stack_dummy 4096 4096 0 0.0
.strtab 468230 468230 0 0.0
.symtab 375104 375104 0 0.0
.text 1411624 1411624 0 0.0
.zero.table 0 0 0 0.0
8 8 0 0.0
lock cy8ckit_062s2_43012 (read only) 846152 846152 0 0.0
(read/write) 1639844 1639844 0 0.0
.ARM.attributes 46 46 0 0.0
.ARM.exidx 8 8 0 0.0
.bss 184720 184720 0 0.0
.comment 204 204 0 0.0
.copy.table 24 24 0 0.0
.cy_m0p_image 6216 6216 0 0.0
.cy_sharedmem 8 8 0 0.0
.data 2472 2472 0 0.0
.debug_abbrev 1055418 1055418 0 0.0
.debug_aranges 104016 104016 0 0.0
.debug_frame 348988 348988 0 0.0
.debug_info 22275869 22275870 1 0.0
.debug_line 3254895 3254896 1 0.0
.debug_loc 3293990 3293990 0 0.0
.debug_ranges 304992 304992 0 0.0
.debug_str 3248278 3248278 0 0.0
.heap 846152 846152 0 0.0
.noinit 148 148 0 0.0
.ramVectors 736 736 0 0.0
.shstrtab 288 288 0 0.0
.stab 156 156 0 0.0
.stabstr 335 335 0 0.0
.stack_dummy 4096 4096 0 0.0
.strtab 474445 474445 0 0.0
.symtab 378288 378288 0 0.0
.text 1444264 1444264 0 0.0
.zero.table 0 0 0 0.0
8 8 0 0.0
telink light-switch-app tlsr9518adk80d (read/write) 808600 808600 0 0.0
bss 71344 71344 0 0.0
noinit 43488 43488 0 0.0
text 571204 571204 0 0.0
lighting-app tlsr9518adk80d (read/write) 830500 830500 0 0.0
bss 72200 72200 0 0.0
noinit 43488 43488 0 0.0
text 589316 589316 0 0.0

@@ -54,8 +54,10 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()

mRunLoopSem = dispatch_semaphore_create(0);

#if !CHIP_SYSTEM_CONFIG_USE_LIBEV
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is still not clear to me. If we are here (or more to the point if the System::Layer will look for this queue), then CHIP_SYSTEM_CONFIG_USE_DISPATCH is defined, right? But that's mutually exclusive with CHIP_SYSTEM_CONFIG_USE_LIBEV. So why do we need the conditioning here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we get here, we're on Darwin, which does have dispatch and could provide a queue to the system layer. But when we're compiling with CHIP_SYSTEM_CONFIG_USE_LIBEV (which is possible even under Darwin), the system layer does not have queue support, and does not have the SetDispatchQueue method.

For clarity, I guess it would be better to just use #if CHIP_SYSTEM_CONFIG_USE_DISPATCHhere instead of #if !CHIP_SYSTEM_CONFIG_USE_LIBEV. Under normal Darwin builds, that conditional is redundant, but it allows to use other mainloops such as the libev based one. Agree?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW: I fully agree this be better a "POSTPONED past 1.0" thing, that's why I posted it as a draft in the first place. I'm happy though about you looking at it right now, and I'm using and testing it daily in my setup right now, so by the time it gets a regular PR it should be reasonably mature :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, I guess it would be better to just use #if CHIP_SYSTEM_CONFIG_USE_DISPATCHhere

Yes, that would be much more clear, since that's the thing we really care about: that the SetDispatchQueue method exists. OK.

#if CHIP_SYSTEM_CONFIG_USE_LIBEV
#include <ev.h>
#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
#error "CHIP_SYSTEM_CONFIG_USE_LIBEV and CHIP_SYSTEM_CONFIG_USE_DISPATCH are mutually exclusive"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without CHIP_SYSTEM_CONFIG_USE_DISPATCH, nothing in the non-Darwin-specific code can run anywhere else than under libev control (timer and IO callbacks) on the same thread/queue.

Does this assume that something other than src/platform/darwin/PlatformManagerImpl.cpp is being used as the platform manager? There's a not-entirely-obvious dependency here where ScheduleLambda on the System::Layer will run it via PostEvent on the platform manager...

And of course various things directly PostEvent and whatnot.

If you're not using the platform manager's event loop at all (so not calling RunEventLoop or StartEventLoopTask), then I'd like to understand the setup and what things are running. If you are, then the assumption is that PostEvent and whatever System::Layer is doing run things on the same thread, or at least interlocked sanely so they can't run at the same time.

Mostly I feel that event loops and the stuff around them are complex enough people get confused easily, so I would rather not have code around that makes them more confused.

Comment on lines +246 to +247
// just a timer with no delay
return StartTimer(Clock::Timeout(0), onComplete, appState);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be buggy if someone calls ScheduleWork twice with the same args: it should schedule twice, but this will cancel the previous one, right?

The Darwin impl gets this right. The select-based impl will get it right once #22375 merges.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a not-entirely-obvious dependency here where ScheduleLambda on the System::Layer will run it via PostEvent on the platform manager...
And of course various things directly PostEvent and whatnot.

It's beginning to dawn on me that this is more complicated - I wasn't aware that PostEvent also runs on dispatch, directly.

Mostly I feel that event loops and the stuff around them are complex enough people get confused easily, so I would rather not have code around that makes them more confused.

I guess you're right. I'll remove the things related to that Darwin/libev Frankenstein's monster type of setup from the PR ;-)

After all, the libev support does not aim at replacing dispatch when it is available, but is useful for embedded linux targets like openwrt. Running the hybrid setup on the mac is just convenient for my develop-in-Xcode workflow, I can easily keep a few private patches for that.

This will be buggy if someone calls ScheduleWork twice with the same args: it should schedule twice, but this will cancel the previous one, right?

Right. I'll have a look at #22375 and find a way to make the libev variant working in the same way.

Copy link
Contributor

@andy31415 andy31415 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking changes requested to wait until after 1.0 branch

plan44 added a commit to plan44/connectedhomeip that referenced this pull request Sep 8, 2022
- as discussed in project-chip#22043
- also add conditional definition for `ev_io_modify()` required for older libev versions.
plan44 added a commit to plan44/connectedhomeip that referenced this pull request Oct 8, 2022
- as discussed in project-chip#22043
- also add conditional definition for `ev_io_modify()` required for older libev versions.
plan44 added a commit to plan44/connectedhomeip that referenced this pull request Oct 28, 2022
- as discussed in project-chip#22043
- also add conditional definition for `ev_io_modify()` required for older libev versions.
@stale
Copy link

stale bot commented Nov 7, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale Stale issue or PR label Nov 7, 2022
@Kxuan
Copy link
Contributor

Kxuan commented Nov 30, 2022

Hi @plan44 , are you still working on this PR? It is a very useful feature!

@plan44
Copy link
Contributor Author

plan44 commented Nov 30, 2022

Hi @plan44 , are you still working on this PR? It is a very useful feature!

Yes, I'm actively using it but currently from a fork of connectedhomeip, see top few commits there.

I'll rebase and post it upstream as soon as I find the time to do so with the needed care - unfortunately I'm currently distracted from working a lot on matter right now, it might take a few weeks to get it ready for posting again (and remove the draft status).

plan44 added a commit to plan44/connectedhomeip that referenced this pull request Dec 29, 2022
- as discussed in project-chip#22043
- also add conditional definition for `ev_io_modify()` required for older libev versions.
@stale
Copy link

stale bot commented Dec 30, 2022

This stale pull request has been automatically closed. Thank you for your contributions.

@stale stale bot closed this Dec 30, 2022
@plan44
Copy link
Contributor Author

plan44 commented Dec 30, 2022

dear bot, the PR is not really stale... ;-)

plan44 added a commit to plan44/connectedhomeip that referenced this pull request Jan 3, 2023
- as discussed in project-chip#22043
- also add conditional definition for `ev_io_modify()` required for older libev versions (taken from current libev source, blown up to multiline by clang-format)
@bzbarsky-apple
Copy link
Contributor

@plan44 For future reference, I could have reopened this PR (and kept the history), if you had not created a new one from the same branch.... I was just on vacation a few days ago and did not get to this until now.

@plan44
Copy link
Contributor Author

plan44 commented Jan 3, 2023

@bzbarsky-apple sorry, I did not realize it is possible at all to reopen a PR once it is closed - I did not want to stress anybody, just follow up on my promise to update the old PRs from pre-1.0 before Jan 3rd 😉

@bzbarsky-apple
Copy link
Contributor

@plan44 Yeah, not a problem, just letting you know for future. ;)

plan44 added a commit to plan44/connectedhomeip that referenced this pull request Apr 6, 2023
- as discussed in project-chip#22043
- also add conditional definition for `ev_io_modify()` required for older libev versions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants