mirror of
https://github.com/FeralInteractive/gamemode.git
synced 2025-06-06 07:37:21 +02:00
Refactor getting config values to stop duplicating the rwlock
This commit is contained in:
parent
1c38a6047e
commit
20828da140
@ -1,6 +1,7 @@
|
|||||||
1.2
|
1.2
|
||||||
|
|
||||||
Store the initial governor state on mode enter
|
Store the initial governor state on mode enter
|
||||||
|
Config now supports defaultgov and desiredgov
|
||||||
|
|
||||||
1.1
|
1.1
|
||||||
|
|
||||||
|
@ -64,6 +64,9 @@ struct GameModeConfig {
|
|||||||
char startscripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
|
char startscripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
|
||||||
char endscripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
|
char endscripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
|
||||||
|
|
||||||
|
char defaultgov[CONFIG_VALUE_MAX];
|
||||||
|
char desiredgov[CONFIG_VALUE_MAX];
|
||||||
|
|
||||||
long reaper_frequency;
|
long reaper_frequency;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -121,6 +124,16 @@ static bool get_long_value(const char *value_name, const char *value, long *outp
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get a string value
|
||||||
|
*/
|
||||||
|
static bool get_string_value(const char *value, char output[CONFIG_VALUE_MAX])
|
||||||
|
{
|
||||||
|
strncpy(output, value, CONFIG_VALUE_MAX - 1);
|
||||||
|
output[CONFIG_VALUE_MAX - 1] = '\0';
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Handler for the inih callback
|
* Handler for the inih callback
|
||||||
*/
|
*/
|
||||||
@ -140,6 +153,10 @@ static int inih_handler(void *user, const char *section, const char *name, const
|
|||||||
/* General subsection */
|
/* General subsection */
|
||||||
if (strcmp(name, "reaper_freq") == 0) {
|
if (strcmp(name, "reaper_freq") == 0) {
|
||||||
valid = get_long_value(name, value, &self->reaper_frequency);
|
valid = get_long_value(name, value, &self->reaper_frequency);
|
||||||
|
} else if (strcmp(name, "defaultgov") == 0) {
|
||||||
|
valid = get_string_value(value, self->defaultgov);
|
||||||
|
} else if (strcmp(name, "desiredgov") == 0) {
|
||||||
|
valid = get_string_value(value, self->desiredgov);
|
||||||
}
|
}
|
||||||
} else if (strcmp(section, "custom") == 0) {
|
} else if (strcmp(section, "custom") == 0) {
|
||||||
/* Custom subsection */
|
/* Custom subsection */
|
||||||
@ -195,6 +212,8 @@ static void load_config_files(GameModeConfig *self)
|
|||||||
memset(self->blacklist, 0, sizeof(self->blacklist));
|
memset(self->blacklist, 0, sizeof(self->blacklist));
|
||||||
memset(self->startscripts, 0, sizeof(self->startscripts));
|
memset(self->startscripts, 0, sizeof(self->startscripts));
|
||||||
memset(self->endscripts, 0, sizeof(self->endscripts));
|
memset(self->endscripts, 0, sizeof(self->endscripts));
|
||||||
|
memset(self->defaultgov, 0, sizeof(self->defaultgov));
|
||||||
|
memset(self->desiredgov, 0, sizeof(self->desiredgov));
|
||||||
self->reaper_frequency = DEFAULT_REAPER_FREQ;
|
self->reaper_frequency = DEFAULT_REAPER_FREQ;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -234,6 +253,21 @@ static void load_config_files(GameModeConfig *self)
|
|||||||
pthread_rwlock_unlock(&self->rwlock);
|
pthread_rwlock_unlock(&self->rwlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copy a config parameter with a lock
|
||||||
|
*/
|
||||||
|
static void memcpy_locked_config(GameModeConfig *self, void *dst, void *src, size_t n)
|
||||||
|
{
|
||||||
|
/* Take the read lock */
|
||||||
|
pthread_rwlock_rdlock(&self->rwlock);
|
||||||
|
|
||||||
|
/* copy the data */
|
||||||
|
memcpy(dst, src, n);
|
||||||
|
|
||||||
|
/* release the lock */
|
||||||
|
pthread_rwlock_unlock(&self->rwlock);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create a context object
|
* Create a context object
|
||||||
*/
|
*/
|
||||||
@ -329,17 +363,9 @@ bool config_get_client_blacklisted(GameModeConfig *self, const char *client)
|
|||||||
/*
|
/*
|
||||||
* Gets the reaper frequency
|
* Gets the reaper frequency
|
||||||
*/
|
*/
|
||||||
long config_get_reaper_thread_frequency(GameModeConfig *self)
|
void config_get_reaper_thread_frequency(GameModeConfig *self, long *value)
|
||||||
{
|
{
|
||||||
long value;
|
memcpy_locked_config(self, value, &self->reaper_frequency, sizeof(long));
|
||||||
/* Take the read lock */
|
|
||||||
pthread_rwlock_rdlock(&self->rwlock);
|
|
||||||
|
|
||||||
value = self->reaper_frequency;
|
|
||||||
|
|
||||||
/* release the lock */
|
|
||||||
pthread_rwlock_unlock(&self->rwlock);
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -348,13 +374,7 @@ long config_get_reaper_thread_frequency(GameModeConfig *self)
|
|||||||
void config_get_gamemode_start_scripts(GameModeConfig *self,
|
void config_get_gamemode_start_scripts(GameModeConfig *self,
|
||||||
char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX])
|
char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX])
|
||||||
{
|
{
|
||||||
/* Take the read lock */
|
memcpy_locked_config(self, scripts, self->startscripts, sizeof(self->startscripts));
|
||||||
pthread_rwlock_rdlock(&self->rwlock);
|
|
||||||
|
|
||||||
memcpy(scripts, self->startscripts, sizeof(self->startscripts));
|
|
||||||
|
|
||||||
/* release the lock */
|
|
||||||
pthread_rwlock_unlock(&self->rwlock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -363,11 +383,21 @@ void config_get_gamemode_start_scripts(GameModeConfig *self,
|
|||||||
void config_get_gamemode_end_scripts(GameModeConfig *self,
|
void config_get_gamemode_end_scripts(GameModeConfig *self,
|
||||||
char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX])
|
char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX])
|
||||||
{
|
{
|
||||||
/* Take the read lock */
|
memcpy_locked_config(self, scripts, self->endscripts, sizeof(self->startscripts));
|
||||||
pthread_rwlock_rdlock(&self->rwlock);
|
}
|
||||||
|
|
||||||
memcpy(scripts, self->endscripts, sizeof(self->startscripts));
|
/*
|
||||||
|
* Get the chosen default governor
|
||||||
/* release the lock */
|
*/
|
||||||
pthread_rwlock_unlock(&self->rwlock);
|
void config_get_default_governor(GameModeConfig *self, char governor[CONFIG_VALUE_MAX])
|
||||||
|
{
|
||||||
|
memcpy_locked_config(self, governor, self->defaultgov, sizeof(self->defaultgov));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get the chosen desired governor
|
||||||
|
*/
|
||||||
|
void config_get_desired_governor(GameModeConfig *self, char governor[CONFIG_VALUE_MAX])
|
||||||
|
{
|
||||||
|
memcpy_locked_config(self, governor, self->desiredgov, sizeof(self->desiredgov));
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ bool config_get_client_blacklisted(GameModeConfig *self, const char *client);
|
|||||||
/*
|
/*
|
||||||
* Get the frequency (in seconds) for the reaper thread
|
* Get the frequency (in seconds) for the reaper thread
|
||||||
*/
|
*/
|
||||||
long config_get_reaper_thread_frequency(GameModeConfig *self);
|
void config_get_reaper_thread_frequency(GameModeConfig *self, long *value);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get a set of scripts to call when gamemode starts
|
* Get a set of scripts to call when gamemode starts
|
||||||
|
@ -460,7 +460,8 @@ static void *game_mode_context_reaper(void *userdata)
|
|||||||
/* Stack, not allocated, won't disappear. */
|
/* Stack, not allocated, won't disappear. */
|
||||||
GameModeContext *self = userdata;
|
GameModeContext *self = userdata;
|
||||||
|
|
||||||
long reaper_interval = config_get_reaper_thread_frequency(self->config);
|
long reaper_interval = 0.0f;
|
||||||
|
config_get_reaper_thread_frequency(self->config, &reaper_interval);
|
||||||
|
|
||||||
struct timespec ts = { 0, 0 };
|
struct timespec ts = { 0, 0 };
|
||||||
ts.tv_sec = time(NULL) + reaper_interval;
|
ts.tv_sec = time(NULL) + reaper_interval;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user