[chuck-users] Shred Utilities: waitWithTimeout()

Robert Poor rdpoor at gmail.com
Mon Mar 16 11:56:55 EDT 2009


Here's a small module I wrote mostly out of necessity and feel it's my  
first bit of ChucK code worth submitting to a library repository.  I  
checked out lick (http://github.com/heuermh/lick/), but noticed that  
all code there was written by heuermh and felt that maybe this  
wouldn't fit into lick's Grand Scheme.  So I'm not sure where to send  
it except to this list.  Comments, suggestions are welcome (but go  
easy on me -- I'm new to ChucK!).   Thanks.

file: ShredUtilities.ck

/* ================================================================
    Shred Utilities for the ChucK programming language

    Copyright (c) 2009 Robert Poor.  All rights reserved.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/ 
 >.
    ================================================================ */

// ================================================================
// File: ShredUtilities.ck
//
// Description: ShredUtilities extend the basic shred (thread) mechanism
// with waitWithTimeout() methods.
//	
// Author: Robert Poor <r at alum.mit.edu>
// Date: March 2009
// ================================================================

public class ShredUtilities {

     // waitWithTimeout(Event e, time t) - Block execution of the
     // caller's thread until event e receives a signal or time t
     // arrives, whichever comes first.
     //
     // Implementation: spawn two threads, one to wake us when event e
     // receives a signal, the other to wake us when the appointed time
     // arrives.  Pass both threads a common event 'union' on which to
     // notify this thread, then block on union.
     //
     fun static void waitWithTimeout(Event e, time t) {
	Event union;
	spork ~ _waitForEvent(e, union) @=> Shred @ eventWaiter;
	spork ~ _waitForTime(t, union) @=> Shred @ timeWaiter;
	union => now;
	eventWaiter.exit();
	timeWaiter.exit();
     }

     // waitWithTimeout(Event e, dur d) - Block execution of the
     // caller's thread until event e receives a signal or d time
     // has elapsed, whichever comes first.
     //
     fun static void waitWithTimeout(Event e, dur d) {
	waitWithTimeout(e, now+d);
     }

     // ================================================================
     // internal helper functions

     // wait for an event on e, unblock event union.
     fun static void _waitForEvent(Event e, Event union) {
	e => now;
	union.broadcast();
     }

     // wait for time t, unblock event union.
     fun static void _waitForTime(time t, Event union) {
	t => now;
	union.broadcast();
     }

}


More information about the chuck-users mailing list