code:timeout - ikarishinjieva/unixV6-code-analyze-chs GitHub Wiki
- 向 事件执行队列(参看clock) 添加一个事件
- 输入3个参数 fun , arg , tim
- fun : 事件函数
- arg : 事件参数
- tim : 事件延迟时间
3833
3834 /*
3835 * timeout is called to arrange that
3836 * fun(arg) is called in tim/HZ seconds.
3837 * An entry is sorted into the callout
3838 * structure. The time in each structure
3839 * entry is the number of HZ’s more
3840 * than the previous entry.
3841 * In this way, decrementing the
3842 * first entry has the effect of
3843 * updating all entries.
3844 */
3845 timeout(fun, arg, tim)
3846 {
3847 register struct callo *p1, *p2;
3848 register t;
3849 int s;
3850
3851 t = tim;
3852 s = PS->integ;
3853 p1 = &callout[0];
3854 spl7();
3855 while(p1->c_func != 0 && p1->c_time <= t) {
- 关中断
- 防止 时钟中断 处理 事件执行队列
3856 t =- p1->c_time;
3857 p1++;
3858 }
3859 p1->c_time =- t;
3860 p2 = p1;
- 在事件执行队列中,寻找 插入事件 的合适位置
3861 while(p2->c_func != 0)
3862 p2++;
3863 while(p2 >= p1) {
3864 (p2+1)->c_time = p2->c_time;
3865 (p2+1)->c_func = p2->c_func;
3866 (p2+1)->c_arg = p2->c_arg;
3867 p2--;
3868 }
3869 p1->c_time = t;
3870 p1->c_func = fun;
3871 p1->c_arg = arg;
3872 PS->integ = s;
- 插入事件
3873 }
- 还原 PS
- 相当于 开中断
3874 /* ------------------------- */
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899