Re: [fpc-pascal] Portable coroutines

2015-03-22 Thread rpzrpz...@gmail.com

Maybe look at the coroutine implementation in Go (Golang).

Maybe a better refrence.  Maybe an optional switch in compiler so
you are not required to have all CPU architectures working on day 1.

Intel and ARM seem nice...

md

On 3/21/2015 4:29 PM, Marco van de Voort wrote:

In our previous episode, Mark Morgan Lloyd said:

Efficient implementation of coroutines requires CPU-specific code in the
RTL and possibly the compiler. However
http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html suggests a
way that coroutines can be implemented in a portable fashion in C, how
can this be done in Object Pascal?


Seems more an oddity than a system, but it relies heavily on preprocessor
and fallthrough case.

The preprocessor part can be done, just use whatever preprocessor
(maybe even cpp) and then haul the resulting code through fpc.

But there is no fallthrough case (and personally I think that is a good
thing)
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



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


Re: [fpc-pascal] File Descriptor in Windows ?

2015-03-22 Thread Michael Van Canneyt



On Sat, 21 Mar 2015, fredvs wrote:


What do you do with the inHandle ?
Michael.


It is used by mp123 mp3-decoder library.

=>function mpg123_open_fd(mph: Tmpg123_handle; fd: integer);

fp (file descriptor) := InHandle ;

It seems that InHandle as file descriptor does not work on Windows.
But in *nix system, it works.


It should work, because TProcess relies on this function to do it's job.
However, pipes work differently from normal file descriptors.

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


Re: [fpc-pascal] Portable coroutines

2015-03-22 Thread Mark Morgan Lloyd

rpzrpz...@gmail.com wrote:

Maybe look at the coroutine implementation in Go (Golang).

Maybe a better refrence.  Maybe an optional switch in compiler so
you are not required to have all CPU architectures working on day 1.

Intel and ARM seem nice...


The issue isn't the syntax, it's that proper implementation of 
coroutines means switching stacks which is highly architecture-specific; 
adding something like that to the RTL is a non-starter.


--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] File Descriptor in Windows ?

2015-03-22 Thread fredvs
@ Andrew

> Check for writeln's in the htmlthread unit you added.

Hum, i did not add htmlthread unit (only fphttpclient and pipes).
What do you mean ?  

@ Martin
> It should work, because TProcess relies on this function to do it's job.

Yes, maybe, but it does not work ;-(
Could it be a problem of conversion into file descriptor ?
It seems that fle descriptor are posix and unix only...
Microsoft added support to  for open/write, but renamed the (non-C-standard)
functions to _open/_write.

Aaargh, Microsoft, you are so lonely

Fre;D




-
Many thanks ;-)
--
View this message in context: 
http://free-pascal-general.1045716.n5.nabble.com/File-Descriptor-in-Windows-tp5721448p5721464.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Portable coroutines

2015-03-22 Thread Mark Morgan Lloyd

Marco van de Voort wrote:

In our previous episode, Mark Morgan Lloyd said:
Efficient implementation of coroutines requires CPU-specific code in the 
RTL and possibly the compiler. However 
http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html suggests a 
way that coroutines can be implemented in a portable fashion in C, how 
can this be done in Object Pascal?


Seems more an oddity than a system, but it relies heavily on preprocessor
and fallthrough case.

The preprocessor part can be done, just use whatever preprocessor
(maybe even cpp) and then haul the resulting code through fpc.

But there is no fallthrough case (and personally I think that is a good
thing)


Generally agreed. I'm not particularly bothered about the syntax, 
although obviously having it fairly compact would be an advantage. I 
don't think it's possible to use a case/goto arrangement since this 
wouldn't be happy jumping into repeat or while loops, but it does look 
as though it's possible to use LongJmp() (insomnia has its advantages):



program coroutines;

uses SysUtils;

typeTState= record
   env: jmp_buf;
   line: cardinal
end;

