Skip to content

Commit

Permalink
check that the systems are started before trying to stop them, duh.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebrady committed Feb 5, 2019
1 parent 916edea commit 79e2206
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 28 deletions.
13 changes: 9 additions & 4 deletions activity_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
enum am_state { am_inactive, am_active, am_timing_out } state;
enum ps_state { ps_inactive, ps_active } player_state;

int activity_monitor_running = 0;

pthread_t activity_monitor_thread;
pthread_mutex_t activity_monitor_mutex;
pthread_cond_t activity_monitor_cv;
Expand Down Expand Up @@ -218,11 +220,14 @@ void *activity_monitor_thread_code(void *arg) {
void activity_monitor_start() {
// debug(1,"activity_monitor_start");
pthread_create(&activity_monitor_thread, NULL, activity_monitor_thread_code, NULL);
activity_monitor_running = 1;
}

void activity_monitor_stop() {
debug(1, "activity_monitor_stop start...");
pthread_cancel(activity_monitor_thread);
pthread_join(activity_monitor_thread, NULL);
debug(1, "activity_monitor_stop complete");
if (activity_monitor_running) {
debug(1, "activity_monitor_stop start...");
pthread_cancel(activity_monitor_thread);
pthread_join(activity_monitor_thread, NULL);
debug(1, "activity_monitor_stop complete");
}
}
16 changes: 10 additions & 6 deletions dacp.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ typedef struct {
void *port_monitor_private_storage;
} dacp_server_record;

int dacp_monitor_initialised = 0;
pthread_t dacp_monitor_thread;
dacp_server_record dacp_server;
void *mdns_dacp_monitor_private_storage_pointer;
Expand Down Expand Up @@ -888,15 +889,18 @@ void dacp_monitor_start() {
memset(&dacp_server, 0, sizeof(dacp_server_record));

pthread_create(&dacp_monitor_thread, NULL, dacp_monitor_thread_code, NULL);
dacp_monitor_initialised = 1;
}

void dacp_monitor_stop() {
debug(1, "dacp_monitor_stop");
pthread_cancel(dacp_monitor_thread);
pthread_join(dacp_monitor_thread, NULL);
pthread_mutex_destroy(&dacp_server_information_lock);
debug(1, "DACP Conversation Lock Mutex Destroyed");
pthread_mutex_destroy(&dacp_conversation_lock);
if (dacp_monitor_initialised) { // only if it's been started and initialised
debug(1, "dacp_monitor_stop");
pthread_cancel(dacp_monitor_thread);
pthread_join(dacp_monitor_thread, NULL);
pthread_mutex_destroy(&dacp_server_information_lock);
debug(1, "DACP Conversation Lock Mutex Destroyed");
pthread_mutex_destroy(&dacp_conversation_lock);
}
}

uint32_t dacp_tlv_crawl(char **p, int32_t *length) {
Expand Down
23 changes: 14 additions & 9 deletions metadata_hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
#include <openssl/md5.h>
#endif

int metadata_hub_initialised = 0;

pthread_rwlock_t metadata_hub_re_lock = PTHREAD_RWLOCK_INITIALIZER;
struct track_metadata_bundle *track_metadata; // used for a temporary track metadata store

Expand Down Expand Up @@ -100,18 +102,21 @@ void metadata_hub_init(void) {
// debug(1, "Metadata bundle initialisation.");
memset(&metadata_store, 0, sizeof(metadata_store));
track_metadata = NULL;
metadata_hub_initialised = 1;
}

void metadata_hub_stop(void) {
debug(1, "metadata_hub_stop.");
metadata_hub_release_track_artwork();
if (metadata_store.track_metadata) {
metadata_hub_release_track_metadata(metadata_store.track_metadata);
metadata_store.track_metadata = NULL;
}
if (track_metadata) {
metadata_hub_release_track_metadata(track_metadata);
track_metadata = NULL;
if (metadata_hub_initialised) {
debug(1, "metadata_hub_stop.");
metadata_hub_release_track_artwork();
if (metadata_store.track_metadata) {
metadata_hub_release_track_metadata(metadata_store.track_metadata);
metadata_store.track_metadata = NULL;
}
if (track_metadata) {
metadata_hub_release_track_metadata(track_metadata);
track_metadata = NULL;
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions rtsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ enum rtsp_read_request_response {

// Mike Brady's part...

int metadata_running = 0;

// always lock use this when accessing the playing conn value
static pthread_mutex_t playing_conn_lock = PTHREAD_MUTEX_INITIALIZER;

Expand Down Expand Up @@ -1454,12 +1456,15 @@ void metadata_init(void) {
int ret = pthread_create(&metadata_thread, NULL, metadata_thread_function, NULL);
if (ret)
debug(1, "Failed to create metadata thread!");
metadata_running = 1;
}

void metadata_stop(void) {
debug(1, "metadata_stop called.");
pthread_cancel(metadata_thread);
pthread_join(metadata_thread, NULL);
if (metadata_running) {
debug(1, "metadata_stop called.");
pthread_cancel(metadata_thread);
pthread_join(metadata_thread, NULL);
}
}

int send_metadata(uint32_t type, uint32_t code, char *data, uint32_t length, rtsp_message *carrier,
Expand Down
14 changes: 8 additions & 6 deletions shairport.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ int parse_options(int argc, char **argv) {
}

#if defined(CONFIG_DBUS_INTERFACE) || defined(CONFIG_MPRIS_INTERFACE)
GMainLoop *g_main_loop;
static GMainLoop *g_main_loop = NULL;

pthread_t dbus_thread;
void *dbus_thread_func(__attribute__((unused)) void *arg) {
Expand Down Expand Up @@ -1138,7 +1138,7 @@ const char *pid_file_proc(void) {
#endif

void main_cleanup_handler(__attribute__((unused)) void *arg) {
// it doesn't look like this is called when the main function terminates.
// it doesn't look like this is called when the main function is cancelled eith a pthread cancel.
debug(1, "main cleanup handler called.");
#ifdef CONFIG_MQTT
if (config.mqtt_enabled) {
Expand All @@ -1153,9 +1153,11 @@ void main_cleanup_handler(__attribute__((unused)) void *arg) {
#ifdef CONFIG_DBUS_INTERFACE
stop_dbus_service();
#endif
debug(1, "Stopping DBUS Loop Thread");
g_main_loop_quit(g_main_loop);
pthread_join(dbus_thread, NULL);
if (g_main_loop) {
debug(1, "Stopping DBUS Loop Thread");
g_main_loop_quit(g_main_loop);
pthread_join(dbus_thread, NULL);
}
#endif

#ifdef CONFIG_DACP_CLIENT
Expand All @@ -1174,7 +1176,7 @@ void main_cleanup_handler(__attribute__((unused)) void *arg) {

activity_monitor_stop(0);

if (config.output->deinit) {
if ((config.output) && (config.output->deinit)) {
debug(1, "Deinitialise the audio backend.");
config.output->deinit();
}
Expand Down

0 comments on commit 79e2206

Please sign in to comment.