Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2012-12

Delphi's current TThread implementation

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Sven Barth

Hello together!

I'm currently working on bringing FPC's TThread implementation up to par with XE3's, but while the wiki of Embarcadero is a very important source of information I can't get everything I need from it. Thus I'd like to ask someone with access to XE3's source code (or maybe also older versions; so please write which version you used :) ) to describe (not copy!!!) how the following methods/functions are implemented:

CheckSynchronize (in respect to entries queued by TThread.Queue; see below) TThread.CurrentThread (or more precise: TThread.GetCurrentThread) TThread.GetIsSingleProcessor (I suspect it's just a "Result := FProcessorCount = 1")
TThread.SpinWait
TThread.TerminatedSet
TThread.CheckThreadError
TThread.NameThreadForDebugging (I don't think that we'll implement that, but I'm curious ;) )

Some further questions regarding Delphi's threading implementation:

Does CheckSynchronize walk the whole queue or just a specific amount? What does CheckSynchronize do if an exception happens in one of the queued methods?
Does TThread.CurrentThread return something for externally created threads? Where/How is TThread.FExternalThread set?
Where/How is TThread.FProcessorCount set?
Is RemoveQueuedMethods called if the thread terminates?

Regards,
Sven

Reply by email

Nicholas Ring
to Sven Barth

From XE3...

Sven Barth wrote
CheckSynchronize (in respect to entries queued by TThread.Queue; see below)

See below?

TThread.CurrentThread (or more precise: TThread.GetCurrentThread)

if the current thread (private variables) is nil then create an external thread object.

   if external thread list is nil then
     create an external thread list
     Atomic Compare Exchange with the 'global' list. If the result is

not nil,
free the external thread list that was just created.' add the newly created external thread object to the external thread list
Set the private current thread object to be the newly created external thread object.
return private current thread object

Not sure if this is getting close on infringing on things....

TThread.GetIsSingleProcessor (I suspect it's just a "Result := FProcessorCount = 1")

Close. Checks to see if FProcessorCount is less than two (I hope it is never zero :-D ).

TThread.SpinWait

Checks to see if parameter is zero, if so, exit.
Loops parameter times, calling PAUSE instruction (for Intel compatible processors).

TThread.TerminatedSet

Empty method.

TThread.CheckThreadError

If the parameter is non-zero, raise an EThread exception.

TThread.NameThreadForDebugging (I don't think that we'll implement that, but I'm curious ;) )

If a debugger is present, raise a specific exception with pointer to a structure containing the name for the thread.
http://stackoverflow.com/questions/9089973/is-this-the-correct-way-to-set-a-threads-name-in-delphi-6 mentions that Delphi uses the same exception value as visual studio.

Some further questions regarding Delphi's threading implementation: Does CheckSynchronize walk the whole queue or just a specific amount?

Seems to be the whole queue (but I am not sure).

What does CheckSynchronize do if an exception happens in one of the queued methods?

If it is not queued, it attaches the exception object to the item, otherwise re-raises.

Does TThread.CurrentThread return something for externally created threads?

Don't quite understand the question.

Where/How is TThread.FExternalThread set?

Only when an external thread object is created.

Where/How is TThread.FProcessorCount set?

Gotten from system CPUCount variable (which is set from Windows API GetSystemInfo).

Is RemoveQueuedMethods called if the thread terminates?

RemoveQueuedEvents is called in the TThread destructor.

I hope that what I have done is helpful (and doesn't infringe on anything)...

Cheers,
Nick

Reply by email

Sven Barth
to Michael Schnell
"Michael Schnell" wrote
On 12/16/2012 05:41 PM, Sven Barth wrote: Please note that (as I already described in another message)

TThread.Queue now is an officially documented function (using overlay procedures with alternate parameters).

I know that (partly from your mail), but this does not explain some of the things they are doing in the background (like what happens with exceptions raised inside a queued function)

Regards,
Sven

Reply by email

Michael Schnell
to Sven Barth
Sven Barth wrote 6 lines
I know that (partly from your mail), but this does not explain some of the things they are doing in the background (like what happens with exceptions raised inside a queued function)

I see. Interesting stuff :-) .

Thanks,
-Michael

Reply by email