From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <1daea15b89e55261c46164029eac58c4@iram.fr> To: 9fans@9fans.net From: "Mathieu Lonjaret" Date: Tue, 14 Sep 2010 10:57:45 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Subject: [9fans] alt and chanfree Topicbox-Message-UUID: 56d04186-ead6-11e9-9d60-3106f5b1d025 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; itor = 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() } } }