1 /*-
2 * Copyright (c) 2014 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * todo-list scheduler
27 * ===================
28 *
29 * This is a simple "TODO-list" scheduler for calling things at certain
30 * times. Jobs can be one-shot or repeated and repeated jobs can abort.
31 *
32 * For ease of debugging, TODO jobs have a name.
33 */
34
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #include "ntimed.h"
40
41 struct todo {
42 unsigned magic;
43 #define TODO_MAGIC 0x5279009a
44 TAILQ_ENTRY(todo) list;
45
46 todo_f *func;
47 void *priv;
48 struct timestamp when;
49 double repeat;
50
51 char what[40];
52 };
53
54 struct todolist {
55 unsigned magic;
56 #define TODOLIST_MAGIC 0x7db66255
57 TAILQ_HEAD(,todo) todolist;
58 };
59
60 struct todolist *
TODO_NewList(void)61 TODO_NewList(void)
62 {
63 struct todolist *tdl;
64
65 ALLOC_OBJ(tdl, TODOLIST_MAGIC);
66 AN(tdl);
67 TAILQ_INIT(&tdl->todolist);
68 return (tdl);
69 }
70
71 static void
todo_insert(struct todolist * tdl,struct todo * tp)72 todo_insert(struct todolist *tdl, struct todo *tp)
73 {
74 struct todo *tp1;
75
76 TAILQ_FOREACH(tp1, &tdl->todolist, list) {
77 if (TS_Diff(&tp1->when, &tp->when) > 0.0) {
78 TAILQ_INSERT_BEFORE(tp1, tp, list);
79 return;
80 }
81 }
82 TAILQ_INSERT_TAIL(&tdl->todolist, tp, list);
83 }
84
85 /**********************************************************************
86 */
87
88 void
TODO_Cancel(struct todolist * tdl,uintptr_t * tp)89 TODO_Cancel(struct todolist *tdl, uintptr_t *tp)
90 {
91 struct todo *tp2;
92
93 CHECK_OBJ_NOTNULL(tdl, TODOLIST_MAGIC);
94 AN(tp);
95 AN(*tp);
96
97 TAILQ_FOREACH(tp2, &tdl->todolist, list)
98 if ((uintptr_t)tp2 == *tp)
99 break;
100 CHECK_OBJ_NOTNULL(tp2, TODO_MAGIC);
101 TAILQ_REMOVE(&tdl->todolist, tp2, list);
102 FREE_OBJ(tp2);
103 *tp = 0;
104 }
105
106 /**********************************************************************
107 */
108
109 uintptr_t
TODO_ScheduleAbs(struct todolist * tdl,todo_f * func,void * priv,const struct timestamp * when,double repeat,const char * fmt,...)110 TODO_ScheduleAbs(struct todolist *tdl, todo_f *func, void *priv,
111 const struct timestamp *when, double repeat, const char *fmt, ...)
112 {
113 struct todo *tp;
114 va_list ap;
115
116 CHECK_OBJ_NOTNULL(tdl, TODOLIST_MAGIC);
117 AN(func);
118 CHECK_OBJ_NOTNULL(when, TIMESTAMP_MAGIC);
119 assert(repeat >= 0.0);
120 AN(fmt);
121
122 ALLOC_OBJ(tp, TODO_MAGIC);
123 AN(tp);
124 tp->func = func;
125 tp->priv = priv;
126 tp->when = *when;
127 tp->repeat = repeat;
128 va_start(ap, fmt);
129 (void)vsnprintf(tp->what, sizeof tp->what, fmt, ap);
130 va_end(ap);
131 todo_insert(tdl, tp);
132 return ((uintptr_t)tp);
133 }
134
135 uintptr_t
TODO_ScheduleRel(struct todolist * tdl,todo_f * func,void * priv,double when,double repeat,const char * fmt,...)136 TODO_ScheduleRel(struct todolist *tdl, todo_f *func, void *priv,
137 double when, double repeat, const char *fmt, ...)
138 {
139 struct todo *tp;
140 va_list ap;
141
142 CHECK_OBJ_NOTNULL(tdl, TODOLIST_MAGIC);
143 AN(func);
144 assert(when >= 0.0);
145 assert(repeat >= 0.0);
146 AN(fmt);
147
148 ALLOC_OBJ(tp, TODO_MAGIC);
149 AN(tp);
150 tp->func = func;
151 tp->priv = priv;
152 TB_Now(&tp->when);
153 TS_Add(&tp->when, when);
154 tp->repeat = repeat;
155 va_start(ap, fmt);
156 (void)vsnprintf(tp->what, sizeof tp->what, fmt, ap);
157 va_end(ap);
158 todo_insert(tdl, tp);
159 return ((uintptr_t)tp);
160 }
161
162 /**********************************************************************
163 * Schedule TODO list until failure or empty
164 */
165
166 enum todo_e
TODO_Run(struct ocx * ocx,struct todolist * tdl)167 TODO_Run(struct ocx *ocx, struct todolist *tdl)
168 {
169 struct todo *tp;
170 enum todo_e ret = TODO_OK;
171 char buf[40];
172 int i;
173
174 CHECK_OBJ_NOTNULL(tdl, TODOLIST_MAGIC);
175 while(!TAILQ_EMPTY(&tdl->todolist)) {
176 tp = TAILQ_FIRST(&tdl->todolist);
177 i = TS_SleepUntil(&tp->when);
178 if (i == 1)
179 return (TODO_INTR);
180 AZ(i);
181 TS_Format(buf, sizeof buf, &tp->when);
182 Put(ocx, OCX_TRACE, "Now %s %s\n", buf, tp->what);
183 ret = tp->func(ocx, tdl, tp->priv);
184 if (ret == TODO_FAIL)
185 break;
186 if (ret == TODO_DONE || tp->repeat == 0.0) {
187 TAILQ_REMOVE(&tdl->todolist, tp, list);
188 FREE_OBJ(tp);
189 } else if (ret == TODO_OK) {
190 TS_Add(&tp->when, tp->repeat);
191 TAILQ_REMOVE(&tdl->todolist, tp, list);
192 todo_insert(tdl, tp);
193 } else {
194 WRONG("Invalid Return from todo->func");
195 }
196 }
197 return (ret);
198 }
199