Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-24 Thread Marco van de Voort
In our previous episode, Sven Barth said:
 And no one bothers to read my answer :(

I did. And the part about tthread not being expressed in terms of
the procedural api still stands. Apples and oranges.

They are IMHO two independent public API with no relation to eachother.

 Again: The only RTL that currently(!) does something inside CloseThread 
 is the Windows RTL: it closes the thread handle (which was returned by 
 BeginThread and is passed as an argument to CloseThread) using CloseHandle.

True. That's what it was introduced for.
 
 The Windows TThread.SysDestroy also calls CloseHandle on the handle 
 returned by BeginThread, so the semantic behavior of 
 Begin-/End-/CloseThread and TThread is the same.
 
 Nevertheless in my opinion TThread needs to be adjusted so that it calls 
 CloseThread for all RTLs and the direct CloseHandle call needs to be 
 removed. And this is exactly what I will do now...

If it makes you happy, but IMHO it is pointless.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-24 Thread Sven Barth

Am 23.04.2013 22:40, schrieb Anthony Walter:

Thanks for so much Sven. I have another question.

Is it okay for a thread to close itself before it's finished? Because 
I believe that is what happens when FreeOnTerminate is set to True. 
Here is a brief synopsis of what can execute:


In rtl/objpas/classes/classes.inc

Line 168: FreeThread := Thread.FFreeOnTerminate; // captures free on 
termiante

Line 173: Thread.Free; // invokes the destructor from within the thread
Line 197: CloseThread(FHandle); // thread is closed in destructor 
TThread.Destroy;
Line 174: EndThread(Result); // flow returns here, but the thread was 
already closed


So what it looks like is that we expect code inside a thread continue 
running after it has been closed.


That's ok, because the FHandle is information which is retrieved in the 
context of the calling thread (e.g. the main thread). And if we don't 
need any more information about the thread we just close it's handle 
(equivalent to CloseThread) and be done with it. EndThread on the other 
hand is called in the context of the called thread.


Afterall you also normally close the thread handle which you get 
returned by a CreateProcess(?) call, but keep the process handle around.


With CloseHandle (and the semantically equal CloseThread) you just tell 
the system that you don't plan to interact with the object that the 
handle references (in this case a thread) anymore.


Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-24 Thread Sven Barth

Am 24.04.2013 09:35, schrieb Marco van de Voort:



The Windows TThread.SysDestroy also calls CloseHandle on the handle
returned by BeginThread, so the semantic behavior of
Begin-/End-/CloseThread and TThread is the same.

Nevertheless in my opinion TThread needs to be adjusted so that it calls
CloseThread for all RTLs and the direct CloseHandle call needs to be
removed. And this is exactly what I will do now...

If it makes you happy, but IMHO it is pointless.

No, it's not pointless. If we should ever have the need to add code to 
the CloseThread functions of other platforms then we don't need to 
adjust the platform specific TThread implementations. TThread uses 
BeginThread (and also EndThread) and thus by definition it should also 
use CloseThread (the only exception is the old BeOS threading 
implementation, but this is only kept around for reference).


TThread is just an object oriented wrapper around the procedural 
threading API, so it must adhere to its rules as well.


Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-24 Thread Marco van de Voort
In our previous episode, Sven Barth said:
  CloseThread for all RTLs and the direct CloseHandle call needs to be
  removed. And this is exactly what I will do now...
  If it makes you happy, but IMHO it is pointless.
 
 No, it's not pointless. If we should ever have the need to add code to 
 the CloseThread functions of other platforms then we don't need to 
 adjust the platform specific TThread implementations. TThread uses 
 BeginThread (and also EndThread) and thus by definition it should also 
 use CloseThread (the only exception is the old BeOS threading 
 implementation, but this is only kept around for reference).

That is just one side. The other side is that modifications to the underused
procedural api potentially also could break the much used tthread
implementation.

IOW, IMHO the tthread implementation is leading.
 
 TThread is just an object oriented wrapper around the procedural 
 threading API, so it must adhere to its rules as well.

Around the threadmanager. Not the procedural api, it never was.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-24 Thread Sven Barth

Am 24.04.2013 12:43, schrieb Marco van de Voort:

TThread is just an object oriented wrapper around the procedural
threading API, so it must adhere to its rules as well.

Around the threadmanager. Not the procedural api, it never was.
Considering that all TThread implementations call BeginThread and 
EndThread and none calls ThreadManager.BeginThread and 
ThreadManager.EndThread directly this is not true (the only TThread 
implementation that in fact calls GetThreadManager is the Unix one and 
that only to call SemaphoreWait which is not exposed otherwise.


Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-24 Thread Anthony Walter
On Wed, Apr 24, 2013 at 3:53 AM, Sven Barth pascaldra...@googlemail.comwrote:

 That's ok, because the FHandle is information which is retrieved in the
 context of the calling thread (e.g. the main thread). And if we don't need
 any more information about the thread we just close it's handle (equivalent
 to CloseThread) and be done with it. EndThread on the other hand is called
 in the context of the called thread.

 Afterall you also normally close the thread handle which you get returned
 by a CreateProcess(?) call, but keep the process handle around.

 With CloseHandle (and the semantically equal CloseThread) you just tell
 the system that you don't plan to interact with the object that the handle
 references (in this case a thread) anymore.


Make sense, got it. Thank you.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Marco van de Voort
In our previous episode, Anthony Walter said:
 I was looking at threading support in the System unit and also in the
 TThread class.
 
 According to the documentation at
 http://www.freepascal.org/docs-html/rtl/system/closethread.html
 
 CloseThread must be called on any thread started with BeginThread. It must
 be called after the thread has ended (either by exiting the thread function
 or after calling EndThread).

Yes, Closethread closes possible handles deallocating anything that must
remain allocated to read out thread results.

 Looking at the TThread class, which does indeed use BeginThread in
 TThread.SysCreate (tthread.inc), I see no corresponding call the
 CloseThread. I grepped the entire fpc source tree and found no references
 anywhere to any code which calls CloseThread.

_BUT_ tthread.inc's are target dependent and thus don't necessarily go
through the platform independent procedural beginthread interface. 
(actually, some parts of the procedural interface, including chlosethread
were only crafted _after_ tthreads completion, when it turned out that the
procedural interface had leaks, and the tthread not)

E.g. the Closehandle at rtl/win/tthread.inc:35 does the same thing.
 
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Anthony Walter
I get your point about Windows + CloseHandle

But I'm still confused with regards to every other platform. You say yes,
affirming the documentation ... that CloseThread must be called ... nowhere
in any of the platform code I've search is CloseThread used, ever. (even
though BeginThread is used)
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Sven Barth

