// 通过handle获取skynet_context, skynet_context的引用计数加1 struct skynet_context * skynet_handle_grab(uint32_t handle) { struct handle_storage *s = H; struct skynet_context * result = NULL; rwlock_rlock(&s->lock); uint32_t hash = handle & (s->slot_size-1); struct skynet_context * ctx = s->slot[hash]; if (ctx && skynet_context_handle(ctx) == handle) { result = ctx; skynet_context_grab(result); // __sync_add_and_fetch(&ctx->ref,1); skynet_context引用计数加1 } rwlock_runlock(&s->lock); return result; }
struct skynet_context * skynet_handle_grab(uint32_t handle) { struct handle_storage *s = H; struct skynet_context * result = NULL; // 保证线程安全 rwlock_rlock(&s->lock); uint32_t hash = handle & (s->slot_size - 1); struct skynet_context * ctx = s->slot[hash]; if (ctx // 1. 判断是否在同一个节点 // 2. 确认 handle 是没有超出 slot_size 的, 因为可能 slot_size = 8, handle = 15, skynet_context_handle(ctx) = 7 // 3. 其他线程没有修改 ctx && skynet_context_handle(ctx) == handle) { result = ctx; skynet_context_grab(result); } rwlock_runlock(&s->lock); return result; }