Sign in Join

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

Mailing lists / lazarus / 2008-06

Anyone using LCLIntf critical section routines?

12 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

Felipe Monteiro de Carvalho

Hello,

We are considering removing the critical section routines in LCLIntf, because there is already a TCriticalSection in the RTL in the SyncObjs unit.

If noone objects, and proves that those routines are useful for something in 1 week, we will remove them.

thanks,

Felipe Monteiro de Carvalho

Reply by email

Boian Mitov
to Felipe Monteiro de Carvalho

Hi Felipe,

We use them for code compatibility with Delphi. Windows threads do not tolerate non "cdecl" Delphi calling conventions insade them, so we are forced to use Win32 API critical sections to work around this problem. We have not tested enough yet to see if the same problem exists in Lazarus. If the problem is confirmed the CriticalSection Enter/Leave routines may have to be changed to be "cdecl" (at least under Windows) for Windows system threads compatibility. Becouse of this problem I have seen number of Delphi libraryes use Win32 API CriticalSection routines instead of the SyncObjs object. If you decide the functions are not needed, we can create our own CriticalSection wrapper, and call the Win32 API when using Delphi and the TCriticalSection from SyncObjs in Lazarus. You may however still consider keeping them for making it easier for other developers to port from existing Delphi code.

I must admit however that there are not so many heavily threaded applications and libraries. The only ones I have seen having this "cdecl" problem are multimedia (both DirectShow and VFW) calls and the "timeSetEvent" timers. This probably will have relatively small effect on the overall number of libraries and Delphi applications, so you can probably safely remove it, with limited effect. I don't have any serious objections, and if you plan the removal will start modifying our code to use our own wrapper.

With best regards,
Boian Mitov

-------------------------------------------------------------------- Mitov Software
http://www.mitov.com
--------------------------------------------------------------------

Earlier message 23 lines
----- Original Message ----- From: "Felipe Monteiro de Carvalho" To: "General mailing list" Sent: Sunday, June 22, 2008 11:35 AM Subject: [Lazarus] Anyone using LCLIntf critical section routines? > Hello, > > We are considering removing the critical section routines in LCLIntf, > because there is already a TCriticalSection in the RTL in the SyncObjs > unit. > > If noone objects, and proves that those routines are useful for > something in 1 week, we will remove them. > > thanks, > -- > Felipe Monteiro de Carvalho > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Felipe Monteiro de Carvalho
to Boian Mitov

The problem of keeping them is that we have to maintain them (duplicating work with the fpc team) and make sure they are working, which is not at all guaranteed today.

About your problem, well, you could send an e-mail to the fpc mailling list asking the fpc team to change those routines to cdecl under windows.

Felipe Monteiro de Carvalho

Reply by email

Boian Mitov
to Felipe Monteiro de Carvalho

Thank you!
I will remove any usage of them. I understand your point. If I confirm that Lazarus has the same problem as Delphi with the pascal calling convention in CriticalSection, I will make sure the issue is reported.

With best regards,
Boian Mitov

-------------------------------------------------------------------- Mitov Software
http://www.mitov.com
--------------------------------------------------------------------

Earlier message 21 lines
----- Original Message ----- From: "Felipe Monteiro de Carvalho" To: "General mailing list" Sent: Sunday, June 22, 2008 6:56 PM Subject: Re: [Lazarus] Anyone using LCLIntf critical section routines? > The problem of keeping them is that we have to maintain them > (duplicating work with the fpc team) and make sure they are working, > which is not at all guaranteed today. > > About your problem, well, you could send an e-mail to the fpc mailling > list asking the fpc team to change those routines to cdecl under > windows. > > -- > Felipe Monteiro de Carvalho > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Luca Olivetti
to Boian Mitov: We use them for code compatibility with Delphi. Windows threads do not tolerate non…

En/na Boian Mitov ha escrit:

