Merge pull request #227 from pieh/weather_builtin
[vuplus_xbmc] / xbmc / SectionLoader.cpp
1 /*
2  *      Copyright (C) 2005-2008 Team XBMC
3  *      http://www.xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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
19  *
20  */
21
22 #include "system.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"
28
29 using namespace std;
30
31 #define g_sectionLoader XBMC_GLOBAL_USE(CSectionLoader)
32
33 //  delay for unloading dll's
34 #define UNLOAD_DELAY 30*1000 // 30 sec.
35
36 //Define this to get loggin on all calls to load/unload sections/dlls
37 //#define LOGALL
38
39 CSectionLoader::CSectionLoader(void)
40 {}
41
42 CSectionLoader::~CSectionLoader(void)
43 {
44   UnloadAll();
45 }
46
47 bool CSectionLoader::IsLoaded(const CStdString& strSection)
48 {
49   CSingleLock lock(g_sectionLoader.m_critSection);
50
51   for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedSections.size(); ++i)
52   {
53     CSection& section = g_sectionLoader.m_vecLoadedSections[i];
54     if (section.m_strSectionName == strSection && section.m_lReferenceCount > 0) return true;
55   }
56   return false;
57 }
58
59 bool CSectionLoader::Load(const CStdString& strSection)
60 {
61   CSingleLock lock(g_sectionLoader.m_critSection);
62
63   for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedSections.size(); ++i)
64   {
65     CSection& section = g_sectionLoader.m_vecLoadedSections[i];
66     if (section.m_strSectionName == strSection)
67     {
68
69 #ifdef LOGALL
70       CLog::Log(LOGDEBUG,"SECTION:LoadSection(%s) count:%i\n", strSection.c_str(), section.m_lReferenceCount);
71 #endif
72
73       section.m_lReferenceCount++;
74       return true;
75     }
76   }
77
78 #ifdef HAS_SECTIONS
79   if ( NULL == XLoadSection(strSection.c_str() ) )
80   {
81     CLog::Log(LOGDEBUG,"SECTION:LoadSection(%s) load failed!!\n", strSection.c_str());
82     return false;
83   }
84   HANDLE hHandle = XGetSectionHandle(strSection.c_str());
85
86   CLog::Log(LOGDEBUG,"SECTION:Section %s loaded count:1 size:%i\n", strSection.c_str(), XGetSectionSize(hHandle) );
87 #endif
88
89   CSection newSection;
90   newSection.m_strSectionName = strSection;
91   newSection.m_lReferenceCount = 1;
92   g_sectionLoader.m_vecLoadedSections.push_back(newSection);
93   return true;
94 }
95
96 void CSectionLoader::Unload(const CStdString& strSection)
97 {
98   CSingleLock lock(g_sectionLoader.m_critSection);
99   if (!CSectionLoader::IsLoaded(strSection)) return ;
100
101   ivecLoadedSections i;
102   i = g_sectionLoader.m_vecLoadedSections.begin();
103   while (i != g_sectionLoader.m_vecLoadedSections.end())
104   {
105     CSection& section = *i;
106     if (section.m_strSectionName == strSection)
107     {
108 #ifdef LOGALL
109       CLog::Log(LOGDEBUG,"SECTION:FreeSection(%s) count:%i\n", strSection.c_str(), section.m_lReferenceCount);
110 #endif
111       section.m_lReferenceCount--;
112       if ( 0 == section.m_lReferenceCount)
113       {
114         section.m_unloadDelayStartTick = CTimeUtils::GetTimeMS();
115         return ;
116       }
117     }
118     ++i;
119   }
120 }
121
122 LibraryLoader *CSectionLoader::LoadDLL(const CStdString &dllname, bool bDelayUnload /*=true*/, bool bLoadSymbols /*=false*/)
123 {
124   CSingleLock lock(g_sectionLoader.m_critSection);
125
126   if (!dllname) return NULL;
127   // check if it's already loaded, and increase the reference count if so
128   for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
129   {
130     CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
131     if (dll.m_strDllName.Equals(dllname))
132     {
133       dll.m_lReferenceCount++;
134       return dll.m_pDll;
135     }
136   }
137
138   // ok, now load the dll
139   CLog::Log(LOGDEBUG, "SECTION:LoadDLL(%s)\n", dllname.c_str());
140   LibraryLoader* pDll = DllLoaderContainer::LoadModule(dllname.c_str(), NULL, bLoadSymbols);
141   if (!pDll)
142     return NULL;
143
144   CDll newDLL;
145   newDLL.m_strDllName = dllname;
146   newDLL.m_lReferenceCount = 1;
147   newDLL.m_bDelayUnload=bDelayUnload;
148   newDLL.m_pDll=pDll;
149   g_sectionLoader.m_vecLoadedDLLs.push_back(newDLL);
150
151   return newDLL.m_pDll;
152 }
153
154 void CSectionLoader::UnloadDLL(const CStdString &dllname)
155 {
156   CSingleLock lock(g_sectionLoader.m_critSection);
157
158   if (!dllname) return;
159   // check if it's already loaded, and decrease the reference count if so
160   for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
161   {
162     CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
163     if (dll.m_strDllName.Equals(dllname))
164     {
165       dll.m_lReferenceCount--;
166       if (0 == dll.m_lReferenceCount)
167       {
168         if (dll.m_bDelayUnload)
169           dll.m_unloadDelayStartTick = CTimeUtils::GetTimeMS();
170         else
171         {
172           CLog::Log(LOGDEBUG,"SECTION:UnloadDll(%s)", dllname.c_str());
173           if (dll.m_pDll)
174             DllLoaderContainer::ReleaseModule(dll.m_pDll);
175           g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
176         }
177
178         return;
179       }
180     }
181   }
182 }
183
184 void CSectionLoader::UnloadDelayed()
185 {
186   CSingleLock lock(g_sectionLoader.m_critSection);
187
188   ivecLoadedSections i = g_sectionLoader.m_vecLoadedSections.begin();
189   while( i != g_sectionLoader.m_vecLoadedSections.end() )
190   {
191     CSection& section = *i;
192     if( section.m_lReferenceCount == 0 && CTimeUtils::GetTimeMS() - section.m_unloadDelayStartTick > UNLOAD_DELAY)
193     {
194       CLog::Log(LOGDEBUG,"SECTION:UnloadDelayed(SECTION: %s)", section.m_strSectionName.c_str());
195 #ifdef HAS_SECTIONS
196       XFreeSection(section.m_strSectionName.c_str());
197 #endif
198       i = g_sectionLoader.m_vecLoadedSections.erase(i);
199       continue;
200     }
201     i++;
202   }
203
204   // check if we can unload any unreferenced dlls
205   for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
206   {
207     CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
208     if (dll.m_lReferenceCount == 0 && CTimeUtils::GetTimeMS() - dll.m_unloadDelayStartTick > UNLOAD_DELAY)
209     {
210       CLog::Log(LOGDEBUG,"SECTION:UnloadDelayed(DLL: %s)", dll.m_strDllName.c_str());
211
212       if (dll.m_pDll)
213         DllLoaderContainer::ReleaseModule(dll.m_pDll);
214       g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
215       return;
216     }
217   }
218 }
219
220 void CSectionLoader::UnloadAll()
221 {
222   ivecLoadedSections i;
223   i = g_sectionLoader.m_vecLoadedSections.begin();
224   while (i != g_sectionLoader.m_vecLoadedSections.end())
225   {
226     CSection& section = *i;
227     //g_sectionLoader.m_vecLoadedSections.erase(i);
228     CLog::Log(LOGDEBUG,"SECTION:UnloadAll(SECTION: %s)", section.m_strSectionName.c_str());
229 #ifdef HAS_SECTIONS
230     XFreeSection(section.m_strSectionName.c_str());
231 #endif
232     i = g_sectionLoader.m_vecLoadedSections.erase(i);
233   }
234
235   // delete the dll's
236   CSingleLock lock(g_sectionLoader.m_critSection);
237   vector<CDll>::iterator it = g_sectionLoader.m_vecLoadedDLLs.begin();
238   while (it != g_sectionLoader.m_vecLoadedDLLs.end())
239   {
240     CDll& dll = *it;
241     CLog::Log(LOGDEBUG,"SECTION:UnloadAll(DLL: %s)", dll.m_strDllName.c_str());
242     if (dll.m_pDll)
243       DllLoaderContainer::ReleaseModule(dll.m_pDll);
244     it = g_sectionLoader.m_vecLoadedDLLs.erase(it);
245   }
246 }