Skip to content
This repository has been archived by the owner on Sep 30, 2018. It is now read-only.

Commit

Permalink
Cleaned up some of the grammar and spelling in the comments, thanks i…
Browse files Browse the repository at this point in the history
…n large part to a patch submitted by 'PaulePanter.' Also changed the globals fix to use boost::shared_ptr rather than the custom Reference counting. Thanks to elupus insisting that the globals fix be done with macros, this turned out to be fairly simple.
  • Loading branch information
Jim Carroll committed Mar 5, 2011
1 parent d8391d9 commit c3a6379
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 230 deletions.
6 changes: 2 additions & 4 deletions project/VS2010Express/XBMC.vcxproj
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug (DirectX)|Win32">
Expand Down Expand Up @@ -686,7 +686,6 @@
<ClCompile Include="..\..\xbmc\utils\PCMAmplifier.cpp" />
<ClCompile Include="..\..\xbmc\utils\PerformanceSample.cpp" />
<ClCompile Include="..\..\xbmc\utils\PerformanceStats.cpp" />
<ClCompile Include="..\..\xbmc\utils\ReferenceCounting.cpp" />
<ClCompile Include="..\..\xbmc\utils\RegExp.cpp" />
<ClCompile Include="..\..\xbmc\utils\RingBuffer.cpp" />
<ClCompile Include="..\..\xbmc\utils\RssReader.cpp" />
Expand Down Expand Up @@ -1517,7 +1516,6 @@
<ClInclude Include="..\..\xbmc\utils\PCMAmplifier.h" />
<ClInclude Include="..\..\xbmc\utils\PerformanceSample.h" />
<ClInclude Include="..\..\xbmc\utils\PerformanceStats.h" />
<ClInclude Include="..\..\xbmc\utils\ReferenceCounting.h" />
<ClInclude Include="..\..\xbmc\utils\RegExp.h" />
<ClInclude Include="..\..\xbmc\utils\RingBuffer.h" />
<ClInclude Include="..\..\xbmc\utils\RssReader.h" />
Expand Down Expand Up @@ -1999,4 +1997,4 @@
<UserProperties RESOURCE_FILE="XBMC_PC.rc" />
</VisualStudio>
</ProjectExtensions>
</Project>
</Project>
10 changes: 2 additions & 8 deletions project/VS2010Express/XBMC.vcxproj.filters
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="win32">
Expand Down Expand Up @@ -2456,9 +2456,6 @@
<ClCompile Include="..\..\xbmc\cores\VideoRenderers\RenderCapture.cpp">
<Filter>cores\VideoRenderers</Filter>
</ClCompile>
<ClCompile Include="..\..\xbmc\utils\ReferenceCounting.cpp">
<Filter>utils</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\xbmc\win32\pch.h">
Expand Down Expand Up @@ -4911,9 +4908,6 @@
<ClInclude Include="..\..\xbmc\cores\VideoRenderers\RenderCapture.h">
<Filter>cores\VideoRenderers</Filter>
</ClInclude>
<ClInclude Include="..\..\xbmc\utils\ReferenceCounting.h">
<Filter>utils</Filter>
</ClInclude>
<ClInclude Include="..\..\xbmc\utils\GlobalsHandling.h">
<Filter>utils</Filter>
</ClInclude>
Expand All @@ -4933,4 +4927,4 @@
<Filter>win32</Filter>
</CustomBuild>
</ItemGroup>
</Project>
</Project>
2 changes: 1 addition & 1 deletion xbmc/SectionLoader.h
Expand Up @@ -27,7 +27,7 @@
// forward
class LibraryLoader;

