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 220
|
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
* LADI Session Handler (ladish)
*
* Copyright (C) 2009,2010,2011 Nedko Arnaudov <nedko@arnaudov.name>
*
**************************************************************************
* This file contains interface to the command queue
**************************************************************************
*
* LADI Session Handler is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* LADI Session Handler 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
* or write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "cmd.h"
#include "control.h"
void ladish_cqueue_init(struct ladish_cqueue * queue_ptr)
{
queue_ptr->cancel = false;
INIT_LIST_HEAD(&queue_ptr->queue);
}
void ladish_cqueue_run(struct ladish_cqueue * queue_ptr)
{
struct list_head * node_ptr;
struct ladish_command * cmd_ptr;
loop:
if (list_empty(&queue_ptr->queue))
{
return;
}
node_ptr = queue_ptr->queue.next;
cmd_ptr = list_entry(node_ptr, struct ladish_command, siblings);
ASSERT(cmd_ptr->run != NULL);
ASSERT(cmd_ptr->state == LADISH_COMMAND_STATE_PENDING || cmd_ptr->state == LADISH_COMMAND_STATE_WAITING);
if (cmd_ptr->state == LADISH_COMMAND_STATE_PENDING)
{ /* if this is a new command, put a separator so its impact is clearly visible in the log */
log_info("-------");
}
if (!cmd_ptr->run(cmd_ptr->context))
{
ladish_cqueue_clear(queue_ptr);
emit_queue_execution_halted();
return;
}
switch (cmd_ptr->state)
{
case LADISH_COMMAND_STATE_DONE:
break;
case LADISH_COMMAND_STATE_WAITING:
return;
default:
log_error("unexpected cmd state %u after run()", cmd_ptr->state);
ASSERT_NO_PASS;
ladish_cqueue_clear(queue_ptr);
emit_queue_execution_halted();
return;
}
list_del(node_ptr);
if (cmd_ptr->destructor != NULL)
{
cmd_ptr->destructor(cmd_ptr->context);
}
free(cmd_ptr);
if (queue_ptr->cancel && list_empty(&queue_ptr->queue))
{
queue_ptr->cancel = false;
}
goto loop;
}
void ladish_cqueue_cancel(struct ladish_cqueue * queue_ptr)
{
struct list_head * node_ptr;
struct ladish_command * cmd_ptr;
if (list_empty(&queue_ptr->queue))
{ /* nothing to cancel */
return;
}
node_ptr = queue_ptr->queue.next; /* current command */
cmd_ptr = list_entry(node_ptr, struct ladish_command, siblings);
if (cmd_ptr->state != LADISH_COMMAND_STATE_WAITING)
{
ladish_cqueue_clear(queue_ptr);
return;
}
/* clear all commands except currently waiting one */
while (queue_ptr->queue.next != queue_ptr->queue.prev)
{
node_ptr = queue_ptr->queue.prev;
list_del(node_ptr);
cmd_ptr = list_entry(node_ptr, struct ladish_command, siblings);
if (cmd_ptr->destructor != NULL)
{
cmd_ptr->destructor(cmd_ptr->context);
}
free(cmd_ptr);
}
queue_ptr->cancel = true;
cmd_ptr->cancel = true;
}
bool ladish_cqueue_add_command(struct ladish_cqueue * queue_ptr, struct ladish_command * cmd_ptr)
{
ASSERT(cmd_ptr->run != NULL);
if (queue_ptr->cancel)
{
return false;
}
ASSERT(cmd_ptr->state == LADISH_COMMAND_STATE_PREPARE);
cmd_ptr->state = LADISH_COMMAND_STATE_PENDING;
list_add_tail(&cmd_ptr->siblings, &queue_ptr->queue);
return true;
}
void ladish_cqueue_clear(struct ladish_cqueue * queue_ptr)
{
struct list_head * node_ptr;
struct ladish_command * cmd_ptr;
while (!list_empty(&queue_ptr->queue))
{
node_ptr = queue_ptr->queue.next;
list_del(node_ptr);
cmd_ptr = list_entry(node_ptr, struct ladish_command, siblings);
if (cmd_ptr->destructor != NULL)
{
cmd_ptr->destructor(cmd_ptr->context);
}
free(cmd_ptr);
}
queue_ptr->cancel = false;
}
void ladish_cqueue_drop_command(struct ladish_cqueue * queue_ptr)
{
struct list_head * node_ptr;
struct ladish_command * cmd_ptr;
ASSERT(!list_empty(&queue_ptr->queue)); /* there is nothing to drop */
/* remove the last command from the queue */
node_ptr = queue_ptr->queue.prev;
list_del(node_ptr);
cmd_ptr = list_entry(node_ptr, struct ladish_command, siblings);
ASSERT(cmd_ptr->run != NULL);
/* waiting commands are not supposed to be dropped */
/* commands that are not yet prepared and those that are already processed are not supposed to be in the queue */
ASSERT(cmd_ptr->state == LADISH_COMMAND_STATE_PENDING);
if (cmd_ptr->destructor != NULL)
{
cmd_ptr->destructor(cmd_ptr->context);
}
free(cmd_ptr);
}
void * ladish_command_new(size_t size)
{
struct ladish_command * cmd_ptr;
ASSERT(size >= sizeof(struct ladish_command));
cmd_ptr = malloc(size);
if (cmd_ptr == NULL)
{
log_error("malloc failed to allocate command with size %zu", size);
return NULL;
}
cmd_ptr->state = LADISH_COMMAND_STATE_PREPARE;
cmd_ptr->cancel = false;
cmd_ptr->context = cmd_ptr;
cmd_ptr->run = NULL;
cmd_ptr->destructor = NULL;
return cmd_ptr;
}
|