| 221 | } |
| 222 | |
| 223 | static int |
| 224 | taskqueue_enqueue_locked(struct taskqueue *queue, struct task *task) |
| 225 | { |
| 226 | struct task *ins; |
| 227 | struct task *prev; |
| 228 | |
| 229 | KASSERT(task->ta_func != NULL, ("enqueueing task with NULL func")); |
| 230 | /* |
| 231 | * Count multiple enqueues. |
| 232 | */ |
| 233 | if (task->ta_pending) { |
| 234 | if (task->ta_pending < USHRT_MAX) |
| 235 | task->ta_pending++; |
| 236 | TQ_UNLOCK(queue); |
| 237 | return (0); |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Optimise cases when all tasks use small set of priorities. |
| 242 | * In case of only one priority we always insert at the end. |
| 243 | * In case of two tq_hint typically gives the insertion point. |
| 244 | * In case of more then two tq_hint should halve the search. |
| 245 | */ |
| 246 | prev = STAILQ_LAST(&queue->tq_queue, task, ta_link); |
| 247 | if (!prev || prev->ta_priority >= task->ta_priority) { |
| 248 | STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link); |
| 249 | } else { |
| 250 | prev = queue->tq_hint; |
| 251 | if (prev && prev->ta_priority >= task->ta_priority) { |
| 252 | ins = STAILQ_NEXT(prev, ta_link); |
| 253 | } else { |
| 254 | prev = NULL; |
| 255 | ins = STAILQ_FIRST(&queue->tq_queue); |
| 256 | } |
| 257 | for (; ins; prev = ins, ins = STAILQ_NEXT(ins, ta_link)) |
| 258 | if (ins->ta_priority < task->ta_priority) |
| 259 | break; |
| 260 | |
| 261 | if (prev) { |
| 262 | STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link); |
| 263 | queue->tq_hint = task; |
| 264 | } else |
| 265 | STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link); |
| 266 | } |
| 267 | |
| 268 | task->ta_pending = 1; |
| 269 | if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) != 0) |
| 270 | TQ_UNLOCK(queue); |
| 271 | if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0) |
| 272 | queue->tq_enqueue(queue->tq_context); |
| 273 | if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) == 0) |
| 274 | TQ_UNLOCK(queue); |
| 275 | |
| 276 | /* Return with lock released. */ |
| 277 | return (0); |
| 278 | } |
| 279 | |
| 280 | int |
no outgoing calls
no test coverage detected