1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/CleanerThread.cc
12 #include <zypp/base/CleanerThread_p.h>
16 #include <condition_variable>
19 #include <sys/types.h>
24 static CleanerData &instance ()
26 // C++11 requires that this is thread safe "magic statics"
27 // this is a intentional leak and will live until the application exits
28 static CleanerData *data( new CleanerData );
34 std::thread t ( [&](){
37 t.detach(); //we will control the thread otherwise
42 std::unique_lock<std::mutex> lk( _m );
46 auto filter = []( pid_t pid ){
48 int res = waitpid( pid, &status, WNOHANG );
49 // we either got an error, or the child has exited, remove it from list
50 bool removeMe = ( res == -1 || res == pid );
53 _watchedPIDs.erase( std::remove_if( _watchedPIDs.begin(), _watchedPIDs.end(), filter ), _watchedPIDs.end() );
55 if ( _watchedPIDs.size() )
56 _cv.wait_for( lk, std::chrono::milliseconds(100) );
62 std::mutex _m; // < locks all data in CleanerData, do not access it without owning the mutex
63 std::condition_variable _cv;
65 std::vector<pid_t> _watchedPIDs;
69 void zypp::CleanerThread::watchPID( pid_t pid_r )
71 CleanerData &data = CleanerData::instance();
73 std::lock_guard<std::mutex> guard( data._m );
74 data._watchedPIDs.push_back( pid_r );
77 data._cv.notify_one();