Show quoted text 5 lines
Hi Felipe, We use them for code compatibility with Delphi. Windows threads do not tolerate non "cdecl" Delphi calling conventions insade them, so we are forced to use Win32 API critical sections to work around this problem.

I don't get you here: I'm using SyncObjs.TCriticalSection under windows (and linux) and I see no problem. I don't remember if I used it with delphi though.

Bye

Luca Olivetti Wetron Automatizaci?n S.A. http://www.wetron.es/ Tel. +34 93 5883004 Fax +34 93 5883007

Reply by email

Boian Mitov
to Luca Olivetti

The problem is only when the CriticalSection Enter/Leave is called from a callback of a windows thread such as DirectShow Grabber or VFW frame callback or a media timer callback. In all other cases TCriticalSection works fine. This is issue under Delphi. I have not confirmed if it exists under Lazarus yet, as I have just started to port to Lazarus.

To reproduce the bug in Delphi if you wish, start a media timer, and in the callback function call a Pascal calling convention function. It can be an empty body procedure as example. Run it for about 1H. Suddenly Delphi will stop and will report stack corruption. Change the procedure to be "cdecl", and it will run forever.

I state again, we are not at the point when we are running our code for hours under Lazarus. Once we are there I will report if the problem appears.

With best regards,
Boian Mitov

-------------------------------------------------------------------- Mitov Software
http://www.mitov.com
--------------------------------------------------------------------

Earlier message 17 lines
----- Original Message ----- From: "Luca Olivetti" To: "General mailing list" Sent: Monday, June 23, 2008 12:30 AM Subject: Re: [Lazarus] Anyone using LCLIntf critical section routines? I don't get you here: I'm using SyncObjs.TCriticalSection under windows (and linux) and I see no problem. I don't remember if I used it with delphi though. Bye -- Luca Olivetti Wetron Automatizaci?n S.A. http://www.wetron.es/ Tel. +34 93 5883004 Fax +34 93 5883007

Reply by email

Luca Olivetti
to Boian Mitov

En/na Boian Mitov ha escrit:

The problem is only when the CriticalSection Enter/Leave is called from a callback of a windows thread such as DirectShow Grabber or VFW frame callback or a media timer callback.

Ah, ok, I don't use it that way

Bye

Luca Olivetti Wetron Automatizaci?n S.A. http://www.wetron.es/ Tel. +34 93 5883004 Fax +34 93 5883007

Reply by email

Marco van de Voort
to Boian Mitov: The problem is only when the CriticalSection Enter/Leave is called from a callback of a…
Boian Mitov wrote 14 lines
The problem is only when the CriticalSection Enter/Leave is called from a callback of a windows thread such as DirectShow Grabber or VFW frame callback or a media timer callback. In all other cases TCriticalSection works fine. This is issue under Delphi. I have not confirmed if it exists under Lazarus yet, as I have just started to port to Lazarus. To reproduce the bug in Delphi if you wish, start a media timer, and in the callback function call a Pascal calling convention function. It can be an empty body procedure as example. Run it for about 1H. Suddenly Delphi will stop and will report stack corruption. Change the procedure to be "cdecl", and it will run forever. I state again, we are not at the point when we are running our code for hours under Lazarus. Once we are there I will report if the problem appears.

Hmm, it might be a problem of those api's callbacks, or their declarations that assume some registers are saved that aren't saved when the register calling convention is used.

IOW, you could be looking at the wrong place, the problem not being the cdecl adding, but a wrong declaration of the callbacks (cdecl vs stdcall?)

Reply by email

Boian Mitov
to Marco van de Voort

