Skip to content

Commit cf3a00c

Browse files
committed
Fix more places where multiple threads access the same data
1 parent ca8d921 commit cf3a00c

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

merge.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <queue>
66
#include <iterator>
77
#include <algorithm>
8+
#include <atomic>
89
#include <pthread.h>
910
#include <sys/mman.h>
1011
#include "merge.hpp"
@@ -22,7 +23,7 @@ struct merger {
2223
}
2324
};
2425

25-
unsigned char *do_merge1(std::vector<merger> &merges, size_t nmerges, unsigned char *f, int bytes, long long nrec, int zoom, bool quiet, volatile int *progress, size_t shard, size_t nshards, size_t also_todo, size_t also_did) {
26+
unsigned char *do_merge1(std::vector<merger> &merges, size_t nmerges, unsigned char *f, int bytes, long long nrec, int zoom, bool quiet, std::atomic<int> *progress, size_t shard, size_t nshards, size_t also_todo, size_t also_did) {
2627
std::priority_queue<merger> q;
2728

2829
unsigned long long mask = 0;
@@ -106,7 +107,7 @@ struct merge_arg {
106107
size_t also_todo;
107108
size_t also_did;
108109

109-
int *progress;
110+
std::atomic<int> *progress;
110111
size_t shard;
111112
size_t nshards;
112113
};
@@ -194,7 +195,7 @@ void do_merge(struct merge *merges, size_t nmerges, int f, int bytes, long long
194195
}
195196
}
196197

197-
int progress[cpus];
198+
std::atomic<int> progress[cpus];
198199
std::vector<merge_arg> args;
199200

200201
for (size_t i = 0; i < cpus; i++) {

tile.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <math.h>
1818
#include <time.h>
1919
#include <png.h>
20+
#include <atomic>
2021
#include "tippecanoe/projection.hpp"
2122
#include "protozero/varint.hpp"
2223
#include "protozero/pbf_reader.hpp"
@@ -86,7 +87,7 @@ struct tiler {
8687
sqlite3 *outdb;
8788
int maxzoom;
8889

89-
volatile int *progress;
90+
std::atomic<int> *progress;
9091
size_t shard;
9192
size_t cpus;
9293
std::string layername;
@@ -1322,6 +1323,7 @@ int main(int argc, char **argv) {
13221323
break;
13231324

13241325
default:
1326+
fprintf(stderr, "Unknown option -%c\n", i);
13251327
usage(argv);
13261328
exit(EXIT_FAILURE);
13271329
}
@@ -1383,13 +1385,13 @@ int main(int argc, char **argv) {
13831385
}
13841386

13851387
if (zooms == 0) {
1386-
if (optind + 1 != argc || (maxzoom < 0 && bin < 0) || outfile == NULL) {
1387-
usage(argv);
1388+
if (maxzoom < 0 && bin < 0) {
1389+
fprintf(stderr, "%s: Must specify either maxzoom (-z) or bin size (-s)\n", argv[0]);
13881390
exit(EXIT_FAILURE);
13891391
}
13901392

1391-
if (maxzoom < 0 && bin < 0) {
1392-
fprintf(stderr, "%s: Must specify either maxzoom (-z) or bin size (-s)\n", argv[0]);
1393+
if (optind + 1 != argc || (maxzoom < 0 && bin < 0) || outfile == NULL) {
1394+
usage(argv);
13931395
exit(EXIT_FAILURE);
13941396
}
13951397

@@ -1440,7 +1442,7 @@ int main(int argc, char **argv) {
14401442
}
14411443

14421444
for (size_t pass = 0; pass < 2; pass++) {
1443-
volatile int progress[cpus];
1445+
std::atomic<int> progress[cpus];
14441446
std::vector<tiler> tilers;
14451447
tilers.resize(cpus);
14461448

tippecanoe/projection.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string.h>
33
#include <stdlib.h>
44
#include <math.h>
5+
#include <atomic>
56
#include "projection.hpp"
67

78
struct projection projections[] = {
@@ -107,11 +108,11 @@ unsigned long long encode(unsigned int wx, unsigned int wy) {
107108
return out;
108109
}
109110

110-
static unsigned char decodex[256];
111-
static unsigned char decodey[256];
111+
static std::atomic<unsigned char> decodex[256];
112+
static std::atomic<unsigned char> decodey[256];
112113

113114
void decode(unsigned long long index, unsigned *wx, unsigned *wy) {
114-
static int initialized = 0;
115+
static std::atomic<int> initialized(0);
115116
if (!initialized) {
116117
for (size_t ix = 0; ix < 256; ix++) {
117118
size_t xx = 0, yy = 0;

0 commit comments

Comments
 (0)