Skip to content

Commit cd4e0f2

Browse files
committed
Answer the stop-request word through the routine this environment already has
⭐ AND THIS IS WHY THE INTERFACE IS A WORD RATHER THAN A HANDLER. The notification here arrives on a context the environment starts for it, which is nothing like a disposition interrupting whatever was running. An interface shaped like the other system's signals would have had to pretend one was the other; a word both can set needs no pretending. The routine stores and wakes and returns false, so the default handling proceeds and a program that never reads the word behaves as it always did.
1 parent 0393b6c commit cd4e0f2

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/process.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,35 @@ int kal_process_spawn(const kal_spawn* how,
264264
return kal_ok;
265265
}
266266

267+
// ⭐⭐ A WORD THIS ENVIRONMENT SETS WHEN SOMEBODY HAS ASKED THIS PROGRAM TO END.
268+
//
269+
// ⚠️ AND THIS IS WHY THE INTERFACE IS A WORD RATHER THAN A HANDLER. The
270+
// notification here arrives ON A CONTEXT OF ITS OWN --- the environment starts one
271+
// to run the routine --- which is nothing like a disposition interrupting whatever
272+
// was running. An interface shaped like the other system's signals would have
273+
// had to pretend one was the other; a word both can set needs no pretending.
274+
//
275+
// The routine stores and wakes, which is all `kal_task_wait' needs on the other
276+
// side. Returning false lets the default handling proceed, so a program that
277+
// never reads the word behaves as it always did.
278+
namespace {
279+
kal_u32 g_stop_word = 0;
280+
int g_stop_armed = 0;
281+
282+
BOOL OKW_API stop_routine(DWORD) {
283+
g_stop_word = 1;
284+
WakeByAddressAll(&g_stop_word);
285+
return FALSE;
286+
}
287+
} // namespace
288+
289+
// ⚠️ Armed on the first enquiry, so that adding this operation changes nothing
290+
// for a program that does not use it.
291+
const kal_u32* kal_process_stop_requested(void) {
292+
if (!g_stop_armed) { g_stop_armed = 1; SetConsoleCtrlHandler(stop_routine, TRUE); }
293+
return &g_stop_word;
294+
}
295+
267296
// This program itself joins or forms a unit. ⭐ NATURAL HERE TOO, and by the
268297
// route this environment already offers: a job object is created before it has
269298
// members, so the caller simply becomes its first one.

src/win32.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ OKW_IMPORT BOOL OKW_API TerminateProcess(HANDLE, UINT);
316316
OKW_IMPORT HANDLE OKW_API CreateJobObjectW(SECURITY_ATTRIBUTES*, LPCWSTR);
317317
OKW_IMPORT BOOL OKW_API AssignProcessToJobObject(HANDLE, HANDLE);
318318
OKW_IMPORT BOOL OKW_API TerminateJobObject(HANDLE, UINT);
319+
320+
// openkal 0.11: the word set when this program is asked to end. The routine runs
321+
// on a context this environment starts, which is why the interface is a word and
322+
// not a disposition --- see kal_process_stop_requested.
323+
OKW_IMPORT BOOL OKW_API SetConsoleCtrlHandler(BOOL (OKW_API*)(DWORD), BOOL);
319324
OKW_IMPORT HANDLE OKW_API GetCurrentProcess(void);
320325
OKW_IMPORT DWORD OKW_API WaitForSingleObject(HANDLE, DWORD);
321326

0 commit comments

Comments
 (0)