2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * update.c: Update scheduler
30 * Known contributors to this file:
33 * Ron Koenderink, 2005
34 * Markus Armbruster, 2007
44 #include "empthread.h"
48 #include "prototypes.h"
51 static empth_t *update_thread;
52 empth_rwlock_t *update_lock;
55 static int update_forced;
57 static void update_sched(void *);
58 static void update_force(void *);
59 static void update_run(void);
60 static int run_hook(char *cmd, char *name);
69 logerror("bad value for s_p_etu (%d)", s_p_etu);
71 logerror("setting s_p_etu to %d", s_p_etu);
74 update_lock = empth_rwlock_create("Update");
81 /* FIXME ancient black magic; figure out true stack need */
83 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
85 update_thread = empth_create(PP_UPDATE, update_sched, stacksize, 0,
93 update_sched(void *unused)
98 player->proc = empth_self();
104 next_update_time(&now, &update_time, &delta);
105 if (update_window > 0) {
106 wind = (random() % update_window);
110 logerror("Next update at %s", ctime(&update_time));
111 logerror("Next update in %ld seconds", (long)delta);
112 /* sleep until update is scheduled to go off */
114 empth_sleep(update_time);
115 if (!update_forced) {
118 if (!gamehours(now)) {
119 logerror("No update permitted (hours restriction)");
122 if (!updatetime(&now)) {
123 logerror("No update wanted");
126 if (updates_disabled()) {
127 logerror("Updates disabled...skipping update");
137 * Trigger an update SECS_FROM_NOW seconds from now.
138 * Return 0 on success, -1 on failure.
141 update_trigger(time_t secs_from_now)
145 if (secs_from_now < 0)
148 if (secs_from_now == 0) {
150 empth_wakeup(update_thread);
154 /* FIXME make triggers overwrite, not accumulate */
155 secp = malloc(sizeof(time_t));
158 *secp = secs_from_now;
159 if (!empth_create(PP_SCHED, update_force, 50 * 1024, 0,
160 "forceUpdate", secp))
166 update_force(void *seconds)
171 empth_sleep(now + *(time_t *)seconds);
173 empth_wakeup(update_thread);
184 for (p = player_next(0); p != 0; p = player_next(p)) {
185 if (p->state != PS_PLAYING)
188 pr_flash(p, "Update aborting command\n");
190 empth_wakeup(p->proc);
193 empth_rwlock_wrlock(update_lock);
194 if (*pre_update_hook) {
195 if (run_hook(pre_update_hook, "pre-update")) {
197 empth_rwlock_unlock(update_lock);
203 empth_rwlock_unlock(update_lock);
207 run_hook(char *cmd, char *name)
213 status = system(cmd);
215 ; /* successful exit */
216 else if (status == -1)
217 logerror("couldn't execute command processor for %s hook (%s)",
218 name, strerror(errno));
220 else if (WIFEXITED(status))
221 logerror("%s hook terminated unsuccessfully (exit status %d)",
222 name, WEXITSTATUS(status));
223 else if (WIFSIGNALED(status))
224 logerror("%s hook terminated abnormally (signal %d)",
225 name, WTERMSIG(status));
228 logerror("%s hook terminated strangely (status %d)",