01: // Copyright (c) Corporation for National Research Initiatives
02: package org.python.modules;
03:
04: import org.python.core.*;
05:
06: class SynchronizedCallable extends PyObject {
07: PyObject callable;
08:
09: public SynchronizedCallable(PyObject callable) {
10: this .callable = callable;
11: }
12:
13: public PyObject _doget(PyObject container) {
14: // TBD: third arg == null? Hmm...
15: return new PyMethod(container, this , null);
16: }
17:
18: public PyObject __call__() {
19: throw Py.TypeError("synchronized callable called with 0 args");
20: }
21:
22: public PyObject __call__(PyObject arg) {
23: synchronized (synchronize._getSync(arg)) {
24: return callable.__call__(arg);
25: }
26: }
27:
28: public PyObject __call__(PyObject arg1, PyObject arg2) {
29: synchronized (synchronize._getSync(arg1)) {
30: return callable.__call__(arg1, arg2);
31: }
32: }
33:
34: public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) {
35: synchronized (synchronize._getSync(arg1)) {
36: return callable.__call__(arg1, arg2, arg3);
37: }
38: }
39:
40: public PyObject __call__(PyObject[] args, String[] keywords) {
41: if (args.length == 0) {
42: throw Py
43: .TypeError("synchronized callable called with 0 args");
44: }
45: synchronized (synchronize._getSync(args[0])) {
46: return callable.__call__(args, keywords);
47: }
48: }
49:
50: public PyObject __call__(PyObject arg1, PyObject[] args,
51: String[] keywords) {
52: synchronized (synchronize._getSync(arg1)) {
53: return callable.__call__(arg1, args, keywords);
54: }
55: }
56:
57: }
58:
59: public class synchronize {
60: public static Object _getSync(PyObject obj) {
61: return Py.tojava(obj, Object.class);
62: }
63:
64: public static PyObject apply_synchronized(PyObject sync_object,
65: PyObject callable, PyObject args) {
66: synchronized (_getSync(sync_object)) {
67: return __builtin__.apply(callable, args);
68: }
69: }
70:
71: public static PyObject apply_synchronized(PyObject sync_object,
72: PyObject callable, PyObject args, PyDictionary kws) {
73: synchronized (_getSync(sync_object)) {
74: return __builtin__.apply(callable, args, kws);
75: }
76: }
77:
78: public static PyObject make_synchronized(PyObject callable) {
79: return new SynchronizedCallable(callable);
80: }
81: }
|