aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/worker_queue.c
blob: c83274681d775792817433d4456080fce3fdcd0d (plain) (blame)
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
#include "worker_queue.h"
#include "log.h"

#include <stdlib.h>
#include <sys/queue.h>
#include <unistd.h>

int worker_queue_entry_create(struct worker_queue_entry **entry, int fd)
{
	int newfd = dup(fd);

	if (newfd < 0) {
		print_errno("malloc");
		return -1;
	}

	*entry = malloc(sizeof(struct worker_queue_entry));
	if (!*entry) {
		print_errno("malloc");
		goto close_newfd;
	}
	(*entry)->fd = newfd;

	return 0;

close_newfd:
	check_errno(close(newfd), "close");

	return -1;
}

void worker_queue_entry_destroy(struct worker_queue_entry *entry)
{
	check_errno(close(entry->fd), "close");
	free(entry);
}

void worker_queue_create(struct worker_queue *queue)
{
	STAILQ_INIT(queue);
}

void worker_queue_destroy(struct worker_queue *queue)
{
	struct worker_queue_entry *entry1, *entry2;

	entry1 = STAILQ_FIRST(queue);
	while (entry1) {
		entry2 = STAILQ_NEXT(entry1, entries);
		worker_queue_entry_destroy(entry1);
		entry1 = entry2;
	}
	STAILQ_INIT(queue);
}

int worker_queue_is_empty(const struct worker_queue *queue)
{
	return STAILQ_EMPTY(queue);
}

void worker_queue_push(struct worker_queue *queue, struct worker_queue_entry *entry)
{
	STAILQ_INSERT_TAIL(queue, entry, entries);
}

void worker_queue_push_head(struct worker_queue *queue, struct worker_queue_entry *entry)
{
	STAILQ_INSERT_HEAD(queue, entry, entries);
}

struct worker_queue_entry *worker_queue_pop(struct worker_queue *queue)
{
	struct worker_queue_entry *entry;

	entry = STAILQ_FIRST(queue);
	STAILQ_REMOVE_HEAD(queue, entries);

	return entry;
}