1 /*
2 * Copyright (C) 2010 Red Hat, Inc.
3 *
4 * Author: Angus Salkeld <asalkeld@redhat.com>
5 *
6 * This file is part of libqb.
7 *
8 * libqb is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * libqb is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with libqb. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "os_base.h"
22
23 #include <qb/qbdefs.h>
24 #include <qb/qblist.h>
25 #include <qb/qbloop.h>
26 #include "loop_int.h"
27 #include "util_int.h"
28
29 static struct qb_loop *default_intance = NULL;
30
31 static void
32 qb_loop_run_level(struct qb_loop_level *level)
33 {
34 struct qb_loop_item *job;
35 int32_t processed = 0;
36
37 Ill_have_another:
38
39 if (!qb_list_empty(&level->job_head)) {
40 job = qb_list_first_entry(&level->job_head, struct qb_loop_item, list);
41 qb_list_del(&job->list);
42 qb_list_init(&job->list);
43 job->source->dispatch_and_take_back(job, level->priority);
44 level->todo--;
45 processed++;
46 if (level->l->stop_requested) {
47 return;
48 }
49 if (processed < level->to_process) {
50 goto Ill_have_another;
51 }
52 }
53 }
54
55 void
56 qb_loop_level_item_add(struct qb_loop_level *level, struct qb_loop_item *job)
57 {
58 qb_list_init(&job->list);
59 qb_list_add_tail(&job->list, &level->job_head);
60 level->todo++;
61 }
62
63 void
64 qb_loop_level_item_del(struct qb_loop_level *level, struct qb_loop_item *job)
65 {
66 qb_list_del(&job->list);
67 qb_list_init(&job->list);
68 level->todo--;
69 }
70
71 struct qb_loop *
72 qb_loop_default_get(void)
73 {
74 return default_intance;
75 }
76
77 struct qb_loop *
78 qb_loop_create(void)
79 {
80 struct qb_loop *l = malloc(sizeof(struct qb_loop));
81 int32_t p;
82
83 if (l == NULL) {
84 return NULL;
85 }
86 for (p = QB_LOOP_LOW; p <= QB_LOOP_HIGH; p++) {
(1) Event mixed_enum_type: |
enumerated type mixed with another type |
(2) Event caretline: |
^ |
87 l->level[p].priority = p;
88 l->level[p].to_process = 4;
89 l->level[p].todo = 0;
90 l->level[p].l = l;
91
92 qb_list_init(&l->level[p].job_head);
93 qb_list_init(&l->level[p].wait_head);
94 }
95
96 l->stop_requested = QB_FALSE;
97 l->timer_source = qb_loop_timer_create(l);
98 l->job_source = qb_loop_jobs_create(l);
99 l->fd_source = qb_loop_poll_create(l);
100 l->signal_source = qb_loop_signals_create(l);
101
102 if (default_intance == NULL) {
103 default_intance = l;
104 }
105 return l;
106 }
107
108 void
109 qb_loop_destroy(struct qb_loop *l)
110 {
111 qb_loop_timer_destroy(l);
112 qb_loop_jobs_destroy(l);
113 qb_loop_poll_destroy(l);
114 qb_loop_signals_destroy(l);
115
116 if (default_intance == l) {
117 default_intance = NULL;
118 }
119 free(l);
120 }
121
122 void
123 qb_loop_stop(struct qb_loop *l)
124 {
125 if (l == NULL) {
126 default_intance->stop_requested = QB_TRUE;
127 } else {
128 l->stop_requested = QB_TRUE;
129 }
130 }
131
132 void
133 qb_loop_run(struct qb_loop *lp)
134 {
135 int32_t p;
136 int32_t p_stop = QB_LOOP_LOW;
137 int32_t rc;
138 int32_t remaining_todo = 0;
139 int32_t job_todo;
140 int32_t timer_todo;
141 int32_t ms_timeout;
142 struct qb_loop *l = lp;
143
144 if (l == NULL) {
145 l = default_intance;
146 }
147 l->stop_requested = QB_FALSE;
148
149 do {
150 if (p_stop == QB_LOOP_LOW) {
151 p_stop = QB_LOOP_HIGH;
152 } else {
153 p_stop--;
154 }
155
156 job_todo = 0;
157 if (l->job_source && l->job_source->poll) {
158 rc = l->job_source->poll(l->job_source, 0);
159 if (rc > 0) {
160 job_todo = rc;
161 } else if (rc == -1) {
162 errno = -rc;
163 qb_util_perror(LOG_WARNING, "job->poll");
164 }
165 }
166 timer_todo = 0;
167 if (l->timer_source && l->timer_source->poll) {
168 rc = l->timer_source->poll(l->timer_source, 0);
169 if (rc > 0) {
170 timer_todo = rc;
171 } else if (rc == -1) {
172 errno = -rc;
173 qb_util_perror(LOG_WARNING, "timer->poll");
174 }
175 }
176 if (remaining_todo > 0 || timer_todo > 0) {
177 /*
178 * if there are remaining todos or timer todos then don't wait.
179 */
180 ms_timeout = 0;
181 } else if (job_todo > 0) {
182 /*
183 * if we only have jobs to do (not timers or old todos)
184 * then set a non-zero timeout. Jobs can spin out of
185 * control if someone keeps adding them.
186 */
187 ms_timeout = 50;
188 } else {
189 if (l->timer_source) {
190 ms_timeout = qb_loop_timer_msec_duration_to_expire(l->timer_source);
191 } else {
192 ms_timeout = -1;
193 }
194 }
195 rc = l->fd_source->poll(l->fd_source, ms_timeout);
196 if (rc < 0) {
197 errno = -rc;
198 qb_util_perror(LOG_WARNING, "fd->poll");
199 }
200
201 remaining_todo = 0;
202 for (p = QB_LOOP_HIGH; p >= QB_LOOP_LOW; p--) {
203 if (p >= p_stop) {
204 qb_loop_run_level(&l->level[p]);
205 if (l->stop_requested) {
206 return;
207 }
208 }
209 remaining_todo += l->level[p].todo;
210 }
211 } while (!l->stop_requested);
212 }
213