class CSectionLoader : public virtual xbmcutil::Referenced
class CSectionLoader
{
public:
class CSection
Expand Down
2 changes: 1 addition & 1 deletion xbmc/guilib/GraphicContext.h
Expand Up @@ -61,7 +61,7 @@ enum VIEW_TYPE { VIEW_TYPE_NONE = 0,
VIEW_TYPE_MAX };


class CGraphicContext : public CCriticalSection, public virtual xbmcutil::Referenced
class CGraphicContext : public CCriticalSection
{
public:
CGraphicContext(void);
Expand Down
2 changes: 1 addition & 1 deletion xbmc/settings/AdvancedSettings.h
Expand Up @@ -60,7 +60,7 @@ struct RefreshOverride

typedef std::vector<TVShowRegexp> SETTINGS_TVSHOWLIST;

class CAdvancedSettings : public virtual xbmcutil::Referenced
class CAdvancedSettings
{
public:
CAdvancedSettings();
Expand Down
2 changes: 1 addition & 1 deletion xbmc/utils/CharsetConverter.h
Expand Up @@ -28,7 +28,7 @@

#include <vector>

class CCharsetConverter : public virtual xbmcutil::Referenced
class CCharsetConverter
{
public:
CCharsetConverter();
Expand Down
97 changes: 68 additions & 29 deletions xbmc/utils/GlobalsHandling.h
Expand Up @@ -21,13 +21,13 @@

#pragma once

#include "utils/ReferenceCounting.h"
#include "boost/shared_ptr.hpp"

/**
* This file contains the pattern for moving "globals" from the BSS Segment to the heap.
* A note on usage of this pattern for globals replacement:
*
* This pattern uses a singleton pattern and some compiler/cpreprocessor sugar to allow
* This pattern uses a singleton pattern and some compiler/C preprocessor sugar to allow
* "global" variables to be lazy instantiated and initialized and moved from the BSS segment
* to the heap (that is, they are instantiated on the heap when they are first used rather
* than relying on the startup code to initialize the BSS segment). This eliminates the
Expand All @@ -40,35 +40,33 @@
* pointer in the header file of the global class (did you ever think you'd see a file scope
* 'static' variable in a header file - on purpose?)
*
* For more details see xbmc/utils/ReferenceCounting.h/cpp.
*
* There are two different ways to use this pattern when replacing global variables.
* The selection of which one to use depends on whether or not there is a possiblity
* that the code in the .cpp file for the global can be executed from a static method
* somewhere. This may take some explanation.
*
* The (at least) two ways to do this:
*
* 1) You can use the reference object Referenced::ref to access the global variable.
* 1) You can use the reference object boost::shared_ptr to access the global variable.
*
* This would be the preferred means since it is (very slightly) more efficent than
* the alternative. To use this pattern you replace standard static references to
* the global with access through the reference. If you use the c-preprocessor to
* the global with access through the reference. If you use the C preprocessor to
* do this for you can put the following code in the header file where the global's
* class is declared:
*
* static xbmcutil::Referenced::ref<GlobalVariableClass> g_globalVariableRef(xbmcutil::GlobalsSingleton<GlobalVariableClass>::getInstance);
* #define g_globalVariable (g_globalVariableRef.getRef())
* static boost::shared_ptr<GlobalVariableClass> g_globalVariableRef(xbmcutil::GlobalsSingleton<GlobalVariableClass>::getInstance());
* #define g_globalVariable (*(g_globalVariableRef.get()))
*
* Note what this does. In every file that includes this header there will be a *static*
* instance of the Referenced::ref<GlobalVariableClass> smart pointer. This effectively
* instance of the boost::shared_ptr<GlobalVariableClass> smart pointer. This effectively
* reference counts the singleton from every compilation unit (ie, object code file that
* results from a compilation of a .c/.cpp file) that references this global directly.
*
* There is a problem with this, however. Keep in mind that the instance of the smart pointer
* (being in the BSS segment of the compilation unit) is ITSELF an object that depends on
* the BSS segment initialization in order to be initialized with an instance from the
* Singleton. That means, depending on the code structure, it is possible to get into a
* singleton. That means, depending on the code structure, it is possible to get into a
* circumstance where the above #define could be exercised PRIOR TO the setting of the
* value of the smart pointer.
*
Expand All @@ -81,15 +79,15 @@
* Because of the "indirectly" in the above statement, this situation can be difficult to
* determine beforehand.
*
* 2) Alternately, when you KNOW that the global variable can suffer from the above described
* problem, you can restrict all access to the varaible to the singleton by changing
* 2) Alternatively, when you KNOW that the global variable can suffer from the above described
* problem, you can restrict all access to the variable to the singleton by changing
* the #define to:
*
* #define g_globalVariable (*(xbmcutil::Singleton<GlobalVariableClass>::getInstance()))
*
* A few things to note about this. First, this separates the reference counting aspect
* from the access aspect of this solution. The smart pointers are no longer used for
* reference, only for access. Secondly, all access is through the Singleton directly
* access, only for reference counting. Secondly, all access is through the singleton directly
* so there is no reliance on the state of the BSS segment for the code to operate
* correctly.
*
Expand All @@ -102,6 +100,12 @@ namespace xbmcutil
{

/**
* This class is an implementation detail of the macros defined below and
* is NOT meant to be used as a general purpose utility. IOW, DO NOT USE THIS
* CLASS to support a general singleton design pattern, it's specialized
* for solving the initialization/finalization order/dependency problem
* with global variables and should only be used via the macros below.
*
* Currently THIS IS NOT THREAD SAFE! Why not just add a lock you ask?
* Because this singleton is used to initialize global variables and
* there is an issue with having the lock used prior to its
Expand All @@ -111,29 +115,64 @@ namespace xbmcutil
*
* Therefore this hack depends on the fact that compilation unit global/static
* initialization is done in a single thread.
*
* In the spirit of making changes incrementally I've opted to not add Atomic*
* locking to this class yet. It doesn't need it (yet) as it's only called
* from the static initialization thread prior to main.
*/
template <class T> class GlobalsSingleton
{
static T* instance;
/**
* Is it possible that getInstance can be called prior to the shared_ptr 'instance'
* being initialized as a global? If so, then the shared_ptr constructor would
* effectively 'reset' the shared pointer after it had been set by the prior
* getInstance call, and a second instance would be created. We really don't
* want this to happen so 'instance' is a pointer to a smart pointer so that
* we can deterministally handle its construction.
*/
static boost::shared_ptr<T>* instance;

/**
* See 'getQuick' below.
*/
static T* quick;
public:
static T* getInstance()

/**
* Retrieve an instance of the singleton using a shared pointer for
* referenece counting.
*/
inline static boost::shared_ptr<T> getInstance()
{
if (instance == NULL)
instance = new T;
return instance;
if (!instance)
{
if (!quick)
quick = new T;
instance = new boost::shared_ptr<T>(quick);
}
return *instance;
}

/**
* This is for quick access when using form (2) of the pattern. Before 'mdd' points
* it out, this might be a case of 'solving problems we don't have' but this access
* is used frequently within the event loop so any help here should benefit the
* overall performance and there is nothing complicated or tricky here and not
* a lot of code to maintain.
*/
inline static T* getQuick()
{
if (!quick)
quick = new T;

return quick;
}

};

template <class T> T* GlobalsSingleton<T>::instance;
template <class T> boost::shared_ptr<T>* GlobalsSingleton<T>::instance;
template <class T> T* GlobalsSingleton<T>::quick;
}

/**
* For pattern (2) above, you can use the following macro. This pattern is safe to
* use in all cases but may be very slightly less efficent.
* use in all cases but may be very slightly less efficient.
*
* Also, you must also use a #define to replace the actual global variable since
* there's no way to use a macro to add a #define. An example would be:
Expand All @@ -143,22 +182,22 @@ namespace xbmcutil
*
*/
#define XBMC_GLOBAL_REF(classname,g_variable) \
static xbmcutil::Referenced::ref<classname> g_variable##Ref(xbmcutil::GlobalsSingleton<classname>::getInstance)
static boost::shared_ptr<classname> g_variable##Ref(xbmcutil::GlobalsSingleton<classname>::getInstance())

/**
* This declares the actual use of the variable. It needs to be used in another #define
* of the form:
*
* #define g_variable XBMC_GLOBAL_USE(classname)
*/
#define XBMC_GLOBAL_USE(classname) (*(xbmcutil::GlobalsSingleton<classname>::getInstance()))
#define XBMC_GLOBAL_USE(classname) (*(xbmcutil::GlobalsSingleton<classname>::getQuick()))

/**
* For pattern (1) above, you can use the following macro. WARNING: this should only
* For pattern (1) above, you can use the following macro. WARNING: This should only
* be used when the global in question is never accessed, directly or indirectly, from
* a static method called (again, directly or indirectly) durring startup or shutdown.
* a static method called (again, directly or indirectly) during startup or shutdown.
*/
#define XBMC_GLOBAL(classname,g_variable) \
XBMC_GLOBAL_REF(classname,g_variable); \
static classname & g_variable = g_variable##Ref.getRef()
static classname & g_variable = (*(g_variable##Ref.get()))

1 change: 0 additions & 1 deletion xbmc/utils/Makefile
Expand Up @@ -31,7 +31,6 @@ SRCS=AlarmClock.cpp \
PCMRemap.cpp \
PerformanceSample.cpp \
PerformanceStats.cpp \
ReferenceCounting.cpp \
RegExp.cpp \
RingBuffer.cpp \
RssReader.cpp \
Expand Down
71 changes: 0 additions & 71 deletions xbmc/utils/ReferenceCounting.cpp

This file was deleted.

0 comments on commit c3a6379

Please sign in to comment.