国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

skynet學習之skynet_handle

2019-11-11 03:27:52
字體:
來源:轉載
供稿:網友

今天學習skynet_handle文件。

這個文件是管理句柄的。建立skynet_context與skynet_handle之間的唯一映射關系。

即一個sc對應一個唯一的handle。

先看初始化:

void skynet_handle_init(int harbor) {	assert(H==NULL);	struct handle_storage * s = skynet_malloc(sizeof(*H));	s->slot_size = DEFAULT_SLOT_SIZE;	s->slot = skynet_malloc(s->slot_size * sizeof(struct skynet_context *));	memset(s->slot, 0, s->slot_size * sizeof(struct skynet_context *));	rwlock_init(&s->lock);	// reserve 0 for system	s->harbor = (uint32_t) (harbor & 0xff) << HANDLE_REMOTE_SHIFT;	s->handle_index = 1;	s->name_cap = 2;	s->name_count = 0;	s->name = skynet_malloc(s->name_cap * sizeof(struct handle_name));	H = s;	// Don't need to free H}

生成一個handle_storage對象即句柄倉庫結構體。初始化分配DEFAULT_SLOT_SIZE個元素的slot數組。

生成harbor的ID,只能占用高8位。

初始化name數組,暫時不清楚用處,猜測可能是建立一個字符串名與handle之間的映射方便查詢吧。

sc被存放在一個數組中,數組的下標選取是通過hash來計算的。

uint32_tskynet_handle_register(struct skynet_context *ctx) {	struct handle_storage *s = H;	rwlock_wlock(&s->lock);		for (;;) {		int i;		for (i=0;i<s->slot_size;i++) {			uint32_t handle = (i+s->handle_index) & HANDLE_MASK;//HANDLE_MASK用于取低24位,高8位是遠程節點ID			int hash = handle & (s->slot_size-1);//保證下標不會超過孔位數量。孔位數量始終是2的N次方。			if (s->slot[hash] == NULL) {				s->slot[hash] = ctx;				s->handle_index = handle + 1;//單調遞增,假設了最多不會超過0xffffff個。				rwlock_wunlock(&s->lock);				handle |= s->harbor;				return handle;			}		}		assert((s->slot_size*2 - 1) <= HANDLE_MASK);		struct skynet_context ** new_slot = skynet_malloc(s->slot_size * 2 * sizeof(struct skynet_context *));//雙倍擴充容量		memset(new_slot, 0, s->slot_size * 2 * sizeof(struct skynet_context *));		for (i=0;i<s->slot_size;i++) {			int hash = skynet_context_handle(s->slot[i]) & (s->slot_size * 2 - 1);			assert(new_slot[hash] == NULL);			new_slot[hash] = s->slot[i];		}		skynet_free(s->slot);		s->slot = new_slot;		s->slot_size *= 2;	}}

存儲好后,其他業務就可以用handle來尋找sc了。

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);	}	rwlock_runlock(&s->lock);	return result;}當然,還有回收釋放handle的
intskynet_handle_retire(uint32_t handle)

每個sc在生成的時候,都會調用skynet_handle_register來進行注冊。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临高县| 钟祥市| 西充县| 普兰店市| 通州区| 博罗县| 岱山县| 浦江县| 辽中县| 盐津县| 克什克腾旗| 报价| 武陟县| 昌吉市| 易门县| 石泉县| 古交市| 拜泉县| 辉县市| 金坛市| 新龙县| 湘西| 乌鲁木齐市| 平乡县| 呈贡县| 高州市| 乌兰浩特市| 松阳县| 临桂县| 哈尔滨市| 观塘区| 房产| 都匀市| 宣汉县| 光泽县| 察隅县| 嵩明县| 泉州市| 会昌县| 如东县| 青龙|