Merge pull request #4775 from jmarshallnz/empty_episode_playcount
[vuplus_xbmc] / xbmc / SectionLoader.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://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, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "threads/SystemClock.h"
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 LibraryLoader *CSectionLoader::LoadDLL(const CStdString &dllname, bool bDelayUnload /*=true*/, bool bLoadSymbols /*=false*/)
48 {
49   CSingleLock lock(g_sectionLoader.m_critSection);
50
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)
54   {
55     CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
56     if (dll.m_strDllName.Equals(dllname))
57     {
58       dll.m_lReferenceCount++;
59       return dll.m_pDll;
60     }
61   }
62
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);
66   if (!pDll)
67     return NULL;
68
69   CDll newDLL;
70   newDLL.m_strDllName = dllname;
71   newDLL.m_lReferenceCount = 1;
72   newDLL.m_bDelayUnload=bDelayUnload;
73   newDLL.m_pDll=pDll;
74   g_sectionLoader.m_vecLoadedDLLs.push_back(newDLL);
75
76   return newDLL.m_pDll;
77 }
78
79 void CSectionLoader::UnloadDLL(const CStdString &dllname)
80 {
81   CSingleLock lock(g_sectionLoader.m_critSection);
82
83   if (!dllname) return;
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)
86   {
87     CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
88     if (dll.m_strDllName.Equals(dllname))
89     {
90       dll.m_lReferenceCount--;
91       if (0 == dll.m_lReferenceCount)
92       {
93         if (dll.m_bDelayUnload)
94           dll.m_unloadDelayStartTick = XbmcThreads::SystemClockMillis();
95         else
96         {
97           CLog::Log(LOGDEBUG,"SECTION:UnloadDll(%s)", dllname.c_str());
98           if (dll.m_pDll)
99             DllLoaderContainer::ReleaseModule(dll.m_pDll);
100           g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
101         }
102
103         return;
104       }
105     }
106   }
107 }
108
109 void CSectionLoader::UnloadDelayed()
110 {
111   CSingleLock lock(g_sectionLoader.m_critSection);
112
113   // check if we can unload any unreferenced dlls
114   for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
115   {
116     CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
117     if (dll.m_lReferenceCount == 0 && XbmcThreads::SystemClockMillis() - dll.m_unloadDelayStartTick > UNLOAD_DELAY)
118     {
119       CLog::Log(LOGDEBUG,"SECTION:UnloadDelayed(DLL: %s)", dll.m_strDllName.c_str());
120
121       if (dll.m_pDll)
122         DllLoaderContainer::ReleaseModule(dll.m_pDll);
123       g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
124       return;
125     }
126   }
127 }
128
129 void CSectionLoader::UnloadAll()
130 {
131   // delete the dll's
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())
135   {
136     CDll& dll = *it;
137     CLog::Log(LOGDEBUG,"SECTION:UnloadAll(DLL: %s)", dll.m_strDllName.c_str());
138     if (dll.m_pDll)
139       DllLoaderContainer::ReleaseModule(dll.m_pDll);
140     it = g_sectionLoader.m_vecLoadedDLLs.erase(it);
141   }
142 }