X-Git-Url: http://git.pond.sub.org/?p=empserver;a=blobdiff_plain;f=src%2Fserver%2Fmain.c;h=52c4c8cc3fb2eb72494b8cf155e96979aaf9683c;hp=a3010419ca2386ce9a1a6fa6a0b2b3272735f09f;hb=9102ecce5;hpb=86f029457300e29749d19a098ab15bdd2c1dfd5d diff --git a/src/server/main.c b/src/server/main.c index a3010419c..52c4c8cc3 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -1,6 +1,6 @@ /* * Empire - A multi-player, client/server Internet based war game. - * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak, + * Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak, * Ken Stevens, Steve McClure, Markus Armbruster * * Empire is free software: you can redistribute it and/or modify @@ -31,7 +31,7 @@ * Steve McClure, 1996, 1998 * Doug Hay, 1998 * Ron Koenderink, 2004-2009 - * Markus Armbruster, 2005-2011 + * Markus Armbruster, 2005-2012 */ #include @@ -50,6 +50,7 @@ #include "sys/socket.h" #endif +#include "chance.h" #include "empio.h" #include "empthread.h" #include "file.h" @@ -75,10 +76,16 @@ static void loc_NTInit(void); #endif /* - * Lock to synchronize player threads with update and shutdown. - * Update and shutdown takes it exclusive, commands take it shared. + * Lock to synchronize player threads with update. + * Update holds it exclusive, commands hold it shared. */ -empth_rwlock_t *play_lock; +empth_rwlock_t *update_lock; +/* + * Lock to synchronize player threads with shutdown. + * Shutdown holds it exclusive, player threads in state PS_PLAYING + * hold it shared. + */ +empth_rwlock_t *shutdown_lock; static char pidfname[] = "server.pid"; @@ -136,7 +143,8 @@ main(int argc, char **argv) char *config_file = NULL; int force_bad_state = 0; int op, idx, sig; - unsigned seed = time(NULL); + unsigned seed = 0; + int seed_set = 0; oops_handler = ignore; @@ -187,6 +195,7 @@ main(int argc, char **argv) break; case 'R': seed = strtoul(optarg, NULL, 10); + seed_set = 1; break; case 'v': printf("%s\n\n%s", version, legal); @@ -247,6 +256,8 @@ main(int argc, char **argv) return install_service(program_name, service_name, config_file); #endif /* _WIN32 */ + if (!seed_set) + seed = pick_seed(); init_server(seed, force_bad_state); #if defined(_WIN32) @@ -280,7 +291,6 @@ main(int argc, char **argv) } #endif /* !_WIN32 */ start_server(flags); - journal_prng(seed); for (;;) { sig = empth_wait_for_signal(); @@ -344,7 +354,7 @@ crash_dump(void) static void init_server(unsigned seed, int force_bad_state) { - srandom(seed); + seed_prng(seed); #if defined(_WIN32) loc_NTInit(); #endif @@ -353,6 +363,9 @@ init_server(unsigned seed, int force_bad_state) io_init(); init_nreport(); + if (journal_startup() < 0) + exit(1); + journal_prng(seed); loginit("server"); } @@ -371,8 +384,10 @@ start_server(int flags) empth_init((void **)&player, flags); - if (journal_startup() < 0) - exit(1); + update_lock = empth_rwlock_create("Update"); + shutdown_lock = empth_rwlock_create("Shutdown"); + if (!update_lock || !shutdown_lock) + exit_nomem(); market_init(); update_init(); @@ -423,9 +438,10 @@ shutdwn(int sig) } empth_wakeup(p->proc); } - empth_rwlock_wrlock(play_lock); + empth_rwlock_wrlock(shutdown_lock); empth_yield(); + for (i = 1; i <= 3 && player_next(NULL); i++) { logerror("Waiting for player threads to terminate\n"); empth_sleep(now + i);