]> git.pond.sub.org Git - empserver/blob - src/server/update.c
Update copyright notice.
[empserver] / src / server / update.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "empthread.h"
38 #include "prototypes.h"
39 #include "optlist.h"
40 #include "server.h"
41
42 empth_sem_t *update_sem;
43
44 static void update_wait(void *unused);
45 time_t update_time;
46
47 /*ARGSUSED*/
48 void
49 update_sched(void *unused)
50 {
51     int wind;
52     time_t now, delta;
53
54     update_sem = empth_sem_create("Update", 0);
55     empth_create(PP_SCHED, update_wait, (50 * 1024), 0, "UpdateWait",
56                  "Waits until players idle", 0);
57     time(&now);
58     if (s_p_etu <= 0) {
59         logerror("bad value for s_p_etu (%d)", s_p_etu);
60         s_p_etu = 2 * 60;
61         logerror("setting s_p_etu to %d", s_p_etu);
62     }
63     while (1) {
64         time(&now);
65         next_update_time(&now, &update_time, &delta);
66         if (update_window > 0) {
67             wind = (random() % update_window);
68             update_time += wind;
69             delta += wind;
70         }
71         logerror("Next update at %s", ctime(&update_time));
72         logerror("Next update in %ld seconds", (long)delta);
73         /* sleep until update is scheduled to go off */
74         empth_sleep(update_time);
75         time(&now);
76         now += adj_update;
77         if (!gamehours(now)) {
78             logerror("No update permitted (hours restriction)");
79             continue;
80         }
81         if (!updatetime(&now)) {
82             logerror("No update wanted");
83             continue;
84         }
85         if (updates_disabled()) {
86             logerror("Updates disabled...skipping update");
87             continue;
88         }
89         empth_sem_signal(update_sem);
90     }
91     /*NOTREACHED*/
92 }
93
94 /*ARGSUSED*/
95 static void
96 update_wait(void *unused)
97 {
98     struct player *p;
99     int running;
100     time_t now;
101     int stacksize;
102     struct player *dp;
103
104     while (1) {
105         empth_sem_wait(update_sem);
106         running = 0;
107         for (p = player_next(0); p != 0; p = player_next(p)) {
108             if (p->state != PS_PLAYING)
109                 continue;
110             if (p->command) {
111                 pr_flash(p, "Update aborting command\n");
112                 p->aborted = 1;
113                 empth_wakeup(p->proc);
114                 running++;
115             }
116         }
117         time(&now);
118         if (running) {
119             /* sleep a few, wait for aborts to take effect */
120             empth_sleep(now + 2);
121         }
122         /* 
123          * we rely on the fact that update's priority is the highest
124          * in the land so it can finish before it yields.
125          */
126         dp = player_new(0, 0);
127         if (!dp) {
128             logerror("can't create dummy player for update");
129             continue;
130         }
131         stacksize = 100000 +
132 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
133                                         sizeof(s_char *));
134
135         empth_create(PP_UPDATE, update_main, stacksize, 0,
136                      "UpdateRun", "Updates the world", dp);
137     }
138     /*NOTREACHED*/
139 }