📄 dce_timer.c
字号:
/***************************************Ho Chi Minh City University of Technology Computer Science Department Distributed Computing Environment (DCE)Written by Nguyen Van Noi -59406114File dce_timer.c on Mon Nov 9 22:11:00 1998****************************************/#include <stdlib.h>#include <signal.h>#include "dce_timer.h"#define SZT sizeof(struct timeout_q)typedef struct timeout_q *p_timeout_q;static int id = 0;static struct timeout_q *head = 0;static int in_callout = 0;intdce_setalarm (){ if (sigset (SIGALRM, dce_timer_handle) == SIG_ERR) { return 0; } alarm (1); return 1;}voiddce_timer_handle (){ p_timeout_q p = head->link; alarm (1); if (in_callout) return; in_callout = 1; while (p) { if (!p->time) { if (p->func) p->func (p->data); head->link = p->link; free (p); p = head->link; } else { p->time--; in_callout = 0; return; } } in_callout = 0; return;}intdce_timer_init (){ head = (p_timeout_q) malloc (SZT); if (!head) { return 0; } head->id = -1; head->link = 0; dce_setalarm (); return 1;}voiddce_timer_free (){ p_timeout_q p = head; while (p) { head = head->link; free (p); p = head; }}intdce_settimer (int delay, cfunc_t action, char *data){ return dce_timer_settimer (delay, action, data);}intdce_timer_settimer (int delay, cfunc_t action, char *data){ p_timeout_q p, node, prev;/*if (in_callout) return -1; in_callout=1; */ node = (p_timeout_q) malloc (SZT); if (!node) { /*in_callout=0; */ return -1; } node->func = action; node->data = data; node->time = delay; node->link = 0; node->id = ++id; prev = head; p = head->link; while (p) { if (delay < p->time) { node->link = p; prev->link = node; p->time -= node->time; /*in_callout=0; */ return node->id; } else { delay -= p->time; node->time = delay; prev = p; p = p->link; } } prev->link = node;/*in_callout=0; */ return node->id;}voiddce_killtimer (int timer_id){ dce_timer_killtimer (timer_id);}voiddce_timer_killtimer (int timer_id){ p_timeout_q p, prev, pt; if (in_callout) return; if (!timer_id) return; in_callout = 1; prev = head; p = prev->link; while (p) { if (p->id == timer_id) { pt = p->link; prev->link = pt; if (pt) pt->time += p->time; free (p); in_callout = 0; return; } else { prev = p; p = p->link; } } in_callout = 0;}voiddce_timerzero (void *p){ int *pi = (int *) p; *pi = 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -