kcontext_t *new_ThreadContext(CTX) { KNH_SYSLOCK(_ctx); kcontext_t *newCtx = new_hcontext(_ctx); newCtx->share = ctx->share; newCtx->stat = ctx->stat; newCtx->spi = ctx->spi; newCtx->script = ctx->script; newCtx->parent = WCTX(_ctx); // newCtx->freeObjectList = NULL; // newCtx->freeObjectTail = NULL; KINITv(newCtx->gma, new_(GammaBuilder)); knh_GammaBuilder_init(newCtx); CommonContext_init(_ctx, newCtx); knh_stack_initexpand(newCtx, NULL, K_STACKSIZE); ctx->wshare->contextCounter++; ctx->wshare->threadCounter++; if(newCtx->ctxobjNC == NULL) { newCtx->ctxobjNC = knh_toContext(newCtx); } kArray_add(ctx->share->contextListNULL, newCtx->ctxobjNC); newCtx->safepoint = ctx->share->ctx0->safepoint; KNH_SYS_UNLOCK(_ctx); return newCtx; }
static void trapILL(int sig RECARG) { static const char *emsg[] = { /* FPE_NOOP 0*/ "SIGILL", /* ILL_ILLOPC 1*/ "illegal opcode", /* ILL_ILLTRP 2*/ "illegal trap", /* ILL_PRVOPC 3*/ "privileged opcode", /* ILL_ILLOPN 4*/ "illegal operand", /* 5 */ "illegal addressing mode", /* 6 */ "privileged register", /* 7 */ "coprocessor error", /* 8 */ "internal stack error"}; CTX ctx = knh_getCurrentContext(); record_signal(ctx, sig RECDATA); if(ctx != NULL) { #if defined(K_USING_MINGW_) int si_code = 0; #else int si_code = (si->si_code < 9) ? si->si_code : 0; #endif /* defined(K_USING_MINGW_) */ WCTX(ctx)->signal = sig; THROW_Halt(ctx, NULL, emsg[si_code]); } _Exit(EX_SOFTWARE); }
static void knh_shell(CTX ctx) { void *shell_status = NULL; BEGIN_LOCAL(ctx, lsfp, 2); // LOCAL_NEW(ctx, lsfp, 0, kInputStream *, bin, new_BytesInputStream(ctx, new_Bytes(ctx, "shell", K_PAGESIZE))); { CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf); knh_showWelcome(ctx, cwb->w); knh_showSecurityAlert(ctx, cwb->w); shell_status = shell_init(ctx, CWB_totext(ctx, cwb), NULL); CWB_close(ctx, cwb); } while(1) { { CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf); kstatus_t status = readstmt(ctx, cwb); if(status == K_BREAK) { CWB_close(ctx, cwb); break; } if(CWB_size(cwb) == 0) { CWB_close(ctx, cwb); continue; } status = shell_command(ctx, CWB_totext(ctx, cwb)); if(status == K_BREAK) { CWB_close(ctx, cwb); break; } if(status == K_REDO) { CWB_close(ctx, cwb); continue; } #ifdef K_USING_SUGAR kString *script = CWB_newString(ctx, cwb, 0); KNH_SETv(ctx, lsfp[0].o, script); knh_beval2(ctx, S_totext(script), 1); #else kInputStream *bin = new_BytesInputStream(ctx, CWB_totext(ctx, cwb), CWB_size(cwb)); KNH_SETv(ctx, lsfp[0].o, bin); knh_beval(ctx, bin, 1); #endif } knh_OutputStream_flush(ctx, ctx->out); if(ctx->isEvaled == 1) { CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf); knh_write_Object(ctx, cwb->w, ctx->evaled, FMT_dump); knh_showSecurityAlert(ctx, cwb->w); if(CWB_size(cwb) !=0) { shell_display(ctx, shell_status, CWB_totext(ctx, cwb)); } CWB_close(ctx, cwb); WCTX(ctx)->isEvaled = 0; } } shell_cleanup(ctx, shell_status); END_LOCAL(ctx, lsfp); }
static void knh_setsignal(CTX ctx, void *block, size_t n) #endif /* defined(K_USING_MINGW_) */ { #if !defined(K_USING_MINGW_) struct sigaction sa = {}; struct sigaction *sa_orig = (struct sigaction*)block; #endif /* !defined(K_USING_MINGW_) */ WCTX(ctx)->signal = 0; WCTX(ctx)->siginfo = NULL; #ifndef K_USING_DEBUG #if defined(K_USING_MINGW_) KNH_SIGNAL(SIGSEGV, trapSEGV); KNH_SIGNAL(SIGILL, trapILL); #else sa.sa_sigaction = trapSEGV; sa.sa_flags = SA_SIGINFO; KNH_SIGACTION(SIGSEGV, &sa, sa_orig, n); sa.sa_sigaction = trapILL; sa.sa_flags = SA_SIGINFO; KNH_SIGACTION(SIGILL, &sa, sa_orig, n); sa.sa_sigaction = trapBUS; sa.sa_flags = SA_SIGINFO; KNH_SIGACTION(SIGBUS, &sa, sa_orig, n); #endif /* defined(K_USING_MINGW_) */ #endif #if defined(K_USING_MINGW_) KNH_SIGNAL(SIGFPE, trapSIGFPE); #else sa.sa_sigaction = trapSIGFPE; sa.sa_flags = SA_SIGINFO|SA_NODEFER; KNH_SIGACTION(SIGFPE, &sa, sa_orig, n); #endif /* defined(K_USING_MINGW_) */ if(CTX_isInteractive(ctx)) { DBG_P("set SIGINT This is not so good"); #if defined(K_USING_MINGW_) KNH_SIGNAL(SIGINT, trapSIGINT); #else sa.sa_sigaction = trapSIGINT; sa.sa_flags = SA_SIGINFO|SA_NODEFER; KNH_SIGACTION(SIGINT, &sa, sa_orig, n); #endif /* defined(K_USING_MINGW_) */ } }
static void knh_unsetsignal(CTX ctx, void *block, size_t n) #endif /* defined(K_USING_MINGW_) */ { #if !defined(K_USING_MINGW_) struct sigaction *sa_orig = (struct sigaction*)block; if(sa_orig != NULL) { #endif /* !defined(K_USING_MINGW_) */ #ifndef K_USING_DEBUG KNH_SIGACTION2(SIGILL, sa_orig, n); #if !defined(K_USING_MINGW_) KNH_SIGACTION2(SIGBUS, sa_orig, n); #endif /* !defined(K_USING_MINGW_) */ KNH_SIGACTION2(SIGSEGV, sa_orig, n); #endif KNH_SIGACTION2(SIGFPE, sa_orig, n); if(CTX_isInteractive(ctx)) { KNH_SIGACTION2(SIGINT, sa_orig, n); } #if !defined(K_USING_MINGW_) } #endif /* !defined(K_USING_MINGW_) */ WCTX(ctx)->signal = 0; WCTX(ctx)->siginfo = NULL; }
static void trapBUS(int sig RECARG) { static const char *emsg[] = { /* BUS_NOOP 0*/ "BUS_NOOP", /* BUS_ADRALN 1*/ "invalid address alignment", /* BUS_ADRERR 2*/ "nonexistent physical address", /* BUS_OBJERR 3*/ "object-specific HW error"}; CTX ctx = knh_getCurrentContext(); record_signal(ctx, sig RECDATA); if(ctx != NULL) { int si_code = (si->si_code < 4) ? si->si_code : 1; WCTX(ctx)->signal = sig; THROW_Halt(ctx, NULL, emsg[si_code]); } _Exit(EX_SOFTWARE); }
static void trapSEGV(int sig RECARG) { CTX ctx = knh_getCurrentContext(); record_signal(ctx, sig RECDATA); #if !defined(K_USING_MINGW_) if (si->si_code == SEGV_ACCERR) { void* address = (void*)si->si_addr; fprintf(stderr, "address=%p\n", address); } #endif /* defined(K_USING_MINGW_) */ if(ctx != NULL) { WCTX(ctx)->signal = sig; THROW_Halt(ctx, NULL, "segmentation fault"); } _Exit(EX_SOFTWARE); }
knh_mutex_free(ctx, (kmutex_t *)po->rawptr); po->rawptr = NULL; } } /* ======================================================================== */ // [KMETHODS] //## @Native Thread Thread.spawn(dynamic f, dynamic[] args) KMETHOD Thread_spawn(CTX ctx, ksfp_t *sfp _RIX) { kFunc *f = sfp[1].fo; kArray *args = sfp[2].a; if(IS_NOTNULL(((kObject *)f))) { Thread_t *t = (Thread_t *)KNH_MALLOC(ctx, sizeof(Thread_t)); kcontext_t *newCtx = new_ThreadContext(WCTX(ctx)); t->ctx = newCtx; t->func = f; t->args = args; kthread_create(ctx, &(t->thread), NULL, spawn_start, t); RETURN_(new_ReturnRawPtr(ctx, sfp, t)); } } //## @Native void Thread.join(); KMETHOD Thread_join(CTX ctx, ksfp_t *sfp _RIX) { Thread_t *t = RawPtr_to(Thread_t *, sfp[0]); void *v; KNH_SYSLOCK(ctx);