Permalink
Browse files

made TSynPersistentLock and TSynPersistentLocked aliases

since allocated PSynLocker on heap is no overhead in comparison to maintaining a lock
  • Loading branch information...
Arnaud Bouchez
Arnaud Bouchez committed Feb 7, 2019
1 parent c427027 commit 16554b43f67b40ef48d9840e7664824828eba8ac
Showing with 30 additions and 58 deletions.
  1. +6 −6 SQLite3/mORMot.pas
  2. +1 −1 SynBidirSock.pas
  3. +19 −47 SynCommons.pas
  4. +1 −1 SynEcc.pas
  5. +1 −1 SynLog.pas
  6. +1 −1 SynTable.pas
  7. +1 −1 SynopseCommit.inc
@@ -5020,7 +5020,7 @@ TSynMonitorUsageTrack = record
/// abstract class to track, compute and store TSynMonitor detailed statistics
// - you should inherit from this class to implement proper data persistence,
// e.g. using TSynMonitorUsageRest for ORM-based storage
TSynMonitorUsage = class(TSynPersistentLocked)
TSynMonitorUsage = class(TSynPersistentLock)
protected
fLog: TSynLogFamily;
fTracked: array of TSynMonitorUsageTrack;
TSQLRestObjArray = array of TSQLRest;

/// used to store the execution parameters for a TSQLRest instance
TSQLRestAcquireExecution = class(TSynPersistentLocked)
TSQLRestAcquireExecution = class(TSynPersistentLock)
public
/// how read or write operations will be executed
Mode: TSQLRestServerAcquireMode;
// "asynchronous write"), or maintain a versioned image of the content
// - all public methods (AddCopy/AddOwned/Update/Delete/FlushAsBatch) are
// thread-safe, protected by a mutex lock
TSQLRestTempStorage = class(TSynPersistentLocked)
TSQLRestTempStorage = class(TSynPersistentLock)
protected
fStoredClass: TSQLRecordClass;
fStoredClassRecordProps: TSQLRecordProperties;
TServicesPublishedInterfacesDynArray = array of TServicesPublishedInterfaces;

/// used e.g. by TSQLRestServer to store a list of TServicesPublishedInterfaces
TServicesPublishedInterfacesList = class(TSynPersistentLocked)
TServicesPublishedInterfacesList = class(TSynPersistentLock)
private
fDynArray: TDynArray;
fDynArrayTimeoutTix: TDynArray;
PSQLRestClientCallbackItem = ^TSQLRestClientCallbackItem;

/// store the references to active interface callbacks on a REST Client
TSQLRestClientCallbacks = class(TSynPersistentLocked)
TSQLRestClientCallbacks = class(TSynPersistentLock)
protected
fCurrentID: integer;
function UnRegisterByIndex(index: integer): boolean;
procedure TSQLRest.EndCurrentThread(Sender: TThread);
begin // most will be done e.g. in TSQLRestServer.EndCurrentThread
{$ifdef WITHLOG}
fLogClass.Add.NotifyThreadEnded;
fLogFamily.OnThreadEnded(Sender);
{$endif}
end;

@@ -699,7 +699,7 @@ TWebSocketProtocolBinary = class(TWebSocketProtocolRest)
end;

/// used to maintain a list of websocket protocols (for the server side)
TWebSocketProtocolList = class(TSynPersistentLocked)
TWebSocketProtocolList = class(TSynPersistentLock)
protected
fProtocols: array of TWebSocketProtocol;
// caller should make fSafe.Lock/UnLock
@@ -5100,7 +5100,7 @@ TDynArray = record
// - do nothing if index is out of range
procedure ElemCopyAt(index: PtrInt; var Dest); {$ifdef FPC}inline;{$endif}
/// will move one element content from its index into another variable
// - will erase the internal item ater copy
// - will erase the internal item after copy
// - do nothing if index is out of range
procedure ElemMoveTo(index: PtrInt; var Dest);
/// will copy one variable content into an indexed element
@@ -5690,7 +5690,7 @@ TSynPersistent = class(TObject)
/// optimized x86 asm finalization code
// - warning: this version won't release either any allocated TMonitor
// (as available since Delphi 2009) - do not use TMonitor with
// TSynPersistent, but rather the faster TSynPersistentLocked class
// TSynPersistent, but rather the faster TSynPersistentLock class
procedure FreeInstance; override;
{$endif}
end;
@@ -5869,19 +5869,22 @@ {$ifdef UNICODE}TSynLocker = record{$else}TSynLocker = object{$endif}
/// adding locking methods to a TSynPersistent with virtual constructor
// - you may use this class instead of the RTL TCriticalSection, since it
// would use a TSynLocker which does not suffer from CPU cache line conflit
TSynPersistentLocked = class(TSynPersistent)
TSynPersistentLock = class(TSynPersistent)
protected
fSafe: TSynLocker;
fSafe: PSynLocker; // TSynLocker would increase inherited fields offset
public
/// initialize the object instance, and its associated lock
/// initialize the instance, and its associated lock
constructor Create; override;
/// release the instance (including the locking resource)
/// finalize the instance, and its associated lock
destructor Destroy; override;
/// access to the locking methods of this instance
// - use Safe.Lock/TryLock with a try ... finally Safe.Unlock block
property Safe: TSynLocker read fSafe;
/// access to the associated instance critical section
// - call Safe.Lock/UnLock to protect multi-thread access on this storage
property Safe: PSynLocker read fSafe;
end;

/// used for backward compatibility only with existing code
TSynPersistentLocked = class(TSynPersistentLock);

