Branch data Line data Source code
1 : : /* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
2 : : *
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:
6 : : *
7 : : * http://www.apache.org/licenses/LICENSE-2.0
8 : : *
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.
14 : : */
15 : :
16 : : #include <config.h>
17 : :
18 : : #include "trigger.h"
19 : :
20 : : #include <limits.h>
21 : :
22 : : #include "openvswitch/json.h"
23 : : #include "jsonrpc.h"
24 : : #include "ovsdb.h"
25 : : #include "poll-loop.h"
26 : : #include "server.h"
27 : :
28 : : static bool ovsdb_trigger_try(struct ovsdb_trigger *, long long int now);
29 : : static void ovsdb_trigger_complete(struct ovsdb_trigger *);
30 : :
31 : : void
32 : 12229 : ovsdb_trigger_init(struct ovsdb_session *session, struct ovsdb *db,
33 : : struct ovsdb_trigger *trigger,
34 : : struct json *request, long long int now,
35 : : bool read_only)
36 : : {
37 : 12229 : trigger->session = session;
38 : 12229 : trigger->db = db;
39 : 12229 : ovs_list_push_back(&trigger->db->triggers, &trigger->node);
40 : 12229 : trigger->request = request;
41 : 12229 : trigger->result = NULL;
42 : 12229 : trigger->created = now;
43 : 12229 : trigger->timeout_msec = LLONG_MAX;
44 : 12229 : trigger->read_only = read_only;
45 : 12229 : ovsdb_trigger_try(trigger, now);
46 : 12229 : }
47 : :
48 : : void
49 : 12229 : ovsdb_trigger_destroy(struct ovsdb_trigger *trigger)
50 : : {
51 : 12229 : ovs_list_remove(&trigger->node);
52 : 12229 : json_destroy(trigger->request);
53 : 12229 : json_destroy(trigger->result);
54 : 12229 : }
55 : :
56 : : bool
57 : 12229 : ovsdb_trigger_is_complete(const struct ovsdb_trigger *trigger)
58 : : {
59 : 12229 : return trigger->result != NULL;
60 : : }
61 : :
62 : : struct json *
63 : 12229 : ovsdb_trigger_steal_result(struct ovsdb_trigger *trigger)
64 : : {
65 : 12229 : struct json *result = trigger->result;
66 : 12229 : trigger->result = NULL;
67 : 12229 : return result;
68 : : }
69 : :
70 : : void
71 : 62997 : ovsdb_trigger_run(struct ovsdb *db, long long int now)
72 : : {
73 : : struct ovsdb_trigger *t, *next;
74 : : bool run_triggers;
75 : :
76 : 62997 : run_triggers = db->run_triggers;
77 : 62997 : db->run_triggers = false;
78 [ + + ][ + + ]: 63013 : LIST_FOR_EACH_SAFE (t, next, node, &db->triggers) {
79 [ + + ][ + + ]: 16 : if (run_triggers || now - t->created >= t->timeout_msec) {
80 : 6 : ovsdb_trigger_try(t, now);
81 : : }
82 : : }
83 : 62997 : }
84 : :
85 : : void
86 : 62997 : ovsdb_trigger_wait(struct ovsdb *db, long long int now)
87 : : {
88 [ + + ]: 62997 : if (db->run_triggers) {
89 : 4 : poll_immediate_wake();
90 : : } else {
91 : 62993 : long long int deadline = LLONG_MAX;
92 : : struct ovsdb_trigger *t;
93 : :
94 [ + + ]: 63003 : LIST_FOR_EACH (t, node, &db->triggers) {
95 [ + - ]: 10 : if (t->created < LLONG_MAX - t->timeout_msec) {
96 : 10 : long long int t_deadline = t->created + t->timeout_msec;
97 [ + + ]: 10 : if (deadline > t_deadline) {
98 : 8 : deadline = t_deadline;
99 [ - + ]: 8 : if (now >= deadline) {
100 : 0 : break;
101 : : }
102 : : }
103 : : }
104 : : }
105 : :
106 [ + + ]: 62993 : if (deadline < LLONG_MAX) {
107 : 8 : poll_timer_wait_until(deadline);
108 : : }
109 : : }
110 : 62997 : }
111 : :
112 : : static bool
113 : 12235 : ovsdb_trigger_try(struct ovsdb_trigger *t, long long int now)
114 : : {
115 : 12235 : t->result = ovsdb_execute(t->db, t->session,
116 : 24470 : t->request, t->read_only,
117 : 12235 : now - t->created, &t->timeout_msec);
118 [ + + ]: 12235 : if (t->result) {
119 : 12229 : ovsdb_trigger_complete(t);
120 : 12229 : return true;
121 : : } else {
122 : 6 : return false;
123 : : }
124 : : }
125 : :
126 : : static void
127 : 12229 : ovsdb_trigger_complete(struct ovsdb_trigger *t)
128 : : {
129 [ - + ]: 12229 : ovs_assert(t->result != NULL);
130 : 12229 : ovs_list_remove(&t->node);
131 : 12229 : ovs_list_push_back(&t->session->completions, &t->node);
132 : 12229 : }
|