Added mutex lock to send_queue

This commit is contained in:
James Booth
2015-05-24 21:58:43 +01:00
parent dda02514df
commit 5b1efdfeab

View File

@@ -23,6 +23,7 @@
#define STREAM_END "</stream:stream>" #define STREAM_END "</stream:stream>"
pthread_mutex_t send_queue_lock;
static GList *send_queue; static GList *send_queue;
static XMPPClient *client; static XMPPClient *client;
@@ -72,6 +73,7 @@ read_stream(void)
errno = 0; errno = 0;
while (TRUE) { while (TRUE) {
// send anything from queue // send anything from queue
pthread_mutex_lock(&send_queue_lock);
GList *curr_send = send_queue; GList *curr_send = send_queue;
while (curr_send) { while (curr_send) {
write_stream(curr_send->data); write_stream(curr_send->data);
@@ -80,6 +82,7 @@ read_stream(void)
g_list_free_full(send_queue, free); g_list_free_full(send_queue, free);
send_queue = NULL; send_queue = NULL;
pthread_mutex_unlock(&send_queue_lock);
int read_size = recv(client->sock, buf, 1, 0); int read_size = recv(client->sock, buf, 1, 0);
@@ -228,7 +231,10 @@ _start_server_cb(void* userdata)
int int
server_run(int port) server_run(int port)
{ {
pthread_mutex_lock(&send_queue_lock);
send_queue = NULL; send_queue = NULL;
pthread_mutex_unlock(&send_queue_lock);
client = NULL; client = NULL;
log_init(); log_init();
log_println("Starting on port: %d...", port); log_println("Starting on port: %d...", port);
@@ -290,7 +296,9 @@ server_run(int port)
void void
server_send(char *stream) server_send(char *stream)
{ {
pthread_mutex_lock(&send_queue_lock);
send_queue = g_list_append(send_queue, strdup(stream)); send_queue = g_list_append(send_queue, strdup(stream));
pthread_mutex_unlock(&send_queue_lock);
} }
void void
@@ -314,7 +322,10 @@ _shutdown(void)
prime_free_all(); prime_free_all();
stanza_free_all(); stanza_free_all();
pthread_mutex_lock(&send_queue_lock);
g_list_free_full(send_queue, free); g_list_free_full(send_queue, free);
send_queue = NULL; send_queue = NULL;
pthread_mutex_unlock(&send_queue_lock);
log_close(); log_close();
} }