-
Notifications
You must be signed in to change notification settings - Fork 2
/
port_update.c
180 lines (139 loc) · 4.04 KB
/
port_update.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "port.h"
#include "cargo.h"
#include "item.h"
#include "log.h"
#include "universe.h"
#define PORT_UPDATE_INTERVAL 10 /* in seconds, no larger than once a day */
#define SECONDS_PER_DAY (24 * 60 * 60)
#define PORT_UPDATE_FRACTION (SECONDS_PER_DAY / PORT_UPDATE_INTERVAL)
pthread_t thread;
int terminate;
pthread_condattr_t termination_attr;
pthread_cond_t termination_cond;
pthread_mutex_t termination_lock;
static void update_port(struct port *port, uint32_t iteration)
{
struct cargo *cargo;
long change, mod, fraction_iteration;
pthread_rwlock_wrlock(&port->items_lock);
list_for_each_entry(cargo, &port->items, list) {
if (!cargo->daily_change)
continue;
change = cargo->daily_change / PORT_UPDATE_FRACTION;
mod = cargo->daily_change % PORT_UPDATE_FRACTION;
fraction_iteration = PORT_UPDATE_FRACTION / cargo->daily_change;
if (mod && fraction_iteration && iteration % fraction_iteration == 0) {
if (cargo->daily_change > 0)
change++;
else if (cargo->daily_change < 0)
change--;
}
if (change < 0 && cargo->amount < -change)
change = -cargo->amount;
else if (change > 0 && cargo->max - cargo->amount < change)
change = cargo->max - cargo->amount;
struct cargo *req;
struct list_head *lh;
ptrlist_for_each_entry(req, &cargo->requires, lh) {
if (change > 0 && req->amount < change)
change = req->amount;
else if (change < 0 && req->amount < -change)
change = -req->amount;
}
cargo->amount += change;
if (change < 0) {
ptrlist_for_each_entry(req, &cargo->requires, lh)
req->amount += change;
} else {
ptrlist_for_each_entry(req, &cargo->requires, lh)
req->amount -= change;
}
}
pthread_rwlock_unlock(&port->items_lock);
}
static void update_all_ports(uint32_t iteration)
{
struct port *port;
list_for_each_entry(port, &univ.ports, list)
update_port(port, iteration);
}
static void* port_update_worker(void *ptr)
{
struct timespec next, now;
uint32_t iteration = 0;
if (clock_gettime(CLOCK_MONOTONIC, &next))
goto clock_err;
do {
pthread_rwlock_rdlock(&univ.ports_lock);
update_all_ports(iteration);
pthread_rwlock_unlock(&univ.ports_lock);
iteration++;
if (iteration >= PORT_UPDATE_FRACTION)
iteration = 0;
next.tv_sec += PORT_UPDATE_INTERVAL;
do {
pthread_mutex_lock(&termination_lock);
if (terminate) {
pthread_mutex_unlock(&termination_lock);
break;
}
pthread_cond_timedwait(&termination_cond, &termination_lock, &next);
pthread_mutex_unlock(&termination_lock);
if (clock_gettime(CLOCK_MONOTONIC, &now))
goto clock_err;
} while (!terminate && now.tv_sec < next.tv_sec);
} while (!terminate);
return NULL;
clock_err:
log_printfn(LOG_PORT_UPDATE, "clock_gettime() failed");
return NULL;
}
int start_updating_ports(void)
{
sigset_t old, new;
sigfillset(&new);
if (pthread_condattr_init(&termination_attr))
goto err;
if (pthread_condattr_setclock(&termination_attr, CLOCK_MONOTONIC))
goto err_free_attr;
if (pthread_mutex_init(&termination_lock, NULL))
goto err_free_attr;
if (pthread_cond_init(&termination_cond, &termination_attr))
goto err_free_mutex;
if (pthread_sigmask(SIG_SETMASK, &new, &old))
goto err_free_mutex;
if (pthread_create(&thread, NULL, port_update_worker, NULL))
goto err_free_cond;
if (pthread_sigmask(SIG_SETMASK, &old, NULL))
goto err_cancel_thread;
return 0;
err_cancel_thread:
pthread_cancel(thread);
err_free_cond:
pthread_cond_destroy(&termination_cond);
err_free_mutex:
pthread_mutex_destroy(&termination_lock);
err_free_attr:
pthread_condattr_destroy(&termination_attr);
err:
return -1;
}
void stop_updating_ports(void)
{
pthread_mutex_lock(&termination_lock);
terminate = 1;
pthread_cond_signal(&termination_cond);
pthread_mutex_unlock(&termination_lock);
pthread_join(thread, NULL);
pthread_cond_destroy(&termination_cond);
pthread_condattr_destroy(&termination_attr);
pthread_mutex_destroy(&termination_lock);
}