struct link {
struct link *next;
};
void link_init(struct link *link) {
link->next = NULL;
}
void link_add(struct link *link, struct link *it) {
it->next = link->next;
link->next = it;
}
struct link *link_del(struct link *link) {
if (!link->next) return NULL;
struct link *it = link->next;
link->next = it->next;
return it;
}
typedef void (*func)(void *arg);
struct tw_node {
struct link link;
int32_t expire_tick;
int32_t period_ticks;
int flags;
func cb;
void *arg;
bool need_free;
};
struct tw {
uint32_t tick_ms;
uint32_t cur_tick;
struct link slots[TW_LEVEL][TW_SIZE];
pthread_spinlock_t lock;
};
struct tw_node *tw_node_new(struct tw *tw, int expire_ms, int period_ms, int flags, func cb, void *arg, bool need_free) {
if ((expire_ms < TICK_MS) || (period_ms < TICK_MS)) {
return NULL;
}
struct tw_node *node = (struct tw_node*)malloc(sizeof(struct tw_node));
if (!node) return NULL;
memset(node, 0, sizeof(struct tw_node));
node->expire_tick = MS_TO_TICKS(expire_ms, tw->tick_ms);
node->period_ticks = MS_TO_TICKS(period_ms, tw->tick_ms);
node->flags = flags;
node->cb = cb;
node->arg = arg;
node->need_free = need_free;
return node;
}
void tw_node_free(struct tw_node *node) {
if (node->arg && node->need_free) {
uint32_t *task_id = (uint32_t *)node->arg;
printf("free task id:%u node\n", *task_id);
free(node->arg);
}
free(node);
}
void tw_init(struct tw *tw, uint32_t tick_ms) {
memset(tw, 0, sizeof(struct tw));
tw->tick_ms = tick_ms;
pthread_spin_init(&tw->lock, PTHREAD_PROCESS_PRIVATE);
}
void tw_free(struct tw *tw) {
pthread_spin_lock(&tw->lock);
for (uint32_t i = 0; i < TW_LEVEL; i++) {
for (uint32_t j = 0; j < TW_SIZE; j++) {
struct link *link = &tw->slots[i][j];
struct link *it;
while(it = link_del(link)) {
printf("free i:%u, j:%u\n", i, j);
struct tw_node *node = (struct tw_node *)it;
tw_node_free(node);
}
}
}
pthread_spin_unlock(&tw->lock);
pthread_spin_destroy(&tw->lock);
}
void tw_add(struct tw *tw, struct tw_node *node, bool nolock) {
if (!nolock) pthread_spin_lock(&tw->lock);
uint32_t expire_tick = node->expire_tick;
node->expire_tick += tw->cur_tick;
printf("tw add cur tick:%u, period ticks:%u, old expire tick:%u, node expire tick:%u\n",
tw->cur_tick,
node->period_ticks,
expire_tick,
node->expire_tick);
uint8_t idx0 = IDX0(tw->cur_tick);
uint8_t idx1 = IDX1(tw->cur_tick);
uint8_t idx2 = IDX2(tw->cur_tick);
uint8_t e0 = IDX0(node->expire_tick);
uint8_t e1 = IDX1(node->expire_tick);
uint8_t e2 = IDX2(node->expire_tick);
struct link *it = &node->link;
if (e2 != idx2) {
struct link *link = &tw->slots[2][e2];
link_add(link, it);
} else if (e1 != idx1) {
struct link *link = &tw->slots[1][e1];
link_add(link, it);
} else if (e0 != idx0){
struct link *link = &tw->slots[0][e0];
link_add(link, it);
}
if (!nolock) pthread_spin_unlock(&tw->lock);
}
int tw_update(struct tw *tw) {
pthread_spin_lock(&tw->lock);
tw->cur_tick++;
uint8_t idx0 = IDX0(tw->cur_tick);
uint8_t idx1 = IDX1(tw->cur_tick);
uint8_t idx2 = IDX2(tw->cur_tick);
struct link active = {0};
if ((idx0 == 0) && (idx1 == 0) && (idx2 > 0)) {
struct link *link = &tw->slots[2][idx2];
struct link *it;
while(it = link_del(link)) {
struct tw_node *node = (struct tw_node *)it;
uint8_t e0 = IDX0(node->expire_tick);
uint8_t e1 = IDX1(node->expire_tick);
if ((e0 == 0) && (e1 == 0)) {
link_add(&active, it);
} else if (e1 > 0) {
struct link *l = &tw->slots[1][e1];
link_add(l, it);
} else {
struct link *l = &tw->slots[0][e0];
link_add(l, it);
}
}
} else if ((idx0 == 0) && (idx1 > 0)) {
struct link *link = &tw->slots[1][idx1];
struct link *it;
while(it = link_del(link)) {
struct tw_node *node = (struct tw_node *)it;
uint8_t e0 = IDX0(node->expire_tick);
if (e0 == 0) {
link_add(&active, it);
} else {
struct link *l = &tw->slots[0][e0];
link_add(l, it);
}
}
} else if (idx0 > 0) {
struct link *link = &tw->slots[0][idx0];
struct link *it;
while(it = link_del(link)) {
link_add(&active, it);
}
}
struct link *it;
while(it = link_del(&active)) {
struct tw_node *node = (struct tw_node *)it;
node->cb(node->arg);
if (node->flags & PERIOD_FLAG) {
node->expire_tick = node->period_ticks;
tw_add(tw, node, true);
} else {
tw_node_free(node);
}
}
pthread_spin_unlock(&tw->lock);
return 0;
}
void get_time(char *buffer) {
struct timeval tv;
gettimeofday(&tv, NULL);
strftime(buffer, 40, "%Y-%m-%d %H:%M:%S", localtime(&tv.tv_sec));
sprintf(buffer + strlen(buffer), ".%03ld", tv.tv_usec / 1000);
}
void *tw_driver_thread(void *arg) {
struct tw *tw = (struct tw *)arg;
int efd = epoll_create(1024);
if (efd == -1) {
perror("epoll create error");
return NULL;
}
struct epoll_event ev, events[MAX_EVENTS];
while(1) {
usleep(TICK_MS * 1000);
tw_update(tw);
int nfds = epoll_wait(efd, events, MAX_EVENTS, TICK_MS);
if (nfds == -1) {
perror("epoll wait error");
break;
}
tw_update(tw);
}
}
struct skill {
char skill_name[20];
char ch;
int cool_time;
int duration_time;
bool ready;
};
struct hero {
char hero_name[20];
struct skill Q;
struct skill W;
struct skill E;
struct skill R;
};
void skill_task(void *arg) {
struct skill *sk = (struct skill *)arg;
sk->ready = true;
char buf[40] = {0};
get_time(buf);
printf("%s %s 完成冷却!\n", buf, sk->skill_name);
}
void *task_scheduler_thread(void *arg) {
struct tw *tw = (struct tw *)arg;
struct hero yi = (struct hero) {
.hero_name = "易大师",
.Q = {.skill_name = "Q技能", .ch = 'q', .cool_time = 14, .duration_time = 1, .ready = true},
.W = {.skill_name = "W技能", .ch = 'w', .cool_time = 35, .duration_time = 5, .ready = true},
.E = {.skill_name = "E技能", .ch = 'e', .cool_time = 14, .duration_time = 5, .ready = true},
.R = {.skill_name = "R技能", .ch = 'r', .cool_time = 75, .duration_time = 10, .ready = true},
};
while(1) {
int ch = getchar();
if (ch == '\n') continue;
switch (ch) {
case 'q':
if (yi.Q.ready == false) {
printf("%s %s 冷却中......\n", yi.hero_name, yi.Q.skill_name);
} else {
char buf[40] = {0};
get_time(buf);
printf("%s %s 施放 %s QQQQQQQQQQQQQQQ\n", buf, yi.hero_name, yi.Q.skill_name);
yi.Q.ready = false;
struct tw_node *node = tw_node_new(tw, yi.Q.cool_time * 1000, 1000, ONESHOT_FLAG, skill_task, (void *)&yi.Q, false);
if (!node) {
printf("tw node new error\n");
break;
}
tw_add(tw, node, false);
}
break;
case 'w':
if (yi.W.ready == false) {
printf("%s %s 冷却中......\n", yi.hero_name, yi.W.skill_name);
} else {
char buf[40] = {0};
get_time(buf);
printf("%s %s 施放 %s WWWWWWWWWWWWWW\n", buf, yi.hero_name, yi.W.skill_name);
yi.W.ready = false;
struct tw_node *node = tw_node_new(tw, yi.W.cool_time * 1000, 1000, ONESHOT_FLAG, skill_task, (void *)&yi.W, false);
if (!node) {
printf("tw node new error\n");
break;
}
tw_add(tw, node, false);
}
break;
case 'e':
if (yi.E.ready == false) {
printf("%s %s 冷却中......\n", yi.hero_name, yi.E.skill_name);
} else {
char buf[40] = {0};
get_time(buf);
yi.E.ready = false;
printf("%s %s 施放 %s EEEEEEEEEEEEEE\n", buf, yi.hero_name, yi.E.skill_name);
struct tw_node *node = tw_node_new(tw, yi.E.cool_time * 1000, 1000, ONESHOT_FLAG, skill_task, (void *)&yi.E, false);
if (!node) {
printf("tw node new error\n");
break;
}
tw_add(tw, node, false);
}
break;
case 'r':
if (yi.R.ready == false) {
printf("%s %s 冷却中......\n", yi.hero_name, yi.R.skill_name);
} else {
char buf[40] = {0};
get_time(buf);
printf("%s %s 施放 %s RRRRRRRRRRRRRRR\n", buf, yi.hero_name, yi.R.skill_name);
yi.R.ready = false;
struct tw_node *node = tw_node_new(tw, yi.R.cool_time * 1000, 1000, ONESHOT_FLAG, skill_task, (void *)&yi.R, false);
if (!node) {
printf("tw node new error\n");
break;
}
tw_add(tw, node, false);
}
break;
default:
printf("xxx无效技能:%c\n", ch);
break;
}
}
return NULL;
}
int main(int argc, char *argv[]) {
struct tw tw;
tw_init(&tw, TICK_MS);
pthread_t th1;
pthread_create(&th1, NULL, task_scheduler_thread, (void *)&tw);
pthread_t th2;
pthread_create(&th2, NULL, tw_driver_thread, (void *)&tw);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
printf("start free timewheel\n");
tw_free(&tw);
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
- 264.
- 265.
- 266.
- 267.
- 268.
- 269.
- 270.
- 271.
- 272.
- 273.
- 274.
- 275.
- 276.
- 277.
- 278.
- 279.
- 280.
- 281.
- 282.
- 283.
- 284.
- 285.
- 286.
- 287.
- 288.
- 289.
- 290.
- 291.
- 292.
- 293.
- 294.
- 295.
- 296.
- 297.
- 298.
- 299.
- 300.
- 301.
- 302.
- 303.
- 304.
- 305.
- 306.
- 307.
- 308.
- 309.
- 310.
- 311.
- 312.
- 313.
- 314.
- 315.
- 316.
- 317.
- 318.
- 319.
- 320.
- 321.
- 322.
- 323.
- 324.
- 325.
- 326.
- 327.
- 328.
- 329.
- 330.
- 331.
- 332.
- 333.
- 334.
- 335.
- 336.
- 337.
- 338.
- 339.
- 340.
- 341.
- 342.
- 343.
- 344.
- 345.
- 346.
- 347.
- 348.
- 349.
- 350.
- 351.
- 352.
- 353.
- 354.
- 355.
- 356.
- 357.
- 358.
- 359.
- 360.
- 361.
- 362.
- 363.
- 364.
- 365.
- 366.
- 367.
- 368.
- 369.
- 370.
- 371.
- 372.
- 373.
- 374.
- 375.
- 376.
- 377.
- 378.
- 379.
- 380.
- 381.
- 382.
- 383.
- 384.
- 385.
- 386.
- 387.
- 388.
- 389.
- 390.
- 391.
- 392.
- 393.
- 394.
- 395.
- 396.