2 * Copyright (C) 2005-2008 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "threads/SystemClock.h"
24 #include "SectionLoader.h"
25 #include "cores/DllLoader/DllLoaderContainer.h"
26 #include "threads/SingleLock.h"
27 #include "utils/log.h"
28 #include "utils/TimeUtils.h"
32 #define g_sectionLoader XBMC_GLOBAL_USE(CSectionLoader)
34 // delay for unloading dll's
35 #define UNLOAD_DELAY 30*1000 // 30 sec.
37 //Define this to get loggin on all calls to load/unload sections/dlls
40 CSectionLoader::CSectionLoader(void)
43 CSectionLoader::~CSectionLoader(void)
48 LibraryLoader *CSectionLoader::LoadDLL(const CStdString &dllname, bool bDelayUnload /*=true*/, bool bLoadSymbols /*=false*/)
50 CSingleLock lock(g_sectionLoader.m_critSection);
52 if (!dllname) return NULL;
53 // check if it's already loaded, and increase the reference count if so
54 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
56 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
57 if (dll.m_strDllName.Equals(dllname))
59 dll.m_lReferenceCount++;
64 // ok, now load the dll
65 CLog::Log(LOGDEBUG, "SECTION:LoadDLL(%s)\n", dllname.c_str());
66 LibraryLoader* pDll = DllLoaderContainer::LoadModule(dllname.c_str(), NULL, bLoadSymbols);
71 newDLL.m_strDllName = dllname;
72 newDLL.m_lReferenceCount = 1;
73 newDLL.m_bDelayUnload=bDelayUnload;
75 g_sectionLoader.m_vecLoadedDLLs.push_back(newDLL);
80 void CSectionLoader::UnloadDLL(const CStdString &dllname)
82 CSingleLock lock(g_sectionLoader.m_critSection);
85 // check if it's already loaded, and decrease the reference count if so
86 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
88 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
89 if (dll.m_strDllName.Equals(dllname))
91 dll.m_lReferenceCount--;
92 if (0 == dll.m_lReferenceCount)
94 if (dll.m_bDelayUnload)
95 dll.m_unloadDelayStartTick = XbmcThreads::SystemClockMillis();
98 CLog::Log(LOGDEBUG,"SECTION:UnloadDll(%s)", dllname.c_str());
100 DllLoaderContainer::ReleaseModule(dll.m_pDll);
101 g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
110 void CSectionLoader::UnloadDelayed()
112 CSingleLock lock(g_sectionLoader.m_critSection);
114 // check if we can unload any unreferenced dlls
115 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
117 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
118 if (dll.m_lReferenceCount == 0 && XbmcThreads::SystemClockMillis() - dll.m_unloadDelayStartTick > UNLOAD_DELAY)
120 CLog::Log(LOGDEBUG,"SECTION:UnloadDelayed(DLL: %s)", dll.m_strDllName.c_str());
123 DllLoaderContainer::ReleaseModule(dll.m_pDll);
124 g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
130 void CSectionLoader::UnloadAll()
133 CSingleLock lock(g_sectionLoader.m_critSection);
134 vector<CDll>::iterator it = g_sectionLoader.m_vecLoadedDLLs.begin();
135 while (it != g_sectionLoader.m_vecLoadedDLLs.end())
138 CLog::Log(LOGDEBUG,"SECTION:UnloadAll(DLL: %s)", dll.m_strDllName.c_str());
140 DllLoaderContainer::ReleaseModule(dll.m_pDll);
141 it = g_sectionLoader.m_vecLoadedDLLs.erase(it);