Light Weight SemaCondvar & SemaMonitor

Light weight SemaCondvar & SemaMonitor

Light weight SemaCondvar and SemaMonitor version 2.04

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,0,4);

You have 5 options in the defines.inc file for setting the kind of locks, just look inside defines.inc , if you want to set it for the Mutex that is energy efficient because it blocks the threads, uncomment the option Mutex, if you want to set it for my node based scalable Lock, uncomment the option MLock, if you want to set it for my scalable array based lock called AMLock just uncomment the option AMLock inside defines.inc, if you want to set it for Ticket Spinlock just uncomment the option TicketSpinlock ,If you want to set it for Spinlock just uncomment the option Spinlock.

Here is the methods that i have implemented :

TSemaCondvar = class

public

constructor

Create(m1:TCriticalSection;state1:boolean=false;InitialCount1:longword=0;MaximumCount1:longword=MaxLong1);

destructor Destroy; override;

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

procedure signal();overload;

procedure signal_all();

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

function WaitersBlocked:integer;

end;

TSemaMonitor = class

public

constructor

Create(state1:boolean=false;InitialCount1:longword=0;MaximumCount1:longword=MaxLong1);

destructor Destroy; override;

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

procedure signal();overload;

procedure signal_all();

function signal(nbr: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.

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

the signal(nbr:long) method will signal nbr number of waiting threads, but nbr must be less or equal to MaximumCount1.

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

Required FPC switches: -O3 -Sd -dFPC -dFreePascal

-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)

Please click on the small arrow on the right of the zip file bellow to download...