bug-kawa
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-kawa] [bug #14098] runnable rfe


From: tk
Subject: [Bug-kawa] [bug #14098] runnable rfe
Date: Thu, 11 Aug 2005 21:19:46 +0000
User-agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412.6 (KHTML, like Gecko) Safari/412.2

URL:
  <http://savannah.gnu.org/bugs/?func=detailitem&item_id=14098>

                 Summary: runnable rfe
                 Project: Kawa
            Submitted by: huh
            Submitted on: Thu 08/11/05 at 21:19
                Category: Scheme library
                Severity: 3 - Normal
              Item Group: Feature Request
                  Status: None
                 Privacy: Public
             Assigned to: None
             Open/Closed: Open

    _______________________________________________________

Details:


In response to savannah bug #14072, following is a strawman implementation
of an api for creating a java.lang.Runnable from a procedure and its
environment. The implementation is based on Future.

Example usage that illustrates need for such an api:

    (let ((graphics (invoke canvas 'getGraphics)))
      (invoke-static <java.awt.EventQueue> 'invokeLater
        (runnable
          (lambda ()
            (invoke graphics 'drawRect 0 0 1 1)))))
          

kawa/lib/runnable.scm:

(define (runnable (proc :: <procedure>)) :: <gnu.mapping.RunnableClosure>
  (make <gnu.mapping.RunnableClosure> proc))



gnu/mapping/RunnableClosure.java:

package gnu.mapping;

public class RunnableClosure implements Runnable
{
  Object result;
  CallContext context;
  public Environment environment;
  Throwable exception;
  Procedure action;
  String name;
  static int nrunnables=0;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name=name;
  }

  public RunnableClosure (Procedure action, CallContext parentContext)
  {
    this(action, parentContext, parentContext.getEnvironment()); 
  }

  public RunnableClosure (Procedure action,
                            CallContext parentContext, Environment
penvironment)
  {
    setName("r"+nrunnables++);
    this.action = action;
    SimpleEnvironment env = Environment.make(getName(), penvironment); 
    env.flags |= Environment.THREAD_SAFE;
    env.flags &= ~Environment.DIRECT_INHERITED_ON_SET;
    env.setIndirectDefines();   // note -- not in Future
    this.environment = env;
    int n = parentContext.pushedFluidsCount;
    for (int i = 0;  i < n; i++)
      {
        // If we're inside a fluid-let, then the child thread should inherit
        // the fluid-let binding, even if it isn't accessed in the child
until
        // after the parent exits the fluid-let.  Set things up so only the
        // binding in the parent is restored, but they share utnil then.
        Location loc = parentContext.pushedFluids[i];
        Symbol name = loc.getKeySymbol();
        Object property = loc.getKeyProperty();
        if (name != null && loc instanceof NamedLocation)
          {
            NamedLocation nloc = (NamedLocation) loc;
            if (nloc.base == null)
              {
                SharedLocation sloc = new SharedLocation(name, property, 0);
                sloc.value = nloc.value;
                nloc.base = sloc;
                nloc.value = null;
                nloc = sloc;
              }
            int hash = name.hashCode() ^ System.identityHashCode(property);
            NamedLocation xloc = env.addUnboundLocation(name, property,
hash);
            xloc.base = nloc;
          }
      }
  }

  public RunnableClosure (Procedure action)
  {
    this(action, CallContext.getInstance());
  }

  public RunnableClosure (Procedure action, Environment penvironment)
  {
    this(action, CallContext.getInstance(), penvironment);
  }

  /** Get the CallContext we use for this Thread. */
  public final CallContext getCallContext() { return context; }

  public void run ()
  {
    try
      {
        if (context == null)
          context = CallContext.getInstance();
        else
          CallContext.setInstance(context);
        context.curEnvironment = environment;
        result = action.apply0 ();
      }
    catch (Throwable ex)
      {
        exception = ex;
      }
  }

  public String toString() {
    StringBuffer buf = new StringBuffer();
    buf.append ("#<runnable ");
    buf.append(getName());
    buf.append(">");
    return buf.toString();
  }
}




    _______________________________________________________

Carbon-Copy List:

CC Address                          | Comment
------------------------------------+-----------------------------
address@hidden                 | 




    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?func=detailitem&item_id=14098>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]