9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Mathieu Lonjaret" <mathieu.lonjaret@gmail.com>
To: 9fans@9fans.net
Subject: [9fans] alt and chanfree
Date: Tue, 14 Sep 2010 10:57:45 +0200	[thread overview]
Message-ID: <1daea15b89e55261c46164029eac58c4@iram.fr> (raw)

Hi all,

I'm doing something like the following to synchronize a bunch of
coroutines.
Everything goes fine while the different threads are working (and are
regularly sending over their respective channel). However, as soon as
one terminates and calls chanfree(), the next call to alt() fails (as
in I get a "pagefault" message). I can tell because if I never call
chanfree() I never hit that error.
Now in this simplified version I haven't shown that I in fact actually
dynamically reorganize the Alt * (adding/removing an Alt everytime a
caller starts/terminates) so it's totally possible I messed up there.

Before I dig deeper to find out what I'm doing wrong in my Alt *,
I wanted to make sure that I'm globally doing the right thing, i.e. if
it's ok and even recommended to free the Channel as soon as the thread
that was sending on it does not need it anymore.
Can anyone please confirm that?

Thanks,
Mathieu

static void
caller(void *arg)
{
	struct Params{ Torrent *tor; Peer *peer; Channel *c;} *params;
	uchar chanmsg[1];

	params = arg;
	// in callerworks() we send() on params->c at various points to synchronize, all goes well.
	callerworks(params->tor, params->peer, params->c);
	chanmsg[0] = 0;
	send(params->c, chanmsg);
	// peer is done working now
	chanfree(params->c);
}

void
callers(void *arg)
{
	Torrent *tor = arg;
	Peer *peer;
	uchar m[1];
	int i;
	int n;
	Alt *a;
	struct Params{Torrent *tor; Peer *peer; Channel *c;} *params;

	a = emalloc((MAXPEERS+1)*sizeof(Alt));
	for (i = 0; i<MAXPEERS; i++){
		a[i].v = m;
		a[i].c = chancreate(sizeof(m), 0);
		a[i].op = CHANRCV;

		// prepare params for the thread
		peer = emalloc(sizeof(Peer));
		params = emalloc(sizeof(struct Params));
		params->tor = tor;
		params->peer = peer;
		params->c = a[i].c;
		threadcreate(caller, params, STACK);
	}
	a[MAXPEERS].v = nil;
	a[MAXPEERS].c = nil;
	a[MAXPEERS].op = CHANEND;

	for(;;){
		n = alt(a);
		if(n < 0)
			error("with alt");

		if (m[0] == 0){
			// a caller has terminated
			dosomestuff()
		}
	}
}




             reply	other threads:[~2010-09-14  8:57 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-14  8:57 Mathieu Lonjaret [this message]
2010-09-14  9:55 ` roger peppe
2010-09-14 10:04   ` Mathieu Lonjaret
2010-09-14 13:01     ` erik quanstrom
2010-09-14 13:27       ` Mathieu Lonjaret
2010-09-14 13:30         ` erik quanstrom
2010-09-14 13:46           ` Mathieu Lonjaret
2010-09-14 13:49         ` Gorka Guardiola
2010-09-14 14:17           ` erik quanstrom
2010-09-15  8:15             ` Mathieu Lonjaret
2010-09-15  9:47               ` roger peppe
2010-09-15 11:07                 ` Mathieu Lonjaret

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=1daea15b89e55261c46164029eac58c4@iram.fr \
    --to=mathieu.lonjaret@gmail.com \
    --cc=9fans@9fans.net \
    /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).