Directory: | src/ |
---|---|
File: | src/worker_queue.c |
Date: | 2024-04-25 03:45:42 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 32 | 36 | 88.9% |
Branches: | 5 | 10 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2023 Egor Tensin <egor@tensin.name> | ||
3 | * This file is part of the "cimple" project. | ||
4 | * For details, see https://github.com/egor-tensin/cimple. | ||
5 | * Distributed under the MIT License. | ||
6 | */ | ||
7 | |||
8 | #include "worker_queue.h" | ||
9 | #include "log.h" | ||
10 | #include "net.h" | ||
11 | |||
12 | #include <stdlib.h> | ||
13 | #include <sys/queue.h> | ||
14 | |||
15 | struct worker { | ||
16 | int fd; | ||
17 | SIMPLEQ_ENTRY(worker) entries; | ||
18 | }; | ||
19 | |||
20 | 9234 | int worker_create(struct worker **_entry, int fd) | |
21 | { | ||
22 | 9234 | struct worker *entry = malloc(sizeof(struct worker)); | |
23 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9234 times.
|
9234 | if (!entry) { |
24 | ✗ | log_errno("malloc"); | |
25 | ✗ | return -1; | |
26 | } | ||
27 | |||
28 | 9234 | entry->fd = fd; | |
29 | |||
30 | 9234 | *_entry = entry; | |
31 | 9234 | return 0; | |
32 | } | ||
33 | |||
34 | 9234 | void worker_destroy(struct worker *entry) | |
35 | { | ||
36 | 9234 | net_close(entry->fd); | |
37 | 9234 | free(entry); | |
38 | 9234 | } | |
39 | |||
40 | 36774 | int worker_get_fd(const struct worker *entry) | |
41 | { | ||
42 | 36774 | return entry->fd; | |
43 | } | ||
44 | |||
45 | 29 | void worker_queue_create(struct worker_queue *queue) | |
46 | { | ||
47 | 29 | SIMPLEQ_INIT(queue); | |
48 | 29 | } | |
49 | |||
50 | 29 | void worker_queue_destroy(struct worker_queue *queue) | |
51 | { | ||
52 | 29 | struct worker *entry1 = SIMPLEQ_FIRST(queue); | |
53 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 29 times.
|
83 | while (entry1) { |
54 | 54 | struct worker *entry2 = SIMPLEQ_NEXT(entry1, entries); | |
55 | 54 | worker_destroy(entry1); | |
56 | 54 | entry1 = entry2; | |
57 | } | ||
58 | 29 | SIMPLEQ_INIT(queue); | |
59 | 29 | } | |
60 | |||
61 | 27255 | int worker_queue_is_empty(const struct worker_queue *queue) | |
62 | { | ||
63 | 27255 | return SIMPLEQ_EMPTY(queue); | |
64 | } | ||
65 | |||
66 | ✗ | void worker_queue_add_first(struct worker_queue *queue, struct worker *entry) | |
67 | { | ||
68 | ✗ | SIMPLEQ_INSERT_HEAD(queue, entry, entries); | |
69 | } | ||
70 | |||
71 | 9234 | void worker_queue_add_last(struct worker_queue *queue, struct worker *entry) | |
72 | { | ||
73 | 9234 | SIMPLEQ_INSERT_TAIL(queue, entry, entries); | |
74 | 9234 | } | |
75 | |||
76 | 9180 | struct worker *worker_queue_remove_first(struct worker_queue *queue) | |
77 | { | ||
78 | 9180 | struct worker *entry = SIMPLEQ_FIRST(queue); | |
79 |
2/2✓ Branch 0 taken 9098 times.
✓ Branch 1 taken 82 times.
|
9180 | SIMPLEQ_REMOVE_HEAD(queue, entries); |
80 | 9180 | return entry; | |
81 | } | ||
82 |