21 |
|
private |
22 |
|
function GetSa: PSecurityAttributes; |
23 |
|
protected |
24 |
< |
FInitialiser: boolean; static; |
24 |
> |
class var FInitialiser: boolean; |
25 |
|
FSa : TSecurityAttributes; |
26 |
|
private |
27 |
|
Sd : TSecurityDescriptor; |
143 |
|
FOwner: TGlobalInterface; |
144 |
|
FEvent: THandle; |
145 |
|
FLockCount: PInteger; |
146 |
< |
function GetLockCount: integer; |
146 |
> |
FMutex: TMutex; |
147 |
> |
function GetLockCount: integer; |
148 |
|
public |
149 |
|
constructor Create(EventName: string; AOwner: TGlobalInterface); |
150 |
|
destructor Destroy; override; |
341 |
|
inherited Create; |
342 |
|
FOwner := AOwner; |
343 |
|
FLockCount := PInteger(FOwner.SharedMemory.Allocate(sizeof(FLockCount))); |
344 |
+ |
FMutex := TMutex.Create(EventName + '.Mutex'); |
345 |
|
if FInitialiser then |
346 |
|
begin |
347 |
|
FEvent := CreateEvent(sa, true, true, PChar(EventName)); |
356 |
|
|
357 |
|
destructor TMultilockGate.Destroy; |
358 |
|
begin |
359 |
+ |
if assigned(FMutex) then FMutex.Free; |
360 |
|
CloseHandle(FEvent); |
361 |
|
inherited Destroy; |
362 |
|
end; |
368 |
|
|
369 |
|
procedure TMultilockGate.Lock; |
370 |
|
begin |
371 |
< |
InterlockedIncrement(FLockCount^); |
372 |
< |
ResetEvent(FEvent); |
371 |
> |
FMutex.Lock; |
372 |
> |
try |
373 |
> |
Inc(FLockCount^); |
374 |
> |
ResetEvent(FEvent); |
375 |
> |
finally |
376 |
> |
FMutex.Unlock; |
377 |
> |
end; |
378 |
|
//writeln('Lock '+IntToStr(FLockCount^)); |
379 |
|
end; |
380 |
|
|
381 |
|
procedure TMultilockGate.Unlock; |
382 |
|
begin |
383 |
|
//writeln('Start UnLock '+IntToStr(FLockCount^)); |
384 |
< |
InterlockedDecrement(FLockCount^); |
385 |
< |
if FLockCount^ <= 0 then |
386 |
< |
begin |
387 |
< |
SetEvent(FEvent); |
388 |
< |
FLockCount^ := 0 |
384 |
> |
FMutex.Lock; |
385 |
> |
try |
386 |
> |
Dec(FLockCount^); |
387 |
> |
if FLockCount^ <= 0 then |
388 |
> |
begin |
389 |
> |
SetEvent(FEvent); |
390 |
> |
FLockCount^ := 0 |
391 |
> |
end; |
392 |
> |
finally |
393 |
> |
FMutex.Unlock; |
394 |
|
end; |
395 |
|
//writeln('UnLock '+IntToStr(FLockCount^)); |
396 |
|
end; |