From a9afd1efef60470e2eeadc29b59d949abbecc6ca Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 20 Jul 2007 19:09:34 +0000 Subject: [PATCH] (update_lock, play_lock, update_pending, play_wrlock_wanted): Move to main.c and rename to better reflect their purpose. Fix misleading comments. --- include/server.h | 9 +++++---- src/lib/gen/io.c | 4 ++-- src/lib/player/dispatch.c | 4 ++-- src/server/main.c | 15 ++++++++++++++- src/server/update.c | 28 ++++++++-------------------- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/include/server.h b/include/server.h index 15908b44..413a95eb 100644 --- a/include/server.h +++ b/include/server.h @@ -36,11 +36,12 @@ #include "empthread.h" -extern int shutdown_pending; -extern int update_pending; -extern int update_running; -extern empth_rwlock_t *update_lock; #define UPDATE_TIME_LEN 16 + +extern int shutdown_pending; +extern empth_rwlock_t *play_lock; +extern int play_wrlock_wanted; +extern int update_running; extern time_t update_time[UPDATE_TIME_LEN]; void market_init(void); diff --git a/src/lib/gen/io.c b/src/lib/gen/io.c index 7d87c330..8744df63 100644 --- a/src/lib/gen/io.c +++ b/src/lib/gen/io.c @@ -324,9 +324,9 @@ io_output_all(struct iop *iop) * Mustn't block a player thread while update is pending, or else * a malicous player could delay the update indefinitely */ - while (((n = io_output(iop, IO_NOWAIT)) > 0) && !update_pending) { + while ((n = io_output(iop, IO_NOWAIT)) > 0 && !play_wrlock_wanted) empth_select(iop->fd, EMPTH_FD_WRITE); - } + return n; } diff --git a/src/lib/player/dispatch.c b/src/lib/player/dispatch.c index c1d9182e..22117f16 100644 --- a/src/lib/player/dispatch.c +++ b/src/lib/player/dispatch.c @@ -88,7 +88,7 @@ dispatch(char *buf, char *redir) pr("Command not implemented\n"); return 0; } - empth_rwlock_rdlock(update_lock); + empth_rwlock_rdlock(play_lock); if (redir) { prredir(redir); uprnf(buf); @@ -110,7 +110,7 @@ dispatch(char *buf, char *redir) logerror("%s: returned bad value", command->c_form); break; } - empth_rwlock_unlock(update_lock); + empth_rwlock_unlock(play_lock); player->command = 0; return 0; } diff --git a/src/server/main.c b/src/server/main.c index 637dbe4d..183b04d5 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -78,6 +78,19 @@ static void loc_NTInit(void); static void loc_NTTerm(void); #endif +/* + * Lock to synchronize player threads with update and shutdown. + * Update and shutdown takes it exclusive, commands take it shared. + */ +empth_rwlock_t *play_lock; + +/* + * Is a thread attempting to take an exclusive play_lock? + * Threads holding a shared play_lock must not sleep while this is + * true. + */ +int play_wrlock_wanted; + static char pidfname[] = "server.pid"; /* Run as daemon? If yes, detach from controlling terminal etc. */ @@ -373,7 +386,7 @@ shutdwn(int sig) } empth_wakeup(p->proc); } - empth_rwlock_wrlock(update_lock); + empth_rwlock_wrlock(play_lock); /* rely on player_kill_idle() for killing hung player threads */ if (sig) logerror("Server shutting down on signal %d", sig); diff --git a/src/server/update.c b/src/server/update.c index 2704cad1..1240cc31 100644 --- a/src/server/update.c +++ b/src/server/update.c @@ -48,18 +48,6 @@ #include "prototypes.h" #include "server.h" -/* - * Lock to synchronize player threads with the update. - * Update takes it exclusive, commands take it shared. - */ -empth_rwlock_t *update_lock; - -/* - * Update is pending, player threads must give up update_lock ASAP. - * This means they must not block while update_pending. - */ -int update_pending; - /* * Update is running. * Can be used to suppress messages, or direct them to bulletins. @@ -86,8 +74,8 @@ update_init(void) if (update_get_schedule() < 0) exit(1); - update_lock = empth_rwlock_create("Update"); - if (!update_lock) + play_lock = empth_rwlock_create("Update"); + if (!play_lock) exit_nomem(); dp = player_new(-1); @@ -205,7 +193,7 @@ update_run(void) { struct player *p; - update_pending = 1; + play_wrlock_wanted = 1; for (p = player_next(0); p != 0; p = player_next(p)) { if (p->state != PS_PLAYING) continue; @@ -215,18 +203,18 @@ update_run(void) empth_wakeup(p->proc); } } - empth_rwlock_wrlock(update_lock); + empth_rwlock_wrlock(play_lock); if (*pre_update_hook) { if (run_hook(pre_update_hook, "pre-update")) { - update_pending = 0; - empth_rwlock_unlock(update_lock); + play_wrlock_wanted = 0; + empth_rwlock_unlock(play_lock); return; } } update_running = 1; update_main(); - update_pending = update_running = 0; - empth_rwlock_unlock(update_lock); + play_wrlock_wanted = update_running = 0; + empth_rwlock_unlock(play_lock); } static int