Merge pull request #886 from Montellese/jsonrpc_runtime_enums
[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 "threads/SystemClock.h"
23 #include "system.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"
29
30 using namespace std;
31
32 #define g_sectionLoader XBMC_GLOBAL_USE(CSectionLoader)
33
34 //  delay for unloading dll's
35 #define UNLOAD_DELAY 30*1000 // 30 sec.
36
37 //Define this to get loggin on all calls to load/unload sections/dlls
38 //#define LOGALL
39
40 CSectionLoader::CSectionLoader(void)
41 {}
42
43 CSectionLoader::~CSectionLoader(void)
44 {
45   UnloadAll();
46 }
47
48 LibraryLoader *CSectionLoader::LoadDLL(const CStdString &dllname, bool bDelayUnload /*=true*/, bool bLoadSymbols /*=false*/)
49 {
50   CSingleLock lock(g_sectionLoader.m_critSection);
51
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)
55   {
56     CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
57     if (dll.m_strDllName.Equals(dllname))
58     {
59       dll.m_lReferenceCount++;
60       return dll.m_pDll;
61     }
62   }
63
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);
67   if (!pDll)
68     return NULL;
69
70   CDll newDLL;
71   newDLL.m_strDllName = dllname;
72   newDLL.m_lReferenceCount = 1;
73   newDLL.m_bDelayUnload=bDelayUnload;
74   newDLL.m_pDll=pDll;
75   g_sectionLoader.m_vecLoadedDLLs.push_back(newDLL);
76
77   return newDLL.m_pDll;
78 }
79
80 void CSectionLoader::UnloadDLL(const CStdString &dllname)
81 {
82   CSingleLock lock(g_sectionLoader.m_critSection);
83
84   if (!dllname) return;
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)
87   {
88     CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
89     if (dll.m_strDllName.Equals(dllname))
90     {
91       dll.m_lReferenceCount--;
92       if (0 == dll.m_lReferenceCount)
93       {
94         if (dll.m_bDelayUnload)
95           dll.m_unloadDelayStartTick = XbmcThreads::SystemClockMillis();
96         else
97         {
98           CLog::Log(LOGDEBUG,"SECTION:UnloadDll(%s)", dllname.c_str());
99           if (dll.m_pDll)
100             DllLoaderContainer::ReleaseModule(dll.m_pDll);
101           g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
102         }
103
104         return;
105       }
106     }
107   }
108 }
109
110 void CSectionLoader::UnloadDelayed()
111 {
112   CSingleLock lock(g_sectionLoader.m_critSection);
113
114   // check if we can unload any unreferenced dlls
115   for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
116   {
117     CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
118     if (dll.m_lReferenceCount == 0 && XbmcThreads::SystemClockMillis() - dll.m_unloadDelayStartTick > UNLOAD_DELAY)
119     {
120       CLog::Log(LOGDEBUG,"SECTION:UnloadDelayed(DLL: %s)", dll.m_strDllName.c_str());
121
122       if (dll.m_pDll)
123         DllLoaderContainer::ReleaseModule(dll.m_pDll);
124       g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
125       return;
126     }
127   }
128 }
129
130 void CSectionLoader::UnloadAll()
131 {
132   // delete the dll's
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())
136   {
137     CDll& dll = *it;
138     CLog::Log(LOGDEBUG,"SECTION:UnloadAll(DLL: %s)", dll.m_strDllName.c_str());
139     if (dll.m_pDll)
140       DllLoaderContainer::ReleaseModule(dll.m_pDll);
141     it = g_sectionLoader.m_vecLoadedDLLs.erase(it);
142   }
143 }