2 * Copyright (C) 2005-2013 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, see
17 * <http://www.gnu.org/licenses/>.
21 #include "threads/SystemClock.h"
23 #include "SectionLoader.h"
24 #include "cores/DllLoader/DllLoaderContainer.h"
25 #include "threads/SingleLock.h"
26 #include "utils/log.h"
27 #include "utils/TimeUtils.h"
31 #define g_sectionLoader XBMC_GLOBAL_USE(CSectionLoader)
33 // delay for unloading dll's
34 #define UNLOAD_DELAY 30*1000 // 30 sec.
36 //Define this to get loggin on all calls to load/unload sections/dlls
39 CSectionLoader::CSectionLoader(void)
42 CSectionLoader::~CSectionLoader(void)
47 LibraryLoader *CSectionLoader::LoadDLL(const CStdString &dllname, bool bDelayUnload /*=true*/, bool bLoadSymbols /*=false*/)
49 CSingleLock lock(g_sectionLoader.m_critSection);
51 if (!dllname) return NULL;
52 // check if it's already loaded, and increase the reference count if so
53 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
55 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
56 if (dll.m_strDllName.Equals(dllname))
58 dll.m_lReferenceCount++;
63 // ok, now load the dll
64 CLog::Log(LOGDEBUG, "SECTION:LoadDLL(%s)\n", dllname.c_str());
65 LibraryLoader* pDll = DllLoaderContainer::LoadModule(dllname.c_str(), NULL, bLoadSymbols);
70 newDLL.m_strDllName = dllname;
71 newDLL.m_lReferenceCount = 1;
72 newDLL.m_bDelayUnload=bDelayUnload;
74 g_sectionLoader.m_vecLoadedDLLs.push_back(newDLL);
79 void CSectionLoader::UnloadDLL(const CStdString &dllname)
81 CSingleLock lock(g_sectionLoader.m_critSection);
84 // check if it's already loaded, and decrease the reference count if so
85 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
87 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
88 if (dll.m_strDllName.Equals(dllname))
90 dll.m_lReferenceCount--;
91 if (0 == dll.m_lReferenceCount)
93 if (dll.m_bDelayUnload)
94 dll.m_unloadDelayStartTick = XbmcThreads::SystemClockMillis();
97 CLog::Log(LOGDEBUG,"SECTION:UnloadDll(%s)", dllname.c_str());
99 DllLoaderContainer::ReleaseModule(dll.m_pDll);
100 g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
109 void CSectionLoader::UnloadDelayed()
111 CSingleLock lock(g_sectionLoader.m_critSection);
113 // check if we can unload any unreferenced dlls
114 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
116 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
117 if (dll.m_lReferenceCount == 0 && XbmcThreads::SystemClockMillis() - dll.m_unloadDelayStartTick > UNLOAD_DELAY)
119 CLog::Log(LOGDEBUG,"SECTION:UnloadDelayed(DLL: %s)", dll.m_strDllName.c_str());
122 DllLoaderContainer::ReleaseModule(dll.m_pDll);
123 g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
129 void CSectionLoader::UnloadAll()
132 CSingleLock lock(g_sectionLoader.m_critSection);
133 vector<CDll>::iterator it = g_sectionLoader.m_vecLoadedDLLs.begin();
134 while (it != g_sectionLoader.m_vecLoadedDLLs.end())
137 CLog::Log(LOGDEBUG,"SECTION:UnloadAll(DLL: %s)", dll.m_strDllName.c_str());
139 DllLoaderContainer::ReleaseModule(dll.m_pDll);
140 it = g_sectionLoader.m_vecLoadedDLLs.erase(it);