apache / celix

Apache Celix is a framework for C and C++14 to develop dynamic modular software applications using component and in-process service-oriented programming.
https://celix.apache.org/
Apache License 2.0
162 stars 86 forks source link

Scope-based resource management #574

Closed PengZheng closed 1 year ago

PengZheng commented 1 year ago

Recently, there is a kernel patch implementing systematic scope-based resource management based on __attribute__((__cleanup__)): https://lwn.net/Articles/934423/

A detailed explanation can be found here: https://lwn.net/SubscriberLink/934679/a50a2e836fcfaee7/

I strongly recommend we introduce similar mechanism into Celix.

pnoltes commented 1 year ago

Nice read and good to see that there is already a initiative and commitment to use the cleanup attribute from a very important and relevant source.

Reading the article I think we can use all 3 setups: cleanup for pointer variables, cleanup for "classes" and lock guards. I think in the case of lock guards, we can use a simpler approach (because we only need to support the celix mutex and rwlock).

Reading this I think he "class" approach fits our _create / _destroy pairs. But maybe we can also introduce a "retain guard", so that we a shared_ptr like RAII ref counting support, e.g.

void foo(foo_t* foo, celix_event_t* event) {
   celix_event_retain_guard(localEvent) = event ; // calls celix_event_retain
   ......
} // calls celix_event_release

But lets start with mimicking what is already used for the Linux kernel.