9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Philippe Anel <philippe.anel@noos.fr>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] libthread help ...
Date: Wed,  1 Jan 2003 14:45:44 +0100	[thread overview]
Message-ID: <5.2.0.9.0.20030101141705.00a69a70@pop.noos.fr> (raw)
In-Reply-To: <139f376dbb00f86c77be49c96b3abfc0@plan9.bell-labs.com>

At 23:35 31/12/02 -0500, you wrote:
>if you run acid -l thread, it loads some
>useful functions.

Is there any doc about these functions/libs ?

>the problem is that the menuhitproc and main are
>both reading from the mouse.  main got a new mouse
>event to send to menuhitproc, and menuhitproc is
>trying to send its result to main at the same time.
>perhaps you should wait for the menuhitproc to
>send its result before waiting for more mouse ops.

With some 'threadsetname()' and the acid function
threads(), I saw the problem. Thanks.

>you could change a[0].op to CHANNOP after sending
>to menuhitproc and then change it back after getting
>the result.

I've just made this fix and now it works.


>i don't see why it's a separate proc.  you might want
>main not to stop processing clock ticks while you
>wait for the menu event, i guess, but even then you
>could do that with a thread rather than a proc.

I didn't realize that my thread/proc 'menuhit' could
yield the the processor waiting the mouseproc channel.
Now, it is a thread.

         Thank you,
         Philippe.

---------------------------------------------th_exmpl.c----

#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <mouse.h>
#include <frame.h>

enum
{
         STACKSIZE = 2048,
};

static void
clockproc(void *arg)
{
         int t;
         Channel *c;

         threadsetname("clockproc");
         c = arg;
         for(t=0;; t++){
                 sleep(1000);
                 sendul(c, t);
         }
}

struct mh {
         Mousectl *mctl;
         Channel *cmd;
         Channel *res;
};

static void
menuhitthread(void *arg)
{
         struct mh *marg;
         int     t;
         char *items[] = {
                 "exit",
                 nil
         };
         Menu menu = {
                 items,
                 nil,
                 0
         };

         threadsetname("menuhitthread");
         marg = arg;
         for (;;) {
                 t = recvul(marg->cmd);
                 t = menuhit(t, marg->mctl, &menu, nil);
                 sendul(marg->res, t);
         }
}

void
threadmain(int argc, char *argv[])
{
         int tick, item;
         Mousectl *mctl;
         Alt a[] = {
         //       c              v               op
                 {nil,   nil,    CHANRCV},                       // 0:   mouse
                 {nil,   nil,    CHANRCV},                       // 1:   resize
                 {nil,   &tick,  CHANRCV},                       // 2:   clock
                 {nil,   &item,  CHANRCV},                       // 
3:   menuhit
                 {nil,   nil,    CHANEND},
         };
         struct mh marg;

         USED(argc);
         USED(argv);

         initdraw(0, 0, "frame");

         // create mouse event channel and process
         mctl = initmouse(0, screen);
         a[0].c = mctl->c;
         a[1].c = mctl->resizec;

         // create clock event channel and process
         a[2].c = chancreate(sizeof(ulong), 0);
         proccreate(clockproc, a[2].c, STACKSIZE);

         // create menuhit channel and process
         a[3].c = chancreate(sizeof(ulong), 0);
         marg.mctl = mctl;
         marg.cmd = chancreate(sizeof(ulong), 0);
         marg.res = a[3].c;
         threadcreate(menuhitthread, &marg, STACKSIZE);

         for(;;){
                 switch(alt(a)){
                 case 0: // mouse event
                         if (mctl->buttons & 2) {
                                 a[0].op = CHANNOP;
                                 sendul(marg.cmd, 2);
                         }
                         if (mctl->buttons & 1)
                                 fprint(2, "button1 ");
                         if (mctl->buttons & 4)
                                 fprint(2, "button4 ");
                         break;
                 case 1: // resize event
                         fprint(2, "resize ");
                         break;
                 case 2: // clock event
                         fprint(2, "tic%d ", tick);
                         break;
                 case 3: // menu event
                         a[0].op = CHANRCV;
                         fprint(2, "menu%d ", item);
                         if (item == 0)
                                 threadexitsall(nil);
                         break;
                 default:
                         sysfatal("can't happen");
                 }
         }
}
---------------------------------------------------------------- 



  reply	other threads:[~2003-01-01 13:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-31 20:48 Philippe Anel
2003-01-01  4:35 ` Russ Cox
2003-01-01 13:45   ` Philippe Anel [this message]
2003-01-01 17:00     ` Russ Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5.2.0.9.0.20030101141705.00a69a70@pop.noos.fr \
    --to=philippe.anel@noos.fr \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).