1*87d2adbcSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*87d2adbcSAndrew Rist * distributed with this work for additional information 6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance 9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an 15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the 17*87d2adbcSAndrew Rist * specific language governing permissions and limitations 18*87d2adbcSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*87d2adbcSAndrew Rist *************************************************************/ 21*87d2adbcSAndrew Rist 22*87d2adbcSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_sal.hxx" 26cdf0e10cSrcweir #include <rtl/unload.h> 27cdf0e10cSrcweir #include <rtl/alloc.h> 28cdf0e10cSrcweir #include <rtl/ustring.hxx> 29cdf0e10cSrcweir #include <osl/mutex.hxx> 30cdf0e10cSrcweir #include <hash_map> 31cdf0e10cSrcweir #include "rtl/allocator.hxx" 32cdf0e10cSrcweir 33cdf0e10cSrcweir #include <functional> 34cdf0e10cSrcweir #include <list> 35cdf0e10cSrcweir #include <deque> 36cdf0e10cSrcweir 37cdf0e10cSrcweir using osl::MutexGuard; 38cdf0e10cSrcweir 39cdf0e10cSrcweir //---------------------------------------------------------------------------- 40cdf0e10cSrcweir 41cdf0e10cSrcweir static void rtl_notifyUnloadingListeners(); 42cdf0e10cSrcweir 43cdf0e10cSrcweir static sal_Bool isEqualTimeValue ( const TimeValue* time1, const TimeValue* time2) 44cdf0e10cSrcweir { 45cdf0e10cSrcweir if( time1->Seconds == time2->Seconds && 46cdf0e10cSrcweir time1->Nanosec == time2->Nanosec) 47cdf0e10cSrcweir return sal_True; 48cdf0e10cSrcweir else 49cdf0e10cSrcweir return sal_False; 50cdf0e10cSrcweir } 51cdf0e10cSrcweir 52cdf0e10cSrcweir static sal_Bool isGreaterTimeValue( const TimeValue* time1, const TimeValue* time2) 53cdf0e10cSrcweir { 54cdf0e10cSrcweir sal_Bool retval= sal_False; 55cdf0e10cSrcweir if ( time1->Seconds > time2->Seconds) 56cdf0e10cSrcweir retval= sal_True; 57cdf0e10cSrcweir else if ( time1->Seconds == time2->Seconds) 58cdf0e10cSrcweir { 59cdf0e10cSrcweir if( time1->Nanosec > time2->Nanosec) 60cdf0e10cSrcweir retval= sal_True; 61cdf0e10cSrcweir } 62cdf0e10cSrcweir return retval; 63cdf0e10cSrcweir } 64cdf0e10cSrcweir 65cdf0e10cSrcweir static sal_Bool isGreaterEqualTimeValue( const TimeValue* time1, const TimeValue* time2) 66cdf0e10cSrcweir { 67cdf0e10cSrcweir if( isEqualTimeValue( time1, time2) ) 68cdf0e10cSrcweir return sal_True; 69cdf0e10cSrcweir else if( isGreaterTimeValue( time1, time2)) 70cdf0e10cSrcweir return sal_True; 71cdf0e10cSrcweir else 72cdf0e10cSrcweir return sal_False; 73cdf0e10cSrcweir } 74cdf0e10cSrcweir 75cdf0e10cSrcweir static void addTimeValue( const TimeValue* value1, const TimeValue* value2, TimeValue* result) 76cdf0e10cSrcweir { 77cdf0e10cSrcweir sal_uInt64 sum; 78cdf0e10cSrcweir result->Nanosec=0; 79cdf0e10cSrcweir result->Seconds=0; 80cdf0e10cSrcweir 81cdf0e10cSrcweir sum= value1->Nanosec + value2->Nanosec; 82cdf0e10cSrcweir if( sum >= 1000000000 ) 83cdf0e10cSrcweir { 84cdf0e10cSrcweir result->Seconds=1; 85cdf0e10cSrcweir sum -= 1000000000; 86cdf0e10cSrcweir } 87cdf0e10cSrcweir result->Nanosec= (sal_uInt32)sum; 88cdf0e10cSrcweir result->Seconds += value1->Seconds + value2->Seconds; 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir 92cdf0e10cSrcweir static sal_Bool hasEnoughTimePassed( const TimeValue* unusedSince, const TimeValue* timespan) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir sal_Bool retval= sal_False; 95cdf0e10cSrcweir TimeValue currentTime; 96cdf0e10cSrcweir if( osl_getSystemTime( ¤tTime)) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir TimeValue addedTime; 99cdf0e10cSrcweir addTimeValue( unusedSince, timespan, &addedTime); 100cdf0e10cSrcweir if( isGreaterEqualTimeValue( ¤tTime, &addedTime)) 101cdf0e10cSrcweir retval= sal_True; 102cdf0e10cSrcweir } 103cdf0e10cSrcweir 104cdf0e10cSrcweir return retval; 105cdf0e10cSrcweir } 106cdf0e10cSrcweir 107cdf0e10cSrcweir static osl::Mutex* getUnloadingMutex() 108cdf0e10cSrcweir { 109cdf0e10cSrcweir static osl::Mutex * g_pMutex= NULL; 110cdf0e10cSrcweir if (!g_pMutex) 111cdf0e10cSrcweir { 112cdf0e10cSrcweir MutexGuard guard( osl::Mutex::getGlobalMutex() ); 113cdf0e10cSrcweir if (!g_pMutex) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir static osl::Mutex g_aMutex; 116cdf0e10cSrcweir g_pMutex= &g_aMutex; 117cdf0e10cSrcweir } 118cdf0e10cSrcweir } 119cdf0e10cSrcweir return g_pMutex; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir 122cdf0e10cSrcweir extern "C" void rtl_moduleCount_acquire(rtl_ModuleCount * that ) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir rtl_StandardModuleCount* pMod= (rtl_StandardModuleCount*)that; 125cdf0e10cSrcweir osl_incrementInterlockedCount( &pMod->counter); 126cdf0e10cSrcweir } 127cdf0e10cSrcweir 128cdf0e10cSrcweir extern "C" void rtl_moduleCount_release( rtl_ModuleCount * that ) 129cdf0e10cSrcweir { 130cdf0e10cSrcweir rtl_StandardModuleCount* pMod= (rtl_StandardModuleCount*)that; 131cdf0e10cSrcweir OSL_ENSURE( pMod->counter >0 , "library counter incorrect" ); 132cdf0e10cSrcweir osl_decrementInterlockedCount( &pMod->counter); 133cdf0e10cSrcweir if( pMod->counter == 0) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir MutexGuard guard( getUnloadingMutex()); 136cdf0e10cSrcweir 137cdf0e10cSrcweir if( sal_False == osl_getSystemTime( &pMod->unusedSince) ) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir // set the time to 0 if we could not get the time 140cdf0e10cSrcweir pMod->unusedSince.Seconds= 0; 141cdf0e10cSrcweir pMod->unusedSince.Nanosec= 0; 142cdf0e10cSrcweir } 143cdf0e10cSrcweir } 144cdf0e10cSrcweir } 145cdf0e10cSrcweir 146cdf0e10cSrcweir 147cdf0e10cSrcweir struct hashModule 148cdf0e10cSrcweir { 149cdf0e10cSrcweir size_t operator()( const oslModule& rkey) const 150cdf0e10cSrcweir { 151cdf0e10cSrcweir return (size_t)rkey; 152cdf0e10cSrcweir } 153cdf0e10cSrcweir }; 154cdf0e10cSrcweir 155cdf0e10cSrcweir typedef std::hash_map< 156cdf0e10cSrcweir oslModule, 157cdf0e10cSrcweir std::pair<sal_uInt32, component_canUnloadFunc>, 158cdf0e10cSrcweir hashModule, 159cdf0e10cSrcweir std::equal_to<oslModule>, 160cdf0e10cSrcweir rtl::Allocator<oslModule> 161cdf0e10cSrcweir > ModuleMap; 162cdf0e10cSrcweir 163cdf0e10cSrcweir typedef ModuleMap::iterator Mod_IT; 164cdf0e10cSrcweir 165cdf0e10cSrcweir static ModuleMap& getModuleMap() 166cdf0e10cSrcweir { 167cdf0e10cSrcweir static ModuleMap * g_pMap= NULL; 168cdf0e10cSrcweir if (!g_pMap) 169cdf0e10cSrcweir { 170cdf0e10cSrcweir MutexGuard guard( getUnloadingMutex() ); 171cdf0e10cSrcweir if (!g_pMap) 172cdf0e10cSrcweir { 173cdf0e10cSrcweir static ModuleMap g_aModuleMap; 174cdf0e10cSrcweir g_pMap= &g_aModuleMap; 175cdf0e10cSrcweir } 176cdf0e10cSrcweir } 177cdf0e10cSrcweir return *g_pMap; 178cdf0e10cSrcweir } 179cdf0e10cSrcweir 180cdf0e10cSrcweir extern "C" sal_Bool rtl_moduleCount_canUnload( rtl_StandardModuleCount * that, TimeValue * libUnused) 181cdf0e10cSrcweir { 182cdf0e10cSrcweir if (that->counter == 0) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir MutexGuard guard( getUnloadingMutex()); 185cdf0e10cSrcweir if (libUnused && (that->counter == 0)) 186cdf0e10cSrcweir { 187cdf0e10cSrcweir rtl_copyMemory(libUnused, &that->unusedSince, sizeof(TimeValue)); 188cdf0e10cSrcweir } 189cdf0e10cSrcweir } 190cdf0e10cSrcweir return (that->counter == 0); 191cdf0e10cSrcweir } 192cdf0e10cSrcweir 193cdf0e10cSrcweir 194cdf0e10cSrcweir extern "C" sal_Bool SAL_CALL rtl_registerModuleForUnloading( oslModule module) 195cdf0e10cSrcweir { 196cdf0e10cSrcweir MutexGuard guard( getUnloadingMutex()); 197cdf0e10cSrcweir ModuleMap& moduleMap= getModuleMap(); 198cdf0e10cSrcweir sal_Bool ret= sal_True; 199cdf0e10cSrcweir 200cdf0e10cSrcweir // If the module has been registered before, then find it and increment 201cdf0e10cSrcweir // its reference cout 202cdf0e10cSrcweir Mod_IT it= moduleMap.find( module); 203cdf0e10cSrcweir if( it != moduleMap.end()) 204cdf0e10cSrcweir { 205cdf0e10cSrcweir //module already registered, increment ref count 206cdf0e10cSrcweir it->second.first++; 207cdf0e10cSrcweir } 208cdf0e10cSrcweir else 209cdf0e10cSrcweir { 210cdf0e10cSrcweir // Test if the module supports unloading (exports component_canUnload) 211cdf0e10cSrcweir rtl::OUString name(RTL_CONSTASCII_USTRINGPARAM( COMPONENT_CANUNLOAD)); 212cdf0e10cSrcweir component_canUnloadFunc pFunc= 213cdf0e10cSrcweir (component_canUnloadFunc)osl_getFunctionSymbol( module, name.pData); 214cdf0e10cSrcweir if (pFunc) 215cdf0e10cSrcweir { 216cdf0e10cSrcweir //register module for the first time, set ref count to 1 217cdf0e10cSrcweir moduleMap[module]= std::make_pair((sal_uInt32)1, pFunc); 218cdf0e10cSrcweir } 219cdf0e10cSrcweir else 220cdf0e10cSrcweir ret= sal_False; 221cdf0e10cSrcweir } 222cdf0e10cSrcweir return ret; 223cdf0e10cSrcweir } 224cdf0e10cSrcweir 225cdf0e10cSrcweir extern "C" void SAL_CALL rtl_unregisterModuleForUnloading( oslModule module) 226cdf0e10cSrcweir { 227cdf0e10cSrcweir MutexGuard guard( getUnloadingMutex()); 228cdf0e10cSrcweir 229cdf0e10cSrcweir ModuleMap& moduleMap= getModuleMap(); 230cdf0e10cSrcweir Mod_IT it= moduleMap.find( module); 231cdf0e10cSrcweir if( it != moduleMap.end() ) 232cdf0e10cSrcweir { 233cdf0e10cSrcweir // The module is registered, decrement ref count. 234cdf0e10cSrcweir it->second.first --; 235cdf0e10cSrcweir 236cdf0e10cSrcweir // If the refcount == 0 then remove the module from the map 237cdf0e10cSrcweir if( it->second.first == 0) 238cdf0e10cSrcweir moduleMap.erase( it); 239cdf0e10cSrcweir } 240cdf0e10cSrcweir } 241cdf0e10cSrcweir 242cdf0e10cSrcweir extern "C" void SAL_CALL rtl_unloadUnusedModules( TimeValue* libUnused) 243cdf0e10cSrcweir { 244cdf0e10cSrcweir MutexGuard guard( getUnloadingMutex()); 245cdf0e10cSrcweir 246cdf0e10cSrcweir typedef std::list< oslModule, rtl::Allocator<oslModule> > list_type; 247cdf0e10cSrcweir list_type unloadedModulesList; 248cdf0e10cSrcweir 249cdf0e10cSrcweir ModuleMap& moduleMap= getModuleMap(); 250cdf0e10cSrcweir Mod_IT it_e= moduleMap.end(); 251cdf0e10cSrcweir 252cdf0e10cSrcweir // notify all listeners 253cdf0e10cSrcweir rtl_notifyUnloadingListeners(); 254cdf0e10cSrcweir 255cdf0e10cSrcweir // prepare default TimeValue if argumetn is NULL 256cdf0e10cSrcweir TimeValue nullTime={0,0}; 257cdf0e10cSrcweir TimeValue* pLibUnused= libUnused? libUnused : &nullTime; 258cdf0e10cSrcweir 259cdf0e10cSrcweir Mod_IT it= moduleMap.begin(); 260cdf0e10cSrcweir for (; it != it_e; ++it) 261cdf0e10cSrcweir { 262cdf0e10cSrcweir //can the module be unloaded? 263cdf0e10cSrcweir component_canUnloadFunc func= it->second.second; 264cdf0e10cSrcweir TimeValue unusedSince= {0, 0}; 265cdf0e10cSrcweir 266cdf0e10cSrcweir if( func( &unusedSince) ) 267cdf0e10cSrcweir { 268cdf0e10cSrcweir // module can be unloaded if it has not been used at least for the time 269cdf0e10cSrcweir // specified by the argument libUnused 270cdf0e10cSrcweir if( hasEnoughTimePassed( &unusedSince, pLibUnused)) 271cdf0e10cSrcweir { 272cdf0e10cSrcweir // get the reference count and unload the module as many times 273cdf0e10cSrcweir sal_uInt32 refCount= it->second.first; 274cdf0e10cSrcweir 275cdf0e10cSrcweir for ( sal_uInt32 i=0; i < refCount; i++) 276cdf0e10cSrcweir osl_unloadModule( it->first); 277cdf0e10cSrcweir 278cdf0e10cSrcweir // mark the module for later removal 279cdf0e10cSrcweir unloadedModulesList.push_front( it->first); 280cdf0e10cSrcweir } 281cdf0e10cSrcweir } 282cdf0e10cSrcweir } 283cdf0e10cSrcweir 284cdf0e10cSrcweir // remove all entries containing invalid (unloaded) modules 285cdf0e10cSrcweir list_type::const_iterator un_it= unloadedModulesList.begin(); 286cdf0e10cSrcweir for (; un_it != unloadedModulesList.end(); ++un_it) 287cdf0e10cSrcweir { 288cdf0e10cSrcweir moduleMap.erase( *un_it); 289cdf0e10cSrcweir } 290cdf0e10cSrcweir } 291cdf0e10cSrcweir 292cdf0e10cSrcweir 293cdf0e10cSrcweir // ============================================================================== 294cdf0e10cSrcweir // Unloading Listener Administration 295cdf0e10cSrcweir //=============================================================================== 296cdf0e10cSrcweir struct hashListener 297cdf0e10cSrcweir { 298cdf0e10cSrcweir size_t operator()( const sal_Int32& rkey) const 299cdf0e10cSrcweir { 300cdf0e10cSrcweir return (size_t)rkey; 301cdf0e10cSrcweir } 302cdf0e10cSrcweir }; 303cdf0e10cSrcweir 304cdf0e10cSrcweir typedef std::hash_map< 305cdf0e10cSrcweir sal_Int32, 306cdf0e10cSrcweir std::pair<rtl_unloadingListenerFunc, void*>, 307cdf0e10cSrcweir hashListener, 308cdf0e10cSrcweir std::equal_to<sal_Int32>, 309cdf0e10cSrcweir rtl::Allocator<sal_Int32> 310cdf0e10cSrcweir > ListenerMap; 311cdf0e10cSrcweir 312cdf0e10cSrcweir typedef ListenerMap::iterator Lis_IT; 313cdf0e10cSrcweir 314cdf0e10cSrcweir static ListenerMap& getListenerMap() 315cdf0e10cSrcweir { 316cdf0e10cSrcweir static ListenerMap * g_pListeners= NULL; 317cdf0e10cSrcweir if (!g_pListeners) 318cdf0e10cSrcweir { 319cdf0e10cSrcweir MutexGuard guard( getUnloadingMutex() ); 320cdf0e10cSrcweir if (!g_pListeners) 321cdf0e10cSrcweir { 322cdf0e10cSrcweir static ListenerMap g_aListenerMap; 323cdf0e10cSrcweir g_pListeners= &g_aListenerMap; 324cdf0e10cSrcweir } 325cdf0e10cSrcweir } 326cdf0e10cSrcweir return *g_pListeners; 327cdf0e10cSrcweir } 328cdf0e10cSrcweir 329cdf0e10cSrcweir 330cdf0e10cSrcweir // This queue contains cookies which have been passed out by rtl_addUnloadingListener and 331cdf0e10cSrcweir // which have been regainded by rtl_removeUnloadingListener. When rtl_addUnloadingListener 332cdf0e10cSrcweir // is called then a cookie has to be returned. First we look into the set if there is one 333cdf0e10cSrcweir // availabe. Otherwise a new cookie will be provided. 334cdf0e10cSrcweir // not a new value is returned. 335cdf0e10cSrcweir 336cdf0e10cSrcweir typedef std::deque< 337cdf0e10cSrcweir sal_Int32, 338cdf0e10cSrcweir rtl::Allocator<sal_Int32> 339cdf0e10cSrcweir > queue_type; 340cdf0e10cSrcweir 341cdf0e10cSrcweir static queue_type& getCookieQueue() 342cdf0e10cSrcweir { 343cdf0e10cSrcweir static queue_type * g_pCookies= NULL; 344cdf0e10cSrcweir if (!g_pCookies) 345cdf0e10cSrcweir { 346cdf0e10cSrcweir MutexGuard guard( getUnloadingMutex() ); 347cdf0e10cSrcweir if (!g_pCookies) 348cdf0e10cSrcweir { 349cdf0e10cSrcweir static queue_type g_aCookieQueue; 350cdf0e10cSrcweir g_pCookies= &g_aCookieQueue; 351cdf0e10cSrcweir } 352cdf0e10cSrcweir } 353cdf0e10cSrcweir return *g_pCookies; 354cdf0e10cSrcweir } 355cdf0e10cSrcweir 356cdf0e10cSrcweir static sal_Int32 getCookie() 357cdf0e10cSrcweir { 358cdf0e10cSrcweir static sal_Int32 cookieValue= 1; 359cdf0e10cSrcweir 360cdf0e10cSrcweir sal_Int32 retval; 361cdf0e10cSrcweir queue_type& regainedCookies= getCookieQueue(); 362cdf0e10cSrcweir if( regainedCookies.empty() ) 363cdf0e10cSrcweir retval= cookieValue++; 364cdf0e10cSrcweir else 365cdf0e10cSrcweir { 366cdf0e10cSrcweir retval= regainedCookies.front(); 367cdf0e10cSrcweir regainedCookies.pop_front(); 368cdf0e10cSrcweir } 369cdf0e10cSrcweir return retval; 370cdf0e10cSrcweir } 371cdf0e10cSrcweir 372cdf0e10cSrcweir static inline void recycleCookie( sal_Int32 i) 373cdf0e10cSrcweir { 374cdf0e10cSrcweir getCookieQueue().push_back(i); 375cdf0e10cSrcweir } 376cdf0e10cSrcweir 377cdf0e10cSrcweir 378cdf0e10cSrcweir // calling the function twice with the same arguments will return tow different cookies. 379cdf0e10cSrcweir // The listener will then notified twice. 380cdf0e10cSrcweir 381cdf0e10cSrcweir extern "C" 382cdf0e10cSrcweir sal_Int32 SAL_CALL rtl_addUnloadingListener( rtl_unloadingListenerFunc callback, void* _this) 383cdf0e10cSrcweir { 384cdf0e10cSrcweir MutexGuard guard( getUnloadingMutex()); 385cdf0e10cSrcweir 386cdf0e10cSrcweir sal_Int32 cookie= getCookie(); 387cdf0e10cSrcweir ListenerMap& listenerMap= getListenerMap(); 388cdf0e10cSrcweir listenerMap[ cookie]= std::make_pair( callback, _this); 389cdf0e10cSrcweir return cookie; 390cdf0e10cSrcweir } 391cdf0e10cSrcweir 392cdf0e10cSrcweir 393cdf0e10cSrcweir extern "C" 394cdf0e10cSrcweir void SAL_CALL rtl_removeUnloadingListener( sal_Int32 cookie ) 395cdf0e10cSrcweir { 396cdf0e10cSrcweir MutexGuard guard( getUnloadingMutex()); 397cdf0e10cSrcweir 398cdf0e10cSrcweir ListenerMap& listenerMap= getListenerMap(); 399cdf0e10cSrcweir size_t removedElements= listenerMap.erase( cookie); 400cdf0e10cSrcweir if( removedElements ) 401cdf0e10cSrcweir recycleCookie( cookie); 402cdf0e10cSrcweir } 403cdf0e10cSrcweir 404cdf0e10cSrcweir 405cdf0e10cSrcweir static void rtl_notifyUnloadingListeners() 406cdf0e10cSrcweir { 407cdf0e10cSrcweir ListenerMap& listenerMap= getListenerMap(); 408cdf0e10cSrcweir for( Lis_IT it= listenerMap.begin(); it != listenerMap.end(); ++it) 409cdf0e10cSrcweir { 410cdf0e10cSrcweir rtl_unloadingListenerFunc callbackFunc= it->second.first; 411cdf0e10cSrcweir callbackFunc( it->second.second); 412cdf0e10cSrcweir } 413cdf0e10cSrcweir } 414