Skip to content
Permalink
Browse files

introducing NewSynLocker / TSynLocker.DoneAndFreemem functions

- used instead of TAutoLocker when possible
  • Loading branch information...
Arnaud Bouchez
Arnaud Bouchez committed May 27, 2019
1 parent d7d39f8 commit 220e32764cea6532154b793b6c7f713d2240d1b4
Showing with 139 additions and 150 deletions.
  1. +1 −0 ReadMe.txt
  2. +32 −34 SQLite3/mORMotHttpServer.pas
  3. +61 −81 SynBidirSock.pas
  4. +28 −9 SynCommons.pas
  5. +16 −25 SynTable.pas
  6. +1 −1 SynopseCommit.inc
@@ -56,6 +56,7 @@ Contributors
Mingda
Mr Yang (ysair)
Nicolas Marchand (MC)
Nortg
Nzsolt
Oleg Tretyakov
Ondrej (reddwarf)
@@ -274,20 +274,19 @@ EHttpServerException = class(ECommunicationException);
// - 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:
// ! DBServer.NoAJAXJSON := false;
TSQLHttpServer = class
TSQLHttpServer = class(TSynPersistentLock)
protected
fOnlyJSONRequests: boolean;
fShutdownInProgress: boolean;
fHttpServer: THttpServerGeneric;
fPort, fDomainName: AnsiString;
fPublicAddress, fPublicPort: RawUTF8;
/// internal servers to compute responses
/// internal servers to compute responses (protected by inherited fSafe)
fDBServers: array of record
Server: TSQLRestServer;
RestAccessRights: PSQLAccessRights;
Security: TSQLHttpServerSecurity;
end;
fDBServersSafe: IAutoLocker;
fHosts: TSynNameValue;
fAccessControlAllowOrigin: RawUTF8;
fAccessControlAllowOriginsMatch: TMatchs;
@@ -586,7 +585,7 @@ function TSQLHttpServer.AddServer(aServer: TSQLRestServer;
if (self=nil) or (aServer=nil) or (aServer.Model=nil) then
exit;
log := fLog.Enter(self, 'AddServer');
fDBServersSafe.Enter;
fSafe.Lock; // protect fDBServers[]
try
n := length(fDBServers);
for i := 0 to n-1 do
@@ -604,7 +603,7 @@ function TSQLHttpServer.AddServer(aServer: TSQLRestServer;
fHttpServer.ProcessName := GetDBServerNames;
result := true;
finally
fDBServersSafe.Leave;
fSafe.UnLock;
log.Log(sllHttp,'AddServer(%,Root=%,Port=%,Public=%:%)=%',
[aServer,aServer.Model.Root,fPort,fPublicAddress,fPublicPort,
BOOL_STR[result]],self);
@@ -613,14 +612,14 @@ function TSQLHttpServer.AddServer(aServer: TSQLRestServer;

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

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

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

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

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

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

0 comments on commit 220e327

Please sign in to comment.
You can’t perform that action at this time.