var state: TState;
once: boolean= false;
scratch: char;


  function getchar(): char;

  var   sanity: jmp_buf;

  begin

// Check that we're not trying to jump into exception blocks etc.

if state.line <> 0 then begin
  SetJmp(sanity);
  Assert(PtrUInt(state.env.sp) + PtrUInt(state.env.bp) =
PtrUInt(sanity.sp) + PtrUInt(sanity.bp),
'Bad SP or BP at xfer to line ' + IntToStr(state.line));
  LongJmp(state.env, 1)
end;

SetJmp(state.env);
if state.line = 0 then begin
  state.line := StrToInt( (*$I %LINE% *) );
  exit('A')
end else
  state.line := 0;

..

repeat
  SetJmp(state.env);
  if state.line = 0 then begin
state.line := StrToInt( (*$I %LINE% *) );
exit(#$ff)
  end else
state.line := 0;
  once := true
until once
  end { getChar } ;


begin
  FillByte(state, SizeOf(state), 0);
  scratch := getChar();
  while scratch <> #$ff do begin
WriteLn(scratch);
scratch := getChar()
  end
end.


The sanity check at the start is obviously processor-specific, but it 
doesn't use anything hidden and can at a pinch be omitted. It's 
obviously excessively verbose, but it is comparatively regular and does 
appear to handle at least some loop types properly.


--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] File Descriptor in Windows ?

2015-03-22 Thread fredvs
> Check for writeln's in the htmlthread unit you added.

Oops, i think i get it (sorry it is Sunday). ;-(
 
- CreatePipeHandles (InHandle,OutHandle, PipeBufferSize);
- Writeln('Input Handle = ' + inttostr(InHandle) + ' Output Handle = ' +
inttostr(OutHandle));
=> Linux => Input Handle = 3 Output Handle = 4
=> Windows => Input Handle = 212 Output Handle = 216

- Output:=TOutputPipeStream.Create (OutHandle);
- if Output = nil then  writeln('===> NO Pipe Stream created.') else
- writeln('===> Output Pipe Stream created.');
=> Linux + Windows => ===> Output Pipe Stream created.

-  err := mpg123_open_fd(MyMPHandle, InHandle);
- if err = 0 then writeln('===> mpg123_open_fd => ok.') else
   writeln('===> mpg123_open_fd NOT ok.') ; 
=> Linux + Windows => mpg123_open_fd => ok. 

- err := mpg123_read(MyMPHandle, blabla);
- writeln('===> mpg123_read error => ' + inttostr(err)) ;
=> Linux => mpg123_read error => 0 => no error
=> Windows => mpg123_read error => 12 => Invalid RVA mode

Ooops, what is RVA mode?...

Thanks.

Fred.







-
Many thanks ;-)
--
View this message in context: 
http://free-pascal-general.1045716.n5.nabble.com/File-Descriptor-in-Windows-tp5721448p5721466.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] File Descriptor in Windows ?

2015-03-22 Thread fredvs
Yep, there is a answer from mpg123 creator in forum list ;-) =>

>> In Linux, no problem, i use the Input-Handle of a pipe as file
>> descriptor.
>> => mpg123_open_fd(MyMP123Handle, InputHandle)
>> But if using Input-Handle of a pipe in Windows, at mpg123_read() i get
>> that error:
>> MPG123_BAD_RVA
>> 
>> What does it mean ?

> Are you mixing C runtimes? A libmpg123 DLL from MinGW and your code built
> with Visual C?
> I reckon that those file descriptors may not be compatible. I don't have a
> Windows system running myself.

Ok, conclusion of the story => fpc is perfect and not the guilty  ;-)

Now, i have to make that descriptors compatible. ;-(

Many thanks.

Fre;D





-
Many thanks ;-)
--
View this message in context: 
http://free-pascal-general.1045716.n5.nabble.com/File-Descriptor-in-Windows-tp5721448p5721467.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal