diff --git a/src/commands.c b/src/commands.c index 91752357..fafcf1a7 100644 --- a/src/commands.c +++ b/src/commands.c @@ -20,14 +20,13 @@ #include #include -#include #include #include #include #include #include "logger.h" - +#include "misc.h" struct command { @@ -79,7 +78,7 @@ command_cb_sync(struct commands_base *cmdbase, struct command *cmd) { enum command_state cmdstate; - pthread_mutex_lock(&cmd->lck); + fork_mutex_lock(&cmd->lck); cmdstate = cmd->func(cmd->arg, &cmd->ret); if (cmdstate == COMMAND_PENDING) @@ -95,8 +94,8 @@ command_cb_sync(struct commands_base *cmdbase, struct command *cmd) cmd->func_bh(cmd->arg, &cmd->ret); // Signal the calling thread that the command execution finished - pthread_cond_signal(&cmd->cond); - pthread_mutex_unlock(&cmd->lck); + fork_cond_signal(&cmd->cond); + fork_mutex_unlock(&cmd->lck); event_add(cmdbase->command_event, NULL); } @@ -284,8 +283,8 @@ commands_exec_end(struct commands_base *cmdbase, int retvalue) { cmdbase->current_cmd->func_bh(cmdbase->current_cmd->arg, &cmdbase->current_cmd->ret); } - pthread_cond_signal(&cmdbase->current_cmd->cond); - pthread_mutex_unlock(&cmdbase->current_cmd->lck); + fork_cond_signal(&cmdbase->current_cmd->cond); + fork_mutex_unlock(&cmdbase->current_cmd->lck); cmdbase->current_cmd = NULL; @@ -318,18 +317,25 @@ commands_exec_sync(struct commands_base *cmdbase, command_function func, command cmd.arg = arg; cmd.nonblock = 0; - pthread_mutex_lock(&cmd.lck); + fork_mutex_init(&cmd.lck); + fork_cond_init(&cmd.cond); + + fork_mutex_lock(&cmd.lck); ret = send_command(cmdbase, &cmd); if (ret < 0) { DPRINTF(E_LOG, L_MAIN, "Error sending command\n"); - pthread_mutex_unlock(&cmd.lck); - return -1; + cmd.ret = -1; } + else + { + fork_cond_wait(&cmd.cond, &cmd.lck); + } + fork_mutex_unlock(&cmd.lck); - pthread_cond_wait(&cmd.cond, &cmd.lck); - pthread_mutex_unlock(&cmd.lck); + fork_cond_destroy(&cmd.cond); + fork_mutex_destroy(&cmd.lck); return cmd.ret; } diff --git a/src/db.c b/src/db.c index 126785fe..567f4522 100644 --- a/src/db.c +++ b/src/db.c @@ -35,8 +35,6 @@ #include #include -#include - #include #include "conffile.h" @@ -532,12 +530,12 @@ unlock_notify_cb(void **args, int nargs) { u = (struct db_unlock *)args[i]; - pthread_mutex_lock(&u->lck); + fork_mutex_lock(&u->lck); u->proceed = 1; - pthread_cond_signal(&u->cond); + fork_cond_signal(&u->cond); - pthread_mutex_unlock(&u->lck); + fork_mutex_unlock(&u->lck); } } @@ -548,25 +546,25 @@ db_wait_unlock(void) int ret; u.proceed = 0; - pthread_mutex_init(&u.lck, NULL); - pthread_cond_init(&u.cond, NULL); + fork_mutex_init(&u.lck); + fork_cond_init(&u.cond); ret = sqlite3_unlock_notify(hdl, unlock_notify_cb, &u); if (ret == SQLITE_OK) { - pthread_mutex_lock(&u.lck); + fork_mutex_lock(&u.lck); if (!u.proceed) { DPRINTF(E_INFO, L_DB, "Waiting for database unlock\n"); - pthread_cond_wait(&u.cond, &u.lck); + fork_cond_wait(&u.cond, &u.lck); } - pthread_mutex_unlock(&u.lck); + fork_mutex_unlock(&u.lck); } - pthread_cond_destroy(&u.cond); - pthread_mutex_destroy(&u.lck); + fork_cond_destroy(&u.cond); + fork_mutex_destroy(&u.lck); return ret; } diff --git a/src/logger.c b/src/logger.c index fa216e2a..93bde3a1 100644 --- a/src/logger.c +++ b/src/logger.c @@ -20,6 +20,8 @@ # include #endif +#include "logger.h" + #include #include #include @@ -27,15 +29,13 @@ #include #include #include -#include #include #include #include "conffile.h" -#include "logger.h" - +#include "misc.h" static pthread_mutex_t logger_lck = PTHREAD_MUTEX_INITIALIZER; static int logdomains; @@ -90,11 +90,11 @@ vlogger(int severity, int domain, const char *fmt, va_list args) if (!((1 << domain) & logdomains) || (severity > threshold)) return; - pthread_mutex_lock(&logger_lck); + fork_mutex_lock(&logger_lck); if (!logfile && !console) { - pthread_mutex_unlock(&logger_lck); + fork_mutex_unlock(&logger_lck); return; } @@ -123,7 +123,7 @@ vlogger(int severity, int domain, const char *fmt, va_list args) va_end(ap); } - pthread_mutex_unlock(&logger_lck); + fork_mutex_unlock(&logger_lck); } void @@ -204,7 +204,7 @@ logger_reinit(void) if (!logfile) return; - pthread_mutex_lock(&logger_lck); + fork_mutex_lock(&logger_lck); fp = fopen(logfilename, "a"); if (!fp) @@ -218,7 +218,7 @@ logger_reinit(void) logfile = fp; out: - pthread_mutex_unlock(&logger_lck); + fork_mutex_unlock(&logger_lck); } diff --git a/src/main.c b/src/main.c index ef7eb84e..77c36be7 100644 --- a/src/main.c +++ b/src/main.c @@ -44,8 +44,6 @@ # include #endif -#include - #include #include #include @@ -422,26 +420,29 @@ signal_kqueue_cb(int fd, short event, void *arg) static int -ffmpeg_lockmgr(void **mutex, enum AVLockOp op) +ffmpeg_lockmgr(void **pmutex, enum AVLockOp op) { switch (op) { case AV_LOCK_CREATE: - *mutex = malloc(sizeof(pthread_mutex_t)); - if (!*mutex) + *pmutex = malloc(sizeof(pthread_mutex_t)); + if (!*pmutex) return 1; - - return !!pthread_mutex_init(*mutex, NULL); + fork_mutex_init(*pmutex); + return 0; case AV_LOCK_OBTAIN: - return !!pthread_mutex_lock(*mutex); + fork_mutex_lock(*pmutex); + return 0; case AV_LOCK_RELEASE: - return !!pthread_mutex_unlock(*mutex); + fork_mutex_unlock(*pmutex); + return 0; case AV_LOCK_DESTROY: - pthread_mutex_destroy(*mutex); - free(*mutex); + fork_mutex_destroy(*pmutex); + free(*pmutex); + *pmutex = NULL; return 0; } diff --git a/src/misc.c b/src/misc.c index b2856051..65230bbd 100644 --- a/src/misc.c +++ b/src/misc.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -918,3 +919,88 @@ timespec_cmp(struct timespec time1, struct timespec time2) else return 0; } + +void +fork_mutex_init(pthread_mutex_t *mutex) +{ + pthread_mutexattr_t mattr; + int err; + + err = pthread_mutexattr_init(&mattr); + assert(err == 0); + err = pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_ERRORCHECK); + assert(err == 0); + err = pthread_mutex_init(mutex, &mattr); + assert(err == 0); + err = pthread_mutexattr_destroy(&mattr); + assert(err == 0); +} + +void +fork_mutex_lock(pthread_mutex_t *mutex) +{ + int err; + err = pthread_mutex_lock(mutex); + assert(err == 0); +} + +void +fork_mutex_unlock(pthread_mutex_t *mutex) +{ + int err; + err = pthread_mutex_unlock(mutex); + assert(err == 0); +} + +void +fork_mutex_destroy(pthread_mutex_t *mutex) +{ + int err; + err = pthread_mutex_destroy(mutex); + assert(err == 0); +} + +/* condition wrappers with checks */ +void +fork_cond_init(pthread_cond_t *cond) +{ + int err; + err = pthread_cond_init(cond, NULL); + assert(err == 0); +} + +void +fork_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) +{ + int err; + err = pthread_cond_wait(cond, mutex); + assert(err == 0); +} + +int +fork_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, + const struct timespec *ts) +{ + int err; + err = pthread_cond_timedwait(cond, mutex, ts); + if(err == ETIMEDOUT) + return err; + assert(err == 0); + return err; +} + +void +fork_cond_signal(pthread_cond_t *cond) +{ + int err; + err = pthread_cond_signal(cond); + assert(err == 0); +} + +void +fork_cond_destroy(pthread_cond_t *cond) +{ + int err; + err = pthread_cond_destroy(cond); + assert(err == 0); +} diff --git a/src/misc.h b/src/misc.h index 7aa615c1..946c64cc 100644 --- a/src/misc.h +++ b/src/misc.h @@ -8,6 +8,7 @@ #include #include +#include struct onekeyval { char *name; @@ -96,4 +97,27 @@ timespec_add(struct timespec time1, struct timespec time2); int timespec_cmp(struct timespec time1, struct timespec time2); +/* mutex wrappers with checks */ +void +fork_mutex_init(pthread_mutex_t *mutex); +void +fork_mutex_lock(pthread_mutex_t *mutex); +void +fork_mutex_unlock(pthread_mutex_t *mutex); +void +fork_mutex_destroy(pthread_mutex_t *mutex); + +/* condition wrappers with checks */ +void +fork_cond_init(pthread_cond_t *cond); +void +fork_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); +int +fork_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, + const struct timespec *ts); +void +fork_cond_signal(pthread_cond_t *cond); +void +fork_cond_destroy(pthread_cond_t *cond); + #endif /* !__MISC_H__ */ diff --git a/src/remote_pairing.c b/src/remote_pairing.c index 0f586e9b..b0a9e40b 100644 --- a/src/remote_pairing.c +++ b/src/remote_pairing.c @@ -39,7 +39,6 @@ #include #include #include -#include #ifdef HAVE_EVENTFD # include @@ -661,7 +660,7 @@ pairing_cb(int fd, short event, void *arg) for (;;) { - pthread_mutex_lock(&remote_lck); + fork_mutex_lock(&remote_lck); for (ri = remote_list; ri; ri = ri->next) { @@ -673,7 +672,7 @@ pairing_cb(int fd, short event, void *arg) } } - pthread_mutex_unlock(&remote_lck); + fork_mutex_unlock(&remote_lck); if (!ri) break; @@ -700,11 +699,11 @@ touch_remote_cb(const char *name, const char *type, const char *domain, const ch * failed; any subsequent attempt will need a new pairing pin, so * we can just forget everything we know about the remote. */ - pthread_mutex_lock(&remote_lck); + fork_mutex_lock(&remote_lck); remove_remote_address_byid(name, family); - pthread_mutex_unlock(&remote_lck); + fork_mutex_unlock(&remote_lck); } else { @@ -762,7 +761,7 @@ touch_remote_cb(const char *name, const char *type, const char *domain, const ch DPRINTF(E_LOG, L_REMOTE, "Discovered remote '%s' (id %s) at %s:%d, paircode %s\n", devname, name, address, port, paircode); /* Add the data to the list, adding the remote to the list if needed */ - pthread_mutex_lock(&remote_lck); + fork_mutex_lock(&remote_lck); ret = add_remote_mdns_data(name, family, address, port, devname, paircode); @@ -776,7 +775,7 @@ touch_remote_cb(const char *name, const char *type, const char *domain, const ch else if (ret == 1) kickoff_pairing(); - pthread_mutex_unlock(&remote_lck); + fork_mutex_unlock(&remote_lck); } } @@ -886,7 +885,7 @@ remote_pairing_read_pin(char *path) DPRINTF(E_LOG, L_REMOTE, "Read Remote pairing data (name '%s', pin '%s') from %s\n", devname, pin, path); - pthread_mutex_lock(&remote_lck); + fork_mutex_lock(&remote_lck); ret = add_remote_pin_data(devname, pin); free(devname); @@ -895,7 +894,7 @@ remote_pairing_read_pin(char *path) else kickoff_pairing(); - pthread_mutex_unlock(&remote_lck); + fork_mutex_unlock(&remote_lck); } diff --git a/src/spotify.c b/src/spotify.c index 53ea8805..33ba9391 100644 --- a/src/spotify.c +++ b/src/spotify.c @@ -1489,7 +1489,7 @@ audio_fifo_flush(void) DPRINTF(E_DBG, L_SPOTIFY, "Flushing audio fifo\n"); - pthread_mutex_lock(&g_audio_fifo->mutex); + fork_mutex_lock(&g_audio_fifo->mutex); while((afd = TAILQ_FIRST(&g_audio_fifo->q))) { TAILQ_REMOVE(&g_audio_fifo->q, afd, link); @@ -1498,7 +1498,7 @@ audio_fifo_flush(void) g_audio_fifo->qlen = 0; g_audio_fifo->fullcount = 0; - pthread_mutex_unlock(&g_audio_fifo->mutex); + fork_mutex_unlock(&g_audio_fifo->mutex); } static enum command_state @@ -1678,7 +1678,7 @@ audio_get(void *arg, int *retval) if (g_state == SPOTIFY_STATE_PAUSED) playback_play(NULL, retval); - pthread_mutex_lock(&g_audio_fifo->mutex); + fork_mutex_lock(&g_audio_fifo->mutex); while ((processed < audio->wanted) && (g_state != SPOTIFY_STATE_STOPPED)) { @@ -1701,7 +1701,7 @@ audio_get(void *arg, int *retval) DPRINTF(E_DBG, L_SPOTIFY, "Waiting for audio\n"); timeout += 5; mk_reltime(&ts, 5); - pthread_cond_timedwait(&g_audio_fifo->cond, &g_audio_fifo->mutex, &ts); + fork_cond_timedwait(&g_audio_fifo->cond, &g_audio_fifo->mutex, &ts); } if ((!afd) && (timeout >= SPOTIFY_TIMEOUT)) @@ -1725,7 +1725,7 @@ audio_get(void *arg, int *retval) if (ret < 0) { DPRINTF(E_LOG, L_SPOTIFY, "Out of memory for evbuffer (tried to add %d bytes)\n", s); - pthread_mutex_unlock(&g_audio_fifo->mutex); + fork_mutex_unlock(&g_audio_fifo->mutex); *retval = -1; return COMMAND_END; } @@ -1733,7 +1733,7 @@ audio_get(void *arg, int *retval) processed += s; } - pthread_mutex_unlock(&g_audio_fifo->mutex); + fork_mutex_unlock(&g_audio_fifo->mutex); *retval = processed; @@ -1744,15 +1744,15 @@ static void artwork_loaded_cb(sp_image *image, void *userdata) { struct artwork_get_param *artwork; - + artwork = userdata; - - pthread_mutex_lock(&artwork->mutex); + + fork_mutex_lock(&artwork->mutex); artwork->is_loaded = 1; - pthread_cond_signal(&artwork->cond); - pthread_mutex_unlock(&artwork->mutex); + fork_cond_signal(&artwork->cond); + fork_mutex_unlock(&artwork->mutex); } static enum command_state @@ -1989,10 +1989,10 @@ logged_out(sp_session *sess) { DPRINTF(E_INFO, L_SPOTIFY, "Logout complete\n"); - pthread_mutex_lock(&login_lck); + fork_mutex_lock(&login_lck); - pthread_cond_signal(&login_cond); - pthread_mutex_unlock(&login_lck); + fork_cond_signal(&login_cond); + fork_mutex_unlock(&login_lck); } /** @@ -2017,7 +2017,7 @@ static int music_delivery(sp_session *sess, const sp_audioformat *format, if (num_frames == 0) return 0; // Audio discontinuity, do nothing - pthread_mutex_lock(&g_audio_fifo->mutex); + fork_mutex_lock(&g_audio_fifo->mutex); /* Buffer three seconds of audio */ if (g_audio_fifo->qlen > (3 * format->sample_rate)) @@ -2033,7 +2033,7 @@ static int music_delivery(sp_session *sess, const sp_audioformat *format, g_audio_fifo->fullcount = 0; } - pthread_mutex_unlock(&g_audio_fifo->mutex); + fork_mutex_unlock(&g_audio_fifo->mutex); return 0; } @@ -2050,8 +2050,8 @@ static int music_delivery(sp_session *sess, const sp_audioformat *format, TAILQ_INSERT_TAIL(&g_audio_fifo->q, afd, link); g_audio_fifo->qlen += num_frames; - pthread_cond_signal(&g_audio_fifo->cond); - pthread_mutex_unlock(&g_audio_fifo->mutex); + fork_cond_signal(&g_audio_fifo->cond); + fork_mutex_unlock(&g_audio_fifo->mutex); return num_frames; } @@ -2371,23 +2371,23 @@ spotify_artwork_get(struct evbuffer *evbuf, char *path, int max_w, int max_h) artwork.max_w = max_w; artwork.max_h = max_h; - pthread_mutex_init(&artwork.mutex, NULL); - pthread_cond_init(&artwork.cond, NULL); + fork_mutex_init(&artwork.mutex); + fork_cond_init(&artwork.cond); ret = commands_exec_sync(cmdbase, artwork_get, NULL, &artwork); - + // Artwork was not ready, wait for callback from libspotify if (ret == 0) { - pthread_mutex_lock(&artwork.mutex); + fork_mutex_lock(&artwork.mutex); mk_reltime(&ts, SPOTIFY_ARTWORK_TIMEOUT); if (!artwork.is_loaded) - pthread_cond_timedwait(&artwork.cond, &artwork.mutex, &ts); - pthread_mutex_unlock(&artwork.mutex); + fork_cond_timedwait(&artwork.cond, &artwork.mutex, &ts); + fork_mutex_unlock(&artwork.mutex); ret = commands_exec_sync(cmdbase, artwork_get_bh, NULL, &artwork); } - + return ret; } @@ -2499,7 +2499,7 @@ spotify_login(char *path) if (SP_CONNECTION_STATE_LOGGED_IN == fptr_sp_session_connectionstate(g_sess)) { - pthread_mutex_lock(&login_lck); + fork_mutex_lock(&login_lck); DPRINTF(E_LOG, L_SPOTIFY, "Logging out of Spotify (current state is %d)\n", g_state); @@ -2509,12 +2509,12 @@ spotify_login(char *path) if (SP_ERROR_OK != err) { DPRINTF(E_LOG, L_SPOTIFY, "Could not logout of Spotify: %s\n", fptr_sp_error_message(err)); - pthread_mutex_unlock(&login_lck); + fork_mutex_unlock(&login_lck); return; } - pthread_cond_wait(&login_cond, &login_lck); - pthread_mutex_unlock(&login_lck); + fork_cond_wait(&login_cond, &login_lck); + fork_mutex_unlock(&login_lck); } DPRINTF(E_INFO, L_SPOTIFY, "Logging into Spotify\n"); @@ -2649,11 +2649,11 @@ spotify_init(void) } TAILQ_INIT(&g_audio_fifo->q); g_audio_fifo->qlen = 0; - pthread_mutex_init(&g_audio_fifo->mutex, NULL); - pthread_cond_init(&g_audio_fifo->cond, NULL); + fork_mutex_init(&g_audio_fifo->mutex); + fork_cond_init(&g_audio_fifo->cond); - pthread_mutex_init(&login_lck, NULL); - pthread_cond_init(&login_cond, NULL); + fork_mutex_init(&login_lck); + fork_cond_init(&login_cond); /* Spawn thread */ ret = pthread_create(&tid_spotify, NULL, spotify, NULL); @@ -2673,11 +2673,11 @@ spotify_init(void) return 0; thread_fail: - pthread_cond_destroy(&login_cond); - pthread_mutex_destroy(&login_lck); + fork_cond_destroy(&login_cond); + fork_mutex_destroy(&login_lck); - pthread_cond_destroy(&g_audio_fifo->cond); - pthread_mutex_destroy(&g_audio_fifo->mutex); + fork_cond_destroy(&g_audio_fifo->cond); + fork_mutex_destroy(&g_audio_fifo->mutex); free(g_audio_fifo); audio_fifo_fail: @@ -2737,12 +2737,12 @@ spotify_deinit(void) close(g_notify_pipe[1]); /* Destroy locks */ - pthread_cond_destroy(&login_cond); - pthread_mutex_destroy(&login_lck); + fork_cond_destroy(&login_cond); + fork_mutex_destroy(&login_lck); /* Clear audio fifo */ - pthread_cond_destroy(&g_audio_fifo->cond); - pthread_mutex_destroy(&g_audio_fifo->mutex); + fork_cond_destroy(&g_audio_fifo->cond); + fork_mutex_destroy(&g_audio_fifo->mutex); free(g_audio_fifo); /* Release libspotify handle */