threads, memory barriers and RTLeventSetEvent / RTLeventWaitFor

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

threads, memory barriers and RTLeventSetEvent / RTLeventWaitFor

Martin Friebe
I am currently looking at a strange issue in FpDebug.

The main thread loads a lot of data from a file, and creates various
objects.

Then some code is executed in a sub-thread. The timing of that
(start/stop of the 2 threads) is handled by RTLeventSetEvent /
RTLeventWaitFor.
This code does not seem to find (some of/ all?) the data. (for
comparison, on windows the same code continues to run in the main
thread, and finds the data).

So the suspicion arises that maybe the thread has older data cached?

Therefore my question:
Does RTLeventSetEvent / RTLeventWaitFor make sure memory is synchronized
between threads?

//MAIN thread
procedure TFpDebugDebugger.ExecuteInDebugThread(AMethod: TFpDbgAsyncMethod);
begin
   assert(not assigned(FFpDebugThread.AsyncMethod));
   FFpDebugThread.AsyncMethod:=AMethod;
   RTLeventSetEvent(FFpDebugThread.StartDebugLoopEvent);
   RTLeventWaitFor(FFpDebugThread.DebugLoopStoppedEvent);
   RTLeventResetEvent(FFpDebugThread.DebugLoopStoppedEvent);
   FFpDebugThread.AsyncMethod:=nil;
end;

// OTHER thread
procedure TFpDebugThread.Execute;
begin
...
     repeat
     RTLeventWaitFor(FStartDebugLoopEvent);
     RTLeventResetEvent(FStartDebugLoopEvent);
       if assigned(FAsyncMethod) then
         begin
         try
           FAsyncMethod();
         finally
           RTLeventSetEvent(FDebugLoopStoppedEvent);
         end;
         end;
     until Terminated;
end;

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: threads, memory barriers and RTLeventSetEvent / RTLeventWaitFor

Free Pascal - General mailing list
Martin <[hidden email]> schrieb am Di., 1. Okt. 2019, 17:19:
I am currently looking at a strange issue in FpDebug.

The main thread loads a lot of data from a file, and creates various
objects.

Then some code is executed in a sub-thread. The timing of that
(start/stop of the 2 threads) is handled by RTLeventSetEvent /
RTLeventWaitFor.
This code does not seem to find (some of/ all?) the data. (for
comparison, on windows the same code continues to run in the main
thread, and finds the data).

So the suspicion arises that maybe the thread has older data cached?

Therefore my question:
Does RTLeventSetEvent / RTLeventWaitFor make sure memory is synchronized
between threads?

//MAIN thread
procedure TFpDebugDebugger.ExecuteInDebugThread(AMethod: TFpDbgAsyncMethod);
begin
   assert(not assigned(FFpDebugThread.AsyncMethod));
   FFpDebugThread.AsyncMethod:=AMethod;
   RTLeventSetEvent(FFpDebugThread.StartDebugLoopEvent);
   RTLeventWaitFor(FFpDebugThread.DebugLoopStoppedEvent);
   RTLeventResetEvent(FFpDebugThread.DebugLoopStoppedEvent);
   FFpDebugThread.AsyncMethod:=nil;
end;

// OTHER thread
procedure TFpDebugThread.Execute;
begin
...
     repeat
     RTLeventWaitFor(FStartDebugLoopEvent);
     RTLeventResetEvent(FStartDebugLoopEvent);
       if assigned(FAsyncMethod) then
         begin
         try
           FAsyncMethod();
         finally
           RTLeventSetEvent(FDebugLoopStoppedEvent);
         end;
         end;
     until Terminated;
end;

The functions themselves AFAIK don't have any memory barriers, so it would depend solely on the OS primitives. 

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: threads, memory barriers and RTLeventSetEvent / RTLeventWaitFor

Jonas Maebe-3
On 01/10/2019 20:48, Sven Barth via fpc-pascal wrote:

> Martin <[hidden email] <mailto:[hidden email]>> schrieb am Di., 1. Okt.
> 2019, 17:19:
>
>     Therefore my question:
>     Does RTLeventSetEvent / RTLeventWaitFor make sure memory is
>     synchronized
>     between threads?
>
> The functions themselves AFAIK don't have any memory barriers, so it
> would depend solely on the OS primitives. 

The used OS-primitives do contain memory barriers (at least on Unix
platforms, but I would assume everywhere).


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal