int rain_exit(struct rain_ctx *ctx,int code) { if(!ctx){ return RAIN_ERROR; } if(__sync_bool_compare_and_swap(&ctx->bmain,true,true)){ if(__sync_bool_compare_and_swap(&ctx->bexit,false,true)){ ctx->exit_code = code; rain_ctx_unref(ctx); return RAIN_OK; } } return RAIN_ERROR; }
int rain_handle_push_message(rain_routine_t dest, struct rain_ctx_message msg) { if(rain_handle_get_localid(dest) == RAIN_OK){ struct rain_ctx * destctx = rain_handle_query_ctx(dest,true); if(destctx){ int ret = rain_ctx_push_message(destctx,msg); rain_ctx_unref(destctx); return ret; } return RAIN_ERROR; }else{ return RAIN_ERROR; } }
struct rain_ctx * rain_ctx_new(rain_routine_t prid, const char * mod_name,const char *args) { if(IS_FULL(H)){ return NULL; } struct rain_moudle *mod = rain_module_query(mod_name); if(!mod){ RAIN_LOG(0,"MODE_QUERY:modname:%s",mod_name); return NULL; } //INIT struct rain_ctx *ctx = malloc(sizeof(struct rain_ctx)); ctx->mod = mod; ctx->bdis = 0; ctx->recv = NULL; ctx->recv_rsp = NULL; ctx->link = NULL; ctx->session = 0; ctx->timeoutfn = NULL; ctx->nexttickfn = NULL; rain_mutex_init(&ctx->mtx); wod_array_init(&ctx->arr,sizeof(rain_routine_t)); ctx->msgQue = rain_message_queue_new(); ctx->bmain = false; _ctx_genid(ctx); ctx->ref = 1; ctx->bexit = 0; ctx->prid = prid; ctx->arg = rain_module_instance_init(mod,ctx,args); //EXEC; if(ctx->arg == NULL){ RAIN_LOG(0,"RAIN_MAIN_FIALED:modname:%s args:%s",mod_name,args); rain_ctx_unref(ctx); return NULL; } __sync_bool_compare_and_swap(&ctx->bmain,false,true); if(rain_message_queue_size(ctx->msgQue) > 0){ if(__sync_bool_compare_and_swap(&ctx->bdis,0,1)){ rain_life_queue_push(ctx->rid); } } RAIN_LOG(0,"LAUNCH.ctx(%x.%s).arguments:%s",ctx->rid,mod_name,args); return ctx; }
static int rain_dipatch_routine(void) { rain_routine_t rid; int ret = rain_life_queue_pop(&rid); if(ret == RAIN_OK){ struct rain_ctx * ctx = rain_handle_query_ctx(rid,false); if(ctx){ ret = rain_ctx_run(ctx); rain_ctx_unref(ctx); if(ret == RAIN_OK){ rain_life_queue_push(rid); } }else{ RAIN_LOG(0,"UNKNOW CTX %x\n",rid); } return RAIN_OK; } return RAIN_ERROR; }
int rain_link(rain_ctx_t *ctx,routine_t rid) { if(!ctx){ return RAIN_ERROR; } if(rain_ctx_handle_local(rid) == RAIN_OK ){ if(rain_ctx_getid(ctx) == rid){ return RAIN_ERROR; } rain_ctx_t *dest_ctx = rain_ctx_handle_query(rid,true); if(dest_ctx){ int ret = rain_ctx_addlink(dest_ctx,rain_ctx_getid(ctx)); rain_ctx_unref(dest_ctx); return ret; }else{ return RAIN_ERROR; } }else{ //TODO return RAIN_ERROR; } }