What about using sched::with_all_threads, to check for each app in apps, if 
all its threads are in terminated state?

void app_registry::join_terminated() {
    SCOPE_LOCK(lock);
    auto it = apps.begin();
    while (it != apps.end()) {
        auto a = *it;
        ++it;
        fprintf(stderr, "APP a->runtime().use_count()=%d\n", 
a->runtime().use_count());
        bool all_threads_terminated = true;
        sched::with_all_threads([&](sched::thread &th) {
            if (a->runtime() == th.app_runtime()) {
                if(th.get_status() != sched::thread::status::terminated) {
                    fprintf(stderr, "    tid=%d ACTIVE\n", th.id());
                    all_threads_terminated = false;
                }
                else {
                    fprintf(stderr, "    tid=%d terminated\n", th.id());
                }
            }
        });
        // Note that join() will take a recursive lock to modify apps.
        // That's fine, it's why we incremented 'it' already.
        if (all_threads_terminated) {
            a->join();
        }
    }
}

That seems to work for me.

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to