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