summaryrefslogtreecommitdiff
path: root/ot_livesync.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_livesync.c')
-rw-r--r--ot_livesync.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/ot_livesync.c b/ot_livesync.c
index 45271bc..4a0ede1 100644
--- a/ot_livesync.c
+++ b/ot_livesync.c
@@ -8,6 +8,7 @@
8#include <pthread.h> 8#include <pthread.h>
9#include <stdlib.h> 9#include <stdlib.h>
10#include <string.h> 10#include <string.h>
11#include <sys/socket.h>
11#include <sys/types.h> 12#include <sys/types.h>
12#include <unistd.h> 13#include <unistd.h>
13 14
@@ -53,6 +54,8 @@ static sync_buffer g_v6_buf;
53static sync_buffer g_v4_buf; 54static sync_buffer g_v4_buf;
54 55
55static pthread_t thread_id; 56static pthread_t thread_id;
57static int thread_started;
58static _Atomic int thread_left;
56void livesync_init(void) { 59void livesync_init(void) {
57 uint32_t packet_type6 = htonl(OT_SYNC_PEER6); 60 uint32_t packet_type6 = htonl(OT_SYNC_PEER6);
58 uint32_t packet_type4 = htonl(OT_SYNC_PEER4); 61 uint32_t packet_type4 = htonl(OT_SYNC_PEER4);
@@ -73,16 +76,24 @@ void livesync_init(void) {
73 g_v6_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY); 76 g_v6_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY);
74 g_v4_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY); 77 g_v4_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY);
75 78
76 pthread_create(&thread_id, NULL, livesync_worker, NULL); 79 thread_started = !pthread_create(&thread_id, NULL, livesync_worker, NULL);
77} 80}
78 81
79void livesync_deinit(void) { 82void livesync_deinit(void) {
83 if (thread_started) {
84 /* Repeats so it interrupts a read already under way */
85 while (!atomic_load(&thread_left)) {
86 pthread_kill(thread_id, OT_SIGNAL_WAKE);
87 usleep(1000);
88 }
89 pthread_join(thread_id, NULL);
90 thread_started = 0;
91 }
92
80 if (g_socket_in != -1) 93 if (g_socket_in != -1)
81 close(g_socket_in); 94 close(g_socket_in);
82 if (g_socket_out != -1) 95 if (g_socket_out != -1)
83 close(g_socket_out); 96 close(g_socket_out);
84
85 pthread_cancel(thread_id);
86} 97}
87 98
88void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { 99void livesync_bind_mcast(ot_ip6 ip, uint16_t port) {
@@ -195,7 +206,7 @@ static void *livesync_worker(void *args) {
195 ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE); 206 ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE);
196 ws.outbuf = ws.reply = 0; 207 ws.outbuf = ws.reply = 0;
197 208
198 while (1) { 209 while (g_opentracker_running) {
199 uint32_t packet_type; 210 uint32_t packet_type;
200 211
201 ws.request_size = socket_recv(g_socket_in, ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, in_ip, &in_port); 212 ws.request_size = socket_recv(g_socket_in, ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, in_ip, &in_port);
@@ -224,7 +235,8 @@ static void *livesync_worker(void *args) {
224 } 235 }
225 } 236 }
226 237
227 /* Never returns. */ 238 free(ws.inbuf);
239 atomic_store(&thread_left, 1);
228 return NULL; 240 return NULL;
229} 241}
230 242