fix sql init/deinit, add broadcast to force sql thread termination

git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@2995 fc73d0e0-1445-4013-8a0c-d673dee63da5
This commit is contained in:
TimeServ 2008-06-02 23:13:03 +00:00
parent afb7e76967
commit 8761bb1485

View file

@ -88,6 +88,7 @@ void PF_InitTempStrings(progfuncs_t *prinst);
#ifdef SQL #ifdef SQL
// SQL prototypes // SQL prototypes
void SQL_Init(); void SQL_Init();
void SQL_KillServers();
void SQL_DeInit(); void SQL_DeInit();
#endif #endif
@ -444,6 +445,8 @@ void Q_SetProgsParms(qboolean forcompiler)
void PR_Deinit(void) void PR_Deinit(void)
{ {
SQL_DeInit();
PR_ClearThreads(); PR_ClearThreads();
if (svprogfuncs) if (svprogfuncs)
{ {
@ -974,6 +977,8 @@ void PR_Init(void)
Cvar_Register (&sv_gameplayfix_honest_tracelines, cvargroup_progs); Cvar_Register (&sv_gameplayfix_honest_tracelines, cvargroup_progs);
Cvar_Register (&sv_gameplayfix_blowupfallenzombies, cvargroup_progs); Cvar_Register (&sv_gameplayfix_blowupfallenzombies, cvargroup_progs);
Cvar_Register (&sv_gameplayfix_noairborncorpse, cvargroup_progs); Cvar_Register (&sv_gameplayfix_noairborncorpse, cvargroup_progs);
SQL_Init();
} }
void Q_InitProgs(void) void Q_InitProgs(void)
@ -1075,11 +1080,7 @@ void Q_InitProgs(void)
if (oldprnum < 0) if (oldprnum < 0)
SV_Error("Couldn't open or compile progs\n"); SV_Error("Couldn't open or compile progs\n");
#ifdef SQL SQL_KillServers(); // TODO: is this the best placement for this?
// TODO: this REALLY needs better placement but this is for testing
SQL_DeInit();
SQL_Init();
#endif
f = PR_FindFunction (svprogfuncs, "AddAddonProgs", oldprnum); f = PR_FindFunction (svprogfuncs, "AddAddonProgs", oldprnum);
/* if (num) /* if (num)
@ -6425,6 +6426,7 @@ queryrequest_t *SQL_PullRequest(sqlserver_t *server, qboolean lock)
sqlserver_t **sqlservers; sqlserver_t **sqlservers;
int sqlservercount; int sqlservercount;
qboolean sqlavailable;
int sql_serverworker(void *sref) int sql_serverworker(void *sref)
{ {
@ -6546,6 +6548,12 @@ void PF_sqlconnect (progfuncs_t *prinst, struct globalvars_s *pr_globals)
int hsize; int hsize;
sqlserver_t *server; sqlserver_t *server;
if (!sqlavailable)
{
G_FLOAT(OFS_RETURN) = -1;
return;
}
// alloc or realloc sql servers array // alloc or realloc sql servers array
if (sqlservers == NULL) if (sqlservers == NULL)
{ {
@ -6604,7 +6612,7 @@ void PF_sqldisconnect (progfuncs_t *prinst, struct globalvars_s *pr_globals)
{ {
int serverref = G_FLOAT(OFS_PARM0); int serverref = G_FLOAT(OFS_PARM0);
if (serverref < 0 || serverref >= sqlservercount || sqlservers[serverref]->active == false) if (!sqlavailable || serverref < 0 || serverref >= sqlservercount || sqlservers[serverref]->active == false)
return; return;
sqlservers[serverref]->active = false; sqlservers[serverref]->active = false;
@ -6622,7 +6630,7 @@ void PF_sqlopenquery (progfuncs_t *prinst, struct globalvars_s *pr_globals)
queryrequest_t *qreq = (queryrequest_t *)ZF_Malloc(sizeof(queryrequest_t) + qsize); queryrequest_t *qreq = (queryrequest_t *)ZF_Malloc(sizeof(queryrequest_t) + qsize);
int querynum; int querynum;
if (!qreq || serverref < 0 || serverref >= sqlservercount || sqlservers[serverref]->active == false) if (!sqlavailable || !qreq || serverref < 0 || serverref >= sqlservercount || sqlservers[serverref]->active == false)
{ {
G_FLOAT(OFS_RETURN) = -1; G_FLOAT(OFS_RETURN) = -1;
return; return;
@ -6662,7 +6670,8 @@ void PF_sqlclosequery (progfuncs_t *prinst, struct globalvars_s *pr_globals)
int serverref = G_FLOAT(OFS_PARM0); int serverref = G_FLOAT(OFS_PARM0);
int queryref = G_FLOAT(OFS_PARM1); int queryref = G_FLOAT(OFS_PARM1);
if (serverref < 0 || if (!sqlavailable ||
serverref < 0 ||
serverref >= sqlservercount || serverref >= sqlservercount ||
sqlservers[serverref]->active == false || sqlservers[serverref]->active == false ||
!sqlservers[serverref]->currentresult || !sqlservers[serverref]->currentresult ||
@ -6683,7 +6692,8 @@ void PF_sqlreadfield (progfuncs_t *prinst, struct globalvars_s *pr_globals)
int row = G_FLOAT(OFS_PARM2); int row = G_FLOAT(OFS_PARM2);
int col = G_FLOAT(OFS_PARM3); int col = G_FLOAT(OFS_PARM3);
if (serverref < 0 || if (!sqlavailable ||
serverref < 0 ||
serverref >= sqlservercount || serverref >= sqlservercount ||
sqlservers[serverref]->active == false || sqlservers[serverref]->active == false ||
!sqlservers[serverref]->currentresult || !sqlservers[serverref]->currentresult ||
@ -6744,7 +6754,7 @@ void PF_sqlerror (progfuncs_t *prinst, struct globalvars_s *pr_globals)
{ {
int serverref = G_FLOAT(OFS_PARM0); int serverref = G_FLOAT(OFS_PARM0);
if (serverref < 0 || serverref >= sqlservercount) if (!sqlavailable || serverref < 0 || serverref >= sqlservercount)
{ // invalid server reference { // invalid server reference
RETURN_TSTRING(""); RETURN_TSTRING("");
return; return;
@ -6784,7 +6794,12 @@ void PF_sqlescape (progfuncs_t *prinst, struct globalvars_s *pr_globals)
toescape = PR_GetStringOfs(prinst, OFS_PARM1); toescape = PR_GetStringOfs(prinst, OFS_PARM1);
if (!toescape || !*toescape || serverref < 0 || serverref >= sqlservercount || sqlservers[serverref]->active == false) if (!toescape ||
!*toescape ||
!sqlavailable ||
serverref < 0 ||
serverref >= sqlservercount ||
sqlservers[serverref]->active == false)
{ // invalid string or server reference { // invalid string or server reference
RETURN_TSTRING(""); RETURN_TSTRING("");
return; return;
@ -6799,7 +6814,10 @@ void PF_sqlversion (progfuncs_t *prinst, struct globalvars_s *pr_globals)
{ {
int serverref = G_FLOAT(OFS_PARM0); int serverref = G_FLOAT(OFS_PARM0);
if (serverref < 0 || serverref >= sqlservercount || sqlservers[serverref]->active == false) if (!sqlavailable ||
serverref < 0 ||
serverref >= sqlservercount ||
sqlservers[serverref]->active == false)
{ // invalid string or server reference { // invalid string or server reference
RETURN_TSTRING(""); RETURN_TSTRING("");
return; return;
@ -6860,7 +6878,7 @@ void PR_SQLCycle()
{ {
globalvars_t *pr_globals; globalvars_t *pr_globals;
if (!svprogfuncs) if (!sqlavailable || !svprogfuncs)
return; return;
pr_globals = PR_globals(svprogfuncs, PR_CURRENT); pr_globals = PR_globals(svprogfuncs, PR_CURRENT);
@ -6874,24 +6892,29 @@ void SQL_Init()
if (!mysql_dll_init()) if (!mysql_dll_init())
{ {
Con_Printf("mysqlclient.dll didn't load\n"); Con_Printf("mysqlclient.dll didn't load\n");
// TODO: disable extension here return;
} }
#endif #endif
if (!mysql_thread_safe()) if (mysql_thread_safe())
{ {
Con_Printf("MYSQL client is not thread safe!\n"); if (mysql_library_init(0, NULL, NULL))
// TODO: disable extension here {
Con_Printf("MYSQL backend loaded\n");
sqlavailable = true;
return;
} }
else
if (!mysql_library_init(0, NULL, NULL))
{
Con_Printf("MYSQL library init failed!\n"); Con_Printf("MYSQL library init failed!\n");
// TODO: disable extension here
} }
else
Con_Printf("MYSQL client is not thread safe!\n");
mysql_dll_close();
sqlavailable = false;
} }
void SQL_DeInit() void SQL_KillServers()
{ {
int i; int i;
for (i = 0; i < sqlservercount; i++) for (i = 0; i < sqlservercount; i++)
@ -6900,8 +6923,9 @@ void SQL_DeInit()
queryrequest_t *qreq, *oldqreq; queryrequest_t *qreq, *oldqreq;
queryresult_t *qres, *oldqres; queryresult_t *qres, *oldqres;
server->active = false; // kill thread server->active = false; // set thread to kill itself
Sys_WaitOnThread(server->thread); Sys_ConditionBroadcast(server->requestcondv); // force condition check
Sys_WaitOnThread(server->thread); // wait on thread to die
// server resource deallocation (TODO: should this be done in the thread itself?) // server resource deallocation (TODO: should this be done in the thread itself?)
Sys_DestroyConditional(server->requestcondv); Sys_DestroyConditional(server->requestcondv);
@ -6942,11 +6966,22 @@ void SQL_DeInit()
Z_Free(sqlservers); Z_Free(sqlservers);
sqlservers = NULL; sqlservers = NULL;
sqlservercount = 0; sqlservercount = 0;
}
void SQL_DeInit()
{
sqlavailable = false;
SQL_KillServers();
mysql_library_end(); mysql_library_end();
mysql_dll_close(); mysql_dll_close();
} }
#else
void SQL_Init() {}
void SQL_KillServers() {}
void SQL_DeInit() {}
#endif #endif