void* doWithin(int limit, void* proc, void* doOnFail) { pthread_t* t = EMALLOC(sizeof(pthread_t)); // printf("CREATING THREAD %d\n", t); struct threadinfo th; th.proc = proc; th.result = NULL; struct timeval tv; gettimeofday(&tv, NULL); int tnow, tthen = do_utime(); pthread_create(t, NULL, runThread, &th); // printf("tthen %d\n", tthen); void* ans; do { // If the answer has been updated, we're done. if (th.result!=NULL) { pthread_join(*t, &ans); return ans; } gettimeofday(&tv, NULL); tnow = do_utime(); usleep(100); // printf("tnow %d\n", tnow); } while(tnow<(tthen+(limit*1000))); pthread_cancel(*t); return DO_EVAL(doOnFail,1); }
void dumpClosureA(Closure* c, int rec) { c = DO_EVAL(c,0); switch(GETTY(c)) { case FUN: printf("FUN["); break; case THUNK: printf("THUNK["); break; case CON: if (!rec) { printf("CON["); } else { printf("["); } dumpCon((con*)c->info, rec); break; case INT: if (!rec) { printf("INT[%ld", ((eint)c)>>1); } else { printf("[%ld", ((eint)c)>>1); }
void* runThread(void* th_in) { struct threadinfo* th = (struct threadinfo*)th_in; void* v = DO_EVAL(th->proc, 1); th->result = v; return v; }
int cgc_program_run(program_t *prog, io_t *io) { int result = EVAL_ERROR; interp_t interp; cgc_memset(&interp, 0, sizeof(interp_t)); interp.io = io; interp.prog = prog; if (!cgc_dict_init(&interp.vars, cgc_free_var)) return 0; if ((interp.buf = cgc_malloc(BUF_SIZE)) == NULL) goto done; if (!cgc_dict_add(&interp.vars, "RS", new_string(cgc_strdup("\n")))) goto done; if (!cgc_dict_add(&interp.vars, "ORS", new_string(cgc_strdup("\n")))) goto done; if (!cgc_dict_add(&interp.vars, "FS", new_string(cgc_strdup(" ")))) goto done; if (!cgc_dict_add(&interp.vars, "OFS", new_string(cgc_strdup(" ")))) goto done; while (1) { pattern_t *p; result = EVAL_FINISHED; if (!read_record(&interp)) break; #define DO_EVAL() do { \ result = cgc_eval_statements(&interp, p->stmt); \ if (result == EVAL_NEXT) \ goto next; \ else if (result != EVAL_FINISHED) \ goto done; \ } while (0) // apply BEGIN patterns for (p = interp.prog->patterns; p != NULL; p = p->next) { if (p->pattern == PATTERN_BEGIN) DO_EVAL(); } // apply normal patterns for (p = interp.prog->patterns; p != NULL; p = p->next) { if (p->pattern == PATTERN_INVALID || p->pattern == PATTERN_BEGIN || p->pattern == PATTERN_END) continue; if (p->pattern == PATTERN_EMPTY) { DO_EVAL(); } else { if (!cgc_eval_expression(&interp, (expr_t*)p->pattern)) goto done; if (coerce_bool(&interp, &interp.result)) DO_EVAL(); } } next: // apply END patterns for (p = interp.prog->patterns; p != NULL; p = p->next) { if (p->pattern == PATTERN_END) DO_EVAL(); } free_fields(&interp); } done: free_fields(&interp); cgc_free(interp.buf); cgc_dict_free(&interp.vars); return result != EVAL_ERROR; }