Am 23.04.2013 09:56, schrieb Anthony Walter:

I get your point about Windows + CloseHandle

But I'm still confused with regards to every other platform. You say 
yes, affirming the documentation ... that CloseThread must be called 
... nowhere in any of the platform code I've search is CloseThread 
used, ever. (even though BeginThread is used)
The only RTL that currently really does something inside CloseThread is 
the Windows RTL which calls CloseHandle like TThread.SysDestroy does. So 
it would probably best to adjust the TThread implementation to always 
call CloseThread as well and remove the CloseHandle in the Windows 
implementation of TThread.SysDestroy call...


Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Marco van de Voort
In our previous episode, Anthony Walter said:
 I get your point about Windows + CloseHandle
 
 But I'm still confused with regards to every other platform. You say yes,
 affirming the documentation ... that CloseThread must be called ... nowhere
 in any of the platform code I've search is CloseThread used, ever. (even
 though BeginThread is used)

True. But that only means that the beginthread interface is relatively
little used because everybody uses tthread.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Anthony Walter
On Tue, Apr 23, 2013 at 7:13 AM, Marco van de Voort mar...@stack.nl wrote:

 True. But that only means that the beginthread interface is relatively
 little used because everybody uses tthread.


But on i386-linux TThread does use BeginThread and never calls CloseThread.
So that seems contrary to the rule we are discussing in the documentation.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Marco van de Voort
In our previous episode, Anthony Walter said:
  True. But that only means that the beginthread interface is relatively
  little used because everybody uses tthread.
 
 But on i386-linux TThread does use BeginThread and never calls CloseThread.
 So that seems contrary to the rule we are discussing in the documentation.

The TThread code was never specified in terms of the procedural api. As said
the procedural api largely came after tthread was stable.

