Skip to content

Commit

Permalink
Try to add proper cancellation code to all threads -- not complete, b…
Browse files Browse the repository at this point in the history
…uggy.
  • Loading branch information
mikebrady committed Jul 20, 2018
1 parent c8c70b6 commit f1d4503
Show file tree
Hide file tree
Showing 19 changed files with 330 additions and 172 deletions.
10 changes: 6 additions & 4 deletions audio_alsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,9 @@ static int init(int argc, char **argv) {
(1.0 * alsa_mix_maxdb) / 100.0);
} else {
// use the linear scale and do the db conversion ourselves
debug(1, "note: the hardware mixer specified -- \"%s\" -- does not have "
"a dB volume scale.",
debug(1,
"note: the hardware mixer specified -- \"%s\" -- does not have "
"a dB volume scale.",
alsa_mix_ctrl);

if (snd_ctl_open(&ctl, alsa_mix_dev, 0) < 0) {
Expand Down Expand Up @@ -661,8 +662,9 @@ int open_alsa_device(void) {
buffer_size);
}
*/
debug(1, "The alsa buffer is smaller (%lu bytes) than the desired backend buffer "
"length (%ld) you have chosen.",
debug(1,
"The alsa buffer is smaller (%lu bytes) than the desired backend buffer "
"length (%ld) you have chosen.",
actual_buffer_length, config.audio_backend_buffer_desired_length);
}

Expand Down
5 changes: 3 additions & 2 deletions audio_soundio.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ static void write_callback(struct SoundIoOutStream *outstream, int frame_count_m
int fill_bytes = soundio_ring_buffer_fill_count(ring_buffer);
int fill_count = fill_bytes / outstream->bytes_per_frame;

debug(3, "[--->>] frame_count_min: %d , frame_count_max: %d , fill_bytes: %d , fill_count: %d , "
"outstream->bytes_per_frame: %d",
debug(3,
"[--->>] frame_count_min: %d , frame_count_max: %d , fill_bytes: %d , fill_count: %d , "
"outstream->bytes_per_frame: %d",
frame_count_min, frame_count_max, fill_bytes, fill_count, outstream->bytes_per_frame);

if (frame_count_min > fill_count) {
Expand Down
29 changes: 11 additions & 18 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ void die(const char *format, ...) {
daemon_log(LOG_EMERG, "% 20.9f|*fatal error: %s", tss, s);
else
daemon_log(LOG_EMERG, "fatal error: %s", s);
shairport_shutdown();
exit(1);
}

Expand Down Expand Up @@ -748,21 +747,17 @@ double flat_vol2attn(double vol, long max_db, long min_db) {

double vol2attn(double vol, long max_db, long min_db) {

// We use a little coordinate geometry to build a transfer function from the volume passed in to the
// device's dynamic range.
// (See the diagram in the documents folder.)
// The x axis is the "volume in" which will be from -30 to 0. The y axis will be the "volume out"
// which will be from the bottom of the range to the top.
// We build the transfer function from one or more lines. We characterise each line with two
// numbers:
// the first is where on x the line starts when y=0 (x can be from 0 to -30); the second is where on
// y the line stops when when x is -30.
// thus, if the line was characterised as {0,-30}, it would be an identity transfer.
// Assuming, for example, a dynamic range of lv=-60 to hv=0
// Typically we'll use three lines -- a three order transfer function
// First: {0,30} giving a gentle slope -- the 30 comes from half the dynamic range
// Second: {-5,-30-(lv+30)/2} giving a faster slope from y=0 at x=-12 to y=-42.5 at x=-30
// Third: {-17,lv} giving a fast slope from y=0 at x=-19 to y=-60 at x=-30
// We use a little coordinate geometry to build a transfer function from the volume passed in to
// the device's dynamic range. (See the diagram in the documents folder.) The x axis is the
// "volume in" which will be from -30 to 0. The y axis will be the "volume out" which will be from
// the bottom of the range to the top. We build the transfer function from one or more lines. We
// characterise each line with two numbers: the first is where on x the line starts when y=0 (x
// can be from 0 to -30); the second is where on y the line stops when when x is -30. thus, if the
// line was characterised as {0,-30}, it would be an identity transfer. Assuming, for example, a
// dynamic range of lv=-60 to hv=0 Typically we'll use three lines -- a three order transfer
// function First: {0,30} giving a gentle slope -- the 30 comes from half the dynamic range
// Second: {-5,-30-(lv+30)/2} giving a faster slope from y=0 at x=-12 to y=-42.5 at x=-30
// Third: {-17,lv} giving a fast slope from y=0 at x=-19 to y=-60 at x=-30

#define order 3

Expand Down Expand Up @@ -966,8 +961,6 @@ int64_t r64i() { return (ranval(&rx) >> 1); }
/* generate an array of 64-bit random numbers */
const int ranarraylength = 1009; // these will be 8-byte numbers.

uint64_t *ranarray;

int ranarraynext;

void ranarrayinit() {
Expand Down
1 change: 1 addition & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ void r64init(uint64_t seed);
uint64_t r64u();
int64_t r64i();

uint64_t *ranarray;
void r64arrayinit();
uint64_t ranarray64u();
int64_t ranarray64i();
Expand Down
39 changes: 31 additions & 8 deletions dacp.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ static void response_code(void *opaque, int code) {
}

static const struct http_funcs responseFuncs = {
response_realloc, response_body, response_header, response_code,
response_realloc,
response_body,
response_header,
response_code,
};

// static pthread_mutex_t dacp_conversation_lock = PTHREAD_MUTEX_INITIALIZER;
Expand Down Expand Up @@ -261,8 +264,9 @@ int dacp_send_command(const char *command, char **body, ssize_t *bodysize) {
// debug(1,"Sent command\"%s\" with a response body of size %d.",command,response.size);
// debug(1,"dacp_conversation_lock released.");
} else {
debug(3, "Could not acquire a lock on the dacp transmit/receive section when attempting to "
"send the command \"%s\". Possible timeout?",
debug(3,
"Could not acquire a lock on the dacp transmit/receive section when attempting to "
"send the command \"%s\". Possible timeout?",
command);
response.code = 494; // This client is already busy
}
Expand Down Expand Up @@ -384,6 +388,12 @@ void dacp_monitor_port_update_callback(char *dacp_id, uint16_t port) {
pthread_cond_signal(&dacp_server_information_cv);
pthread_mutex_unlock(&dacp_server_information_lock);
}

void dacp_monitor_thread_code_cleanup(__attribute__((unused)) void *arg) {
debug(1, "dacp_monitor_thread_code_cleanup called.");
pthread_mutex_unlock(&dacp_server_information_lock);
}

void *dacp_monitor_thread_code(__attribute__((unused)) void *na) {
int scan_index = 0;
// char server_reply[10000];
Expand All @@ -397,14 +407,18 @@ void *dacp_monitor_thread_code(__attribute__((unused)) void *na) {
sps_pthread_mutex_timedlock(
&dacp_server_information_lock, 500000,
"dacp_monitor_thread_code couldn't get DACP server information lock in 0.5 second!.", 2);
int32_t the_volume;

pthread_cleanup_push(dacp_monitor_thread_code_cleanup, NULL);
if (dacp_server.scan_enable == 0) {
metadata_hub_modify_prolog();
int ch = (metadata_store.dacp_server_active != 0) ||
(metadata_store.advanced_dacp_server_active != 0);
metadata_store.dacp_server_active = 0;
metadata_store.advanced_dacp_server_active = 0;
debug(2, "setting dacp_server_active and advanced_dacp_server_active to 0 with an update "
"flag value of %d",
debug(2,
"setting dacp_server_active and advanced_dacp_server_active to 0 with an update "
"flag value of %d",
ch);
metadata_hub_modify_epilog(ch);
while (dacp_server.scan_enable == 0) {
Expand All @@ -416,7 +430,6 @@ void *dacp_monitor_thread_code(__attribute__((unused)) void *na) {
idle_scan_count = 0;
}
scan_index++;
int32_t the_volume;
result = dacp_get_volume(&the_volume); // just want the http code

if ((result == 496) || (result == 403) || (result == 501)) {
Expand All @@ -438,7 +451,9 @@ void *dacp_monitor_thread_code(__attribute__((unused)) void *na) {
debug(1, "DACP server status scanning stopped.");
dacp_server.scan_enable = 0;
}
pthread_mutex_unlock(&dacp_server_information_lock);
pthread_cleanup_pop(1);

// pthread_mutex_unlock(&dacp_server_information_lock);
// debug(1, "DACP Server ID \"%u\" at \"%s:%u\", scan %d.", dacp_server.active_remote_id,
// dacp_server.ip_string, dacp_server.port, scan_index);

Expand Down Expand Up @@ -764,7 +779,7 @@ void *dacp_monitor_thread_code(__attribute__((unused)) void *na) {
sleep(config.scan_interval_when_inactive);
}
}
debug(1, "DACP monitor thread exiting.");
debug(1, "DACP monitor thread exiting -- should never happen.");
pthread_exit(NULL);
}

Expand Down Expand Up @@ -816,6 +831,14 @@ void dacp_monitor_start() {
pthread_create(&dacp_monitor_thread, NULL, dacp_monitor_thread_code, NULL);
}

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);
pthread_mutex_destroy(&dacp_conversation_lock);
}

uint32_t dacp_tlv_crawl(char **p, int32_t *length) {
char typecode[5];
memcpy(typecode, *p, 4);
Expand Down
1 change: 1 addition & 0 deletions dacp.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef struct dacp_speaker_stuff {
} dacp_spkr_stuff;

void dacp_monitor_start();
void dacp_monitor_stop();

uint32_t dacp_tlv_crawl(
char **p,
Expand Down
16 changes: 14 additions & 2 deletions dbus-service.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ ShairportSyncDiagnostics *shairportSyncDiagnosticsSkeleton = NULL;
ShairportSyncRemoteControl *shairportSyncRemoteControlSkeleton = NULL;
ShairportSyncAdvancedRemoteControl *shairportSyncAdvancedRemoteControlSkeleton = NULL;

guint ownerID = 0;

void dbus_metadata_watcher(struct metadata_bundle *argc, __attribute__((unused)) void *userdata) {
char response[100];
const char *th;
Expand Down Expand Up @@ -744,13 +746,15 @@ static void on_dbus_name_acquired(GDBusConnection *connection, const gchar *name
static void on_dbus_name_lost_again(__attribute__((unused)) GDBusConnection *connection,
__attribute__((unused)) const gchar *name,
__attribute__((unused)) gpointer user_data) {
debug(1, "Name lost again.");
warn("Could not acquire a Shairport Sync native D-Bus interface \"%s\" on the %s bus.", name,
(config.dbus_service_bus_type == DBT_session) ? "session" : "system");
}

static void on_dbus_name_lost(__attribute__((unused)) GDBusConnection *connection,
__attribute__((unused)) const gchar *name,
__attribute__((unused)) gpointer user_data) {
debug(1, "Name lost.");
// debug(1, "Could not acquire a Shairport Sync native D-Bus interface \"%s\" on the %s bus --
// will try adding the process "
// "number to the end of it.",
Expand All @@ -774,7 +778,15 @@ int start_dbus_service() {
dbus_bus_type = G_BUS_TYPE_SESSION;
// debug(1, "Looking for a Shairport Sync native D-Bus interface \"org.gnome.ShairportSync\" on
// the %s bus.",(config.dbus_service_bus_type == DBT_session) ? "session" : "system");
g_bus_own_name(dbus_bus_type, "org.gnome.ShairportSync", G_BUS_NAME_OWNER_FLAGS_NONE, NULL,
on_dbus_name_acquired, on_dbus_name_lost, NULL, NULL);
ownerID = g_bus_own_name(dbus_bus_type, "org.gnome.ShairportSync", G_BUS_NAME_OWNER_FLAGS_NONE,
NULL, on_dbus_name_acquired, on_dbus_name_lost, NULL, NULL);
return 0; // this is just to quieten a compiler warning
}

void stop_dbus_service() {
debug(1, "stopping dbus service");
if (ownerID)
g_bus_unown_name(ownerID);
else
debug(1, "Zero OwnerID for \"org.gnome.ShairportSync\".");
}
1 change: 1 addition & 0 deletions dbus-service.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
ShairportSync *shairportSyncSkeleton;

int start_dbus_service();
void stop_dbus_service();

#endif /* #ifndef DBUS_SERVICE_H */
12 changes: 9 additions & 3 deletions mdns_avahi.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,21 +385,27 @@ static int avahi_register(char *srvname, int srvport) {
static void avahi_unregister(void) {
debug(1, "avahi: avahi_unregister.");
if (tpoll) {
avahi_threaded_poll_stop(tpoll);
// debug(1, "avahi: stop the threaded poll.");
// avahi_threaded_poll_stop(tpoll);

if (client) {
debug(1, "avahi: free the client.");
avahi_client_free(client);
client = NULL;
} else {
debug(1, "avahi attempting to unregister a NULL client");
}
debug(1, "avahi: free the threaded poll.");
avahi_threaded_poll_free(tpoll);
tpoll = NULL;
} else {
debug(1, "No avahi threaded poll.");
}

if (service_name)
if (service_name) {
debug(1, "avahi: free the service name.");
free(service_name);
else
} else
debug(1, "avahi attempt to free NULL service name");
service_name = NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion mdns_dns_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/

#include "mdns.h"
#include "common.h"
#include "mdns.h"
#include <arpa/inet.h>
#include <dns_sd.h>
#include <stdlib.h>
Expand Down
2 changes: 1 addition & 1 deletion mdns_external.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/

#include "mdns.h"
#include "common.h"
#include "mdns.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
Expand Down
50 changes: 35 additions & 15 deletions metadata_hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,30 @@ void metadata_hub_release_track_metadata(struct track_metadata_bundle *track_met
}
}

void metadata_hub_release_track_artwork(void) {
// debug(1,"release track artwork");
release_char_string(&metadata_store.cover_art_pathname);
}

void metadata_hub_init(void) {
// debug(1, "Metadata bundle initialisation.");
memset(&metadata_store, 0, sizeof(metadata_store));
track_metadata = NULL;
}

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;
}
}

void add_metadata_watcher(metadata_watcher fn, void *userdata) {
int i;
for (i = 0; i < number_of_watchers; i++) {
Expand All @@ -108,32 +126,34 @@ void add_metadata_watcher(metadata_watcher fn, void *userdata) {
}
}

void metadata_hub_modify_prolog(void) {
// always run this before changing an entry or a sequence of entries in the metadata_hub
// debug(1, "locking metadata hub for writing");
if (pthread_rwlock_trywrlock(&metadata_hub_re_lock) != 0) {
debug(2, "Metadata_hub write lock is already taken -- must wait.");
pthread_rwlock_wrlock(&metadata_hub_re_lock);
debug(2, "Okay -- acquired the metadata_hub write lock.");
}
}

void metadata_hub_release_track_artwork(void) {
// debug(1,"release track artwork");
release_char_string(&metadata_store.cover_art_pathname);
void metadata_hub_unlock_hub_mutex_cleanup(__attribute__((unused)) void *arg) {
debug(1, "metadata_hub_unlock_hub_mutex_cleanup called.");
pthread_rwlock_wrlock(&metadata_hub_re_lock);
}

void run_metadata_watchers(void) {
int i;
// debug(1, "locking metadata hub for reading");
pthread_rwlock_rdlock(&metadata_hub_re_lock);
pthread_cleanup_push(metadata_hub_unlock_hub_mutex_cleanup, NULL);
for (i = 0; i < number_of_watchers; i++) {
if (metadata_store.watchers[i]) {
metadata_store.watchers[i](&metadata_store, metadata_store.watchers_data[i]);
}
}
// debug(1, "unlocking metadata hub for reading");
pthread_rwlock_unlock(&metadata_hub_re_lock);
// pthread_rwlock_unlock(&metadata_hub_re_lock);
pthread_cleanup_pop(1);
}

void metadata_hub_modify_prolog(void) {
// always run this before changing an entry or a sequence of entries in the metadata_hub
// debug(1, "locking metadata hub for writing");
if (pthread_rwlock_trywrlock(&metadata_hub_re_lock) != 0) {
debug(2, "Metadata_hub write lock is already taken -- must wait.");
pthread_rwlock_unlock(&metadata_hub_re_lock);
debug(2, "Okay -- acquired the metadata_hub write lock.");
}
}

void metadata_hub_modify_epilog(int modified) {
Expand Down Expand Up @@ -197,7 +217,7 @@ char *metadata_write_image_file(const char *buf, int len) {
char *path = NULL; // this will be what is returned

uint8_t img_md5[16];
// uint8_t ap_md5[16];
// uint8_t ap_md5[16];

#ifdef HAVE_LIBSSL
MD5_CTX ctx;
Expand Down
Loading

0 comments on commit f1d4503

Please sign in to comment.