Compare commits

..
2 Commits
Author SHA1 Message Date
hyung-hwan baa01b22e3 relocated signal primitives to the sys module 2026-09-11 23:12:40 +09:00
hyung-hwan 58d1234020 added ticker to bin/main.go 2026-09-11 16:01:36 +09:00
10 changed files with 150 additions and 79 deletions
+48
View File
@@ -7,6 +7,7 @@ import (
"io"
"os"
"strings"
"time"
)
/*
@@ -221,11 +222,56 @@ func handle_arguments(param *Param) error {
return nil
}
func start_ticker(x *hak.Hak) func() {
var ticker *time.Ticker
var ticker_stop chan bool
var ticker_done chan bool
var stopper func()
ticker = time.NewTicker(20 * time.Millisecond)
ticker_done = make(chan bool)
ticker_stop = make(chan bool)
go func() {
for {
select {
case <- ticker_stop:
goto done
case <- ticker.C:
x.RaiseTick()
}
}
done:
ticker.Stop()
ticker_done <- true
}()
x.RcvTick(true)
stopper = func() {
x.RcvTick(false)
ticker_stop <- true
<- ticker_done // wait for the ticker to stop
// if i don't close the the two channels below, the multiple calls to the
// returned stopper function wouldn't cause immediate panic for writing
// on a closed channel. but i would still close them as i don't want to
// cater for generic use of this function and this function wasn't
// written to be generic. i don't care to use any other more advanced
// mechanisms. the caller must ensure to call this stopper only once.
close(ticker_stop)
close(ticker_done)
}
return stopper
}
func main() {
var x *hak.Hak = nil
var err error = nil
var param Param
var stop_ticker func()
var rfh hak.CciFileHandler
var sfh hak.UdiFileHandler
@@ -321,7 +367,9 @@ func main() {
* whole of Execute(). */
x.Decode()
stop_ticker = start_ticker(x)
err = x.Execute()
stop_ticker()
if err != nil {
//fmt.Printf("ERROR: %s[%d:%d] - %s\n", herr.File, herr.Line, herr.Colm, herr.Msg)
fmt.Printf("ERROR: %s\n", err.Error())
+10
View File
@@ -482,6 +482,16 @@ func (hak *Hak) Decode() error {
return nil
}
func (hak *Hak) RcvTick(enabled bool) {
var i C.int
if enabled { i = 1 } else { i = 0 }
C.hak_rcvtick(hak.c, i)
}
func (hak *Hak) RaiseTick() {
C.hak_raisetick(hak.c)
}
func (hak *Hak) get_errmsg() string {
return C.GoString(C.hak_geterrbmsg(hak.c))
}
+9
View File
@@ -2272,6 +2272,15 @@ hak_pfrc_t hak_pf_semaphore_group_add_semaphore (hak_t* hak, hak_mod_t* mod, hak
hak_pfrc_t hak_pf_semaphore_group_remove_semaphore (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs);
hak_pfrc_t hak_pf_semaphore_group_wait (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs);
/* the signal primitives live in prim.c but are registered by the sys module,
* which reaches them as sys.sig-getfd, sys.sig-get, sys.sig-set, sys.sig-catch
* and sys.sig-uncatch */
hak_pfrc_t hak_pf_system_get_sigfd (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs);
hak_pfrc_t hak_pf_system_get_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs);
hak_pfrc_t hak_pf_system_set_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs);
hak_pfrc_t hak_pf_system_catch_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs);
hak_pfrc_t hak_pf_system_uncatch_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs);
/* ========================================================================= */
/* std.c */
/* ========================================================================= */
+14 -15
View File
@@ -1256,7 +1256,7 @@ hak_pfrc_t hak_pf_object_new (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
/* ------------------------------------------------------------------------- */
static hak_pfrc_t pf_system_get_sigfd (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
hak_pfrc_t hak_pf_system_get_sigfd (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
{
hak_ooi_t fd;
hak_hnd_t* hnd;
@@ -1275,7 +1275,7 @@ static hak_pfrc_t pf_system_get_sigfd (hak_t* hak, hak_mod_t* mod, hak_ooi_t nar
return HAK_PF_SUCCESS;
}
static hak_pfrc_t pf_system_get_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
hak_pfrc_t hak_pf_system_get_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
{
hak_uint8_t sig;
int n;
@@ -1289,12 +1289,12 @@ static hak_pfrc_t pf_system_get_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs
return HAK_PF_SUCCESS;
}
/* (system-catch-sig signo) - route an operating system signal into the
* signal descriptor, where hak code can wait for
* it with core.sem-signal-on-input
* (system-uncatch-sig signo) - release it again
/* (sys.sig-catch signo) - route an operating system signal into the signal
* descriptor, where hak code can wait for it with
* core.sem-signal-on-input
* (sys.sig-uncatch signo) - release it again
*
* Note the difference from system-set-sig, which does not touch the operating
* Note the difference from sys.sig-set, which does not touch the operating
* system at all: that one injects a number into the descriptor directly, as a
* way for hak code to post a synthetic signal to itself.
*/
@@ -1323,17 +1323,17 @@ static hak_pfrc_t __system_catch_sig (hak_t* hak, hak_ooi_t nargs, int enable)
return HAK_PF_SUCCESS;
}
static hak_pfrc_t pf_system_catch_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
hak_pfrc_t hak_pf_system_catch_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
{
return __system_catch_sig(hak, nargs, 1);
}
static hak_pfrc_t pf_system_uncatch_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
hak_pfrc_t hak_pf_system_uncatch_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
{
return __system_catch_sig(hak, nargs, 0);
}
static hak_pfrc_t pf_system_set_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
hak_pfrc_t hak_pf_system_set_sig (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs)
{
hak_oop_t tmp;
hak_uint8_t sig;
@@ -1366,11 +1366,10 @@ static pf_t builtin_prims[] =
{ 1, HAK_TYPE_MAX(hak_oow_t), pf_scanf, 5, { 's','c','a','n','f' } },
{ 1, HAK_TYPE_MAX(hak_oow_t), pf_sprintf, 7, { 's','p','r','i','n','t','f' } },
{ 0, 0, pf_system_get_sigfd, 16, { 's','y','s','t','e','m','-','g','e','t','-','s','i','g','f','d' } },
{ 0, 0, pf_system_get_sig, 14, { 's','y','s','t','e','m','-','g','e','t','-','s','i','g' } },
{ 1, 1, pf_system_set_sig, 14, { 's','y','s','t','e','m','-','s','e','t','-','s','i','g' } },
{ 1, 1, pf_system_catch_sig, 16, { 's','y','s','t','e','m','-','c','a','t','c','h','-','s','i','g' } },
{ 1, 1, pf_system_uncatch_sig, 18, { 's','y','s','t','e','m','-','u','n','c','a','t','c','h','-','s','i','g' } },
/* the signal primitives are registered by the sys module instead - see
* pfinfos[] in mod/sys.c. they are reached as sys.sig-getfd, sys.sig-get,
* sys.sig-set, sys.sig-catch and sys.sig-uncatch. the implementations stay
* here and are declared in lib/hak-prv.h. */
{ 0, 0, pf_gc, 2, { 'g','c' } },
-1
View File
@@ -3535,7 +3535,6 @@ static HAK_INLINE int start_ticker (void)
nanosleep(&ts, HAK_NULL);
#elif defined(HAVE_USLEEP)
usleep(HAK_TICKER_INTERVAL_USECS * 2);
#else
# error UNDEFINED SLEEP
#endif
+8 -2
View File
@@ -31,9 +31,8 @@
#endif
#include "_sys.h"
#include <hak-hnd.h>
#include "../lib/hak-prv.h"
#include <hak-pio.h>
#include <hak-str.h>
#include <stdlib.h>
#include <signal.h>
@@ -813,6 +812,13 @@ static hak_pfinfo_t pfinfos[] =
{ "pwait", { HAK_PFBASE_FUNC, pf_sys_pwait, 1, 1 } },
{ "random", { HAK_PFBASE_FUNC, pf_sys_random, 0, 0 } },
{ "read", { HAK_PFBASE_FUNC, pf_sys_read, 2, 4 } },
{ "sig-catch", { HAK_PFBASE_FUNC, hak_pf_system_catch_sig, 1, 1 } },
{ "sig-get", { HAK_PFBASE_FUNC, hak_pf_system_get_sig, 0, 0 } },
{ "sig-getfd", { HAK_PFBASE_FUNC, hak_pf_system_get_sigfd, 0, 0 } },
{ "sig-set", { HAK_PFBASE_FUNC, hak_pf_system_set_sig, 1, 1 } },
{ "sig-uncatch", { HAK_PFBASE_FUNC, hak_pf_system_uncatch_sig, 1, 1 } },
{ "srandom", { HAK_PFBASE_FUNC, pf_sys_srandom, 1, 1 } },
{ "stime", { HAK_PFBASE_FUNC, pf_sys_stime, 1, 1 } },
{ "time", { HAK_PFBASE_FUNC, pf_sys_time, 0, 0 } },
+4 -4
View File
@@ -107,10 +107,10 @@ class ChildGroup(
## semaphore on the signal descriptor for the whole group, once.
if (not self.shared) {
set shared true
system-catch-sig sys.SIGCHLD
sys.sig-catch sys.SIGCHLD
set sigsem (core.sem-new)
core.semgr-add self.sg self.sigsem
core.sem-signal-on-input self.sigsem (system-get-sigfd)
core.sem-signal-on-input self.sigsem (sys.sig-getfd)
}
} else {
## A handle of its own, so a wakeup identifies this child directly.
@@ -180,7 +180,7 @@ class ChildGroup(
if self.shared {
## the signal descriptor spoke: take the byte, then look at
## everyone, since one SIGCHLD may stand for several exits
system-get-sig
sys.sig-get
kid := (self:finished)
if (not (nil? kid)) { return kid }
## otherwise it was a SIGCHLD for a child of the host
@@ -228,7 +228,7 @@ class ChildGroup(
}
if self.shared {
core.sem-unsignal self.sigsem
system-uncatch-sig sys.SIGCHLD
sys.sig-uncatch sys.SIGCHLD
set shared false
}
}
+8 -8
View File
@@ -27,17 +27,17 @@ sys.close (core.basicAt q 0)
sys.close (core.basicAt q 1)
## --- catch and uncatch are idempotent ---
chk (= (system-catch-sig sys.SIGUSR1) sys.SIGUSR1) "system-catch-sig returns the signal number"
chk (= (system-catch-sig sys.SIGUSR1) sys.SIGUSR1) "catching an already caught signal is fine"
chk (= (system-uncatch-sig sys.SIGUSR1) sys.SIGUSR1) "system-uncatch-sig returns the signal number"
chk (= (system-uncatch-sig sys.SIGUSR1) sys.SIGUSR1) "uncatching an uncaught signal is fine"
chk (= (sys.sig-catch sys.SIGUSR1) sys.SIGUSR1) "sys.sig-catch returns the signal number"
chk (= (sys.sig-catch sys.SIGUSR1) sys.SIGUSR1) "catching an already caught signal is fine"
chk (= (sys.sig-uncatch sys.SIGUSR1) sys.SIGUSR1) "sys.sig-uncatch returns the signal number"
chk (= (sys.sig-uncatch sys.SIGUSR1) sys.SIGUSR1) "uncatching an uncaught signal is fine"
## --- a real signal reaches hak code, without stalling the coprocesses ---
## SIGCHLD is used because a child exiting is something this test can arrange
## on its own, with no outside help.
system-catch-sig sys.SIGCHLD
sys.sig-catch sys.SIGCHLD
h := (system-get-sigfd)
h := (sys.sig-getfd)
s := (core.sem-new)
tmo := (core.sem-new)
sg := (core.semgr-new)
@@ -58,7 +58,7 @@ fun waiter() {
core.sem-unsignal s
core.sem-unsignal tmo
if (eqv? w tmo) { signo := -2 } \
else { signo := (system-get-sig) }
else { signo := (sys.sig-get) }
core.sem-signal fin
return 0
}
@@ -79,4 +79,4 @@ chk (= ticks 4) "coprocesses ran while a coprocess waited on a signal"
chk (= signo sys.SIGCHLD) "the signal number came through the signal descriptor"
chk (= (sys.pwait proc) 4) "and the child's exit status is readable"
sys.pclose proc
system-uncatch-sig sys.SIGCHLD
sys.sig-uncatch sys.SIGCHLD
+3 -3
View File
@@ -54,8 +54,8 @@ chk (== sys.SIGTERM 15) "SIGTERM is 15"
## and the constants behave: a routable one can be caught, an unroutable one
## cannot, whatever the numbers happen to be here
chk (== (system-catch-sig sys.SIGUSR1) sys.SIGUSR1) "SIGUSR1 is catchable"
chk (== (system-uncatch-sig sys.SIGUSR1) sys.SIGUSR1) "SIGUSR1 is uncatchable again"
chk (== (sys.sig-catch sys.SIGUSR1) sys.SIGUSR1) "SIGUSR1 is catchable"
chk (== (sys.sig-uncatch sys.SIGUSR1) sys.SIGUSR1) "SIGUSR1 is uncatchable again"
raised := false
try { system-catch-sig sys.SIGKILL } catch (e) { raised := true }
try { sys.sig-catch sys.SIGKILL } catch (e) { raised := true }
chk raised "SIGKILL is refused"
+10 -10
View File
@@ -1,41 +1,41 @@
## signals that cannot be caught
system-catch-sig sys.SIGKILL ##ERROR: not routable
sys.sig-catch sys.SIGKILL ##ERROR: not routable
---
system-catch-sig sys.SIGSTOP ##ERROR: not routable
sys.sig-catch sys.SIGSTOP ##ERROR: not routable
---
## signals that indicate a crash: turning one into a byte on a pipe and
## carrying on would hide the fault rather than report it
system-catch-sig sys.SIGSEGV ##ERROR: not routable
sys.sig-catch sys.SIGSEGV ##ERROR: not routable
---
system-catch-sig sys.SIGBUS ##ERROR: not routable
sys.sig-catch sys.SIGBUS ##ERROR: not routable
---
system-catch-sig sys.SIGFPE ##ERROR: not routable
sys.sig-catch sys.SIGFPE ##ERROR: not routable
---
system-catch-sig sys.SIGILL ##ERROR: not routable
sys.sig-catch sys.SIGILL ##ERROR: not routable
---
## the timer signal hak itself uses to switch processes
system-catch-sig sys.SIGVTALRM ##ERROR: not routable
sys.sig-catch sys.SIGVTALRM ##ERROR: not routable
---
system-catch-sig 0 ##ERROR: 0 not routable
sys.sig-catch 0 ##ERROR: 0 not routable
---
system-catch-sig 9999 ##ERROR: 9999 not routable
sys.sig-catch 9999 ##ERROR: 9999 not routable
---
system-catch-sig "two" ##ERROR: number not a small integer
sys.sig-catch "two" ##ERROR: number not a small integer