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 empth_sem_t *update_sem;
52 empth_rwlock_t *update_lock;
56 static void update_sched(void *);
57 static void update_force(void *);
58 static void update_wait(void *unused);
59 static int run_hook(char *cmd, char *name);
67 update_sem = empth_sem_create("Update", 0);
68 update_lock = empth_rwlock_create("Update");
69 if (!update_sem || !update_lock)
75 /* FIXME ancient black magic; figure out true stack need */
77 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
79 if (!empth_create(PP_UPDATE, update_wait, stacksize, 0,
80 "Update", "Updates the world", dp))
83 if (!empth_create(PP_SCHED, update_sched, 50 * 1024, 0,
84 "UpdateSched", "Schedules updates to occur", NULL))
90 update_sched(void *unused)
96 logerror("bad value for s_p_etu (%d)", s_p_etu);
98 logerror("setting s_p_etu to %d", s_p_etu);
102 next_update_time(&now, &update_time, &delta);
103 if (update_window > 0) {
104 wind = (random() % update_window);
108 logerror("Next update at %s", ctime(&update_time));
109 logerror("Next update in %ld seconds", (long)delta);
110 /* sleep until update is scheduled to go off */
111 empth_sleep(update_time);
114 if (!gamehours(now)) {
115 logerror("No update permitted (hours restriction)");
118 if (!updatetime(&now)) {
119 logerror("No update wanted");
122 if (updates_disabled()) {
123 logerror("Updates disabled...skipping update");
126 empth_sem_signal(update_sem);
132 * Trigger an update SECS_FROM_NOW seconds from now.
133 * Return 0 on success, -1 on failure.
136 update_trigger(time_t secs_from_now)
140 if (secs_from_now < 0)
143 if (secs_from_now == 0) {
144 empth_sem_signal(update_sem);
148 /* FIXME make triggers overwrite, not accumulate */
149 secp = malloc(sizeof(time_t));
152 *secp = secs_from_now;
153 if (!empth_create(PP_SCHED, update_force, 50 * 1024, 0, "forceUpdate",
154 "Schedules an update", secp))
160 update_force(void *seconds)
165 empth_sleep(now + *(time_t *)seconds);
166 empth_sem_signal(update_sem);
173 update_wait(void *unused)
177 player->proc = empth_self();
182 empth_sem_wait(update_sem);
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);
209 run_hook(char *cmd, char *name)
215 status = system(cmd);
217 ; /* successful exit */
218 else if (status == -1)
219 logerror("couldn't execute command processor for %s hook (%s)",
220 name, strerror(errno));
222 else if (WIFEXITED(status))
223 logerror("%s hook terminated unsuccessfully (exit status %d)",
224 name, WEXITSTATUS(status));
225 else if (WIFSIGNALED(status))
226 logerror("%s hook terminated abnormally (signal %d)",
227 name, WTERMSIG(status));
230 logerror("%s hook terminated strangely (status %d)",