extern "C" dllexport bool __cdecl DllInit(const char* ipcname) { printf("NEW INSTANCE: %08X\n", &s_EmulationControl); char pipename[256]; char eventname[256]; sprintf(pipename, "\\\\.\\Pipe\\%s",ipcname); sprintf(eventname, "%s-event", ipcname); printf("pipe: %s\n",pipename); printf("event: %s\n",eventname); hPipe = CreateFile(pipename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(hPipe == INVALID_HANDLE_VALUE) return false; hMapFile = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, ipcname); if(hMapFile == INVALID_HANDLE_VALUE) return false; hMapFilePtr = MapViewOfFile(hMapFile, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0); //make a coroutine thread to run the emulation in. we'll switch back to this thread when communicating with the frontend co_control = co_active(); co_emu = co_create(65536*sizeof(void*),emuthread); running = true; printf("running\n"); DWORD tid; CreateThread(nullptr, 0, &ThreadProc, nullptr, 0, &tid); return true; }
int32_t test_yieldmain() { args_t args; args.host_ = co_init(nullptr); args.a_ = co_create (args.host_, thread_a, 1024, nullptr, &args); args.b_ = co_create (args.host_, thread_b, 1024, nullptr, &args); co_yield(args.host_, args.a_); co_delete(args.a_); co_delete(args.b_); co_delete(args.host_); return 0; }
int main(int argc, char **argv) { pthread_t *thids; int i; if (co_thread_init() < 0) { perror("co_thread_init failed in main\n"); exit(-1); } task = co_create(count_inc, NULL, NULL, 4096); // launch worker threads thids = (pthread_t *) malloc(NUM_WORKERS * sizeof(pthread_t)); for (i = 0; i < NUM_WORKERS; i++) { int *arg = (int *) malloc( sizeof(int) ); *arg = i; if (pthread_create(&thids[i], NULL, worker, arg)) { perror("creating worker threads"); exit(-1); } } // join on finish for (i = 0; i < NUM_WORKERS; i++) pthread_join(thids[i], NULL); co_delete(task); co_thread_cleanup(); return 0; }
int main() { printf("context-switching timing test\n\n"); time_t start, end; int i, t1, t2; start = clock(); for(thread::counter = 0, i = 0; i < Iterations; i++) { sub_timingtest(); } end = clock(); t1 = (int)difftime(end, start); printf("%2.3f seconds per 50 million subroutine calls (%d iterations)\n", (float)t1 / CLOCKS_PER_SEC, thread::counter); thread::x = co_active(); thread::y = co_create(65536, co_timingtest); start = clock(); for(thread::counter = 0, i = 0; i < Iterations; i++) { co_switch(thread::y); } end = clock(); co_delete(thread::y); t2 = (int)difftime(end, start); printf("%2.3f seconds per 100 million co_switch calls (%d iterations)\n", (float)t2 / CLOCKS_PER_SEC, thread::counter); printf("co_switch skew = %fx\n\n", (double)t2 / (double)t1); return 0; }
int main(){ char para[] = "MainCo"; Coroutine* co = co_create(MainCo, para); co_resume( co ); co_resume( co ); co_delete( co ); return 0; }
void SetUp() override { g_mock_fs = this; srand((unsigned int)time(nullptr)); g_mock_fs->m_hook_ewait = false; auto impl = std::dynamic_pointer_cast<crx::scheduler_impl>(m_sch.m_impl); std::function<void(size_t co_id)> stub; impl->co_create(stub, true, false, "main_coroutine"); }
void retro_init (void){ #ifndef M16B enum retro_pixel_format fmt =RETRO_PIXEL_FORMAT_XRGB8888; #else enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565; #endif const char *system_dir = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system_dir) && system_dir) { // if defined, use the system directory retro_system_directory=system_dir; } const char *content_dir = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_CONTENT_DIRECTORY, &content_dir) && content_dir) { // if defined, use the system directory retro_content_directory=content_dir; } const char *save_dir = NULL; if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &save_dir) && save_dir) { // If save directory is defined use it, otherwise use system directory retro_save_directory = *save_dir ? save_dir : retro_system_directory; } else { // make retro_save_directory the same in case RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY is not implemented by the frontend retro_save_directory=retro_system_directory; } printf("Retro SYSTEM_DIRECTORY %s\n",retro_system_directory); printf("Retro SAVE_DIRECTORY %s\n",retro_save_directory); printf("Retro CONTENT_DIRECTORY %s\n",retro_content_directory); if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { fprintf(stderr, "RGB pixel format is not supported.\n"); exit(0); } if(!emuThread && !mainThread) { mainThread = co_active(); emuThread = co_create(65536*sizeof(void*), retro_wrap_emulator); } }
int async_init(void) { int rc = uv_loop_init(async_loop); if(rc < 0) return rc; master->fiber = co_active(); master->flags = 0; active = master; async_main = master; trampoline = co_create(STACK_DEFAULT, trampoline_fn); if(!trampoline) return UV_ENOMEM; return 0; }
void setup_master_thread(omp_internal_data* data, int rank) { gsoc_task* root_task; _workers[rank].scheduler_task = co_create(gsoc_task_scheduler_loop, NULL, NULL, OMP_TASK_STACK_SIZE_DEFAULT); root_task = gsoc_task_create((void(*)(void*))fib_outlined, data, NULL, OMP_TASK_STACK_SIZE_DEFAULT, NULL); fprintf(stderr, "Root task is %p\n", root_task); gsoc_taskqueue_push(_workers[rank].taskq, root_task); __sync_add_and_fetch(&_num_team_tasks, 1); }
EXPORT void co_start( Coroutine* co ){ unsigned char* p; co->stack.bp = (unsigned char*)&p; co->status = STATUS_READY; co->caller = co_cur ? co_cur : co_create(def_routine,0); if(!setjmp(co->caller->ctx) ){ co_cur = co; co->fn(co->param); co_terminate() ; } }
int main() { printf("cothread parameterized function example\n\n"); thread[0] = co_active(); thread[1] = co_create(65536, co_entrypoint); thread[2] = co_create(65536, co_entrypoint); //use specialized co_switch(cothread_t, int, int) for initial co_switch call co_switch(thread[1], 1, 2); co_switch(thread[2], 4, 8); //after first call, entry point arguments have been initialized, standard //co_switch(cothread_t) can be used from now on co_switch(thread[2]); co_switch(thread[1]); printf("\ndone\n"); #if defined(_MSC_VER) || defined(__DJGPP__) getch(); #endif return 0; }
int async_spawn(size_t const stack, void (*const func)(void *), void *const arg) { cothread_t const fiber = co_create(stack, async_start); if(!fiber) return UV_ENOMEM; arg_func = func; arg_arg = arg; // Similar to async_wakeup but the new thread is not created yet async_t *const original = async_main; async_main = async_active(); co_switch(fiber); async_main = original; return 0; }
void co_sched_run(struct co_sched* sched) { char stack[64]; sched->main = co_create(0, 0, stack, sizeof(stack)); sched->current = sched->main; struct run_queue* rq = &sched->run_queue; while(rq->head) { // printf("run next\n"); co_sched_run_next_(sched); } // printf("run exit\n"); }
static thread_t* new_thread(char *name, void* (*func)(void *), void *arg, thread_attr_t attr) { static unsigned max_tid = 1; thread_t *t = malloc( sizeof(thread_t) ); int stack_size_kb_log2 = get_stack_size_kb_log2(func); void *stack = stack_get_chunk( stack_size_kb_log2 ); int stack_size = 1 << (stack_size_kb_log2 + 10); if( !t || !stack ) { if (t) free(t); if (stack) stack_return_chunk(stack_size_kb_log2, stack); return NULL; } bzero(t, sizeof(thread_t)); t->coro = co_create(new_thread_wrapper, stack - stack_size, stack_size); t->stack = stack; t->stack_size_kb_log2 = stack_size_kb_log2; t->stack_bottom = stack - stack_size; t->stack_fingerprint = 0; t->name = (name ? name : "noname"); t->initial_func = func; t->initial_arg = arg; t->joinable = 1; t->tid = max_tid++; t->sleep = -1; if( attr ) { t->joinable = attr->joinable; t->daemon = attr->daemon; if(t->daemon) num_daemon_threads++; } // FIXME: somehow track the parent thread, for stats creation? // make sure the thread has a valid node before we add it to the scheduling list bg_dummy_node->num_here++; t->curr_stats.node = bg_dummy_node; pl_add_tail(threadlist, t); num_runnable_threads++; sched_add_thread(t); sanity_check_threadcounts(); return t; }
void Scheduler::init() { clock.cpu_freq = snes.region() == SNES::NTSC ? snes.config.cpu.ntsc_clock_rate : snes.config.cpu.pal_clock_rate; clock.smp_freq = snes.region() == SNES::NTSC ? snes.config.smp.ntsc_clock_rate : snes.config.smp.pal_clock_rate; clock.active = THREAD_CPU; clock.cpuppu = 0; clock.cpusmp = 0; clock.smpdsp = 0; if(thread_cpu) co_delete(thread_cpu); if(thread_smp) co_delete(thread_smp); if(thread_ppu) co_delete(thread_ppu); if(thread_dsp) co_delete(thread_dsp); thread_snes = co_active(); thread_cpu = co_create(65536 * sizeof(void*), threadentry_cpu); thread_smp = co_create(65536 * sizeof(void*), threadentry_smp); thread_ppu = co_create(65536 * sizeof(void*), threadentry_ppu); thread_dsp = co_create(65536 * sizeof(void*), threadentry_dsp); }
int32_t test_unchained() { args_t args; args.host_ = co_init(nullptr); args.val_ = 100000; for (uint32_t i = 0; i < num_threads; ++i) { args.thread_[i] = co_create (args.host_, thread_func, 1024, nullptr); assert(args.thread_[i]); co_set_user(args.thread_[i], &args); } co_yield(args.host_, args.thread_[0]); assert(args.val_ == 0); return 0; }
struct mk_http_thread *mk_http_thread_create(int type, struct mk_vhost_handler *handler, struct mk_http_session *session, struct mk_http_request *request, int n_params, struct mk_list *params) { size_t stack_size; struct mk_thread *th = NULL; struct mk_http_thread *mth; struct mk_sched_worker *sched; sched = mk_sched_get_thread_conf(); if (!sched) { return NULL; } th = mk_thread_new(sizeof(struct mk_http_thread), NULL); if (!th) { return NULL; } mth = (struct mk_http_thread *) MK_THREAD_DATA(th); if (!mth) { return NULL; } mth->session = session; mth->request = request; mth->parent = th; mth->close = MK_FALSE; request->thread = mth; mk_list_add(&mth->_head, &sched->threads); th->caller = co_active(); th->callee = co_create(MK_THREAD_STACK_SIZE, thread_cb_init_vars, &stack_size); #ifdef MK_HAVE_VALGRIND th->valgrind_stack_id = VALGRIND_STACK_REGISTER(th->callee, ((char *)th->callee) + stack_size); #endif /* Workaround for makecontext() */ thread_params_set(th, type, handler, session, request, n_params, params); return mth; }
void co_exit_to(struct coroutine *new_co, void *data) { static struct coroutine *helper = 0; static char stk[256]; helper_args[0] = new_co; helper_args[1] = data; if (helper == 0) helper = co_create(del_helper, stk, sizeof(stk)); /* we must leave this coroutine. so call the helper. */ co_call(helper, helper_args); fatal("stale coroutine called"); }
static int pam_auth_init(void** ctx, void *pool, const common_auth_init_st *info) { int pret; struct pam_ctx_st * pctx; if (info->username == NULL || info->username[0] == 0) { syslog(LOG_AUTH, "pam-auth: no username present"); return ERR_AUTH_FAIL; } pctx = talloc_zero(pool, struct pam_ctx_st); if (pctx == NULL) return -1; str_init(&pctx->msg, pctx); pctx->dc.conv = ocserv_conv; pctx->dc.appdata_ptr = pctx; pret = pam_start(PACKAGE, info->username, &pctx->dc, &pctx->ph); if (pret != PAM_SUCCESS) { syslog(LOG_AUTH, "PAM-auth init: %s", pam_strerror(pctx->ph, pret)); goto fail1; } pctx->cr = co_create(co_auth_user, pctx, NULL, PAM_STACK_SIZE); if (pctx->cr == NULL) goto fail2; strlcpy(pctx->username, info->username, sizeof(pctx->username)); if (info->ip != NULL) pam_set_item(pctx->ph, PAM_RHOST, info->ip); *ctx = pctx; return ERR_AUTH_CONTINUE; fail2: pam_end(pctx->ph, pret); fail1: talloc_free(pctx); return -1; }
void co_exit_to(coroutine_t coro) { cothread_ctx *tctx = co_get_thread_ctx(); coroutine *co = (coroutine *) coro; if (tctx->dchelper == NULL && (tctx->dchelper = co_create(co_del_helper, NULL, tctx->stk, sizeof(tctx->stk))) == NULL) { fprintf(stderr, "[PCL] Unable to create delete helper coroutine: curr=%p\n", tctx->co_curr); exit(1); } tctx->co_dhelper = co; co_call((coroutine_t) tctx->dchelper); fprintf(stderr, "[PCL] Stale coroutine called: curr=%p exitto=%p caller=%p\n", tctx->co_curr, co, tctx->co_curr->caller); exit(1); }
void co_exit_to(coroutine_t coro) { coroutine *co = (coroutine *) coro; static coroutine *dchelper = NULL; static char stk[CO_MIN_SIZE]; if (!dchelper && !(dchelper = co_create(co_del_helper, NULL, stk, sizeof(stk)))) { fprintf(stderr, "[PCL] Unable to create delete helper coroutine: curr=%p\n", co_curr); exit(1); } co_dhelper = co; co_call((coroutine_t) dchelper); fprintf(stderr, "[PCL] Stale coroutine called: curr=%p\n", co_curr); exit(1); }
void retro_init (void){ #ifndef M16B enum retro_pixel_format fmt =RETRO_PIXEL_FORMAT_XRGB8888; #else enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565; #endif if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { fprintf(stderr, "RGB pixel format is not supported.\n"); exit(0); } if(!emuThread && !mainThread) { mainThread = co_active(); emuThread = co_create(65536*sizeof(void*), retro_wrap_emulator); } }
void Emu_init() { #ifdef RETRO_AND //you can change this after in core option if device support to setup a 832x576 res retrow=640; retroh=480; MOUSEMODE=1; #endif update_variables(); memset(Key_Sate,0,512); memset(Key_Sate2,0,512); if(!emuThread && !mainThread) { mainThread = co_active(); emuThread = co_create(65536*sizeof(void*), retro_wrap_emulator); } }
void function1(void* p) { char para[] = "Function2"; Coroutine* co = co_create(function2, para); int i = 0; int a[ N ]; for(i=0; i<N; ++i) a[ i ] = i; printf(" %s come...\n",p); printf(" %s pause...\n",p); co_pause(); printf(" %s back...\n",p); co_resume( co ); printf(" %s resume Function2...\n",p); co_resume( co ); printf(" %s has array\n",p); for(i=0; i<N; ++i) printf("%d ",a[ i ]); printf("\n"); printf(" %s terminated\n",p); co_delete( co ); co_terminate(); }
void MainCo(void* p) { printf("MainCo para: %s\n", (char *)p); char para[] = "Function1"; Coroutine* co = co_create(function1, para); int i = 0; int a[ N ]; for(i=0; i<N; ++i) a[ i ] = 5*i; printf("%s come...\n",p); printf("%s pause...\n",p); co_pause(); printf("%s back...\n",p); co_resume( co ); printf("%s resume Function1...\n",p); co_resume( co ); printf("%s has array\n",p); for(i=0; i<N; ++i) printf("%d ",a[ i ]); printf("\n"); printf("%s terminated\n",p); co_delete( co ); co_terminate(); }
void tpl_init_context(tpl_exec_common *exec_obj) { coroutine_t old_co; coroutine_t* co = &(exec_obj->static_desc->context); tpl_stack* stack = &(exec_obj->static_desc->stack); /* This is the entry func passed as data */ void* data = (void*) exec_obj->static_desc; int stacksize = stack->stack_size; void* stackaddr = stack->stack_zone; old_co = *co; assert( stacksize > 0 ); assert( stackaddr != NULL ); assert( data != NULL ); if( stacksize < CO_MIN_SIZE ) { /* co_create will fail if stacksize is < 4096 */ stacksize = stacksize < CO_MIN_SIZE ? CO_MIN_SIZE : stacksize ; } stackaddr = NULL; /* co_create automatically allocate stack data using malloc. */ *co = co_create(tpl_osek_func_stub, data, stackaddr, stacksize); assert( *co != NULL ); assert( *co != old_co ); /* If old_co != NULL, we should garbage it soon. */ if( old_co != NULL ) { if( previous_old_co != NULL ) /* co_delete( previous_old_co ); */ previous_old_co = old_co; } }
static int eph_new_conn(int sfd, void *func) { struct eph_conn *conn = (struct eph_conn *)malloc(sizeof(struct eph_conn)); struct epoll_event ev; if (!conn) return -1; memset(conn, 0, sizeof(*conn)); DBL_INIT_LIST_HEAD(&conn->lnk); conn->sfd = sfd; conn->events = 0; conn->revents = 0; conn->nbytes = conn->rindex = 0; if (!(conn->co = co_create(func, conn, NULL, stksize))) { free(conn); return -1; } DBL_LIST_ADDT(&conn->lnk, &chash[sfd % chash_size]); ev.events = 0; ev.data.ptr = conn; if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, sfd, &ev) < 0) { fprintf(stderr, "epoll set insertion error: fd=%d\n", sfd); DBL_LIST_DEL(&conn->lnk); co_delete(conn->co); free(conn); return -1; } ++numfds; co_call(conn->co); return 0; }
int main( void ) { int i, n; printf( "Thread test\n" ); print_libco_opts(); threads [0] = co_active(); threads [1] = co_create( stack_size, entry ); assert( threads [1] ); for ( n = 0; n < iter; n++ ) { /* if ( !(n & (n - 1)) ) printf( "%d\n", n );*/ for ( i = 1; i < max_threads; i++ ) if ( threads [i] ) co_switch( threads [i] ); } { unsigned all = 0; for ( i = 0; i < 16; i++ ) all ^= shared [i]; if ( all != final_data ) { printf( "0x%08X\n", all ); printf( "Incorrect CRC\n" ); return EXIT_FAILURE; } } printf( "Passed\n\n" ); return 0; }
void co_sched_spawn(struct co_sched* sched, co_proc proc, void* arg, void* stack, unsigned long stackSize) { struct spawn_arg* sarg = (struct spawn_arg*)stack; if (!stack) { stackSize = stackSize > DEFAULT_STACK_SIZE ? stackSize : DEFAULT_STACK_SIZE; stack = (void*)malloc(stackSize); sarg = (struct spawn_arg*)stack; sarg->need_free = 1; } else { sarg->need_free = 0; } sarg->sched = sched; sarg->proc = proc; sarg->arg = arg; stack = (char*)stack + sizeof(struct spawn_arg); stackSize -= sizeof(struct spawn_arg); co_context_t ctx = co_create(co_sched_warp, sarg, stack, stackSize); ctx->flags = 0; co_sched_append(sched, ctx); }
inline void create(void (*entrypoint)(), unsigned frequency) { if(thread) co_delete(thread); thread = co_create(65536 * sizeof(void*), entrypoint); this->frequency = frequency; clock = 0; }