diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-01-22 00:35:37 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-01-22 00:35:37 +0100 |
commit | 542bf871c6eebdc645884960559e15ce400939e6 (patch) | |
tree | 0ce113fefdf83c653c715edb49fd217bad8dcccf /src/util/scheduler.c | |
parent | 9855137d1312c0a3b3d0836b6c0ab2eb199d70ca (diff) | |
download | gnunet-542bf871c6eebdc645884960559e15ce400939e6.tar.gz gnunet-542bf871c6eebdc645884960559e15ce400939e6.zip |
extending scheduler API with 'add_at' functionality to avoid unnecessary time conversions
Diffstat (limited to 'src/util/scheduler.c')
-rw-r--r-- | src/util/scheduler.c | 85 |
1 files changed, 67 insertions, 18 deletions
diff --git a/src/util/scheduler.c b/src/util/scheduler.c index b016b91b7..409a0942f 100644 --- a/src/util/scheduler.c +++ b/src/util/scheduler.c | |||
@@ -155,7 +155,7 @@ struct GNUNET_SCHEDULER_Task | |||
155 | * Is this task run on shutdown? | 155 | * Is this task run on shutdown? |
156 | */ | 156 | */ |
157 | int on_shutdown; | 157 | int on_shutdown; |
158 | 158 | ||
159 | /** | 159 | /** |
160 | * Is this task in the ready list? | 160 | * Is this task in the ready list? |
161 | */ | 161 | */ |
@@ -490,7 +490,7 @@ check_ready (const struct GNUNET_NETWORK_FDSet *rs, | |||
490 | 490 | ||
491 | 491 | ||
492 | /** | 492 | /** |
493 | * Request the shutdown of a scheduler. Marks all tasks | 493 | * Request the shutdown of a scheduler. Marks all tasks |
494 | * awaiting shutdown as ready. Note that tasks | 494 | * awaiting shutdown as ready. Note that tasks |
495 | * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call | 495 | * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call |
496 | * will be delayed until the next shutdown signal. | 496 | * will be delayed until the next shutdown signal. |
@@ -534,13 +534,13 @@ destroy_task (struct GNUNET_SCHEDULER_Task *t) | |||
534 | * Output stack trace of task @a t. | 534 | * Output stack trace of task @a t. |
535 | * | 535 | * |
536 | * @param t task to dump stack trace of | 536 | * @param t task to dump stack trace of |
537 | */ | 537 | */ |
538 | static void | 538 | static void |
539 | dump_backtrace (struct GNUNET_SCHEDULER_Task *t) | 539 | dump_backtrace (struct GNUNET_SCHEDULER_Task *t) |
540 | { | 540 | { |
541 | #if EXECINFO | 541 | #if EXECINFO |
542 | unsigned int i; | 542 | unsigned int i; |
543 | 543 | ||
544 | for (i = 0; i < t->num_backtrace_strings; i++) | 544 | for (i = 0; i < t->num_backtrace_strings; i++) |
545 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 545 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
546 | "Task %p trace %u: %s\n", | 546 | "Task %p trace %u: %s\n", |
@@ -1080,10 +1080,10 @@ GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback tas | |||
1080 | 1080 | ||
1081 | 1081 | ||
1082 | /** | 1082 | /** |
1083 | * Schedule a new task to be run with a specified delay. The task | 1083 | * Schedule a new task to be run at the specified time. The task |
1084 | * will be scheduled for execution once the delay has expired. | 1084 | * will be scheduled for execution at time @a at. |
1085 | * | 1085 | * |
1086 | * @param delay when should this operation time out? | 1086 | * @param at time when the operation should run |
1087 | * @param priority priority to use for the task | 1087 | * @param priority priority to use for the task |
1088 | * @param task main function of the task | 1088 | * @param task main function of the task |
1089 | * @param task_cls closure of @a task | 1089 | * @param task_cls closure of @a task |
@@ -1091,10 +1091,10 @@ GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback tas | |||
1091 | * only valid until @a task is started! | 1091 | * only valid until @a task is started! |
1092 | */ | 1092 | */ |
1093 | struct GNUNET_SCHEDULER_Task * | 1093 | struct GNUNET_SCHEDULER_Task * |
1094 | GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay, | 1094 | GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at, |
1095 | enum GNUNET_SCHEDULER_Priority priority, | 1095 | enum GNUNET_SCHEDULER_Priority priority, |
1096 | GNUNET_SCHEDULER_TaskCallback task, | 1096 | GNUNET_SCHEDULER_TaskCallback task, |
1097 | void *task_cls) | 1097 | void *task_cls) |
1098 | { | 1098 | { |
1099 | struct GNUNET_SCHEDULER_Task *t; | 1099 | struct GNUNET_SCHEDULER_Task *t; |
1100 | struct GNUNET_SCHEDULER_Task *pos; | 1100 | struct GNUNET_SCHEDULER_Task *pos; |
@@ -1110,12 +1110,13 @@ GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay, | |||
1110 | #if PROFILE_DELAYS | 1110 | #if PROFILE_DELAYS |
1111 | t->start_time = GNUNET_TIME_absolute_get (); | 1111 | t->start_time = GNUNET_TIME_absolute_get (); |
1112 | #endif | 1112 | #endif |
1113 | t->timeout = GNUNET_TIME_relative_to_absolute (delay); | 1113 | t->timeout = at; |
1114 | t->priority = priority; | 1114 | t->priority = priority; |
1115 | t->lifeness = current_lifeness; | 1115 | t->lifeness = current_lifeness; |
1116 | /* try tail first (optimization in case we are | 1116 | /* try tail first (optimization in case we are |
1117 | * appending to a long list of tasks with timeouts) */ | 1117 | * appending to a long list of tasks with timeouts) */ |
1118 | if (0 == delay.rel_value_us) | 1118 | if ( (NULL == pending_timeout_head) || |
1119 | (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) ) | ||
1119 | { | 1120 | { |
1120 | GNUNET_CONTAINER_DLL_insert (pending_timeout_head, | 1121 | GNUNET_CONTAINER_DLL_insert (pending_timeout_head, |
1121 | pending_timeout_tail, | 1122 | pending_timeout_tail, |
@@ -1144,9 +1145,9 @@ GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay, | |||
1144 | pending_timeout_tail, | 1145 | pending_timeout_tail, |
1145 | prev, | 1146 | prev, |
1146 | t); | 1147 | t); |
1147 | /* finally, update heuristic insertion point to last insertion... */ | ||
1148 | pending_timeout_last = t; | ||
1149 | } | 1148 | } |
1149 | /* finally, update heuristic insertion point to last insertion... */ | ||
1150 | pending_timeout_last = t; | ||
1150 | 1151 | ||
1151 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1152 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1152 | "Adding task: %p\n", | 1153 | "Adding task: %p\n", |
@@ -1157,6 +1158,30 @@ GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay, | |||
1157 | 1158 | ||
1158 | 1159 | ||
1159 | /** | 1160 | /** |
1161 | * Schedule a new task to be run with a specified delay. The task | ||
1162 | * will be scheduled for execution once the delay has expired. | ||
1163 | * | ||
1164 | * @param delay when should this operation time out? | ||
1165 | * @param priority priority to use for the task | ||
1166 | * @param task main function of the task | ||
1167 | * @param task_cls closure of @a task | ||
1168 | * @return unique task identifier for the job | ||
1169 | * only valid until @a task is started! | ||
1170 | */ | ||
1171 | struct GNUNET_SCHEDULER_Task * | ||
1172 | GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay, | ||
1173 | enum GNUNET_SCHEDULER_Priority priority, | ||
1174 | GNUNET_SCHEDULER_TaskCallback task, | ||
1175 | void *task_cls) | ||
1176 | { | ||
1177 | return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay), | ||
1178 | priority, | ||
1179 | task, | ||
1180 | task_cls); | ||
1181 | } | ||
1182 | |||
1183 | |||
1184 | /** | ||
1160 | * Schedule a new task to be run with a specified priority. | 1185 | * Schedule a new task to be run with a specified priority. |
1161 | * | 1186 | * |
1162 | * @param prio how important is the new task? | 1187 | * @param prio how important is the new task? |
@@ -1178,6 +1203,29 @@ GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio, | |||
1178 | 1203 | ||
1179 | 1204 | ||
1180 | /** | 1205 | /** |
1206 | * Schedule a new task to be run at the specified time. The task | ||
1207 | * will be scheduled for execution once specified time has been | ||
1208 | * reached. It will be run with the DEFAULT priority. | ||
1209 | * | ||
1210 | * @param at time at which this operation should run | ||
1211 | * @param task main function of the task | ||
1212 | * @param task_cls closure of @a task | ||
1213 | * @return unique task identifier for the job | ||
1214 | * only valid until @a task is started! | ||
1215 | */ | ||
1216 | struct GNUNET_SCHEDULER_Task * | ||
1217 | GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at, | ||
1218 | GNUNET_SCHEDULER_TaskCallback task, | ||
1219 | void *task_cls) | ||
1220 | { | ||
1221 | return GNUNET_SCHEDULER_add_at_with_priority (at, | ||
1222 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
1223 | task, | ||
1224 | task_cls); | ||
1225 | } | ||
1226 | |||
1227 | |||
1228 | /** | ||
1181 | * Schedule a new task to be run with a specified delay. The task | 1229 | * Schedule a new task to be run with a specified delay. The task |
1182 | * will be scheduled for execution once the delay has expired. It | 1230 | * will be scheduled for execution once the delay has expired. It |
1183 | * will be run with the DEFAULT priority. | 1231 | * will be run with the DEFAULT priority. |
@@ -1195,7 +1243,8 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay, | |||
1195 | { | 1243 | { |
1196 | return GNUNET_SCHEDULER_add_delayed_with_priority (delay, | 1244 | return GNUNET_SCHEDULER_add_delayed_with_priority (delay, |
1197 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, | 1245 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, |
1198 | task, task_cls); | 1246 | task, |
1247 | task_cls); | ||
1199 | } | 1248 | } |
1200 | 1249 | ||
1201 | 1250 | ||
@@ -1391,7 +1440,7 @@ add_without_sets (struct GNUNET_TIME_Relative delay, | |||
1391 | * scheduled for execution once either the delay has expired or the | 1440 | * scheduled for execution once either the delay has expired or the |
1392 | * socket operation is ready. It will be run with the DEFAULT priority. | 1441 | * socket operation is ready. It will be run with the DEFAULT priority. |
1393 | * | 1442 | * |
1394 | * @param delay when should this operation time out? | 1443 | * @param delay when should this operation time out? |
1395 | * @param rfd read file-descriptor | 1444 | * @param rfd read file-descriptor |
1396 | * @param task main function of the task | 1445 | * @param task main function of the task |
1397 | * @param task_cls closure of @a task | 1446 | * @param task_cls closure of @a task |
@@ -1640,7 +1689,7 @@ GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay, | |||
1640 | * </code> | 1689 | * </code> |
1641 | * | 1690 | * |
1642 | * @param prio how important is this task? | 1691 | * @param prio how important is this task? |
1643 | * @param delay how long should we wait? | 1692 | * @param delay how long should we wait? |
1644 | * @param rs set of file descriptors we want to read (can be NULL) | 1693 | * @param rs set of file descriptors we want to read (can be NULL) |
1645 | * @param ws set of file descriptors we want to write (can be NULL) | 1694 | * @param ws set of file descriptors we want to write (can be NULL) |
1646 | * @param task main function of the task | 1695 | * @param task main function of the task |