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; }
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; }
cothread_t co_create(unsigned int size, void(*entrypoint)(void)) { unsigned long* handle = 0; if (!co_swap) co_init(); if (!co_active_handle) co_active_handle = &co_active_buffer; size += 256; size &= ~15; if ((handle = (unsigned long*)malloc(size))) { unsigned long* p = (unsigned long*)((unsigned char*)handle + size); handle[8] = (unsigned long)p; handle[9] = (unsigned long)entrypoint; } return handle; }
cothread_t co_create(unsigned int size, void (*entrypoint)(void)) { cothread_t handle; if(!co_swap) { co_init(); co_swap = (void (*)(cothread_t, cothread_t))co_swap_function; } if(!co_active_handle) co_active_handle = &co_active_buffer; size += 512; /* allocate additional space for storage */ size &= ~15; /* align stack to 16-byte boundary */ if(handle = (cothread_t)malloc(size)) { long long *p = (long long*)((char*)handle + size); /* seek to top of stack */ *--p = (long long)crash; /* crash if entrypoint returns */ *--p = (long long)entrypoint; /* start of function */ *(long long*)handle = (long long)p; /* stack pointer */ } return handle; }
int main(int argc, char *argv[]) { co_context_t *co; co = co_init(); co_run(co, main_thread, NULL); }
int main(int argc, char*argv[]) { int ret = 1; argp_program_version = "1.0"; static char doc[] = "Commotion Service Manager"; static struct argp_option options[] = { {"bind", 'b', "URI", 0, "commotiond management socket"}, {"nodaemon", 'n', 0, 0, "Do not fork into the background" }, {"out", 'o', "FILE", 0, "Output file to write services to when USR1 signal is received" }, {"pid", 'p', "FILE", 0, "Specify PID file"}, #ifdef USE_UCI {"uci", 'u', 0, 0, "Store service cache in UCI" }, #endif { 0 } }; /* Set defaults */ arguments.co_sock = DEFAULT_CO_SOCK; #ifdef USE_UCI arguments.uci = 0; #endif arguments.nodaemon = 0; arguments.output_file = DEFAULT_FILENAME; arguments.pid_file = PIDFILE; static struct argp argp = { options, parse_opt, NULL, doc }; argp_parse (&argp, argc, argv, 0, 0, &arguments); //fprintf(stdout,"uci: %d, out: %s\n",arguments.uci,arguments.output_file); if (!arguments.nodaemon) daemon_start(arguments.pid_file); CHECK(co_init(),"Failed to initialize Commotion client"); struct sigaction sa = {0}; sa.sa_handler = print_services; CHECK(sigaction(SIGUSR1,&sa,NULL) == 0, "Failed to set signal handler"); sa.sa_handler = shutdown; CHECK(sigaction(SIGINT,&sa,NULL) == 0, "Failed to set signal handler"); CHECK(sigaction(SIGTERM,&sa,NULL) == 0, "Failed to set signal handler"); /* Initialize the psuedo-RNG */ srand(time(NULL)); /* Allocate main loop object */ CHECK((simple_poll = avahi_simple_poll_new()),"Failed to create simple poll object."); /* Do not publish any local records */ avahi_server_config_init(&config); config.publish_hinfo = 0; config.publish_addresses = 0; config.publish_workstation = 0; config.publish_domain = 0; /* Set a unicast DNS server for wide area DNS-SD */ avahi_address_parse("192.168.50.1", AVAHI_PROTO_UNSPEC, &config.wide_area_servers[0]); config.n_wide_area_servers = 1; config.enable_wide_area = 1; // Start timer to create server struct timeval tv = {0}; avahi_elapse_time(&tv, 0, 0); avahi_simple_poll_get(simple_poll)->timeout_new(avahi_simple_poll_get(simple_poll), &tv, start_server, NULL); // create expiration event for service /* Run the main loop */ avahi_simple_poll_loop(simple_poll); ret = 0; error: /* Free the configuration data */ avahi_server_config_free(&config); co_shutdown(); /* Cleanup things */ if (stb) avahi_s_service_type_browser_free(stb); if (server) avahi_server_free(server); if (simple_poll) avahi_simple_poll_free(simple_poll); return ret; }