Skip to content

Commit

Permalink
Atomic for BsModule
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmz committed Mar 31, 2018
1 parent c8e26ff commit c6cdcbd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
10 changes: 5 additions & 5 deletions Source/Foundation/bsfUtility/Utility/BsEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace bs
}

/**
* Called when the event handle no longer keeps a reference to the connection data. This means we might be able to
* Called when the event handle no longer keeps a reference to the connection data. This means we might be able to
* free (and reuse) its memory if the event is done with it too.
*/
void freeHandle(BaseConnectionData* conn)
Expand Down Expand Up @@ -220,8 +220,8 @@ namespace bs
/**
* Allows direct conversion of a handle to bool.
*
* @note
* Additional struct is needed because we can't directly convert to bool since then we can assign pointer to bool
* @note
* Additional struct is needed because we can't directly convert to bool since then we can assign pointer to bool
* and that's wrong.
*/
operator int Bool_struct::*() const
Expand All @@ -243,7 +243,7 @@ namespace bs
private:
BaseConnectionData* mConnection = nullptr;
SPtr<EventInternalData> mEventData;
};
};

/** @} */

Expand All @@ -260,7 +260,7 @@ namespace bs
*
* @note Callback method return value is ignored.
*/
// Note: I could create a policy template argument that allows creation of
// Note: I could create a policy template argument that allows creation of
// lockable and non-lockable events in the case mutex is causing too much overhead.
template <class RetType, class... Args>
class TEvent
Expand Down
4 changes: 2 additions & 2 deletions Source/Foundation/bsfUtility/Utility/BsMessageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace bs
HMessage MessageHandler::listen(MessageId message, std::function<void()> callback)
{
UINT32 callbackId = mNextCallbackId++;

MessageHandlerData data;
data.id = callbackId;
data.callback = callback;
Expand All @@ -66,7 +66,7 @@ namespace bs
Vector<MessageHandlerData>& handlerData = iterFind->second;

handlerData.erase(
std::remove_if(handlerData.begin(), handlerData.end(),
std::remove_if(handlerData.begin(), handlerData.end(),
[&](MessageHandlerData& x)
{
return x.id == handleId;
Expand Down
54 changes: 29 additions & 25 deletions Source/Foundation/bsfUtility/Utility/BsModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace bs
*/

/**
* Represents one engine module. Essentially it is a specialized type of singleton. Module must be manually started up
* Represents one engine module. Essentially it is a specialized type of singleton. Module must be manually started up
* and shut down before and after use.
*/
template <class T>
Expand All @@ -27,55 +27,59 @@ namespace bs
{
if (!isStartedUp())
{
BS_EXCEPT(InternalErrorException,
BS_EXCEPT(InternalErrorException,
"Trying to access a module but it hasn't been started up yet.");
}

if (isDestroyed())
{
BS_EXCEPT(InternalErrorException,
BS_EXCEPT(InternalErrorException,
"Trying to access a destroyed module.");
}

return *_instance();
}

/**
* Returns a pointer to the module instance. Module has to have been started up first otherwise an exception will
* Returns a pointer to the module instance. Module has to have been started up first otherwise an exception will
* be thrown.
*/
static T* instancePtr()
{
if (!isStartedUp())
{
BS_EXCEPT(InternalErrorException,
BS_EXCEPT(InternalErrorException,
"Trying to access a module but it hasn't been started up yet.");
}

if (isDestroyed())
{
BS_EXCEPT(InternalErrorException,
BS_EXCEPT(InternalErrorException,
"Trying to access a destroyed module.");
}

return _instance();
}

/** Constructs and starts the module using the specified parameters. */
template<class ...Args>
static void startUp(Args &&...args)
{
if (isStartedUp())
BS_EXCEPT(InternalErrorException, "Trying to start an already started module.");
T * test = nullptr;
bool success = _instance().compare_exchange_strong(test, bs_new<T>(std::forward<Args>(args)...));

if (!success)
BS_EXCEPT(InternalErrorException, "Race condition when starting module.");

_instance() = bs_new<T>(std::forward<Args>(args)...);
isStartedUp() = true;

((Module*)_instance())->onStartUp();
}

/**
* Constructs and starts a specialized type of the module. Provided type must derive from type the Module is
* Constructs and starts a specialized type of the module. Provided type must derive from type the Module is
* initialized with.
*/
template<class SubType, class ...Args>
Expand All @@ -97,19 +101,19 @@ namespace bs
{
if (isDestroyed())
{
BS_EXCEPT(InternalErrorException,
BS_EXCEPT(InternalErrorException,
"Trying to shut down an already shut down module.");
}
if (!isStartedUp())

if (!isStartedUp())
{
BS_EXCEPT(InternalErrorException,
BS_EXCEPT(InternalErrorException,
"Trying to shut down a module which was never started.");
}

((Module*)_instance())->onShutDown();

bs_delete(_instance());
bs_delete(_instance().load());
isDestroyed() = true;
}

Expand All @@ -131,42 +135,42 @@ namespace bs

/**
* Override if you want your module to be notified once it has been constructed and started.
*
* @note Useful when your module is polymorphic and you cannot perform some implementation specific
*
* @note Useful when your module is polymorphic and you cannot perform some implementation specific
* initialization in constructor itself.
*/
virtual void onStartUp() {}

/**
* Override if you want your module to be notified just before it is deleted.
*
* @note Useful when your module is polymorphic and you might want to perform some kind of clean up perhaps
*
* @note Useful when your module is polymorphic and you might want to perform some kind of clean up perhaps
* overriding that of a base class.
*/
virtual void onShutDown() {}

/** Returns a singleton instance of this module. */
static T*& _instance()
static std::atomic<T*>& _instance()
{
static T* inst = nullptr;
static std::atomic<T*> inst{nullptr};
return inst;
}

/**
* Checks has the Module been shut down.
*
*
* @note If module was never even started, this will return false.
*/
static bool& isDestroyed()
static std::atomic<bool>& isDestroyed()
{
static bool inst = false;
static std::atomic<bool> inst{false};
return inst;
}

/** Checks has the Module been started up. */
static bool& isStartedUp()
static std::atomic<bool>& isStartedUp()
{
static bool inst = false;
static std::atomic<bool> inst{false};
return inst;
}
};
Expand Down
4 changes: 2 additions & 2 deletions Source/Foundation/bsfUtility/Utility/BsPlatformUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace bs
public:
/**
* Terminates the current process.
*
*
* @param[in] force True if the process should be forcefully terminated with no cleanup.
*/
static void terminate(bool force = false);
Expand All @@ -57,7 +57,7 @@ namespace bs
* @{
*/

/**
/**
* Assigns information about GPU hardware. This data will be returned by getSystemInfo() when requested. This is
* expeced to be called by the render API backend when initialized.
*/
Expand Down

0 comments on commit c6cdcbd

Please sign in to comment.