Skip to content

Commit ea8688b

Browse files
committed
fine tuning critical section lock
1 parent 0eccb6c commit ea8688b

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/Libs/Protocol/Implementations/Http/Fpweb/FpwebAppServiceProviderImpl.pas

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ implementation
5050
StdOutIntf,
5151
ProtocolProcessorIntf,
5252
RunnableIntf,
53-
ThreadSafeProtocolProcessorImpl,
5453
FpwebProcessorImpl,
5554
FpwebStdOutWriterImpl,
5655
ThreadSafeFpwebResponseAwareImpl,
@@ -75,22 +74,13 @@ implementation
7574
end;
7675

7776
fProtocol := TFpwebProcessor.create(
77+
fLock,
7878
fStdOut as IFpwebResponseAware,
7979
svrConfig
8080
);
8181

8282
//TFpwebProcessor also act as server
8383
fServer := fProtocol as IRunnableWithDataNotif;
84-
85-
if svrConfig.threaded then
86-
begin
87-
fProtocol := TThreadSafeProtocolProcessor.create(
88-
fLock,
89-
fProtocol,
90-
fServer,
91-
fServer
92-
);
93-
end;
9484
end;
9585

9686
destructor TFpwebAppServiceProvider.destroy();

src/Libs/Protocol/Implementations/Http/Fpweb/FpwebProcessorImpl.pas

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface
1515

1616
uses
1717

18+
SyncObjs,
1819
RunnableIntf,
1920
RunnableWithDataNotifIntf,
2021
ProtocolProcessorIntf,
@@ -38,6 +39,7 @@ interface
3839
*-----------------------------------------------*)
3940
TFpwebProcessor = class(TInterfacedObject, IProtocolProcessor, IRunnable, IRunnableWithDataNotif)
4041
private
42+
fLock : TCriticalSection;
4143
fStdIn : IStreamAdapter;
4244
fRequestReadyListener : IReadyListener;
4345
fDataListener : IDataAvailListener;
@@ -75,6 +77,7 @@ TFpwebProcessor = class(TInterfacedObject, IProtocolProcessor, IRunnable, IR
7577

7678
public
7779
constructor create(
80+
const lock : TCriticalSection;
7881
const conn : IFpwebResponseAware;
7982
const svrConfig : TFpwebSvrConfig
8083
);
@@ -179,10 +182,12 @@ THttpServerThread = class(TThread)
179182
end;
180183

181184
constructor TFpwebProcessor.create(
185+
const lock : TCriticalSection;
182186
const conn : IFpwebResponseAware;
183187
const svrConfig : TFpwebSvrConfig
184188
);
185189
begin
190+
fLock := lock;
186191
fConnection := conn;
187192
fStdIn := nil;
188193
fRequestReadyListener := nil;
@@ -198,6 +203,7 @@ THttpServerThread = class(TThread)
198203
fRequestReadyListener := nil;
199204
fStdIn := nil;
200205
fConnection := nil;
206+
fLock := nil;
201207
inherited destroy();
202208
end;
203209

@@ -264,15 +270,32 @@ THttpServerThread = class(TThread)
264270
var fpwebEnv : ICGIEnvironment;
265271
begin
266272
fConnection.response := response;
267-
fpwebEnv := buildEnv(request);
268-
269-
fRequestReadyListener.ready(
270-
//we will not use socket stream as we will have our own IStdOut
271-
//that write output with TFpHttpServer
272-
TNullStreamAdapter.create(),
273-
fpwebEnv,
274-
TStreamAdapter.create(TStringStream.create(request.content))
275-
);
273+
if (fSvrConfig.threaded) then
274+
begin
275+
fLock.acquire();
276+
try
277+
fpwebEnv := buildEnv(request);
278+
fRequestReadyListener.ready(
279+
//we will not use socket stream as we will have our own IStdOut
280+
//that write output with TFpHttpServer
281+
TNullStreamAdapter.create(),
282+
fpwebEnv,
283+
TStreamAdapter.create(TStringStream.create(request.content))
284+
);
285+
finally
286+
fLock.release();
287+
end;
288+
end else
289+
begin
290+
fpwebEnv := buildEnv(request);
291+
fRequestReadyListener.ready(
292+
//we will not use socket stream as we will have our own IStdOut
293+
//that write output with TFpHttpServer
294+
TNullStreamAdapter.create(),
295+
fpwebEnv,
296+
TStreamAdapter.create(TStringStream.create(request.content))
297+
);
298+
end;
276299
end;
277300

278301
procedure TFpwebProcessor.handleRequest(

0 commit comments

Comments
 (0)