01: /*
02: * @(#)SynchronizedFunction.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 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.*;
12: import pnuts.lang.Package;
13:
14: /**
15: * This class is used in Pnuts script to execute a function within a synchronized block.
16: */
17: public class SynchronizedFunction extends PnutsFunction {
18: private PnutsFunction function;
19: private String name;
20: private Object lock;
21:
22: public SynchronizedFunction(PnutsFunction function, Object lock) {
23: this (function, lock, function.getName());
24: }
25:
26: public SynchronizedFunction(PnutsFunction function, Object lock,
27: String name) {
28: this .function = function;
29: this .lock = lock;
30: this .name = name;
31: }
32:
33: public boolean defined(int nargs) {
34: return function.defined(nargs);
35: }
36:
37: public String unparse(int nargs) {
38: return function.unparse(nargs);
39: }
40:
41: public Package getPackage() {
42: return function.getPackage();
43: }
44:
45: public String[] getImportEnv(int narg) {
46: return function.getImportEnv(narg);
47: }
48:
49: public boolean isBuiltin() {
50: return function.isBuiltin();
51: }
52:
53: public Object accept(int narg, Visitor visitor, Context context) {
54: return function.accept(narg, visitor, context);
55: }
56:
57: public String getName() {
58: return name;
59: }
60:
61: public String toString() {
62: return "synchronized " + function.toString();
63: }
64:
65: protected void added(int narg) {
66: throw new RuntimeException("unsupported operation");
67: }
68:
69: protected Object exec(Object[] args, Context context) {
70: synchronized (lock) {
71: return PnutsFunction.exec(function, args, context);
72: }
73: }
74: }
|