Hi, I am developing a framework to manage data provided by various data sources via a (plugin) api. The plugin can be provided via a dynamic library or added directly to the source. In both cases, I have no control over the plugin content but I need a way to either suspend and resume or stop the running function.
For simplicity, the plugins currently only contain Swift code, but could also include C, C++ or F90 libraries in the future.
The plugin is loaded on demand and its main function is called. The returned data is then processed further.
I know that swift provides some APIs like DispatchWorkItem
or the new Task
, both of which have a cancel
method, but they require manually implemented is-cancelled-checks in the code part that I can't access to work.
Here the simplified plugin workflow:
protocol SomePlugin {
var id: UUID { get }
func run()
}
struct MyPlugin: SomePlugin {
let id: UUID = UUID()
func run() {
/* ..the plugin content - usually a result would be returned.. */
while true {} // runs for ever to demonstrate a severe hang...
}
}
func main() {
var plugin: SomePlugin = MyPlugin()
// plugin call in a different thread / async background
DispatchQueue.global(qos: .background).async {
plugin.run()
}
sleep(4)
// DO: stop plugin.run() from executing *or* suspend and resume later
}
main()
dispatchMain()
and here the simplified public API:
import DTX
let dtx = DTX()
dtx.data_src = [
MyDataSource(),
SomeOtherDataSource()
]
dtx.dylib_folder = .documentsDirectory
dtx.serve()
let res = dtx.latest(for: "NHH")
I hope there is a way to solve this problem.
Thank you in advance.