#Is there a way to invoke a method asynchronously inside an isolated method?

1 messages · Page 1 of 1 (latest)

mossy eagle
#

Check the following code;

isolated class Foo {
    private final Bar[] bars = [];

    isolated function add(Bar bar) {
        lock {
            self.bars.push(bar);
        }
    }

    isolated function read() {
        foreach Bar bar in self.bars {
            var s = start readExternal(bar);
        }
    }
}

isolated function readExternal(Bar bar) {

}

isolated class Bar {

}

I understand that the self.bars cannot be accessed without a lock statement. But we cannot invoke async calls inside a lock statement either. If we make the Bar class readonly, then we can use cloneReadOnly to get a clone of the bar array and then iterate through. But is there a way to achieve this without making the Bar class readonly (and without cloning the objects)?

copper swift
#

If the class is a readonly class, the object values will be immutable, and therefore only a new list will be created and the objects won't be cloned because clone and cloneReadOnly don't create a new value if the value is immutable.

One approach with the current definitions would be to do something like

isolated function read() {
    Bar[] barsCopy = [];

    lock {
        Bar[] bars = self.bars;
        foreach int i in 0 ..< bars.length() {
            barsCopy[i] = bars[i];
        }
    }

    foreach Bar bar in barsCopy {
        var s = start readExternal(bar);
    }
}

But, there could be situations where self.bars is updated between when you create the copy and are calling readExternal, and those changes obviously won't be reflected in barsCopy.

https://github.com/ballerina-platform/ballerina-spec/issues/418 is the spec issue for new strands created within lock statements btw. I'm not sure if we've considered this in the context of restricted variable (self of an isolated object or an isolated variable) usage. We will check and create an issue for this if required.

GitHub

Not clear how lock interacts with start (or more generally with calling a function that creates a named worked that outlives the default worker). Relates #357. It would be simple to say that lock o...

mossy eagle
#

Tried this copying approach already. But we cannot do that due to Trying to pass mutable value outside of a lock statement. If this worked, we could handle our case.

copper swift
#

The code I shared works with your definitions. Is anything different in the actual implementation? Bar needs to be isolated or immutable.