Probably at some point calls to the threadmanager were substituted with
calls to beginthread.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Anthony Walter
On Tue, Apr 23, 2013 at 9:22 AM, Marco van de Voort mar...@stack.nl wrote:

 The TThread code was never specified in terms of the procedural api. As
 said
 the procedural api largely came after tthread was stable.

 Probably at some point calls to the threadmanager were substituted with
 calls to beginthread.


BeginThread, EndThread, CloseThread all directly call thread manager
entries behind the scenes, so substituting a call to the thread manager
with a call to any of those functions are the same.

To explain, I am writing my own light threading library and just want to
ensure my housekeeping is being done correctly. Looking at the current
thread system which people use (TThread) and looking at the documentation
for BeginThread, EndThread, CloseThread, there seems to be a discrepancy.
I'd like to know which is correct.

That is, it is okay to call BeginThread and never call CloseThread? Because
that is how TThread currently works, and is in contradiction to the
documentation.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Marco van de Voort
In our previous episode, Anthony Walter said:
 That is, it is okay to call BeginThread and never call CloseThread? Because
 that is how TThread currently works, and is in contradiction to the
 documentation.

If you want split hairs:

It is ok for tthread, since as in tree code it can legally use non published
interfaces, it is not ok for you :_)
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Anthony Walter
Okay, thanks for the replies. I'll stick with what I have know, which is to
use BeginThread, EndThread, CloseThread.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Sven Barth

On 23.04.2013 16:28, Anthony Walter wrote:

That is, it is okay to call BeginThread and never call CloseThread?
Because that is how TThread currently works, and is in contradiction to
the documentation.


And no one bothers to read my answer :(

Again: The only RTL that currently(!) does something inside CloseThread 
is the Windows RTL: it closes the thread handle (which was returned by 
BeginThread and is passed as an argument to CloseThread) using CloseHandle.


The Windows TThread.SysDestroy also calls CloseHandle on the handle 
returned by BeginThread, so the semantic behavior of 
Begin-/End-/CloseThread and TThread is the same.


Nevertheless in my opinion TThread needs to be adjusted so that it calls 
CloseThread for all RTLs and the direct CloseHandle call needs to be 
removed. And this is exactly what I will do now...


Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Sven Barth

On 23.04.2013 21:40, Sven Barth wrote:

On 23.04.2013 16:28, Anthony Walter wrote:

That is, it is okay to call BeginThread and never call CloseThread?
Because that is how TThread currently works, and is in contradiction to
the documentation.


And no one bothers to read my answer :(

Again: The only RTL that currently(!) does something inside CloseThread
is the Windows RTL: it closes the thread handle (which was returned by
BeginThread and is passed as an argument to CloseThread) using CloseHandle.

The Windows TThread.SysDestroy also calls CloseHandle on the handle
returned by BeginThread, so the semantic behavior of
Begin-/End-/CloseThread and TThread is the same.

Nevertheless in my opinion TThread needs to be adjusted so that it calls
CloseThread for all RTLs and the direct CloseHandle call needs to be
removed. And this is exactly what I will do now...


Changed in revision 24313.

Regards,
Sven

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Anthony Walter
Thanks for so much Sven. I have another question.

Is it okay for a thread to close itself before it's finished? Because I
believe that is what happens when FreeOnTerminate is set to True. Here is a
brief synopsis of what can execute:

In rtl/objpas/classes/classes.inc

Line 168: FreeThread := Thread.FFreeOnTerminate; // captures free on
termiante
Line 173: Thread.Free; // invokes the destructor from within the thread
Line 197: CloseThread(FHandle); // thread is closed in destructor
TThread.Destroy;
Line 174: EndThread(Result); // flow returns here, but the thread was
already closed

So what it looks like is that we expect code inside a thread continue
running after it has been closed.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] What's the deal with CloseThread?

2013-04-23 Thread Anthony Walter
I was read this on MSDN:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx

Closing a thread handle does not terminate the associated thread or remove
the thread object. Closing a process handle does not terminate the
associated process or remove the process object. To remove a thread object,
you must terminate the thread, then close all handles to the thread.

So that would see to indicate on Windows at least, that CloseThread should
come after a thread has exited (I assume this should be the case for all
platforms). What this basically translates to is that threads should be
closed after use, but they cannot (or should not) be closed within its own
running thread context. It looks like something is wrong with whatever we
have at the moment.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal