summaryrefslogtreecommitdiff
path: root/ot_livesync.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_livesync.c')
-rw-r--r--ot_livesync.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/ot_livesync.c b/ot_livesync.c
index e6db720..4a0ede1 100644
--- a/ot_livesync.c
+++ b/ot_livesync.c
@@ -32,9 +32,6 @@ ot_ip6 groupip_1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 224, 0, 23, 5};
32 32
33#define LIVESYNC_MAXDELAY 15 /* seconds */ 33#define LIVESYNC_MAXDELAY 15 /* seconds */
34 34
35/* How long the worker may sit in a read before it looks at the running flag */
36#define LIVESYNC_WORKER_PATIENCE 1 /* seconds */
37
38enum { OT_SYNC_PEER4, OT_SYNC_PEER6 }; 35enum { OT_SYNC_PEER4, OT_SYNC_PEER6 };
39 36
40/* Forward declaration */ 37/* Forward declaration */
@@ -58,6 +55,7 @@ static sync_buffer g_v4_buf;
58 55
59static pthread_t thread_id; 56static pthread_t thread_id;
60static int thread_started; 57static int thread_started;
58static _Atomic int thread_left;
61void livesync_init(void) { 59void livesync_init(void) {
62 uint32_t packet_type6 = htonl(OT_SYNC_PEER6); 60 uint32_t packet_type6 = htonl(OT_SYNC_PEER6);
63 uint32_t packet_type4 = htonl(OT_SYNC_PEER4); 61 uint32_t packet_type4 = htonl(OT_SYNC_PEER4);
@@ -78,13 +76,16 @@ void livesync_init(void) {
78 g_v6_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY); 76 g_v6_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY);
79 g_v4_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY); 77 g_v4_buf.next_packet_time = time_future_seconds(LIVESYNC_MAXDELAY);
80 78
81 socket_set_timeout(g_socket_in, LIVESYNC_WORKER_PATIENCE);
82 thread_started = !pthread_create(&thread_id, NULL, livesync_worker, NULL); 79 thread_started = !pthread_create(&thread_id, NULL, livesync_worker, NULL);
83} 80}
84 81
85void livesync_deinit(void) { 82void livesync_deinit(void) {
86 if (thread_started) { 83 if (thread_started) {
87 shutdown(g_socket_in, SHUT_RD); 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 }
88 pthread_join(thread_id, NULL); 89 pthread_join(thread_id, NULL);
89 thread_started = 0; 90 thread_started = 0;
90 } 91 }
@@ -235,6 +236,7 @@ static void *livesync_worker(void *args) {
235 } 236 }
236 237
237 free(ws.inbuf); 238 free(ws.inbuf);
239 atomic_store(&thread_left, 1);
238 return NULL; 240 return NULL;
239} 241}
240 242