-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathgdalproxypool.cpp
More file actions
1675 lines (1406 loc) · 59 KB
/
gdalproxypool.cpp
File metadata and controls
1675 lines (1406 loc) · 59 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Project: GDAL Core
* Purpose: A dataset and raster band classes that differ the opening of the
* underlying dataset in a limited pool of opened datasets.
* Author: Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_port.h"
#include "gdal_proxy.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_hash_set.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
#include "gdal.h"
#include "gdal_priv.h"
//! @cond Doxygen_Suppress
/* We *must* share the same mutex as the gdaldataset.cpp file, as we are */
/* doing GDALOpen() calls that can indirectly call GDALOpenShared() on */
/* an auxiliary dataset ... */
/* Then we could get dead-locks in multi-threaded use case */
/* ******************************************************************** */
/* GDALDatasetPool */
/* ******************************************************************** */
/* This class is a singleton that maintains a pool of opened datasets */
/* The cache uses a LRU strategy */
class GDALDatasetPool;
static GDALDatasetPool *singleton = nullptr;
void GDALNullifyProxyPoolSingleton()
{
singleton = nullptr;
}
struct _GDALProxyPoolCacheEntry
{
GIntBig responsiblePID;
char *pszFileNameAndOpenOptions;
char *pszOwner;
GDALDataset *poDS;
GIntBig nRAMUsage;
/* Ref count of the cached dataset */
int refCount;
GDALProxyPoolCacheEntry *prev;
GDALProxyPoolCacheEntry *next;
};
// This variable prevents a dataset that is going to be opened in
// GDALDatasetPool::_RefDataset from increasing refCount if, during its
// opening, it creates a GDALProxyPoolDataset.
// We increment it before opening or closing a cached dataset and decrement
// it afterwards
// The typical use case is a VRT made of simple sources that are VRT
// We don't want the "inner" VRT to take a reference on the pool, otherwise
// there is a high chance that this reference will not be dropped and the pool
// remain ghost.
static thread_local int refCountOfDisabledRefCount = 0;
class GDALDatasetPool
{
private:
bool bInDestruction = false;
/* Ref count of the pool singleton */
/* Taken by "toplevel" GDALProxyPoolDataset in its constructor and released
*/
/* in its destructor. See also refCountOfDisabledRefCount for the difference
*/
/* between toplevel and inner GDALProxyPoolDataset */
int refCount = 0;
int maxSize = 0;
int currentSize = 0;
int64_t nMaxRAMUsage = 0;
int64_t nRAMUsage = 0;
GDALProxyPoolCacheEntry *firstEntry = nullptr;
GDALProxyPoolCacheEntry *lastEntry = nullptr;
/* Caution : to be sure that we don't run out of entries, size must be at */
/* least greater or equal than the maximum number of threads */
explicit GDALDatasetPool(int maxSize, int64_t nMaxRAMUsage);
~GDALDatasetPool();
GDALProxyPoolCacheEntry *
_RefDataset(const char *pszFileName, GDALAccess eAccess,
CSLConstList papszOpenOptions, int bShared, bool bForceOpen,
const char *pszOwner, CSLConstList papszAllowedDrivers);
void _CloseDatasetIfZeroRefCount(const char *pszFileName,
CSLConstList papszOpenOptions,
GDALAccess eAccess, const char *pszOwner);
#ifdef DEBUG_PROXY_POOL
// cppcheck-suppress unusedPrivateFunction
void ShowContent();
void CheckLinks();
#endif
CPL_DISALLOW_COPY_ASSIGN(GDALDatasetPool)
public:
static void Ref();
static void Unref();
static GDALProxyPoolCacheEntry *
RefDataset(const char *pszFileName, GDALAccess eAccess,
char **papszOpenOptions, int bShared, bool bForceOpen,
const char *pszOwner, CSLConstList papszAllowedDrivers);
static void UnrefDataset(GDALProxyPoolCacheEntry *cacheEntry);
static void CloseDatasetIfZeroRefCount(const char *pszFileName,
CSLConstList papszOpenOptions,
GDALAccess eAccess,
const char *pszOwner);
static void PreventDestroy();
static void ForceDestroy();
};
/************************************************************************/
/* GDALDatasetPool() */
/************************************************************************/
GDALDatasetPool::GDALDatasetPool(int maxSizeIn, int64_t nMaxRAMUsageIn)
: maxSize(maxSizeIn), nMaxRAMUsage(nMaxRAMUsageIn)
{
}
/************************************************************************/
/* ~GDALDatasetPool() */
/************************************************************************/
GDALDatasetPool::~GDALDatasetPool()
{
bInDestruction = true;
GDALProxyPoolCacheEntry *cur = firstEntry;
GIntBig responsiblePID = GDALGetResponsiblePIDForCurrentThread();
while (cur)
{
GDALProxyPoolCacheEntry *next = cur->next;
CPLFree(cur->pszFileNameAndOpenOptions);
CPLFree(cur->pszOwner);
CPLAssert(cur->refCount == 0);
if (cur->poDS)
{
GDALSetResponsiblePIDForCurrentThread(cur->responsiblePID);
GDALClose(cur->poDS);
}
CPLFree(cur);
cur = next;
}
GDALSetResponsiblePIDForCurrentThread(responsiblePID);
}
#ifdef DEBUG_PROXY_POOL
/************************************************************************/
/* ShowContent() */
/************************************************************************/
void GDALDatasetPool::ShowContent()
{
GDALProxyPoolCacheEntry *cur = firstEntry;
int i = 0;
while (cur)
{
printf("[%d] pszFileName=%s, owner=%s, refCount=%d, " /*ok*/
"responsiblePID=%d\n",
i,
cur->pszFileNameAndOpenOptions ? cur->pszFileNameAndOpenOptions
: "(null)",
cur->pszOwner ? cur->pszOwner : "(null)", cur->refCount,
(int)cur->responsiblePID);
i++;
cur = cur->next;
}
}
/************************************************************************/
/* CheckLinks() */
/************************************************************************/
void GDALDatasetPool::CheckLinks()
{
GDALProxyPoolCacheEntry *cur = firstEntry;
int i = 0;
while (cur)
{
CPLAssert(cur == firstEntry || cur->prev->next == cur);
CPLAssert(cur == lastEntry || cur->next->prev == cur);
++i;
CPLAssert(cur->next != nullptr || cur == lastEntry);
cur = cur->next;
}
(void)i;
CPLAssert(i == currentSize);
}
#endif
/************************************************************************/
/* GetFilenameAndOpenOptions() */
/************************************************************************/
static std::string GetFilenameAndOpenOptions(const char *pszFileName,
CSLConstList papszOpenOptions)
{
std::string osFilenameAndOO(pszFileName);
for (int i = 0; papszOpenOptions && papszOpenOptions[i]; ++i)
{
osFilenameAndOO += "||";
osFilenameAndOO += papszOpenOptions[i];
}
return osFilenameAndOO;
}
/************************************************************************/
/* _RefDataset() */
/************************************************************************/
GDALProxyPoolCacheEntry *
GDALDatasetPool::_RefDataset(const char *pszFileName, GDALAccess eAccess,
CSLConstList papszOpenOptions, int bShared,
bool bForceOpen, const char *pszOwner,
CSLConstList papszAllowedDrivers)
{
CPLMutex **pMutex = GDALGetphDLMutex();
CPLMutexHolderD(pMutex);
if (bInDestruction)
return nullptr;
const GIntBig responsiblePID = GDALGetResponsiblePIDForCurrentThread();
const auto EvictEntryWithZeroRefCount =
[this, responsiblePID](bool evictEntryWithOpenedDataset)
{
GDALProxyPoolCacheEntry *cur = firstEntry;
GDALProxyPoolCacheEntry *candidate = nullptr;
while (cur)
{
GDALProxyPoolCacheEntry *next = cur->next;
if (cur->refCount == 0 &&
(!evictEntryWithOpenedDataset || cur->nRAMUsage > 0))
{
candidate = cur;
}
cur = next;
}
if (candidate == nullptr)
return false;
nRAMUsage -= candidate->nRAMUsage;
candidate->nRAMUsage = 0;
CPLFree(candidate->pszFileNameAndOpenOptions);
candidate->pszFileNameAndOpenOptions = nullptr;
if (candidate->poDS)
{
/* Close by pretending we are the thread that GDALOpen'ed this */
/* dataset */
GDALSetResponsiblePIDForCurrentThread(candidate->responsiblePID);
refCountOfDisabledRefCount++;
GDALClose(candidate->poDS);
refCountOfDisabledRefCount--;
candidate->poDS = nullptr;
GDALSetResponsiblePIDForCurrentThread(responsiblePID);
}
CPLFree(candidate->pszOwner);
candidate->pszOwner = nullptr;
if (!evictEntryWithOpenedDataset && candidate != firstEntry)
{
/* Recycle this entry for the to-be-opened dataset and */
/* moves it to the top of the list */
if (candidate->prev)
candidate->prev->next = candidate->next;
if (candidate->next)
candidate->next->prev = candidate->prev;
else
{
CPLAssert(candidate == lastEntry);
lastEntry->prev->next = nullptr;
lastEntry = lastEntry->prev;
}
candidate->prev = nullptr;
candidate->next = firstEntry;
firstEntry->prev = candidate;
firstEntry = candidate;
#ifdef DEBUG_PROXY_POOL
CheckLinks();
#endif
}
return true;
};
GDALProxyPoolCacheEntry *cur = firstEntry;
const std::string osFilenameAndOO =
GetFilenameAndOpenOptions(pszFileName, papszOpenOptions);
while (cur)
{
GDALProxyPoolCacheEntry *next = cur->next;
if (cur->refCount >= 0 && cur->pszFileNameAndOpenOptions &&
osFilenameAndOO == cur->pszFileNameAndOpenOptions &&
((bShared && cur->responsiblePID == responsiblePID &&
((cur->pszOwner == nullptr && pszOwner == nullptr) ||
(cur->pszOwner != nullptr && pszOwner != nullptr &&
strcmp(cur->pszOwner, pszOwner) == 0))) ||
(!bShared && cur->refCount == 0)))
{
if (cur != firstEntry)
{
/* Move to begin */
if (cur->next)
cur->next->prev = cur->prev;
else
lastEntry = cur->prev;
cur->prev->next = cur->next;
cur->prev = nullptr;
firstEntry->prev = cur;
cur->next = firstEntry;
firstEntry = cur;
#ifdef DEBUG_PROXY_POOL
CheckLinks();
#endif
}
cur->refCount++;
return cur;
}
cur = next;
}
if (!bForceOpen)
return nullptr;
if (currentSize == maxSize)
{
if (!EvictEntryWithZeroRefCount(false))
{
CPLError(
CE_Failure, CPLE_AppDefined,
"Too many threads are running for the current value of the "
"dataset pool size (%d).\n"
"or too many proxy datasets are opened in a cascaded way.\n"
"Try increasing GDAL_MAX_DATASET_POOL_SIZE.",
maxSize);
return nullptr;
}
CPLAssert(firstEntry);
cur = firstEntry;
}
else
{
/* Prepend */
cur = static_cast<GDALProxyPoolCacheEntry *>(
CPLCalloc(1, sizeof(GDALProxyPoolCacheEntry)));
if (lastEntry == nullptr)
lastEntry = cur;
cur->prev = nullptr;
cur->next = firstEntry;
if (firstEntry)
firstEntry->prev = cur;
firstEntry = cur;
currentSize++;
#ifdef DEBUG_PROXY_POOL
CheckLinks();
#endif
}
cur->pszFileNameAndOpenOptions = CPLStrdup(osFilenameAndOO.c_str());
cur->pszOwner = (pszOwner) ? CPLStrdup(pszOwner) : nullptr;
cur->responsiblePID = responsiblePID;
cur->refCount = -1; // to mark loading of dataset in progress
cur->nRAMUsage = 0;
refCountOfDisabledRefCount++;
const int nFlag =
((eAccess == GA_Update) ? GDAL_OF_UPDATE : GDAL_OF_READONLY) |
GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR;
CPLConfigOptionSetter oSetter("CPL_ALLOW_VSISTDIN", "NO", true);
// Release mutex while opening dataset to avoid lock contention.
CPLReleaseMutex(*pMutex);
auto poDS = GDALDataset::Open(pszFileName, nFlag, papszAllowedDrivers,
papszOpenOptions, nullptr);
CPLAcquireMutex(*pMutex, 1000.0);
cur->poDS = poDS;
cur->refCount = 1;
refCountOfDisabledRefCount--;
if (cur->poDS)
{
cur->nRAMUsage =
std::max<GIntBig>(0, cur->poDS->GetEstimatedRAMUsage());
nRAMUsage += cur->nRAMUsage;
}
if (nMaxRAMUsage > 0 && cur->nRAMUsage > 0)
{
while (nRAMUsage > nMaxRAMUsage && nRAMUsage != cur->nRAMUsage &&
EvictEntryWithZeroRefCount(true))
{
// ok
}
}
return cur;
}
/************************************************************************/
/* _CloseDatasetIfZeroRefCount() */
/************************************************************************/
void GDALDatasetPool::_CloseDatasetIfZeroRefCount(const char *pszFileName,
CSLConstList papszOpenOptions,
GDALAccess /* eAccess */,
const char *pszOwner)
{
// May fix https://github.com/OSGeo/gdal/issues/4318
if (bInDestruction)
return;
GDALProxyPoolCacheEntry *cur = firstEntry;
GIntBig responsiblePID = GDALGetResponsiblePIDForCurrentThread();
const std::string osFilenameAndOO =
GetFilenameAndOpenOptions(pszFileName, papszOpenOptions);
while (cur)
{
GDALProxyPoolCacheEntry *next = cur->next;
if (cur->refCount == 0 && cur->pszFileNameAndOpenOptions &&
osFilenameAndOO == cur->pszFileNameAndOpenOptions &&
((pszOwner == nullptr && cur->pszOwner == nullptr) ||
(pszOwner != nullptr && cur->pszOwner != nullptr &&
strcmp(cur->pszOwner, pszOwner) == 0)) &&
cur->poDS != nullptr)
{
/* Close by pretending we are the thread that GDALOpen'ed this */
/* dataset */
GDALSetResponsiblePIDForCurrentThread(cur->responsiblePID);
GDALDataset *poDS = cur->poDS;
nRAMUsage -= cur->nRAMUsage;
cur->nRAMUsage = 0;
cur->poDS = nullptr;
CPLFree(cur->pszFileNameAndOpenOptions);
cur->pszFileNameAndOpenOptions = nullptr;
CPLFree(cur->pszOwner);
cur->pszOwner = nullptr;
refCountOfDisabledRefCount++;
GDALClose(poDS);
refCountOfDisabledRefCount--;
GDALSetResponsiblePIDForCurrentThread(responsiblePID);
break;
}
cur = next;
}
}
/************************************************************************/
/* GDALGetMaxDatasetPoolSize() */
/************************************************************************/
/** Return the maximum number of datasets simultaneously opened in the
* dataset pool.
*/
int GDALGetMaxDatasetPoolSize()
{
int nSize = atoi(CPLGetConfigOption("GDAL_MAX_DATASET_POOL_SIZE", "100"));
if (nSize < 2)
nSize = 2;
else if (nSize > 1000)
nSize = 1000;
return nSize;
}
/************************************************************************/
/* Ref() */
/************************************************************************/
void GDALDatasetPool::Ref()
{
CPLMutexHolderD(GDALGetphDLMutex());
if (singleton == nullptr)
{
// Try to not consume more than 25% of the usable RAM
GIntBig l_nMaxRAMUsage =
(CPLGetUsablePhysicalRAM() - GDALGetCacheMax64()) / 4;
const char *pszMaxRAMUsage =
CPLGetConfigOption("GDAL_MAX_DATASET_POOL_RAM_USAGE", nullptr);
if (pszMaxRAMUsage)
{
l_nMaxRAMUsage = std::strtoll(pszMaxRAMUsage, nullptr, 10);
if (strstr(pszMaxRAMUsage, "MB"))
l_nMaxRAMUsage *= 1024 * 1024;
else if (strstr(pszMaxRAMUsage, "GB"))
l_nMaxRAMUsage *= 1024 * 1024 * 1024;
}
singleton =
new GDALDatasetPool(GDALGetMaxDatasetPoolSize(), l_nMaxRAMUsage);
}
if (refCountOfDisabledRefCount == 0)
singleton->refCount++;
}
/* keep that in sync with gdaldrivermanager.cpp */
void GDALDatasetPool::PreventDestroy()
{
CPLMutexHolderD(GDALGetphDLMutex());
if (!singleton)
return;
refCountOfDisabledRefCount++;
}
/* keep that in sync with gdaldrivermanager.cpp */
extern void GDALDatasetPoolPreventDestroy();
void GDALDatasetPoolPreventDestroy()
{
GDALDatasetPool::PreventDestroy();
}
/************************************************************************/
/* Unref() */
/************************************************************************/
void GDALDatasetPool::Unref()
{
CPLMutexHolderD(GDALGetphDLMutex());
if (!singleton)
{
CPLAssert(false);
return;
}
if (refCountOfDisabledRefCount == 0)
{
singleton->refCount--;
if (singleton->refCount == 0)
{
delete singleton;
singleton = nullptr;
}
}
}
/* keep that in sync with gdaldrivermanager.cpp */
void GDALDatasetPool::ForceDestroy()
{
CPLMutexHolderD(GDALGetphDLMutex());
if (!singleton)
return;
refCountOfDisabledRefCount--;
CPLAssert(refCountOfDisabledRefCount == 0);
singleton->refCount = 0;
delete singleton;
singleton = nullptr;
}
/* keep that in sync with gdaldrivermanager.cpp */
extern void GDALDatasetPoolForceDestroy();
void GDALDatasetPoolForceDestroy()
{
GDALDatasetPool::ForceDestroy();
}
/************************************************************************/
/* RefDataset() */
/************************************************************************/
GDALProxyPoolCacheEntry *
GDALDatasetPool::RefDataset(const char *pszFileName, GDALAccess eAccess,
char **papszOpenOptions, int bShared,
bool bForceOpen, const char *pszOwner,
CSLConstList papszAllowedDrivers)
{
return singleton->_RefDataset(pszFileName, eAccess, papszOpenOptions,
bShared, bForceOpen, pszOwner,
papszAllowedDrivers);
}
/************************************************************************/
/* UnrefDataset() */
/************************************************************************/
void GDALDatasetPool::UnrefDataset(GDALProxyPoolCacheEntry *cacheEntry)
{
CPLMutexHolderD(GDALGetphDLMutex());
cacheEntry->refCount--;
}
/************************************************************************/
/* CloseDatasetIfZeroRefCount() */
/************************************************************************/
void GDALDatasetPool::CloseDatasetIfZeroRefCount(const char *pszFileName,
CSLConstList papszOpenOptions,
GDALAccess eAccess,
const char *pszOwner)
{
CPLMutexHolderD(GDALGetphDLMutex());
singleton->_CloseDatasetIfZeroRefCount(pszFileName, papszOpenOptions,
eAccess, pszOwner);
}
struct GetMetadataElt
{
char *pszDomain;
char **papszMetadata;
};
static unsigned long hash_func_get_metadata(const void *_elt)
{
const GetMetadataElt *elt = static_cast<const GetMetadataElt *>(_elt);
return CPLHashSetHashStr(elt->pszDomain);
}
static int equal_func_get_metadata(const void *_elt1, const void *_elt2)
{
const GetMetadataElt *elt1 = static_cast<const GetMetadataElt *>(_elt1);
const GetMetadataElt *elt2 = static_cast<const GetMetadataElt *>(_elt2);
return CPLHashSetEqualStr(elt1->pszDomain, elt2->pszDomain);
}
static void free_func_get_metadata(void *_elt)
{
GetMetadataElt *elt = static_cast<GetMetadataElt *>(_elt);
CPLFree(elt->pszDomain);
CSLDestroy(elt->papszMetadata);
CPLFree(elt);
}
struct GetMetadataItemElt
{
char *pszName;
char *pszDomain;
char *pszMetadataItem;
};
static unsigned long hash_func_get_metadata_item(const void *_elt)
{
const GetMetadataItemElt *elt =
static_cast<const GetMetadataItemElt *>(_elt);
return CPLHashSetHashStr(elt->pszName) ^ CPLHashSetHashStr(elt->pszDomain);
}
static int equal_func_get_metadata_item(const void *_elt1, const void *_elt2)
{
const GetMetadataItemElt *elt1 =
static_cast<const GetMetadataItemElt *>(_elt1);
const GetMetadataItemElt *elt2 =
static_cast<const GetMetadataItemElt *>(_elt2);
return CPLHashSetEqualStr(elt1->pszName, elt2->pszName) &&
CPLHashSetEqualStr(elt1->pszDomain, elt2->pszDomain);
}
static void free_func_get_metadata_item(void *_elt)
{
GetMetadataItemElt *elt = static_cast<GetMetadataItemElt *>(_elt);
CPLFree(elt->pszName);
CPLFree(elt->pszDomain);
CPLFree(elt->pszMetadataItem);
CPLFree(elt);
}
/* ******************************************************************** */
/* GDALProxyPoolDataset */
/* ******************************************************************** */
/* Note : the bShared parameter must be used with caution. You can */
/* set it to TRUE for being used as a VRT source : in that case, */
/* VRTSimpleSource will take care of destroying it when there are no */
/* reference to it (in VRTSimpleSource::~VRTSimpleSource()) */
/* However this will not be registered as a genuine shared dataset, like it */
/* would have been with MarkAsShared(). But MarkAsShared() is not usable for */
/* GDALProxyPoolDataset objects, as they share the same description as their */
/* underlying dataset. So *NEVER* call MarkAsShared() on a GDALProxyPoolDataset
*/
/* object */
/* pszOwner is only honoured in the bShared case, and restrict the scope */
/* of the sharing. Only calls to _RefDataset() with the same value of */
/* pszOwner can effectively use the same dataset. The use case is */
/* to avoid 2 VRTs (potentially the same one) opened by a single thread,
* pointing to */
/* the same source datasets. In that case, they would use the same dataset */
/* So even if the VRT handles themselves are used from different threads, since
*/
/* the underlying sources are shared, that might cause crashes (#6939). */
/* But we want to allow a same VRT referencing the same source dataset,*/
/* for example if it has multiple bands. So in practice the value of pszOwner */
/* is the serialized value (%p formatting) of the VRT dataset handle. */
GDALProxyPoolDataset::GDALProxyPoolDataset(
const char *pszSourceDatasetDescription, int nRasterXSizeIn,
int nRasterYSizeIn, GDALAccess eAccessIn, int bSharedIn,
const char *pszProjectionRefIn, const GDALGeoTransform *pGT,
const char *pszOwner, CSLConstList papszAllowedDrivers)
: responsiblePID(GDALGetResponsiblePIDForCurrentThread()),
pszProjectionRef(pszProjectionRefIn ? CPLStrdup(pszProjectionRefIn)
: nullptr),
m_aosAllowedDrivers(papszAllowedDrivers)
{
GDALDatasetPool::Ref();
SetDescription(pszSourceDatasetDescription);
nRasterXSize = nRasterXSizeIn;
nRasterYSize = nRasterYSizeIn;
eAccess = eAccessIn;
bShared = CPL_TO_BOOL(bSharedIn);
m_pszOwner = pszOwner ? CPLStrdup(pszOwner) : nullptr;
if (pGT)
{
m_gt = *pGT;
m_bHasSrcGeoTransform = true;
}
if (pszProjectionRefIn)
{
m_poSRS = new OGRSpatialReference();
m_poSRS->importFromWkt(pszProjectionRefIn);
m_bHasSrcSRS = true;
}
}
/* Constructor where the parameters (raster size, etc.) are obtained
* by opening the underlying dataset.
*/
GDALProxyPoolDataset::GDALProxyPoolDataset(
const char *pszSourceDatasetDescription, GDALAccess eAccessIn,
int bSharedIn, const char *pszOwner, CSLConstList papszAllowedDrivers)
: responsiblePID(GDALGetResponsiblePIDForCurrentThread()),
m_aosAllowedDrivers(papszAllowedDrivers)
{
GDALDatasetPool::Ref();
SetDescription(pszSourceDatasetDescription);
eAccess = eAccessIn;
bShared = CPL_TO_BOOL(bSharedIn);
m_pszOwner = pszOwner ? CPLStrdup(pszOwner) : nullptr;
}
/************************************************************************/
/* Create() */
/************************************************************************/
/* Instantiate a GDALProxyPoolDataset where the parameters (raster size, etc.)
* are obtained by opening the underlying dataset.
* Its bands are also instantiated.
*/
GDALProxyPoolDataset *GDALProxyPoolDataset::Create(
const char *pszSourceDatasetDescription, CSLConstList papszOpenOptionsIn,
GDALAccess eAccessIn, int bSharedIn, const char *pszOwner,
CSLConstList papszAllowedDrivers)
{
std::unique_ptr<GDALProxyPoolDataset> poSelf(
new GDALProxyPoolDataset(pszSourceDatasetDescription, eAccessIn,
bSharedIn, pszOwner, papszAllowedDrivers));
poSelf->SetOpenOptions(papszOpenOptionsIn);
GDALDataset *poUnderlyingDS = poSelf->RefUnderlyingDataset();
if (!poUnderlyingDS)
return nullptr;
poSelf->nRasterXSize = poUnderlyingDS->GetRasterXSize();
poSelf->nRasterYSize = poUnderlyingDS->GetRasterYSize();
if (poUnderlyingDS->GetGeoTransform(poSelf->m_gt) == CE_None)
poSelf->m_bHasSrcGeoTransform = true;
const auto poSRS = poUnderlyingDS->GetSpatialRef();
if (poSRS)
{
poSelf->m_poSRS = poSRS->Clone();
poSelf->m_bHasSrcSRS = true;
}
for (int i = 1; i <= poUnderlyingDS->GetRasterCount(); ++i)
{
auto poSrcBand = poUnderlyingDS->GetRasterBand(i);
if (!poSrcBand)
{
poSelf->UnrefUnderlyingDataset(poUnderlyingDS);
return nullptr;
}
int nSrcBlockXSize, nSrcBlockYSize;
poSrcBand->GetBlockSize(&nSrcBlockXSize, &nSrcBlockYSize);
poSelf->AddSrcBandDescription(poSrcBand->GetRasterDataType(),
nSrcBlockXSize, nSrcBlockYSize);
}
poSelf->UnrefUnderlyingDataset(poUnderlyingDS);
return poSelf.release();
}
/************************************************************************/
/* ~GDALProxyPoolDataset() */
/************************************************************************/
GDALProxyPoolDataset::~GDALProxyPoolDataset()
{
GDALDatasetPool::CloseDatasetIfZeroRefCount(
GetDescription(), papszOpenOptions, eAccess, m_pszOwner);
/* See comment in constructor */
/* It is not really a genuine shared dataset, so we don't */
/* want ~GDALDataset() to try to release it from its */
/* shared dataset hashset. This will save a */
/* "Should not happen. Cannot find %s, this=%p in phSharedDatasetSet" debug
* message */
bShared = false;
CPLFree(pszProjectionRef);
CPLFree(pszGCPProjection);
if (nGCPCount)
{
GDALDeinitGCPs(nGCPCount, pasGCPList);
CPLFree(pasGCPList);
}
if (metadataSet)
CPLHashSetDestroy(metadataSet);
if (metadataItemSet)
CPLHashSetDestroy(metadataItemSet);
CPLFree(m_pszOwner);
if (m_poSRS)
m_poSRS->Release();
if (m_poGCPSRS)
m_poGCPSRS->Release();
GDALDatasetPool::Unref();
}
/************************************************************************/
/* SetOpenOptions() */
/************************************************************************/
void GDALProxyPoolDataset::SetOpenOptions(CSLConstList papszOpenOptionsIn)
{
CPLAssert(papszOpenOptions == nullptr);
papszOpenOptions = CSLDuplicate(papszOpenOptionsIn);
}
/************************************************************************/
/* AddSrcBandDescription() */
/************************************************************************/
void GDALProxyPoolDataset::AddSrcBandDescription(GDALDataType eDataType,
int nBlockXSize,
int nBlockYSize)
{
SetBand(nBands + 1, new GDALProxyPoolRasterBand(this, nBands + 1, eDataType,
nBlockXSize, nBlockYSize));
}
/************************************************************************/
/* AddSrcBand() */
/************************************************************************/
void GDALProxyPoolDataset::AddSrcBand(int nBand, GDALDataType eDataType,
int nBlockXSize, int nBlockYSize)
{
SetBand(nBand, new GDALProxyPoolRasterBand(this, nBand, eDataType,
nBlockXSize, nBlockYSize));
}
/************************************************************************/
/* RefUnderlyingDataset() */
/************************************************************************/
GDALDataset *GDALProxyPoolDataset::RefUnderlyingDataset() const
{
return RefUnderlyingDataset(true);
}
GDALDataset *GDALProxyPoolDataset::RefUnderlyingDataset(bool bForceOpen) const
{
/* We pretend that the current thread is responsiblePID, that is */
/* to say the thread that created that GDALProxyPoolDataset object. */
/* This is for the case when a GDALProxyPoolDataset is created by a */
/* thread and used by other threads. These other threads, when doing actual
*/
/* IO, will come there and potentially open the underlying dataset. */
/* By doing this, they can indirectly call GDALOpenShared() on .aux file */
/* for example. So this call to GDALOpenShared() must occur as if it */
/* was done by the creating thread, otherwise it will not be correctly
* closed afterwards... */
/* To make a long story short : this is necessary when warping with
* ChunkAndWarpMulti */
/* a VRT of GeoTIFFs that have associated .aux files */
GIntBig curResponsiblePID = GDALGetResponsiblePIDForCurrentThread();
GDALSetResponsiblePIDForCurrentThread(responsiblePID);
cacheEntry = GDALDatasetPool::RefDataset(
GetDescription(), eAccess, papszOpenOptions, GetShared(), bForceOpen,
m_pszOwner, m_aosAllowedDrivers.List());
GDALSetResponsiblePIDForCurrentThread(curResponsiblePID);
if (cacheEntry != nullptr)
{
if (cacheEntry->poDS != nullptr)
return cacheEntry->poDS;
else
GDALDatasetPool::UnrefDataset(cacheEntry);
}
return nullptr;
}
/************************************************************************/
/* UnrefUnderlyingDataset() */
/************************************************************************/
void GDALProxyPoolDataset::UnrefUnderlyingDataset(
CPL_UNUSED GDALDataset *poUnderlyingDataset) const
{
if (cacheEntry != nullptr)
{
CPLAssert(cacheEntry->poDS == poUnderlyingDataset);
if (cacheEntry->poDS != nullptr)
GDALDatasetPool::UnrefDataset(cacheEntry);
}
}
/************************************************************************/
/* FlushCache() */
/************************************************************************/
CPLErr GDALProxyPoolDataset::FlushCache(bool bAtClosing)
{
CPLErr eErr = CE_None;
GDALDataset *poUnderlyingDataset = RefUnderlyingDataset(false);
if (poUnderlyingDataset)
{
eErr = poUnderlyingDataset->FlushCache(bAtClosing);
UnrefUnderlyingDataset(poUnderlyingDataset);
}
return eErr;
}
/************************************************************************/
/* SetSpatialRef() */
/************************************************************************/
CPLErr GDALProxyPoolDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
{
m_bHasSrcSRS = false;
return GDALProxyDataset::SetSpatialRef(poSRS);
}
/************************************************************************/
/* GetSpatialRef() */
/************************************************************************/
const OGRSpatialReference *GDALProxyPoolDataset::GetSpatialRef() const
{
if (m_bHasSrcSRS)
return m_poSRS;
else
{
if (m_poSRS)
m_poSRS->Release();
m_poSRS = nullptr;
auto poSRS = GDALProxyDataset::GetSpatialRef();
if (poSRS)
m_poSRS = poSRS->Clone();