Dear All, Concerning @kuniaki questions on SWI-Prolog discourse, and @torbjörn tickets. A small correction for:
> NOTE — A built-in predicate call_cleanup/2 > with similar functionality is implemented in > many existing processors. It is often used to free temporary > resources. In the presence of interrupts (7.12 note c), > call_cleanup/2 can cause leakage of resources: > A processor receiving interrupts > > between resource allocation and call_cleanup/2 > is unable to free the resource in a timely manner. > To overcome this problem, setup_call_cleanup/3 > protects resource allocation from interrupts.
https://www.complang.tuwien.ac.at/ulrich/iso-prolog/N215 After having implemented call_cleanup/2 first for formerly Jekejeke Prolog in a multithreaded setting, and then for Dogelog Player in a cooperative multitasking setting, the above formula needs a lot of clarification. Since we have in a system that should always be responsive rather this desiderata:
> setup_call_cleanup/3 SHOULD NOT block resource > allocation from interrupts. Only the transition > form resource allocation to call_cleanup/2.
It is the try resource pattern, which can be mapped to this try finally pattern: ``` Object handle = open("resource"); try { /* do something */ } finally { close(handle); } ``` The desired protection from interrupts is between the assignment of handle, when the open/1 was successful, and entering the try finally block. Because if the code would be interrupted before the finally block is entered, the closeing of the handler wouldn’t be called. But the correction for N215 would be that open/1 itself should nevertheless allow to receive interrupts, for example time out events, so that one can always abort Prolog code. An open/1 routine that would be interrupted by a time out event, would have already closed and/or reclaimed and/or tidied up the return result of handle, respectively because it aborts, it would never expose a new handle object at all. This is pattern is solved for example in Java, that blocking routines like open/1 do have an interruptible signature, and if they succeeds, interrupts dont lead to an exception anymore for the remaining execution of the code, rather an interrupted flag is set. This deferal allows to enter the try block. Hope this Helps! Bye