/* 构建定时器对象, 包括分配内存、初始化触发列表集、初始化锁并将当前时间 time 置为 0. * 此函数返回初始化好的定时器对象. */ static struct timer * timer_create_timer() { struct timer *r=(struct timer *)skynet_malloc(sizeof(struct timer)); /* 将 time 初始化为 0 */ memset(r,0,sizeof(*r)); int i,j; /* 初始化最近的触发列表集 */ for (i=0;i<TIME_NEAR;i++) { link_clear(&r->near[i]); } /* 初始化 4 级触发时间较远的触发列表集 */ for (i=0;i<4;i++) { for (j=0;j<TIME_LEVEL;j++) { link_clear(&r->t[i][j]); } } SPIN_INIT(r) /* current 以及除了 time 以外的其它时间字段还会进一步初始化 */ r->current = 0; return r; }
static void timer_execute(struct timer *T) { while (__sync_lock_test_and_set(&T->lock,1)) {}; int idx=T->time & TIME_NEAR_MASK; struct timer_node *current; int mask,i,time; while (T->near[idx].head.next) { current=link_clear(&T->near[idx]); do { struct timer_event * event = (struct timer_event *)(current+1); struct skynet_message message; message.source = SKYNET_SYSTEM_TIMER; message.session = event->session; message.data = NULL; message.sz = 0; skynet_context_push(event->handle, &message); struct timer_node * temp = current; current=current->next; free(temp); } while (current); } ++T->time; mask = TIME_NEAR; time = T->time >> TIME_NEAR_SHIFT; i=0; while ((T->time & (mask-1))==0) { idx=time & TIME_LEVEL_MASK; if (idx!=0) { --idx; current=link_clear(&T->t[i][idx]); while (current) { struct timer_node *temp=current->next; add_node(T,current); current=temp; } break; } mask <<= TIME_LEVEL_SHIFT; time >>= TIME_LEVEL_SHIFT; ++i; } __sync_lock_release(&T->lock); }
static void timer_shift(struct timer *T) { LOCK(T); int mask = TIME_NEAR; int time = (++T->time) >> TIME_NEAR_SHIFT; int i=0; while ((T->time & (mask-1))==0) { int idx=time & TIME_LEVEL_MASK; if (idx!=0) { --idx; struct timer_node *current = link_clear(&T->t[i][idx]); while (current) { struct timer_node *temp=current->next; add_node(T,current); current=temp; } break; } mask <<= TIME_LEVEL_SHIFT; time >>= TIME_LEVEL_SHIFT; ++i; } UNLOCK(T); }
static inline void timer_execute(struct timer *T) { int idx = T->time & TIME_NEAR_MASK; while (T->near[idx].head.next) { struct timer_node *current = link_clear(&T->near[idx]); UNLOCK(T); // dispatch_list don't need lock T do { struct timer_event * event = (struct timer_event *)(current+1); T->dispather(event->ud, event->data, event->sz); struct timer_node * temp = current; current=current->next; if(event->period > 0) { //update expired temp->next = NULL; temp->expire = TI->time + event->period; add_node(TI, temp ); } else { MM_FREE(temp); } } while (current); LOCK(T); } }
static void move_list(struct timer *T, int level, int idx) { struct timer_node *current = link_clear(&T->t[level][idx]); while (current) { struct timer_node *temp=current->next; add_node(T,current); current=temp; } }
/* 分发此时到期的定时事件, 此函数会以线程安全的方式不断监测是否有新添加进来的当前时间就过期的定时事件. * 如果检查到就一并分发, 如果检查不到就会留至下一次分发时分发. */ static inline void timer_execute(struct timer *T) { int idx = T->time & TIME_NEAR_MASK; while (T->near[idx].head.next) { struct timer_node *current = link_clear(&T->near[idx]); SPIN_UNLOCK(T); // dispatch_list don't need lock T dispatch_list(current); SPIN_LOCK(T); } }
static struct timer * timer_create_timer() { struct timer *r=(struct timer *)malloc(sizeof(struct timer)); memset(r,0,sizeof(*r)); int i,j; for (i=0;i<TIME_NEAR;i++) { link_clear(&r->near[i]); } for (i=0;i<4;i++) { for (j=0;j<TIME_LEVEL-1;j++) { link_clear(&r->t[i][j]); } } r->lock = 0; r->current = 0; return r; }
static inline void timer_execute(struct timer *T) { int idx = T->time & TIME_NEAR_MASK; while (T->near[idx].head.next) { struct timer_node *current = link_clear(&T->near[idx]); do { struct timer_event * event = (struct timer_event *)(current+1); struct skynet_message message; message.source = 0; message.session = event->session; message.data = NULL; message.sz = PTYPE_RESPONSE << HANDLE_REMOTE_SHIFT; skynet_context_push(event->handle, &message); struct timer_node * temp = current; current=current->next; skynet_free(temp); } while (current); } }