01: /*
02: * @(#)fork.java 1.1 05/04/04
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.multithread;
10:
11: import pnuts.lang.Context;
12: import pnuts.lang.PnutsFunction;
13:
14: public class fork extends createThread {
15:
16: public fork() {
17: super ("fork");
18: }
19:
20: public boolean defined(int nargs) {
21: return nargs >= 1 && nargs <= 3;
22: }
23:
24: static Thread fork(PnutsFunction f, int prio, boolean daemon,
25: Context context) {
26: Thread th = createThread(f, prio, daemon, context);
27: th.start();
28: return th;
29: }
30:
31: protected Object exec(Object[] args, Context context) {
32: int nargs = args.length;
33: if (nargs == 1) {
34: PnutsFunction f = (PnutsFunction) args[0];
35: return fork(f, Thread.NORM_PRIORITY, false, context);
36: } else if (nargs == 2) {
37: PnutsFunction f = (PnutsFunction) args[0];
38: int prio = ((Number) args[1]).intValue();
39: return fork(f, prio, false, context);
40: } else if (nargs == 3) {
41: PnutsFunction f = (PnutsFunction) args[0];
42: int prio = ((Number) args[1]).intValue();
43: boolean daemon = ((Boolean) args[2]).booleanValue();
44: return fork(f, prio, daemon, context);
45: } else {
46: undefined(args, context);
47: return null;
48: }
49: }
50:
51: public String toString() {
52: return "function fork(func {, prio {, daemon }})";
53: }
54: }
|