From: Paul on

Hi,

I'm looking to write a C Tcl extension, to use with my application,
which has has the following behaviour:

proc aProc {} {
set i 1
puts "i: $i"
yield
incr i
puts "i: $i"
}

proc bProc {} {
set j 5
puts "j: $j"
yield
incr j
puts "j: $j"
}

schedule aProc
schedule bProc
execute

The schedule, execute and yield commands will be implemented in C.

The schedule commands will schedule two procedures for execution, and
then the execute command will execute the scheduled procedures. The
yield command results in the procedure that is executing to swap, so
the output of the program would be:

i: 1
j: 5
i: 2
j: 6

So in my execute command C code, I will need to be able to run some
commands contained in one procedure, and then swap and run commands in
another procedure.

If the above is possible, which parts of the C API do I need to look
at?

Thanks,

Paul
From: Alexandre Ferrieux on
On Feb 5, 4:40 pm, Paul <pault...(a)googlemail.com> wrote:
> Hi,
>
> I'm looking to write a C Tcl extension, to use with my application,
> which has has the following behaviour:
>
> proc aProc {} {
>     set i 1
>     puts "i: $i"
>     yield
>     incr i
>     puts "i: $i"
>
> }
>
> proc bProc {} {
>     set j 5
>     puts "j: $j"
>     yield
>     incr j
>     puts "j: $j"
>
> }
>
> schedule aProc
> schedule bProc
> execute
>
> The schedule, execute and yield commands will be implemented in C.
>
> The schedule commands will schedule two procedures for execution, and
> then the execute command will execute the scheduled procedures. The
> yield command results in the procedure that is executing to swap, so
> the output of the program would be:
>
> i: 1
> j: 5
> i: 2
> j: 6
>
> So in my execute command C code, I will need to be able to run some
> commands contained in one procedure, and then swap and run commands in
> another procedure.
>
> If the above is possible, which parts of the C API do I need to look
> at?

Are you aware of the Non Recursive revolution that Miguel wrought for
8.6 ? Do the [coroutine]/[yield] keywords ring a bell ?

-Alex

From: Paul on
On Feb 5, 3:59 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
wrote:
> On Feb 5, 4:40 pm, Paul <pault...(a)googlemail.com> wrote:
>
>
>
> > Hi,
>
> > I'm looking to write a C Tcl extension, to use with my application,
> > which has has the following behaviour:
>
> > proc aProc {} {
> >     set i 1
> >     puts "i: $i"
> >     yield
> >     incr i
> >     puts "i: $i"
>
> > }
>
> > proc bProc {} {
> >     set j 5
> >     puts "j: $j"
> >     yield
> >     incr j
> >     puts "j: $j"
>
> > }
>
> > schedule aProc
> > schedule bProc
> > execute
>
> > The schedule, execute and yield commands will be implemented in C.
>
> > The schedule commands will schedule two procedures for execution, and
> > then the execute command will execute the scheduled procedures. The
> > yield command results in the procedure that is executing to swap, so
> > the output of the program would be:
>
> > i: 1
> > j: 5
> > i: 2
> > j: 6
>
> > So in my execute command C code, I will need to be able to run some
> > commands contained in one procedure, and then swap and run commands in
> > another procedure.
>
> > If the above is possible, which parts of the C API do I need to look
> > at?
>
> Are you aware of the Non Recursive revolution that Miguel wrought for
> 8.6 ? Do the [coroutine]/[yield] keywords ring a bell ?
>
> -Alex

I wasn't aware of coroutine/yield in Tcl 8.6. Although it doesn't look
as if that will do exactly what I want. If I can get the simple
example above working, then I'll go on to change it a bit for my final
application, which is controlling a simulator from C code.

In the final simulator application, it won't be a call to a yield
command that swaps the procedure executing, the swap will occur due to
the state of the simulator. But I want to start off simple, hence the
example above.

The crucial point is that I want to be able to run commands contained
in a Tcl procedure from my C code. Then be able to stop, and run
commands in a different Tcl procedure. Then later on carry on
executing commands in the first procedure. I'm hoping that the Tcl C
API will support this. Conceptually it sounds simple, but in reality
it might be more difficult.


From: Alexandre Ferrieux on
On Feb 5, 6:02 pm, Paul <pault...(a)googlemail.com> wrote:
> On Feb 5, 3:59 pm, Alexandre Ferrieux <alexandre.ferri...(a)gmail.com>
> wrote:
>
>
>
>
>
> > On Feb 5, 4:40 pm, Paul <pault...(a)googlemail.com> wrote:
>
> > > Hi,
>
> > > I'm looking to write a C Tcl extension, to use with my application,
> > > which has has the following behaviour:
>
> > > proc aProc {} {
> > >     set i 1
> > >     puts "i: $i"
> > >     yield
> > >     incr i
> > >     puts "i: $i"
>
> > > }
>
> > > proc bProc {} {
> > >     set j 5
> > >     puts "j: $j"
> > >     yield
> > >     incr j
> > >     puts "j: $j"
>
> > > }
>
> > > schedule aProc
> > > schedule bProc
> > > execute
>
> > > The schedule, execute and yield commands will be implemented in C.
>
> > > The schedule commands will schedule two procedures for execution, and
> > > then the execute command will execute the scheduled procedures. The
> > > yield command results in the procedure that is executing to swap, so
> > > the output of the program would be:
>
> > > i: 1
> > > j: 5
> > > i: 2
> > > j: 6
>
> > > So in my execute command C code, I will need to be able to run some
> > > commands contained in one procedure, and then swap and run commands in
> > > another procedure.
>
> > > If the above is possible, which parts of the C API do I need to look
> > > at?
>
> > Are you aware of the Non Recursive revolution that Miguel wrought for
> > 8.6 ? Do the [coroutine]/[yield] keywords ring a bell ?
>
> > -Alex
>
> I wasn't aware of coroutine/yield in Tcl 8.6. Although it doesn't look
> as if that will do exactly what I want. If I can get the simple
> example above working, then I'll go on to change it a bit for my final
> application, which is controlling a simulator from C code.
>
> In the final simulator application, it won't be a call to a yield
> command that swaps the procedure executing, the swap will occur due to
> the state of the simulator. But I want to start off simple, hence the
> example above.
>
> The crucial point is that I want to be able to run commands contained
> in a Tcl procedure from my C code. Then be able to stop, and run
> commands in a different Tcl procedure. Then later on carry on
> executing commands in the first procedure. I'm hoping that the Tcl C
> API will support this. Conceptually it sounds simple, but in reality
> it might be more difficult.

Coroutines in 8.6 do exactly that. Please explain why you feel you
need to reinvent that big wheel.

-Alex
From: Paul on

> Coroutines in 8.6 do exactly that. Please explain why you feel you
> need to reinvent that big wheel.
>
> -Alex

OK Thanks! I'll take a closer look.