1 /* Copyright (c) 2009 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
26 #include "poll-loop.h"
28 static bool ovsdb_trigger_try(struct ovsdb *db, struct ovsdb_trigger *,
30 static void ovsdb_trigger_complete(struct ovsdb_trigger *);
33 ovsdb_trigger_init(struct ovsdb *db, struct ovsdb_trigger *trigger,
34 struct json *request, struct list *completion,
37 list_push_back(&db->triggers, &trigger->node);
38 trigger->completion = completion;
39 trigger->request = request;
40 trigger->result = NULL;
41 trigger->created = now;
42 trigger->timeout_msec = LLONG_MAX;
43 ovsdb_trigger_try(db, trigger, now);
47 ovsdb_trigger_destroy(struct ovsdb_trigger *trigger)
49 list_remove(&trigger->node);
50 json_destroy(trigger->request);
51 json_destroy(trigger->result);
55 ovsdb_trigger_is_complete(const struct ovsdb_trigger *trigger)
57 return trigger->result != NULL;
61 ovsdb_trigger_steal_result(struct ovsdb_trigger *trigger)
63 struct json *result = trigger->result;
64 trigger->result = NULL;
69 ovsdb_trigger_run(struct ovsdb *db, long long int now)
71 struct ovsdb_trigger *t, *next;
74 run_triggers = db->run_triggers;
75 db->run_triggers = false;
76 LIST_FOR_EACH_SAFE (t, next, struct ovsdb_trigger, node, &db->triggers) {
77 if (run_triggers || now - t->created >= t->timeout_msec) {
78 ovsdb_trigger_try(db, t, now);
84 ovsdb_trigger_wait(struct ovsdb *db, long long int now)
86 if (db->run_triggers) {
87 poll_immediate_wake();
89 long long int deadline = LLONG_MAX;
90 struct ovsdb_trigger *t;
92 LIST_FOR_EACH (t, struct ovsdb_trigger, node, &db->triggers) {
93 if (t->created < LLONG_MAX - t->timeout_msec) {
94 long long int t_deadline = t->created + t->timeout_msec;
95 if (deadline > t_deadline) {
96 deadline = t_deadline;
97 if (now >= deadline) {
104 if (deadline < LLONG_MAX) {
105 poll_timer_wait(MIN(deadline - now, INT_MAX));
111 ovsdb_trigger_try(struct ovsdb *db, struct ovsdb_trigger *t, long long int now)
113 t->result = ovsdb_execute(db, t->request, now - t->created,
116 ovsdb_trigger_complete(t);
124 ovsdb_trigger_complete(struct ovsdb_trigger *t)
126 assert(t->result != NULL);
127 list_remove(&t->node);
128 list_push_back(t->completion, &t->node);