The callbacks are "stdcall" . The problem is confirmed to be the Delphi calling convention, and you seem to be right. It appears to be registers not being properly saved. I never had the time to investigate it on assembly level :-( .

To make you laugh at me, and my stupidity, 3 years after I discovered the problem in Delphi after 3 days of cutting code to nail it down to problem with the calling convention I did the same mistake in C++ Builder where the same problem exists :-D . Am I stupid or what, should have known after the first burn, but sometimes you just forget. Now this is the first thing I check when I have unexpected rare crashes.

With best regards,
Boian Mitov

-------------------------------------------------------------------- Mitov Software
http://www.mitov.com
--------------------------------------------------------------------

Earlier message 18 lines
----- Original Message ----- From: "Marco van de Voort" To: "General mailing list" Sent: Monday, June 23, 2008 3:01 AM Subject: Re: [Lazarus] Anyone using LCLIntf critical section routines? > > Hmm, it might be a problem of those api's callbacks, or their declarations > that assume some registers are saved that aren't saved when the register > calling convention is used. > > IOW, you could be looking at the wrong place, the problem not being the > cdecl adding, but a wrong declaration of the callbacks (cdecl vs stdcall?) > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

JoshyFun
to Boian Mitov: The problem is only when the CriticalSection Enter/Leave is called from a callback of a…

Hello Boian,

Monday, June 23, 2008, 9:52:51 AM, you wrote:

BM> The problem is only when the CriticalSection Enter/Leave is called from a BM> callback of a windows thread such as DirectShow Grabber or VFW frame BM> callback or a media timer callback. In all other cases TCriticalSection BM> works fine. This is issue under Delphi. I have not confirmed if it exists BM> under Lazarus yet, as I have just started to port to Lazarus.

I'm using critical sections, in delphi 7, in the callback of a sample-grabber directshow object which is being called up to 175000 times per second (usually around 4000 per second) and the application was running up to 7 days without any problem.

In the other hand I can not see any technical reason to have problems with calling convention unless an ASM block is being used somewhere after the call and stack is being cleared as cdecl instead the current calling convention :-?

Best regards, JoshyFun

Reply by email

Boian Mitov
to JoshyFun

Hi Joshy,

It can be OS version specific problem, or could be related to the codes I have in my DirectShow. I have however seen the problem in multiple systems. It also depends of what thread the Grabber will run from (Who is driving the part of the DS Graph). We have repitedly reproduced the problem on multiple systems, with just empty procedure call, no other code involved. I must admit that I made a mistake after discovering th problem we have switched all calls to "stdcall" not "cdecl", and it all works fine.

With best regards,
Boian Mitov

-------------------------------------------------------------------- Mitov Software
http://www.mitov.com
--------------------------------------------------------------------

Earlier message 29 lines
----- Original Message ----- From: "JoshyFun" To: "General mailing list" Sent: Monday, June 23, 2008 7:53 AM Subject: Re: [Lazarus] Anyone using LCLIntf critical section routines? > Hello Boian, > > Monday, June 23, 2008, 9:52:51 AM, you wrote: > > I'm using critical sections, in delphi 7, in the callback of a > sample-grabber directshow object which is being called up to 175000 > times per second (usually around 4000 per second) and the application > was running up to 7 days without any problem. > > In the other hand I can not see any technical reason to have problems > with calling convention unless an ASM block is being used somewhere > after the call and stack is being cleared as cdecl instead the current > calling convention :-? > > -- > Best regards, > JoshyFun > > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Boian Mitov
to Boian Mitov

I mean in my previous e-mails I intended to say "pascal"->"stdcall" not "pascal"->"cdecl" .

With best regards,
Boian Mitov

-------------------------------------------------------------------- Mitov Software
http://www.mitov.com
--------------------------------------------------------------------

Earlier message 18 lines
----- Original Message ----- From: "Boian Mitov" To: "JoshyFun" ; "General mailing list" Sent: Monday, June 23, 2008 12:55 PM Subject: Re: [Lazarus] Anyone using LCLIntf critical section routines? > admit that I made a mistake after discovering th problem we have switched > all calls to "stdcall" not "cdecl", and it all works fine. > > With best regards, > Boian Mitov > > -------------------------------------------------------------------- > Mitov Software > http://www.mitov.com > --------------------------------------------------------------------

Reply by email