Skip to content

Commit 04e3c28

Browse files
author
Arnaud Bouchez
committed
async web: fixed TPollAsyncConnection locks structure
- ensure are properly aligned on 64-bit POSIX
1 parent c40eabb commit 04e3c28

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/mormot.commit.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
'2.2.8830'
1+
'2.2.8831'

src/net/mormot.net.async.pas

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ TPollAsyncConnection = class(TSynPersistent)
123123
// - uses its own reentrant implementation, faster/lighter than TOSLock
124124
fRW: array[boolean] of record
125125
Lock: PtrUInt;
126-
ThreadID: TThreadID;
127-
RentrantCount: integer;
126+
ThreadID: TThreadID; // pointer on POSIX, DWORD on Windows
127+
ReentrantCount: TThreadID; // fake integer, but aligned to ThreadID field
128128
end;
129129
/// low-level TLS context
130130
fSecure: INetTls;
@@ -1474,16 +1474,16 @@ function TPollAsyncConnection.TryLock(writer: boolean): boolean;
14741474
if Lock <> 0 then
14751475
if ThreadID = tid then
14761476
begin
1477-
inc(RentrantCount);
1477+
inc(PInteger(@ReentrantCount)^); // counter stored as TThreadID field
14781478
result := true;
14791479
end
14801480
else
14811481
exit
1482-
else if LockedExc(Lock, 1, 0) then
1482+
else if LockedExc(Lock, 1, 0) then // this thread we acquired this lock
14831483
begin
14841484
include(fFlags, fWasActive);
14851485
ThreadID := tid;
1486-
RentrantCount := 1;
1486+
ReentrantCount := TThreadID(1); // reset reentrant counter
14871487
result := true;
14881488
end;
14891489
end;
@@ -1493,8 +1493,8 @@ procedure TPollAsyncConnection.UnLock(writer: boolean);
14931493
if self <> nil then
14941494
with fRW[writer and fLockMax] do
14951495
begin
1496-
dec(RentrantCount);
1497-
if RentrantCount <> 0 then
1496+
dec(PInteger(@ReentrantCount)^); // counter stored as TThreadID field
1497+
if ReentrantCount <> TThreadID(0) then
14981498
exit;
14991499
Lock := 0;
15001500
ThreadID := TThreadID(0);
@@ -1638,7 +1638,8 @@ function TPollReadSockets.UnsetPending(tag: TPollSocketTag): boolean;
16381638
(TPollAsyncConnection(tag).fHandle <> 0) and
16391639
// another atpReadPending thread may currently own this connection
16401640
// (occurs if PollForPendingEvents was called in between)
1641-
(TPollAsyncConnection(tag).fRW[{write=}false].RentrantCount = 0) then
1641+
(TPollAsyncConnection(tag).fRW[{write=}false].
1642+
ReentrantCount = TThreadID(0)) then
16421643
begin
16431644
exclude(TPollAsyncConnection(tag).fFlags, fReadPending);
16441645
result := true;
@@ -1743,13 +1744,15 @@ function TPollAsyncSockets.Stop(connection: TPollAsyncConnection;
17431744
{$ifdef USE_WINIOCP}
17441745
fDebugLog.Add.Log(sllDebug, 'Stop(%) sock=% handle=% r=% w=%',
17451746
[caller, pointer(sock), connection.Handle,
1746-
connection.fRW[false].RentrantCount,
1747-
connection.fRW[true].RentrantCount], self);
1747+
PInteger(@connection.fRW[false].ReentrantCount)^,
1748+
PInteger(@connection.fRW[true].ReentrantCount)^], self);
17481749
{$else}
17491750
fDebugLog.Add.Log(sllDebug, 'Stop(%) sock=% handle=% r=%% w=%%',
17501751
[caller, pointer(sock), connection.Handle,
1751-
connection.fRW[false].RentrantCount, _SUB[fSubRead in connection.fFlags],
1752-
connection.fRW[true].RentrantCount, _SUB[fSubWrite in connection.fFlags]],
1752+
PInteger(@connection.fRW[false].ReentrantCount)^,
1753+
_SUB[fSubRead in connection.fFlags],
1754+
PInteger(@connection.fRW[true].ReentrantCount)^,
1755+
_SUB[fSubWrite in connection.fFlags]],
17531756
self);
17541757
{$endif USE_WINIOCP}
17551758
if sock <> nil then

0 commit comments

Comments
 (0)