(bind-func scheduler_init:[[i64,!a]*,SchedEvt{!a}*,[void,!a]*]*
(lambda (el f)
(let ((mutex:i8* (mutex_create))
(thread:i8* (thread_self))
(cnt 0)
(evtlist:SchedEvt* el)
(fp:[void,!a]* f)
(element:SchedEvt* null)
(next:SchedEvt* null)
(prev:SchedEvt* null))
(lambda (time)
(set! cnt 0)
(mutex_lock mutex)
(set! element evtlist)
(while (and (not (null? element))
(<= (tref element 0) time))
(set! cnt (+ cnt 1))
(set! fp (cast (llvm_get_function_ptr (tref element 1))))
;; (println "exec: " time ":" (tref element 0))
(fptrcall fp (tref element 0)) ; (tref element 2))
(set! next (tref element 3))
(set! prev (tref element 2))
(if (null? prev)
(set! evtlist next)
(tset! prev 3 next))
(if (not (null? next)) (tset! next 2 prev))
(free element)
(set! element next))
(mutex_unlock mutex)
cnt))))