| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <proton/object.h> |
| 23 |
#include <proton/reactor.h> |
| 24 |
#include <assert.h> |
| 25 |
|
| 26 |
struct pn_task_t { |
| 27 |
pn_list_t *pool; |
| 28 |
pn_record_t *attachments; |
| 29 |
pn_timestamp_t deadline; |
| 30 |
bool cancelled; |
| 31 |
}; |
| 32 |
|
| 33 |
void pn_task_initialize(pn_task_t *task) { |
| 34 |
task->pool = NULL; |
| 35 |
task->attachments = pn_record(); |
| 36 |
task->deadline = 0; |
| 37 |
task->cancelled = false; |
| 38 |
} |
| 39 |
|
| 40 |
void pn_task_finalize(pn_task_t *task) { |
| 41 |
// if we are the last reference to the pool then don't put ourselves |
| 42 |
// into it |
| 43 |
if (task->pool && pn_refcount(task->pool) > 1) { |
| 44 |
pn_record_clear(task->attachments); |
| 45 |
pn_list_add(task->pool, task); |
| 46 |
pn_decref(task->pool); |
| 47 |
task->pool = NULL; |
| 48 |
} else { |
| 49 |
pn_decref(task->pool); |
| 50 |
pn_decref(task->attachments); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
intptr_t pn_task_compare(pn_task_t *a, pn_task_t *b) { |
| 55 |
return a->deadline - b->deadline; |
| 56 |
} |
| 57 |
|
| 58 |
#define pn_task_inspect NULL |
| 59 |
#define pn_task_hashcode NULL |
| 60 |
|
| 61 |
PN_CLASSDEF(pn_task) |
| 62 |
|
| 63 |
pn_task_t *pn_task(void) { |
| 64 |
pn_task_t *task = pn_task_new(); |
| 65 |
return task; |
| 66 |
} |
| 67 |
|
| 68 |
pn_record_t *pn_task_attachments(pn_task_t *task) { |
| 69 |
assert(task); |
| 70 |
return task->attachments; |
| 71 |
} |
| 72 |
|
| 73 |
void pn_task_cancel(pn_task_t *task) { |
| 74 |
assert(task); |
| 75 |
task->cancelled = true; |
| 76 |
} |
| 77 |
|
| 78 |
// |
| 79 |
// timer |
| 80 |
// |
| 81 |
|
| 82 |
struct pn_timer_t { |
| 83 |
pn_list_t *pool; |
| 84 |
pn_list_t *tasks; |
| 85 |
pn_collector_t *collector; |
| 86 |
}; |
| 87 |
|
| 88 |
static void pn_timer_initialize(pn_timer_t *timer) { |
| 89 |
timer->pool = pn_list(PN_OBJECT, 0); |
| 90 |
timer->tasks = pn_list(PN_OBJECT, 0); |
| 91 |
} |
| 92 |
|
| 93 |
static void pn_timer_finalize(pn_timer_t *timer) { |
| 94 |
pn_decref(timer->pool); |
| 95 |
pn_free(timer->tasks); |
| 96 |
} |
| 97 |
|
| 98 |
#define pn_timer_inspect NULL |
| 99 |
#define pn_timer_compare NULL |
| 100 |
#define pn_timer_hashcode NULL |
| 101 |
|
| 102 |
PN_CLASSDEF(pn_timer) |
| 103 |
|
| 104 |
pn_timer_t *pn_timer(pn_collector_t *collector) { |
| 105 |
pn_timer_t *timer = pn_timer_new(); |
| 106 |
timer->collector = collector; |
| 107 |
return timer; |
| 108 |
} |
| 109 |
|
| 110 |
pn_task_t *pn_timer_schedule(pn_timer_t *timer, pn_timestamp_t deadline) { |
| 111 |
pn_task_t *task = (pn_task_t *) pn_list_pop(timer->pool); |
| 112 |
if (!task) { |
| 113 |
task = pn_task(); |
| 114 |
} |
| 115 |
task->pool = timer->pool; |
| 116 |
pn_incref(task->pool); |
| 117 |
task->deadline = deadline; |
| 118 |
task->cancelled = false; |
| 119 |
pn_list_minpush(timer->tasks, task); |
| 120 |
pn_decref(task); |
| 121 |
return task; |
| 122 |
} |
| 123 |
|
| 124 |
void pni_timer_flush_cancelled(pn_timer_t *timer) { |
| 125 |
while (pn_list_size(timer->tasks)) { |
| 126 |
pn_task_t *task = (pn_task_t *) pn_list_get(timer->tasks, 0); |
| 127 |
if (task->cancelled) { |
| 128 |
pn_task_t *min = (pn_task_t *) pn_list_minpop(timer->tasks); |
| 129 |
assert(min == task); |
| 130 |
pn_decref(min); |
| 131 |
} else { |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
pn_timestamp_t pn_timer_deadline(pn_timer_t *timer) { |
| 138 |
assert(timer); |
| 139 |
pni_timer_flush_cancelled(timer); |
| 140 |
if (pn_list_size(timer->tasks)) { |
| 141 |
pn_task_t *task = (pn_task_t *) pn_list_get(timer->tasks, 0); |
| 142 |
return task->deadline; |
| 143 |
} else { |
| 144 |
return 0; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void pn_timer_tick(pn_timer_t *timer, pn_timestamp_t now) { |
| 149 |
assert(timer); |
| 150 |
while (pn_list_size(timer->tasks)) { |
| 151 |
pn_task_t *task = (pn_task_t *) pn_list_get(timer->tasks, 0); |
| 152 |
if (now >= task->deadline) { |
| 153 |
pn_task_t *min = (pn_task_t *) pn_list_minpop(timer->tasks); |
| 154 |
assert(min == task); |
| 155 |
if (!min->cancelled) |
| 156 |
pn_collector_put(timer->collector, PN_OBJECT, min, PN_TIMER_TASK); |
| 157 |
pn_decref(min); |
| 158 |
} else { |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
int pn_timer_tasks(pn_timer_t *timer) { |
| 165 |
assert(timer); |
| 166 |
pni_timer_flush_cancelled(timer); |
| 167 |
return pn_list_size(timer->tasks); |
| 168 |
} |