Skip to content

Commit

Permalink
introducing NewSynLocker / TSynLocker.DoneAndFreemem functions
Browse files Browse the repository at this point in the history
- used instead of TAutoLocker when possible
  • Loading branch information
Arnaud Bouchez committed May 27, 2019
1 parent d7d39f8 commit 220e327
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 150 deletions.
1 change: 1 addition & 0 deletions ReadMe.txt
Expand Up @@ -56,6 +56,7 @@ Contributors
Mingda Mingda
Mr Yang (ysair) Mr Yang (ysair)
Nicolas Marchand (MC) Nicolas Marchand (MC)
Nortg
Nzsolt Nzsolt
Oleg Tretyakov Oleg Tretyakov
Ondrej (reddwarf) Ondrej (reddwarf)
Expand Down
66 changes: 32 additions & 34 deletions SQLite3/mORMotHttpServer.pas
Expand Up @@ -274,20 +274,19 @@ EHttpServerException = class(ECommunicationException);
// - just create it and it will serve SQL statements as UTF-8 JSON // - just create it and it will serve SQL statements as UTF-8 JSON
// - for a true AJAX server, expanded data is prefered - your code may contain: // - for a true AJAX server, expanded data is prefered - your code may contain:
// ! DBServer.NoAJAXJSON := false; // ! DBServer.NoAJAXJSON := false;
TSQLHttpServer = class TSQLHttpServer = class(TSynPersistentLock)
protected protected
fOnlyJSONRequests: boolean; fOnlyJSONRequests: boolean;
fShutdownInProgress: boolean; fShutdownInProgress: boolean;
fHttpServer: THttpServerGeneric; fHttpServer: THttpServerGeneric;
fPort, fDomainName: AnsiString; fPort, fDomainName: AnsiString;
fPublicAddress, fPublicPort: RawUTF8; fPublicAddress, fPublicPort: RawUTF8;
/// internal servers to compute responses /// internal servers to compute responses (protected by inherited fSafe)
fDBServers: array of record fDBServers: array of record
Server: TSQLRestServer; Server: TSQLRestServer;
RestAccessRights: PSQLAccessRights; RestAccessRights: PSQLAccessRights;
Security: TSQLHttpServerSecurity; Security: TSQLHttpServerSecurity;
end; end;
fDBServersSafe: IAutoLocker;
fHosts: TSynNameValue; fHosts: TSynNameValue;
fAccessControlAllowOrigin: RawUTF8; fAccessControlAllowOrigin: RawUTF8;
fAccessControlAllowOriginsMatch: TMatchs; fAccessControlAllowOriginsMatch: TMatchs;
Expand Down Expand Up @@ -586,7 +585,7 @@ function TSQLHttpServer.AddServer(aServer: TSQLRestServer;
if (self=nil) or (aServer=nil) or (aServer.Model=nil) then if (self=nil) or (aServer=nil) or (aServer.Model=nil) then
exit; exit;
log := fLog.Enter(self, 'AddServer'); log := fLog.Enter(self, 'AddServer');
fDBServersSafe.Enter; fSafe.Lock; // protect fDBServers[]
try try
n := length(fDBServers); n := length(fDBServers);
for i := 0 to n-1 do for i := 0 to n-1 do
Expand All @@ -604,7 +603,7 @@ function TSQLHttpServer.AddServer(aServer: TSQLRestServer;
fHttpServer.ProcessName := GetDBServerNames; fHttpServer.ProcessName := GetDBServerNames;
result := true; result := true;
finally finally
fDBServersSafe.Leave; fSafe.UnLock;
log.Log(sllHttp,'AddServer(%,Root=%,Port=%,Public=%:%)=%', log.Log(sllHttp,'AddServer(%,Root=%,Port=%,Public=%:%)=%',
[aServer,aServer.Model.Root,fPort,fPublicAddress,fPublicPort, [aServer,aServer.Model.Root,fPort,fPublicAddress,fPublicPort,
BOOL_STR[result]],self); BOOL_STR[result]],self);
Expand All @@ -613,14 +612,14 @@ function TSQLHttpServer.AddServer(aServer: TSQLRestServer;


function TSQLHttpServer.DBServerFind(aServer: TSQLRestServer): integer; function TSQLHttpServer.DBServerFind(aServer: TSQLRestServer): integer;
begin begin
fDBServersSafe.Enter; fSafe.Lock; // protect fDBServers[]
try try
for result := 0 to Length(fDBServers)-1 do for result := 0 to Length(fDBServers)-1 do
if fDBServers[result].Server=aServer then if fDBServers[result].Server=aServer then
exit; exit;
result := -1; result := -1;
finally finally
fDBServersSafe.Leave; fSafe.UnLock;
end; end;
end; end;


Expand All @@ -632,7 +631,7 @@ function TSQLHttpServer.RemoveServer(aServer: TSQLRestServer): boolean;
if (self=nil) or (aServer=nil) or (aServer.Model=nil) then if (self=nil) or (aServer=nil) or (aServer.Model=nil) then
exit; exit;
log := fLog.Enter(self, 'RemoveServer'); log := fLog.Enter(self, 'RemoveServer');
fDBServersSafe.Enter; fSafe.Lock; // protect fDBServers[]
try try
n := high(fDBServers); n := high(fDBServers);
for i := n downto 0 do // may appear several times, with another Security for i := n downto 0 do // may appear several times, with another Security
Expand All @@ -652,7 +651,7 @@ function TSQLHttpServer.RemoveServer(aServer: TSQLRestServer): boolean;
result := true; // don't break here: may appear with another Security result := true; // don't break here: may appear with another Security
end; end;
finally finally
fDBServersSafe.Leave; fSafe.UnLock;
log.Log(sllHttp,'%.RemoveServer(Root=%)=%', log.Log(sllHttp,'%.RemoveServer(Root=%)=%',
[self,aServer.Model.Root,BOOL_STR[result]],self); [self,aServer.Model.Root,BOOL_STR[result]],self);
end; end;
Expand Down Expand Up @@ -682,7 +681,6 @@ constructor TSQLHttpServer.Create(const aPort: AnsiString;
log := fLog.Enter('Create % (%) on port %',[ToText(aHttpServerKind)^, log := fLog.Enter('Create % (%) on port %',[ToText(aHttpServerKind)^,
ToText(aHttpServerSecurity)^,aPort],self); ToText(aHttpServerSecurity)^,aPort],self);
{$endif} {$endif}
fDBServersSafe := TAutoLocker.Create;
inherited Create; inherited Create;
SetAccessControlAllowOrigin(''); // deny CORS by default SetAccessControlAllowOrigin(''); // deny CORS by default
fHosts.Init(false); fHosts.Init(false);
Expand Down Expand Up @@ -800,7 +798,7 @@ procedure TSQLHttpServer.Shutdown(noRestServerShutdown: boolean);
log := fLog.Enter('Shutdown(%)',[BOOL_STR[noRestServerShutdown]],self); log := fLog.Enter('Shutdown(%)',[BOOL_STR[noRestServerShutdown]],self);
fShutdownInProgress := true; fShutdownInProgress := true;
fHttpServer.Shutdown; fHttpServer.Shutdown;
fDBServersSafe.Enter; fSafe.Lock; // protect fDBServers[]
try try
for i := 0 to high(fDBServers) do begin for i := 0 to high(fDBServers) do begin
if not noRestServerShutdown then if not noRestServerShutdown then
Expand All @@ -809,7 +807,7 @@ procedure TSQLHttpServer.Shutdown(noRestServerShutdown: boolean);
fDBServers[i].Server.OnNotifyCallback := nil; // avoid unexpected GPF fDBServers[i].Server.OnNotifyCallback := nil; // avoid unexpected GPF
end; end;
finally finally
fDBServersSafe.Leave; fSafe.UnLock;
end; end;
end; end;
end; end;
Expand All @@ -819,12 +817,12 @@ function TSQLHttpServer.GetDBServer(Index: Integer): TSQLRestServer;
result := nil; result := nil;
if self=nil then if self=nil then
exit; exit;
fDBServersSafe.Enter; fSafe.Lock; // protect fDBServers[]
try try
if cardinal(Index)<cardinal(length(fDBServers)) then if cardinal(Index)<cardinal(length(fDBServers)) then
result := fDBServers[Index].Server; result := fDBServers[Index].Server;
finally finally
fDBServersSafe.Leave; fSafe.UnLock;
end; end;
end; end;


Expand All @@ -839,12 +837,12 @@ function TSQLHttpServer.GetDBServerNames: RawUTF8;
result := ''; result := '';
if self=nil then if self=nil then
exit; exit;
fDBServersSafe.Enter; fSafe.Lock; // protect fDBServers[]
try try
for i := 0 to high(fDBServers) do for i := 0 to high(fDBServers) do
result := result+fDBServers[i].Server.Model.Root+' '; result := result+fDBServers[i].Server.Model.Root+' ';
finally finally
fDBServersSafe.Leave; fSafe.UnLock;
end; end;
end; end;


Expand All @@ -853,20 +851,20 @@ procedure TSQLHttpServer.SetDBServerAccessRight(Index: integer;
begin begin
if self=nil then if self=nil then
exit; exit;
fDBServersSafe.Enter; fSafe.Lock; // protect fDBServers[]
try try
if Value=nil then if Value=nil then
Value := HTTP_DEFAULT_ACCESS_RIGHTS; Value := HTTP_DEFAULT_ACCESS_RIGHTS;
if cardinal(Index)<cardinal(length(fDBServers)) then if cardinal(Index)<cardinal(length(fDBServers)) then
fDBServers[Index].RestAccessRights := Value; fDBServers[Index].RestAccessRights := Value;
finally finally
fDBServersSafe.Leave; fSafe.UnLock;
end; end;
end; end;


procedure TSQLHttpServer.SetDBServer(aIndex: integer; aServer: TSQLRestServer; procedure TSQLHttpServer.SetDBServer(aIndex: integer; aServer: TSQLRestServer;
aSecurity: TSQLHttpServerSecurity; aRestAccessRights: PSQLAccessRights); aSecurity: TSQLHttpServerSecurity; aRestAccessRights: PSQLAccessRights);
begin begin // caller should have made fSafe.Lock
if (self<>nil) and (cardinal(aIndex)<cardinal(length(fDBServers))) then if (self<>nil) and (cardinal(aIndex)<cardinal(length(fDBServers))) then
with fDBServers[aIndex] do begin with fDBServers[aIndex] do begin
Server := aServer; Server := aServer;
Expand Down Expand Up @@ -987,20 +985,20 @@ function TSQLHttpServer.Request(Ctxt: THttpServerRequest): cardinal;
result := HTTP_NOTFOUND; // page not found by default (in case of wrong URL) result := HTTP_NOTFOUND; // page not found by default (in case of wrong URL)
serv := nil; serv := nil;
match := rmNoMatch; match := rmNoMatch;
fDBServersSafe.Enter; fSafe.Lock; // protect fDBServers[]
try try
for i := 0 to length(fDBServers)-1 do for i := 0 to length(fDBServers)-1 do
with fDBServers[i] do with fDBServers[i] do
if Ctxt.UseSSL=(Security=secSSL) then begin // registered for http or https if Ctxt.UseSSL=(Security=secSSL) then begin // registered for http or https
match := Server.Model.URIMatch(call.Url); match := Server.Model.URIMatch(call.Url);
if match=rmNoMatch then if match=rmNoMatch then
continue; continue;
call.RestAccessRights := RestAccessRights; call.RestAccessRights := RestAccessRights;
serv := Server; serv := Server;
break; break;
end; end;
finally finally
fDBServersSafe.Leave; fSafe.UnLock;
end; end;
if (match=rmNoMatch) or (serv=nil) then if (match=rmNoMatch) or (serv=nil) then
exit; exit;
Expand Down Expand Up @@ -1058,12 +1056,12 @@ procedure TSQLHttpServer.HttpThreadTerminate(Sender: TThread);
begin begin
if self=nil then if self=nil then
exit; exit;
fDBServersSafe.Enter; fSafe.Lock; // protect fDBServers[]
try try
for i := 0 to high(fDBServers) do for i := 0 to high(fDBServers) do
fDBServers[i].Server.EndCurrentThread(Sender); fDBServers[i].Server.EndCurrentThread(Sender);
finally finally
fDBServersSafe.Leave; fSafe.UnLock;
end; end;
end; end;


Expand All @@ -1073,12 +1071,12 @@ procedure TSQLHttpServer.HttpThreadStart(Sender: TThread);
if self=nil then if self=nil then
exit; exit;
SetCurrentThreadName('% %/%%',[self,fPort,GetDBServerNames,Sender]); SetCurrentThreadName('% %/%%',[self,fPort,GetDBServerNames,Sender]);
fDBServersSafe.Enter; fSafe.Lock; // protect fDBServers[]
try try
for i := 0 to high(fDBServers) do for i := 0 to high(fDBServers) do
fDBServers[i].Server.BeginCurrentThread(Sender); fDBServers[i].Server.BeginCurrentThread(Sender);
finally finally
fDBServersSafe.Leave; fSafe.UnLock;
end; end;
end; end;


Expand Down

0 comments on commit 220e327

Please sign in to comment.