[fpc-devel] Why FreeBSD sem_init() works different to Linux?

Graeme Geldenhuys graeme at geldenhuys.co.uk
Thu Feb 7 17:33:26 CET 2013


On 2013-02-06 19:24, Graeme Geldenhuys wrote:
> Semaphore functionality seems pretty simple though, so I am thinking of
> creating my own Object Pascal based cross-platform semaphore - no low
> level code or OS specific library API's.

Progress... It was rather simple so far. I created a semaphore class,
used TCriticalSection to wrap the access to the semaphore's internal
count field. I API is similar, but not identical to TSynchroObject, so I
opted to descend from TObject instead. If Timeout (in milliseconds) is
specified, then Acquire will loop (ie: a blocking call). If the timeout
lapses, False is returned. If Timeout = INFINITE, it will block until a
successful acquire.

The units tests for this unit, and the tiPool unit tests in tiOPF (which
includes threaded access) now passes for FreeBSD and Linux under 64-bit
platforms. Next up I'll test under 32-bit systems and under Windows. If
all passes, the code will be published in the tiOPF2 code repository.

I have a few more methods I want to add to this class, but below is what
I needed for now to get tiOPF2 to work under FreeBSD.

---------------------------
unit tiSemaphore;

{$IFDEF fpc}
  {$mode objfpc}{$H+}
{$ENDIF}

interface

uses
  Classes, SysUtils, SyncObjs;

type
  TtiSemaphore = class(TObject)
  private
    FCount: LongWord;
    FMaximumCount: LongWord;
    FTimeout: LongWord;
    FCriticalSection: TCriticalSection;
    procedure   SetTimeout(AValue: LongWord);
  public
    constructor Create(const AMaximumCount: LongWord);
    destructor  Destroy; override;
    function    Acquire: Boolean;
    procedure   Release;
    property    Timeout: LongWord read FTimeout write SetTimeout;
    property    MaximumCount: LongWord read FMaximumCount;
    property    Count: LongWord read FCount;
  end;

...
---------------------------


Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/




More information about the fpc-devel mailing list