Skip to content

Commit

Permalink
Client|Plugin: Thread-safety for calling plugin hooks
Browse files Browse the repository at this point in the history
Keep track of the active plugin ID per thread.
  • Loading branch information
skyjake committed Apr 7, 2014
1 parent c40ae6b commit 4e1f63f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
8 changes: 3 additions & 5 deletions doomsday/client/include/dd_main.h
Expand Up @@ -94,17 +94,15 @@ de::LibraryFile const &Plug_FileForPlugin(pluginid_t id);
int DD_CallHooks(int hook_type, int parm, void* data);

/**
* Sets the ID of the currently active plugin. Note that plugin hooks are
* executed in a single-threaded manner; only one can be active at a time.
* Sets the ID of the currently active plugin in the current thread.
*
* @param id Plugin id.
*/
void DD_SetActivePluginId(pluginid_t id);

/**
* @return Unique identifier of the currently active plugin. Note that plugin
* hooks are executed in a single-threaded manner; only one is active at a
* time.
* @return Unique identifier of the currently active plugin. The currently
* active plugin is tracked separately for each thread.
*/
pluginid_t DD_ActivePluginId(void);

Expand Down
13 changes: 10 additions & 3 deletions doomsday/client/src/dd_plugin.cpp
Expand Up @@ -37,6 +37,7 @@
#endif

#include <de/findfile.h>
#include <QThreadStorage>

#define HOOKMASK(x) ((x) & 0xffffff)

Expand All @@ -52,7 +53,13 @@ typedef Library* PluginHandle;

static Library* hInstPlug[MAX_PLUGS]; /// @todo Remove arbitrary MAX_PLUGS.
static hookreg_t hooks[NUM_HOOK_TYPES];
static pluginid_t currentPlugin = 0; // none

struct ThreadState
{
pluginid_t currentPlugin;
ThreadState() : currentPlugin(0) {}
};
static QThreadStorage<ThreadState> pluginState; ///< Thread-local plugin state.

static PluginHandle* findFirstUnusedPluginHandle(void)
{
Expand Down Expand Up @@ -236,7 +243,7 @@ DENG_EXTERN_C int Plug_CheckForHook(int hookType)

void DD_SetActivePluginId(pluginid_t id)
{
currentPlugin = id;
pluginState.localData().currentPlugin = id;
}

int DD_CallHooks(int hookType, int parm, void *data)
Expand Down Expand Up @@ -274,7 +281,7 @@ int DD_CallHooks(int hookType, int parm, void *data)

pluginid_t DD_ActivePluginId(void)
{
return currentPlugin;
return pluginState.localData().currentPlugin;
}

void* DD_FindEntryPoint(pluginid_t pluginId, const char* fn)
Expand Down

0 comments on commit 4e1f63f

Please sign in to comment.