-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirewall.c
More file actions
77 lines (59 loc) · 1.64 KB
/
Copy pathfirewall.c
File metadata and controls
77 lines (59 loc) · 1.64 KB
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
// SPDX-License-Identifier: BSD-3-Clause
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>
#include "ring_buffer.h"
#include "consumer.h"
#include "producer.h"
#include "log/log.h"
#include "packet.h"
#include "utils.h"
#define SO_RING_SZ (PKT_SZ * 1000)
pthread_mutex_t MUTEX_LOG;
void log_lock(bool lock, void *udata)
{
pthread_mutex_t *LOCK = (pthread_mutex_t *) udata;
if (lock)
pthread_mutex_lock(LOCK);
else
pthread_mutex_unlock(LOCK);
}
void __attribute__((constructor)) init()
{
pthread_mutex_init(&MUTEX_LOG, NULL);
log_set_lock(log_lock, &MUTEX_LOG);
}
void __attribute__((destructor)) dest()
{
pthread_mutex_destroy(&MUTEX_LOG);
}
int main(int argc, char **argv)
{
so_ring_buffer_t ring_buffer;
int num_consumers, threads, rc, i;
pthread_t *thread_ids = NULL;
if (argc < 4) {
fprintf(stderr, "Usage %s <input-file> <output-file> <num-consumers:1-32>\n", argv[0]);
exit(EXIT_FAILURE);
}
rc = ring_buffer_init(&ring_buffer, SO_RING_SZ);
DIE(rc < 0, "ring_buffer_init");
num_consumers = strtol(argv[3], NULL, 10);
if (num_consumers <= 0 || num_consumers > 32) {
fprintf(stderr, "num-consumers [%d] must be in the interval [1-32]\n", num_consumers);
exit(EXIT_FAILURE);
}
thread_ids = calloc(num_consumers, sizeof(pthread_t));
DIE(thread_ids == NULL, "calloc pthread_t");
/* create consumer threads */
threads = create_consumers(thread_ids, num_consumers, &ring_buffer, argv[2]);
/* start publishing data */
publish_data(&ring_buffer, argv[1]);
(void) threads;
for (i = 0; i < threads; i++)
pthread_join(thread_ids[i], NULL);
free(thread_ids);
return 0;
}