Light weight SemaCondvar & SemaMonitor

Light weight SemaCondvar and SemaMonitor version 2.31

Author: Amine Moulay Ramdane.

Description: SemaCondvar and SemaMonitor are new and portable synchronization objects , SemaCondvar combines some of the characteristics of a semaphore and all the characteristics of a condition variable and if you want the signal(s) to not be lost, you can configure it by passing a parameter to the constructor, SemaMonitor combines some of the characteristics of a semaphore and all the characteristics of an eventcount , and if you want the signal(s) to not be lost, you can configure it by passing a parameter to the constructor, they only use an event object and and a very fast and very efficient and portable lock.

If you don't want the signal to be lost if the threads are not waiting, just pass True to the state argument of to the constructor, if you pass False to the state argument of the construtor, so the signals will be lost if the threads are not waiting.

You will find the SemaMonitor and SemaCondvar classes inside the SemaMonitor.pas and SemaCondvar.pas files inside the zip file.

When you set the first parameter of the constructor to true, the signal will not be lost if the threads are not waiting for the SemaCondvar or SemaMonitor objects, but when you set the first parameter of the construtor to false, if the threads are not waiting for the SemaCondvar or SemaMonitor the signal will be lost..

Now you can pass the SemaCondvar's or Semamonitor's initialcount and SemaCondvar's or SemaMonitor's MaximumCount to the construtor, it's like the windows Semaphore`s InitialCount and the Semaphore's MaximumCount and it is where the signal(s) will be recorded.

Like this:

t:=TSemaMonitor.create(true,ctMLock,0,4);

If you set it with ctMLock, it will use my scalable node based lock called MLock, you can set it to ctMutex to use a Mutex or to ctCriticalSection to use the TCriticalSection.

Here is the methods that i have implemented :

TSemaCondvar = class

public

constructor

Create(m1:TSyncLock;state1:boolean=false;lock:TMyLocks=ctMLock;InitialCount1:long=0;MaximumCount1:long=MaxLong1);

destructor Destroy; override;

function wait(mstime:longword=INFINITE):boolean;

function signal():boolean;overload;

procedure signal_all();

function signal(nbr:long;var remains:long):boolean;overload;

function WaitersBlocked:integer;

end;

TSemaMonitor = class

public

constructor

Create(state1:boolean=false;lock:TMyLocks=ctMLock;InitialCount1:long=0;MaximumCount1:long=MaxLong1;Size:long=1024);

destructor Destroy; override;

function wait(mstime:longword=INFINITE):boolean;

function signal():boolean;overload;

procedure signal_all();

function signal(nbr:long;var remains:long):boolean;overload;

function WaitersBlocked:integer;

end;

the parameters InitialCount1 and MaximumCount1 is like the semaphore InitialCount and MaximumCount.

The wait() method is for the threads to wait on the SemaMonitor or SemaCondvar object for the signal to be signaled. If wait() fails, that can be that the number of waiters is greater than high(longword).

And the signal() method will signal one time a waiting thread on the SemaMonitor or SemaCondvar object, but if signal() fails , the returned value is false.

the signal_all() method will signal all the waiting threads on the SemaMonitor or SemaCondvar object.

The signal(nbr:long;var remains:long) method will signal nbr of waiting threads on the SemaMonitor or SemaCondvar object, but if signal() fails, the remaining number of signals that were not signaled will be returned in the remains variable.

and WaitersBlocked() will return the number of waiting threads on the SemaMonitor or SemaCondvar object.

You can go to download the zip files by clicking on the following web link:

https://drive.google.com/drive/folders/1ntWwEv4ioXYjXhFbxAtc6AfIM91guOSR?usp=sharing

Required FPC switches: -O3 -Sd

-Sd for delphi mode....

Required Delphi switches: -$H+ -DDelphi

Required Delphi XE-XE7 switch: -$H+ -DXE

{$DEFINE CPU32} and {$DEFINE Windows32} for 32 bit systems

{$DEFINE CPU64} and {$DEFINE Windows64} for 64 bit systems

- Platform: Windows, Unix, Linux and OSX on (x86)