Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #16394 > unrolled thread

[I'm learning C]: Learning to use ucontext

Started byAndrey Popp <8mayday@gmail.com>
First post2012-01-29 08:06 -0800
Last post2012-01-30 17:05 -0800
Articles 6 — 6 participants

Back to article view | Back to comp.lang.c


Contents

  [I'm learning C]: Learning to use ucontext Andrey Popp <8mayday@gmail.com> - 2012-01-29 08:06 -0800
    Re: [I'm learning C]: Learning to use ucontext Shao Miller <sha0.miller@gmail.com> - 2012-01-29 11:42 -0500
    Re: [I'm learning C]: Learning to use ucontext Barry Schwarz <schwarzb@dqel.com> - 2012-01-29 10:07 -0800
    Re: [I'm learning C]: Learning to use ucontext Jens Gustedt <jens.gustedt@loria.fr> - 2012-01-30 20:38 +0100
      Re: [I'm learning C]: Learning to use ucontext Uncle Steve <stevet810@gmail.com> - 2012-01-30 18:58 -0500
        Re: [I'm learning C]: Learning to use ucontext Keith Thompson <kst-u@mib.org> - 2012-01-30 17:05 -0800

#16394 — [I'm learning C]: Learning to use ucontext

FromAndrey Popp <8mayday@gmail.com>
Date2012-01-29 08:06 -0800
Subject[I'm learning C]: Learning to use ucontext
Message-ID<6113912.484.1327853172515.JavaMail.geo-discussion-forums@yqfa18>
Hello, I started to experimenting with ucontext to implement basic cooperative multithreading and stuccoed with this piece of code:

#include <stdlib.h>
#include <stdio.h>
#include <ucontext.h>

static ucontext_t routine_main;
static ucontext_t routine1;
static ucontext_t routine2;

void runner(int n) {
  fprintf(stderr, "runner %d\n", n);
  while(1) {
    fprintf(stderr, "runner %d\n", n);
    if (n == 1)
      swapcontext(&routine1, &routine2);
    else
      swapcontext(&routine2, &routine1);
  }
}

int main(int argc, char **argv) {
  char st1c[8192];
  char st2c[8192];

  if (getcontext(&routine1) == -1)
    abort();
  routine1.uc_link = &routine_main;
  routine1.uc_stack.ss_sp = st1c;
  routine1.uc_stack.ss_size = sizeof st1c;
  makecontext(&routine1, (void(*)(void))runner, 1, 1);

  if (getcontext(&routine2) == -1)
    abort();
  routine2.uc_link = &routine_main;
  routine2.uc_stack.ss_sp = st2c;
  routine2.uc_stack.ss_size = sizeof st2c;
  makecontext(&routine1, (void(*)(void))runner, 1, 2);

  fprintf(stderr, "start executing\n");
  if (swapcontext(&routine_main, &routine1) == -1)
    abort();
  fprintf(stderr, "exiting\n");
  return 0;
}

which doesn't work the way it should — it just constantly prints "start executing", which means it doesn't change execution context from routeine_main to routine1 but instead from routine_main to routine_main.

[toc] | [next] | [standalone]


#16396

FromShao Miller <sha0.miller@gmail.com>
Date2012-01-29 11:42 -0500
Message-ID<jg3spl$b9g$1@dont-email.me>
In reply to#16394
On 1/29/2012 11:06, Andrey Popp wrote:
> Hello, I started to experimenting with ucontext to implement basic cooperative multithreading and stuccoed with this piece of code:
>

As far as I know, 'ucontext.h' isn't standard C.  I think that's worth 
noting.

> #include<stdlib.h>
> #include<stdio.h>
> #include<ucontext.h>
>
> static ucontext_t routine_main;
> static ucontext_t routine1;
> static ucontext_t routine2;
>
> void runner(int n) {
>    fprintf(stderr, "runner %d\n", n);
>    while(1) {
>      fprintf(stderr, "runner %d\n", n);
>      if (n == 1)
>        swapcontext(&routine1,&routine2);
>      else
>        swapcontext(&routine2,&routine1);
>    }
> }
>
> int main(int argc, char **argv) {
>    char st1c[8192];
>    char st2c[8192];
>
>    if (getcontext(&routine1) == -1)
>      abort();
>    routine1.uc_link =&routine_main;
>    routine1.uc_stack.ss_sp = st1c;
>    routine1.uc_stack.ss_size = sizeof st1c;
>    makecontext(&routine1, (void(*)(void))runner, 1, 1);
>
>    if (getcontext(&routine2) == -1)
>      abort();
>    routine2.uc_link =&routine_main;
>    routine2.uc_stack.ss_sp = st2c;
>    routine2.uc_stack.ss_size = sizeof st2c;
>    makecontext(&routine1, (void(*)(void))runner, 1, 2);
>

Does the line immediately above include a typo?  Did you mean to modify 
the 'routine1' context or the 'routine2' context there?

>    fprintf(stderr, "start executing\n");
>    if (swapcontext(&routine_main,&routine1) == -1)
>      abort();
>    fprintf(stderr, "exiting\n");
>    return 0;
> }
>
> which doesn't work the way it should — it just constantly prints "start executing", which means it doesn't change execution context from routeine_main to routine1 but instead from routine_main to routine_main.

That's unfortunate.

-- 
"The stationery store has moved.  Aaargh!"

[toc] | [prev] | [next] | [standalone]


#16402

FromBarry Schwarz <schwarzb@dqel.com>
Date2012-01-29 10:07 -0800
Message-ID<3d1bi7tc0fomngh5um92oimqoq915g2i2h@4ax.com>
In reply to#16394
On Sun, 29 Jan 2012 08:06:12 -0800 (PST), Andrey Popp
<8mayday@gmail.com> wrote:

>Hello, I started to experimenting with ucontext to implement basic cooperative multithreading and stuccoed with this piece of code:
>
>#include <stdlib.h>
>#include <stdio.h>
>#include <ucontext.h>
>
>static ucontext_t routine_main;

snip

>      swapcontext(&routine1, &routine2);

snip

>  if (getcontext(&routine1) == -1)
>    abort();
>  routine1.uc_link = &routine_main;
>  routine1.uc_stack.ss_sp = st1c;
>  routine1.uc_stack.ss_size = sizeof st1c;
>  makecontext(&routine1, (void(*)(void))runner, 1, 1);

snip

>which doesn't work the way it should — it just constantly prints "start executing", which means it doesn't change execution context from routeine_main to routine1 but instead from routine_main to routine_main.

If you gave us the contents of ucontext.h and the definitions of all
the non-standard functions you call, someone might be inclined to help
you debug this.  It might also help if you described what "change
execution context" means.  As it stands, a response other than "logic
error on line 42 of routine x" is just wishful thinking.

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#16454

FromJens Gustedt <jens.gustedt@loria.fr>
Date2012-01-30 20:38 +0100
Message-ID<4F26F1BF.1060405@loria.fr>
In reply to#16394
Hello,

Am 01/29/2012 05:06 PM, schrieb Andrey Popp:
> Hello, I started to experimenting with ucontext to implement basic
> cooperative multithreading and stuccoed with this piece of code:

This sounds like a bad idea to me. My man page tells me:

POSIX.1-2008  removes the specifications of makecontext() and
swapcontext(), citing portability issues, and recommending
       that applications be rewritten to use POSIX threads instead.

so such an implementation looks like a dead end to me.

Jens

[toc] | [prev] | [next] | [standalone]


#16468

FromUncle Steve <stevet810@gmail.com>
Date2012-01-30 18:58 -0500
Message-ID<3058d1a5951c007-6f59a@gmail.com>
In reply to#16454
On Mon, Jan 30, 2012 at 08:38:39PM +0100, Jens Gustedt wrote:
> Hello,
> 
> Am 01/29/2012 05:06 PM, schrieb Andrey Popp:
> > Hello, I started to experimenting with ucontext to implement basic
> > cooperative multithreading and stuccoed with this piece of code:
> 
> This sounds like a bad idea to me. My man page tells me:
> 
> POSIX.1-2008  removes the specifications of makecontext() and
> swapcontext(), citing portability issues, and recommending
>        that applications be rewritten to use POSIX threads instead.
> 
> so such an implementation looks like a dead end to me.
> 
> Jens

Posix threads suck.  You hafta fuck around like you wouldn't believe
in order to make something workable.

Fuck posix, even though I rely on it.  Shit.


Regards,

Uncle Steve

-- 
I don't even want the Charter back, as it was clearly 'used' by a big
blue dude when he ran out of toilet paper.  And the Criminal Code of
Canada is of course now officially listed in Jane's Miscellaneous
Munitions under the Anti-Personnel section.

[toc] | [prev] | [next] | [standalone]


#16470

FromKeith Thompson <kst-u@mib.org>
Date2012-01-30 17:05 -0800
Message-ID<lnpqe0y944.fsf@nuthaus.mib.org>
In reply to#16468
Uncle Steve <stevet810@gmail.com> writes:
[...]
> Posix threads suck.  You hafta f*** around like you wouldn't believe
> in order to make something workable.
>
> F*** posix, even though I rely on it.  S***.

*Plonk*

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web