Is it possible to cancel IO operation from translated C program?

,

Recently I have to use EtherCAT for my program and decided to use SOEM. I managed to translate the code and use it. I think it is a good idea to use IO cancelation ergonomics when using SOEM.

In using SOEM, you have to keep sending and receiving processdata to the slaves otherwise the connection might be terminated once your connection is in operational state.

My approach is to spawn this process into another thread to avoid connection termination. I thought that I can use concurrent() for this thread, so that it also react to any cancelation from another thread. But, I just realized there is no cancelation point in the processdata thread, so whenever other thread trigger cancel(), the processdata thread is not responding to the cancelation.

By writing this topic, I am just curious is there any way to give the processdata a way to react to any cancelation from another thread?

The processdata thread will looks something like this:

pub fn process(ctx: *soem.ecx_contexttt) !void {
     while (true) {
        if (soem.ecx_send_processdata(ctx) != expected_WKC and
            soem.ecx_receive_processdata(ctx, soem.EC_TIMEOUTRET) != expected_WKC)
        {
            return error.InvalidWorkCounter;
        }
    }
}

I didn’t get if this is something that you tried or you are assuming there’s no cancellation point?

I imagine the std.Io.Threaded impl will cancel a thread at any point but I never really checked this (been working on embedded systems for a while).

take a look at std.Io.checkCancel. but you should typically be sleeping between cycles to achieve a uniform cycle time, which would also provide a cancellation point.

https://ziglang.org/documentation/0.16.0/std/#std.Io.checkCancel

Also, I’ve written a library for EtherCAT you might be interested in: https://codeberg.org/jeffective/gatorcat
but its still in development and unstable, the main feature it is missing is support for distributed clocks.

2 Likes

Oh I did not notice there is Io.checkCancel. I was focusing on the implementation of Future, Group, and Select without checking the Io. Thanks for pointing it out.