-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_send_recv.c
136 lines (122 loc) · 3 KB
/
main_send_recv.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "module_api_c.h"
#include "pmatomic.h"
#include <pthread.h>
#include <stdlib.h>
#include <uv.h>
#include <unistd.h>
#define XTM_FIFO_SIZE 16
struct msg {
pthread_t self;
unsigned long long counter;
};
static struct xtm_queue *q1;
static struct xtm_queue *q2;
static pthread_t t1;
static pthread_t t2;
static uv_loop_t *l1, *l2;
static void
alarm_sig_handler(int signum)
{
uv_stop(l1);
uv_stop(l2);
}
static void
alloc_buffer_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
{
buf->base = (char*)malloc(suggested_size);
buf->len = suggested_size;
}
static void
uv_read_pipe(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
{
void *data[XTM_FIFO_SIZE];
if (nread > 0) {
struct xtm_queue *queue = ((uv_handle_t *)client)->data;
unsigned count;
while ((count = xtm_msg_recv(queue, data, XTM_FIFO_SIZE)) != 0) {
for (unsigned i = 0; i < count; i++) {
struct msg *msg = (struct msg *)data[i];
fprintf(stderr, "Thread %p received: %p %llu\n", pthread_self(), msg->self, msg->counter);
free(msg);
}
}
}
if (buf->base)
free(buf->base);
}
static void
uv_enqueue_message(uv_timer_t* handle)
{
static unsigned long long counter_1, counter_2;
struct xtm_queue *queue = ((uv_handle_t *)handle)->data;
struct msg *msg = (struct msg *)malloc(sizeof(struct msg));
if (msg == NULL)
return;
msg->self = pthread_self();
if (pthread_self() == t1) {
msg->counter = counter_1++;
} else if (pthread_self() == t2) {
msg->counter = counter_2++;
} else {
free(msg);
return;
}
xtm_msg_send(queue, msg);
}
void *
thread_func(void *data)
{
uv_loop_t uvloop;
uv_pipe_t pipe;
int pipe_fd;
uv_timer_t timer;
struct xtm_queue *queue_in = NULL, *queue_out = NULL;
if (data == (void *)1) {
queue_in = q1 = xtm_create(XTM_FIFO_SIZE);
while ((queue_out = q2) == NULL)
;
t1 = pthread_self();
l1 = &uvloop;
} else if (data == (void *)2) {
queue_in = q2 = xtm_create(XTM_FIFO_SIZE);
while ((queue_out = q1) == NULL)
;
t2 = pthread_self();
l2 = &uvloop;
}
if (queue_in == NULL)
goto finish;
pipe_fd = xtm_fd(queue_in);
uv_loop_init(&uvloop);
uv_pipe_init(&uvloop, &pipe, 0);
uv_pipe_open(&pipe, pipe_fd);
((uv_handle_t *)&pipe)->data = queue_in;
uv_read_start((uv_stream_t*)&pipe, alloc_buffer_cb, uv_read_pipe);
uv_timer_init(&uvloop, &timer);
uv_timer_start(&timer, uv_enqueue_message, 0, 1);
((uv_handle_t *)&timer)->data = queue_out;
uv_run(&uvloop, UV_RUN_DEFAULT);
uv_timer_stop(&timer);
uv_read_stop((uv_stream_t*)&pipe);
uv_close((uv_handle_t *)&pipe, NULL);
uv_loop_close(&uvloop);
finish:
if (queue_in == NULL)
xtm_delete(queue_in);
return (void *)NULL;
}
int main()
{
pthread_t thread_1, thread_2;
if (pthread_create(&thread_1, NULL, thread_func, (void *)1) < 0)
return EXIT_FAILURE;
if (pthread_create(&thread_2, NULL, thread_func, (void *)2) < 0) {
pthread_join(thread_1, NULL);
return EXIT_FAILURE;
}
alarm(10);
signal(SIGALRM, alarm_sig_handler);
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
return EXIT_SUCCESS;
}