]> git.pond.sub.org Git - empserver/blob - src/server/update.c
Fuse update wait thread and update thread:
[empserver] / src / server / update.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  update.c: Update scheduler
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 1996
33  *     Ron Koenderink, 2005
34  */
35
36 #include <config.h>
37
38 #include <errno.h>
39 #ifndef _WIN32
40 #include <sys/wait.h>
41 #endif
42 #include <time.h>
43 #include "empthread.h"
44 #include "misc.h"
45 #include "optlist.h"
46 #include "player.h"
47 #include "prototypes.h"
48 #include "server.h"
49
50 empth_sem_t *update_sem;
51 empth_rwlock_t *update_lock;
52 int update_pending;
53 time_t update_time;
54
55 static void update_sched(void *);
56 static void update_wait(void *unused);
57 static int run_hook(char *cmd, char *name);
58
59 void
60 update_init(void)
61 {
62     struct player *dp;
63     int stacksize;
64
65     update_sem = empth_sem_create("Update", 0);
66     update_lock = empth_rwlock_create("Update");
67     if (!update_sem || !update_lock)
68         exit_nomem();
69
70     dp = player_new(-1);
71     if (!dp)
72         exit_nomem();
73     /* FIXME ancient black magic; figure out true stack need */
74     stacksize = 100000 +
75 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
76                                         sizeof(char *));
77     if (!empth_create(PP_UPDATE, update_wait, stacksize, 0,
78                       "Update", "Updates the world", dp))
79         exit_nomem();
80
81     if (!empth_create(PP_SCHED, update_sched, 50 * 1024, 0,
82                       "UpdateSched", "Schedules updates to occur", NULL))
83         exit_nomem();
84 }
85
86 /*ARGSUSED*/
87 static void
88 update_sched(void *unused)
89 {
90     int wind;
91     time_t now, delta;
92
93     if (s_p_etu <= 0) {
94         logerror("bad value for s_p_etu (%d)", s_p_etu);
95         s_p_etu = 2 * 60;
96         logerror("setting s_p_etu to %d", s_p_etu);
97     }
98     while (1) {
99         time(&now);
100         next_update_time(&now, &update_time, &delta);
101         if (update_window > 0) {
102             wind = (random() % update_window);
103             update_time += wind;
104             delta += wind;
105         }
106         logerror("Next update at %s", ctime(&update_time));
107         logerror("Next update in %ld seconds", (long)delta);
108         /* sleep until update is scheduled to go off */
109         empth_sleep(update_time);
110         time(&now);
111         now += adj_update;
112         if (!gamehours(now)) {
113             logerror("No update permitted (hours restriction)");
114             continue;
115         }
116         if (!updatetime(&now)) {
117             logerror("No update wanted");
118             continue;
119         }
120         if (updates_disabled()) {
121             logerror("Updates disabled...skipping update");
122             continue;
123         }
124         empth_sem_signal(update_sem);
125     }
126     /*NOTREACHED*/
127 }
128
129 void
130 update_force(void *seconds)
131 {
132     time_t now;
133
134     time(&now);
135     empth_sleep(now + *(int *)seconds);
136     empth_sem_signal(update_sem);
137     empth_exit();
138 }
139
140 /*ARGSUSED*/
141 static void
142 update_wait(void *unused)
143 {
144     struct player *p;
145
146     player->proc = empth_self();
147     player->cnum = 0;
148     player->god = 1;
149
150     while (1) {
151         empth_sem_wait(update_sem);
152         update_pending = 1;
153         for (p = player_next(0); p != 0; p = player_next(p)) {
154             if (p->state != PS_PLAYING)
155                 continue;
156             if (p->command) {
157                 pr_flash(p, "Update aborting command\n");
158                 p->aborted = 1;
159                 empth_wakeup(p->proc);
160             }
161         }
162         empth_rwlock_wrlock(update_lock);
163         if (*pre_update_hook) {
164             if (run_hook(pre_update_hook, "pre-update")) {
165                 update_pending = 0;
166                 empth_rwlock_unlock(update_lock);
167                 continue;
168             }
169         }
170         update_main();
171         update_pending = 0;
172         empth_rwlock_unlock(update_lock);
173     }
174     /*NOTREACHED*/
175 }
176
177 static int
178 run_hook(char *cmd, char *name)
179 {
180     int status;
181     
182     fflush(NULL);
183     
184     status = system(cmd);
185     if (status == 0)
186         ;                       /* successful exit */
187     else if (status == -1)
188         logerror("couldn't execute command processor for %s hook (%s)",
189                  name, strerror(errno));
190 #ifndef _WIN32
191     else if (WIFEXITED(status))
192         logerror("%s hook terminated unsuccessfully (exit status %d)",
193                  name, WEXITSTATUS(status));
194     else if (WIFSIGNALED(status))
195         logerror("%s hook terminated abnormally (signal %d)",
196                  name, WTERMSIG(status));
197 #endif
198     else if (status)
199         logerror("%s hook terminated strangely (status %d)",
200                  name, status);
201     return status;
202 }