/// adding locking methods to a TInterfacedObject with virtual constructor
TInterfacedObjectLocked = class(TInterfacedObjectWithCustomCreate)
protected
@@ -9383,7 +9386,7 @@ TAlgoCompressWithNoDestLen = class(TAlgoCompress)
// - TDynArray is a wrapper which do not store anything, whereas this class
// is able to store both keys and values, and provide convenient methods to
// access the stored data, including JSON serialization and binary storage
TSynDictionary = class(TSynPersistentLocked)
TSynDictionary = class(TSynPersistentLock)
protected
fKeys: TDynArrayHashed;
fValues: TDynArray;
function LoadFromBinary(const binary: RawByteString): boolean;
/// can be assigned to OnCanDeleteDeprecated to check TSynPersistentLock(aValue).Safe.IsLocked
class function OnCanDeleteSynPersistentLock(const aKey, aValue; aIndex: integer): boolean;
/// can be assigned to OnCanDeleteDeprecated to check TSynPersistentLocked(aValue).Safe.IsLocked
/// can be assigned to OnCanDeleteDeprecated to check TSynPersistentLock(aValue).Safe.IsLocked
class function OnCanDeleteSynPersistentLocked(const aKey, aValue; aIndex: integer): boolean;
/// returns how many items are currently stored in this dictionary
// - this method is thread-safe
/// thread-safe FIFO (First-In-First-Out) in-order queue of records
// - uses internally a dynamic array storage, with a sliding algorithm
// (more efficient than the FPC or Delphi TQueue)
TSynQueue = class(TSynPersistentLocked)
TSynQueue = class(TSynPersistentLock)
protected
fValues: TDynArray;
fValueVar: pointer;
public
/// initialize the thread
// - you could define some callbacks to nest the thread execution, e.g.
// assigned to TSQLRestServer.BeginCurrentThread/EndCurrentThread
// assigned to TSQLRestServer.BeginCurrentThread/EndCurrentThread, or
// at least set OnAfterExecute to TSynLogFamily.OnThreadEnded
constructor Create(const aThreadName: RawUTF8; OnBeforeExecute: TNotifyThreadEvent=nil;
OnAfterExecute: TNotifyThreadEvent=nil; CreateSuspended: boolean=false); reintroduce;
/// release used resources
/// allow parallel execution of an index-based process in a thread pool
// - will create its own thread pool, then execute any method by spliting the
// work into each thread
TSynParallelProcess = class(TSynPersistentLocked)
TSynParallelProcess = class(TSynPersistentLock)
protected
fThreadName: RawUTF8;
fPool: array of TSynParallelProcessThread;
property TaskLock: TSynLocker read fTaskLock;
end;

/// an alternative to TSynPersistentLocked, but via PSynLocker and not TSynLocker
// - TSynLocker will increase inherited fields offset so it may be fine if
// you want to avoid allocation of a PSynLocker buffer, but you may
// prefer this implementation for general-purpose processing class
TSynPersistentLock = class(TSynPersistent)
protected
fSafe: PSynLocker; // TSynLocker will increase inherited fields offset
public
/// initialize the instance, and its associated lock
constructor Create; override;
/// finalize the instance, and its associated lock
destructor Destroy; override;
/// access to the associated instance critical section
// - call Safe.Lock/UnLock to protect multi-thread access on this storage
property Safe: PSynLocker read fSafe;
end;

/// class-reference type (metaclass) of an TSynPersistentLock class
TSynPersistentLockClass = class of TSynPersistentLock;

{$endif FPC_OR_PUREPASCAL}


{ TSynPersistentLocked }

constructor TSynPersistentLocked.Create;
begin
inherited Create;
fSafe.Init;
end;

destructor TSynPersistentLocked.Destroy;
begin
inherited Destroy;
fSafe.Done;
end;


{ TSynPersistentLock }

constructor TSynPersistentLock.Create;
class function TSynDictionary.OnCanDeleteSynPersistentLocked(const aKey, aValue;
aIndex: integer): boolean;
begin
result := not TSynPersistentLocked(aValue).Safe.IsLocked;
result := not TSynPersistentLock(aValue).Safe.IsLocked;
end;

function TSynDictionary.SaveToBinary(NoCompression: boolean): RawByteString;
@@ -1259,7 +1259,7 @@ TECCSignatureCertifiedFile = class(TECCSignatureCertified)
// based on JSON objects or even plain base-64 encoded JSON strings
// - consider using TECCCertificateChainFile from mORMot.pas if you want
// to use convenient human-readable JSON serialization in files
TECCCertificateChain = class(TSynPersistentLocked)
TECCCertificateChain = class(TSynPersistentLock)
protected
fItems: TECCCertificateObjArray;
fIsValidCached: boolean;
@@ -349,7 +349,7 @@ TSynLogCallback = record
TSynLogCallbackDynArray = array of TSynLogCallback;

/// can manage a list of ISynLogCallback registrations
TSynLogCallbacks = class(TSynPersistentLocked)
TSynLogCallbacks = class(TSynPersistentLock)
protected
fCount: integer;
fCurrentlyEchoing: boolean;
@@ -1786,7 +1786,7 @@ procedure ToSBFStr(const Value: RawByteString; out Result: TSBFString);
// - internally, several (hardware-accelerated) crc32c hash functions will be
// used, with some random seed values, to simulate several hashing functions
// - Insert/MayExist/Reset methods are thread-safe
TSynBloomFilter = class(TSynPersistentLocked)
TSynBloomFilter = class(TSynPersistentLock)
private
fSize: cardinal;
fFalsePositivePercent: double;
@@ -1 +1 @@
'1.18.5021'
'1.18.5022'

0 comments on commit 16554b4

Please sign in to comment.