-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_pool.c
65 lines (50 loc) · 1.56 KB
/
queue_pool.c
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
#include "queue_pool.h"
#include <stdlib.h>
#include <string.h>
#include "util.h"
static trie_node *_get_trie_node(trie_node *, char *);
static void _free_trie_node(trie_node *);
static trie_node *_get_trie_node(trie_node *root, char *name) {
trie_node *child;
unsigned char first = *name;
if (first == '\0') return root;
child = root->children[first];
if (child == NULL) {
child = malloc(sizeof(*child));
bzero(child, sizeof(*child));
pthread_mutex_init(&child->mutex, NULL);
root->children[first] = child;
}
return _get_trie_node(child, name + 1);
}
static void _free_trie_node(trie_node *root) {
if (root == NULL) return;
for (int i = 0; i < 256; i++)
_free_trie_node(root->children[i]);
if (root->q != NULL)
free_queue(root->q);
free(root);
}
void init_queue_pool(queue_pool *pool) {
bzero(pool, sizeof(*pool));
pthread_mutex_init(&pool->mutex, NULL);
}
queue *create_queue(queue_pool *pool, char *name) {
trie_node *node;
pthread_mutex_lock(&pool->mutex);
node = _get_trie_node(pool, name);
pthread_mutex_unlock(&pool->mutex);
pthread_mutex_lock(&node->mutex);
if (node->q == NULL) node->q = new_queue(name);
pthread_mutex_unlock(&node->mutex);
return node->q;
}
queue *get_queue(queue_pool *pool, char *name) {
trie_node *node = _get_trie_node(pool, name);
return node->q;
}
void free_pool(queue_pool *pool) {
for (int i = 0; i < 256; i++)
if (pool->children[i] != NULL)
_free_trie_node(pool->children[i]);
}