-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththread_local_storage.h
1317 lines (1052 loc) · 51 KB
/
thread_local_storage.h
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
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013, Lawrence Livermore National Security, LLC.
// Produced at the Lawrence Livermore National Laboratory.
// Written by the Greg Bronevetsky <bronevetsky1@llnl.gov> / <greg@bronevetsky.com>.
//
// LLNL-CODE-642002
// All rights reserved.
//
// This file is part of Sight. For details, see https://e-reports-ext.llnl.gov/pdf/781752.pdf or
// https://github.com/bronevet/sight.
//
// Licensed under the GNU Lesser General Public License (Lesser GPU) Version 2.1,
// February 1999; you may not use this file except in compliance with the License.
// The full licence is included in file LICENCE and you may obtain a copy of the
// License at:
// https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the license.
////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <pthread.h>
//#include <stdio.h>
//#include <stdlib.h>
#include <iostream>
#include <assert.h>
#include <set>
#include <vector>
#include <list>
#include <map>
//#include <fstream>
using namespace std;
// Based on http://alex.tapmania.org/2011/03/simple-thread-local-storage-with-pthreads.html
// This class encapsulates the use of thread local storage for global, function static and class static data.
// The advantage of the ThreadLocalStorage class over thread__ is that the latter does not
// work with classes since it does not call the constructor of the object declared thread__ when a
// new thread is created (the object pointers are different for different threads but
// construction/destruction does not occur).
// Use:
// | Single-thread |ThreadLocalStorage
// ---------|--------------------------------------|-----------------------------------------------------------------
// Global | ClassType varA; | ThreadLocalStorage0<ClassType> varA;
// Global | ClassType varB(5); | ThreadLocalStorage1<ClassType, int> varB(15);
// Global | ClassType varC(5, "hi"); | ThreadLocalStorage2<ClassType, int, std::string> varC(15, "hi");
// ---------|--------------------------------------|-----------------------------------------------------------------
// Function | void func() { | void func() {
// Static | ClassType varA; | ThreadLocalStorage0<ClassType, int> varA();
// | ClassType varB(5); | ThreadLocalStorage1<ClassType, int> varB(15);
// | ClassType varC(5, "hi"); | ThreadLocalStorage2<ClassType, int, std::string> varC(15, "hi");
// | } | }
// ---------|--------------------------------------|-----------------------------------------------------------------
// Class | class SomeClass { | class SomeClass {
// Static | ClassType varA; | static ThreadLocalStorage0<ClassType, int> varA;
// | ClassType varB; | static ThreadLocalStorage1<ClassType, int> varB;
// | ClassType varC; | static ThreadLocalStorage2<ClassType, int, std::string> varC;
// | }; | };
// | ClassType SomeClalss::varA; | ThreadLocalStorage0<ClassType, int> SomeClalss::varA;
// | ClassType SomeClalss::varB(5); | ThreadLocalStorage1<ClassType, int> SomeClalss::varB(15);
// | ClassType SomeClalss::varC(5, "hi"); | ThreadLocalStorage2<ClassType, int, std::string> SomeClalss::varC(15, "hi");
template<typename T>
class ThreadLocalStorage
{
private:
pthread_key_t key_;
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorage(const ThreadLocalStorage& that) {}
public:
// Function called when a thread finishes to deallocate the thread-local copy of the object
static void destructor(void* p) {
//cout << ((long)pthread_self())<<": ThreadLocalStorage::destructor("<<p<<")"<<endl;
delete (T*)p;
}
ThreadLocalStorage()
{
pthread_key_create(&key_, &ThreadLocalStorage::destructor);
// cout << "ThreadLocalStorage::ThreadLocalStorage key="<<key_<<", this="<<this<<endl;
}
/*ThreadLocalStorage(const T& val)
{
pthread_key_create(&key_, &ThreadLocalStorage::destructor);
pthread_setspecific(key_, const_cast<ThreadLocalStorage<T>*>(this)->allocate(val));
}*/
ThreadLocalStorage(const T& val) {
pthread_key_create(&key_, &ThreadLocalStorage::destructor);
// cout << "ThreadLocalStorage::ThreadLocalStorage(val) key="<<key_<<endl;
*get() = val;
}
/*ThreadLocalStorage(const ThreadLocalStorage<T>& that) : key_(that.key_) {
cout << "ThreadLocalStorage copy constructor key_"<<key_<<endl;
} */
~ThreadLocalStorage()
{
// cout << "~ThreadLocalStorage deleting key="<<key_<<endl;
pthread_key_delete(key_);
}
/* // Initializes the thread local storage for the current thread with the given object. It will be
// deallocated automatically when the thread exits (the caller must not deallocate).
ThreadLocalStorage& operator =(T* p)
{
// Only one object may be associated with a thread
assert(pthread_getspecific(key_)==NULL);
pthread_setspecific(key_, p);
return *this;
}*/
/*T& operator =(T val)
{
cout << "ThreadLocalStorage operator=(T val)"<<endl;
*((T*)get()) = val;
return *get();
} T& operator =(T& val)
{
cout << "ThreadLocalStorage operator=(T& val)"<<endl;
*((T*)get()) = val;
return *get();
}
T& operator =(const T& val)
{
cout << "ThreadLocalStorage operator=(const T& val)"<<endl;
*((T*)get()) = val;
return *get();
}*/
// Allocates a new instance of an object of type T and returns it. Since
// the arguments to the new call will vary across use-cases, this method is
// not implemented here but rather in classes derive from this one, which
// implement variants for different argument options. Below are some classes of this
// type that allow the called to provide these arguments in the constructor
// and cache them. When user need to use more arguments than we've provided
// classes for, they need to write their own class that extends ThreadLocalStorage and
// implements allocate().
virtual T* allocate()=0;
// Ensure that the thread-specific instance of the object is initialized. This method will be
// called automatically before the user can access the thread-local object or may be called
// directly by the user.
void tlsInit() const {
//cout << " tlsInit("<<key_<<") before "<<pthread_getspecific(key_)<<endl;
if(pthread_getspecific(key_)==NULL) {
T* buf = const_cast<ThreadLocalStorage<T>*>(this)->allocate();
pthread_setspecific(key_, buf);
//cout << pthread_self()<<": buf="<<buf<<", key_="<<key_<<endl;//<<", pthread_getspecific(key_)="<<pthread_getspecific(key_)<<endl;
// pthread_setspecific(key_, const_cast<ThreadLocalStorage<T>*>(this)->allocate());
assert(pthread_getspecific(key_) == buf);
}
//cout << " tlsInit("<<key_<<") after "<<pthread_getspecific(key_)<<endl;
}
// Returns whether a value is currently mapped for this thread or not
bool isValueMappedForThread() {
return pthread_getspecific(key_)!=NULL;
}
// Operators to access the thread-local data
T* operator ->()
{
tlsInit();
return static_cast<T*>(pthread_getspecific(key_));
}
T& operator *()
{
//cout << "operator *\n";
tlsInit();
return *static_cast<T*>(pthread_getspecific(key_));
}
T* operator &()
{
//cout << "operator &\n";
tlsInit();
return static_cast<T*>(pthread_getspecific(key_));
}
/*operator T() const {
tlsInit();
return *static_cast<T*>(pthread_getspecific(key_));
}*/
operator T&() {
//cout << "cast to T&\n";
tlsInit();
return *static_cast<T*>(pthread_getspecific(key_));
}
operator const T&() const {
//cout << "cast to const T&\n";
tlsInit();
return *static_cast<T*>(pthread_getspecific(key_));
}
operator T*()
{
//cout << "cast to T*\n";
tlsInit();
return static_cast<T*>(pthread_getspecific(key_));
}
const T* get() const
{
//cout << "cast to const T* get\n";
tlsInit();
return static_cast<T*>(pthread_getspecific(key_));
}
T* get()
{
//cout << "cast to T* get\n";
tlsInit();
return static_cast<T*>(pthread_getspecific(key_));
}
// Assignment operator
template<typename argT>
T& operator =(const argT& val) {
(*get()) = val;
return *get();
}
// Macro that holds the standard implementation of an assignment operator
// that should be used by derived classes. Derived classes must provide
// their own implementation of this operator since if they provide a copy
// constructor the assignment operator is not inherited.
#define TLS_ASSIGNMENT_OPERATOR \
template <typename argT> \
T& operator =(const argT& val) \
{ \
*((T*)ThreadLocalStorage<T>::get()) = (T)val; \
return *ThreadLocalStorage<T>::get(); \
}
// Arithmetic operators
template<typename argT>
T operator+(const argT& right) const
{ return (*get()) + right; }
template<typename argT>
T operator-(const argT& right) const
{ return (*get()) - right; }
template<typename argT>
T operator*(const argT& right) const
{ return (*get()) * right; }
template<typename argT>
T operator/(const argT& right) const
{ return (*get()) / right; }
// Pre-increment
T operator++() {
(*get())++;
return *get();
}
// Post-increment
T operator++(int) {
T origVal = *get();
(*get())++;
return origVal;
}
// Pre-decrement
T operator--() {
(*get())--;
return *get();
}
// Post-decrement
T operator--(int) {
T origVal = *get();
(*get())--;
return origVal;
}
// Update operators
template<typename argT>
T& operator+=(const argT& right) {
(*get()) += right;
return *get();
}
template<typename argT>
T& operator-=(const argT& right) {
(*get()) -= right;
return *get();
}
template<typename argT>
T& operator*=(const argT& right) {
(*get()) *= right;
return *get();
}
template<typename argT>
T& operator/=(const argT& right) {
(*get()) /= right;
return *get();
}
// Relational operators
template<typename argT>
bool operator==(const argT& right) const
{ return (*get()) == (const T&)right; }
template<typename argT>
bool operator!=(const argT& right) const
{ return (*get()) != (const T&)right; }
template<typename argT>
bool operator<(const argT& right) const
{ return (*get()) < (const T&)right; }
template<typename argT>
bool operator<=(const argT& right) const
{ return (*get()) <= (const T&)right; }
template<typename argT>
bool operator>(const argT& right) const
{ return (*get()) > (const T&)right; }
template<typename argT>
bool operator>=(const argT& right) const
{ return (*get()) <= (const T&)right; }
}; // class ThreadLocalStorage
template<typename T>
std::ostream& operator<< (std::ostream& stream, const ThreadLocalStorage<T>& d) {
stream << *(d.get());
return stream;
}
// Variants of ThreadLocalStorage that are focused on constructing instances of type T
// with between 0 and 9 arguments to the constructor. These arguments are provided to the
// constructor of ThreadLocalStorage and then reused to create thread-specific instances of T.
template<typename T>
class ThreadLocalStorage0: public ThreadLocalStorage<T> {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorage0(const ThreadLocalStorage0<T>& that) {}
public:
ThreadLocalStorage0() {}
ThreadLocalStorage0(const T& val): ThreadLocalStorage<T>(val) {}
//ThreadLocalStorage0(const ThreadLocalStorage0<T>& that) : ThreadLocalStorage<T>(that) {}
TLS_ASSIGNMENT_OPERATOR
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T(); }
}; // class ThreadLocalStorage0
template<typename T, typename T1>
class ThreadLocalStorage1: public ThreadLocalStorage<T> {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorage1(const ThreadLocalStorage1<T, T1>& that) {}
T1 arg1;
public:
ThreadLocalStorage1(const T1& arg1): arg1(arg1) {}
//ThreadLocalStorage1(const ThreadLocalStorage1<T, T1>& that) : ThreadLocalStorage<T>(that), arg1(that.arg1) {}
// For the case where T!=T1 we need another constructor to handler assinment var=val
//ThreadLocalStorage1(const T& arg1): arg1(arg1) {}
TLS_ASSIGNMENT_OPERATOR
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T(arg1); }
}; // class ThreadLocalStorage1
template<typename T, typename T1, typename T2>
class ThreadLocalStorage2: public ThreadLocalStorage<T> {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorage2(const ThreadLocalStorage2<T, T1, T2>& that) {}
T1 arg1;
T2 arg2;
public:
ThreadLocalStorage2(const T1& arg1, const T2& arg2): arg1(arg1), arg2(arg2) {}
ThreadLocalStorage2(const T& val): ThreadLocalStorage<T>(val) {}
TLS_ASSIGNMENT_OPERATOR
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T(arg1, arg2); }
}; // class ThreadLocalStorage2
template<typename T, typename T1, typename T2, typename T3>
class ThreadLocalStorage3: public ThreadLocalStorage<T> {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorage3(const ThreadLocalStorage3<T, T1, T2, T3>& that) {}
T1 arg1;
T2 arg2;
T3 arg3;
public:
ThreadLocalStorage3(const T1& arg1, const T2& arg2, const T3& arg3): arg1(arg1), arg2(arg2), arg3(arg3) {}
ThreadLocalStorage3(const T& val): ThreadLocalStorage<T>(val) {}
TLS_ASSIGNMENT_OPERATOR
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T(arg1, arg2, arg3); }
}; // class ThreadLocalStorage3
template<typename T, typename T1, typename T2, typename T3, typename T4>
class ThreadLocalStorage4: public ThreadLocalStorage<T> {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorage4(const ThreadLocalStorage4<T, T1, T2, T3, T4>& that) {}
T1 arg1;
T2 arg2;
T3 arg3;
T4 arg4;
public:
ThreadLocalStorage4(const T1& arg1, const T2& arg2, const T3& arg3, const T4& arg4): arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4) {}
ThreadLocalStorage4(const T& val): ThreadLocalStorage<T>(val) {}
TLS_ASSIGNMENT_OPERATOR
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T(arg1, arg2, arg3, arg4); }
}; // class ThreadLocalStorage4
template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
class ThreadLocalStorage5: public ThreadLocalStorage<T> {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorage5(const ThreadLocalStorage5<T, T1, T2, T3, T4, T5>& that) {}
T1 arg1;
T2 arg2;
T3 arg3;
T4 arg4;
T5 arg5;
public:
ThreadLocalStorage5(const T1& arg1, const T2& arg2, const T3& arg3, const T4& arg4, const T5& arg5): arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5) {}
ThreadLocalStorage5(const T& val): ThreadLocalStorage<T>(val) {}
TLS_ASSIGNMENT_OPERATOR
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T(arg1, arg2, arg3, arg4, arg5); }
}; // class ThreadLocalStorage5
template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
class ThreadLocalStorage6: public ThreadLocalStorage<T> {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorage6(const ThreadLocalStorage6<T, T1, T2, T3, T4, T5, T6>& that) {}
T1 arg1;
T2 arg2;
T3 arg3;
T4 arg4;
T5 arg5;
T6 arg6;
public:
ThreadLocalStorage6(const T1& arg1, const T2& arg2, const T3& arg3, const T4& arg4, const T5& arg5, const T6& arg6): arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5), arg6(arg6) {}
ThreadLocalStorage6(const T& val): ThreadLocalStorage<T>(val) {}
TLS_ASSIGNMENT_OPERATOR
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T(arg1, arg2, arg3, arg4, arg5, arg6); }
}; // class ThreadLocalStorage6
template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
class ThreadLocalStorage7: public ThreadLocalStorage<T> {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorage7(const ThreadLocalStorage7<T, T1, T2, T3, T4, T5, T6, T7>& that) {}
T1 arg1;
T2 arg2;
T3 arg3;
T4 arg4;
T5 arg5;
T6 arg6;
T7 arg7;
public:
ThreadLocalStorage7(const T1& arg1, const T2& arg2, const T3& arg3, const T4& arg4, const T5& arg5, const T6& arg6, const T7& arg7): arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5), arg6(arg6), arg7(arg7) {}
ThreadLocalStorage7(const T& val): ThreadLocalStorage<T>(val) {}
TLS_ASSIGNMENT_OPERATOR
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T(arg1, arg2, arg3, arg4, arg5, arg6, arg7); }
}; // class ThreadLocalStorage7
template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
class ThreadLocalStorage8: public ThreadLocalStorage<T> {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorage8(const ThreadLocalStorage8<T, T1, T2, T3, T4, T5, T6, T7, T8>& that) {}
T1 arg1;
T2 arg2;
T3 arg3;
T4 arg4;
T5 arg5;
T6 arg6;
T7 arg7;
T8 arg8;
public:
ThreadLocalStorage8(const T1& arg1, const T2& arg2, const T3& arg3, const T4& arg4, const T5& arg5, const T6& arg6, const T7& arg7, const T8& arg8): arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5), arg6(arg6), arg7(arg7), arg8(arg8) {}
ThreadLocalStorage8(const T& val): ThreadLocalStorage<T>(val) {}
TLS_ASSIGNMENT_OPERATOR
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); }
}; // class ThreadLocalStorage8
template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
class ThreadLocalStorage9: public ThreadLocalStorage<T> {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorage9(const ThreadLocalStorage9<T, T1, T2, T3, T4, T5, T6, T7, T8, T9>& that) {}
T1 arg1;
T2 arg2;
T3 arg3;
T4 arg4;
T5 arg5;
T6 arg6;
T7 arg7;
T8 arg8;
T9 arg9;
public:
ThreadLocalStorage9(const T1& arg1, const T2& arg2, const T3& arg3, const T4& arg4, const T5& arg5, const T6& arg6, const T7& arg7, const T8& arg8, const T9& arg9): arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5), arg6(arg6), arg7(arg7), arg8(arg8), arg9(arg9) {}
ThreadLocalStorage9(const T& val): ThreadLocalStorage<T>(val) {}
TLS_ASSIGNMENT_OPERATOR
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); }
}; // class ThreadLocalStorage9
/*// Specialized type of ThreadLocalStorage that is focused on numeric datatypes
template<typename T>
class ThreadLocalStorageNumeric: public ThreadLocalStorage<T> {
// Records whether the initial value of this numeric variable was specified
bool initValKnown;
// If was, the initial value itself
T initVal;
public:
ThreadLocalStorageNumeric(const T& initVal): initVal(initVal) { initValKnown=true; }
ThreadLocalStorageNumeric() { initValKnown=false; }
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T; }
// Overload of the bracket operator for array indexing
T& operator[](unsigned int i)
{ return ((T*)(ThreadLocalStorage<T>::get()))[i]; }
const T& operator[](unsigned int i) const
{ return ((T*)(ThreadLocalStorage<T>::get()))[i]; }
}; // class ThreadLocalStorageNumeric
*/
// ------------------------------------------------------------------
// ----- Specialized type of ThreadLocalStorage that is focused -----
// ----- on arrays of items of type T. -----
// ------------------------------------------------------------------
template<typename T>
class ThreadLocalStorageArray: public ThreadLocalStorage<T> {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorageArray(const ThreadLocalStorageArray<T>& that) {}
// Number of entries in each thread's instance of the array
size_t size;
public:
ThreadLocalStorageArray(size_t size): size(size) {}
// Allocates a new instance of an object of type T and returns it.
T* allocate() { return new T[size]; }
// Overload of the bracket operator for array indexing
T& operator[](unsigned int i)
{ return ((T*)(ThreadLocalStorage<T>::get()))[i]; }
const T& operator[](unsigned int i) const
{ return ((T*)(ThreadLocalStorage<T>::get()))[i]; }
}; // class ThreadLocalStorageArray
// Specialized type of ThreadLocalStorage that is focused on arrays of items of type std::map.
template<typename valT>
class ThreadLocalStorageSet: public ThreadLocalStorage<std::set<valT> > {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorageSet(const ThreadLocalStorageSet<valT>& that) {}
public:
ThreadLocalStorageSet() {}
// Allocates a new instance of an object of type T and returns it.
std::set<valT>* allocate() { return new std::set<valT>(); }
// Iterators
typename std::set<valT>::iterator begin()
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->begin(); }
typename std::set<valT>::const_iterator begin() const
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->begin(); }
typename std::set<valT>::iterator end()
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->end(); }
typename std::set<valT>::const_iterator end() const
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->end(); }
// Capacity
bool empty() const
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->empty(); }
size_t size() const
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->size(); }
size_t max_size() const
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->max_size(); }
// Modifiers
typename std::pair<typename std::set<valT>::iterator, bool> insert(const valT& val)
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->insert(val); }
typename std::set<valT>::iterator insert(typename std::set<valT>::iterator position, const valT& val)
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->insert(position, val); }
template<typename InputIterator>
void insert(InputIterator first, InputIterator last)
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->insert(first, last); }
typename std::set<valT>::iterator erase(typename std::set<valT>::const_iterator position)
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->erase(position); }
typename std::set<valT>::size_type erase(const valT& val)
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->erase(val); }
typename std::set<valT>::iterator erase(typename std::set<valT>::const_iterator first, typename std::set<valT>::const_iterator last)
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->erase(first, last); }
void swap(std::set<valT>& x)
{ return (*(std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get())).swap(x); }
size_t clear() const
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->clear(); }
// Observers
typename std::set<valT>::key_compare key_comp()
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->key_comp(); }
typename std::set<valT>::value_compare value_comp()
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->value_comp(); }
// Operations
typename std::set<valT>::iterator find(const valT& val)
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->find(val); }
typename std::set<valT>::size_type count(const valT& val) const
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->count(val); }
typename std::set<valT>::iterator lower_bound(const valT& val) const
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->lower_bound(val); }
typename std::set<valT>::iterator upper_bound(const valT& val) const
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->upper_bound(val); }
typename std::pair<typename std::set<valT>::iterator, typename std::set<valT>::iterator> equal_range(const valT& val) const
{ return ((std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get()))->equal_range(val); }
// Allocator
typename std::set<valT>::allocator_type get_allocator()
{ return (*(std::set<valT>*)(ThreadLocalStorage<std::set<valT> >::get())).get_allocator(); }
}; // class ThreadLocalStorageSet
// Specialized type of ThreadLocalStorage that is focused on arrays of items of type std::map.
template<typename keyT, typename valT>
class ThreadLocalStorageMap: public ThreadLocalStorage<std::map<keyT, valT> > {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorageMap(const ThreadLocalStorageMap<keyT, valT>& that) {}
public:
ThreadLocalStorageMap() {}
// Allocates a new instance of an object of type T and returns it.
std::map<keyT, valT>* allocate() {
std::map<keyT, valT>* ret = new std::map<keyT, valT>();
//cout << "ThreadLocalStorageMap allocated "<<ret<<endl;
return ret;
}
// Overload of the bracket operator for map indexing
valT& operator[](const keyT& key)
{ return (*(std::map<keyT, valT>*)(ThreadLocalStorage<std::map<keyT, valT> >::get()))[key]; }
const valT& operator[](const keyT& key) const
{ return (*(std::map<keyT, valT>*)(ThreadLocalStorage<std::map<keyT, valT> >::get()))[key]; }
// Iteration methods
typename std::map<keyT, valT>::iterator find(const keyT& key)
{ return ((std::map<keyT, valT>*)(ThreadLocalStorage<std::map<keyT, valT> >::get()))->find(key); }
typename std::map<keyT, valT>::const_iterator find(const keyT& key) const
{ return ((std::map<keyT, valT>*)(ThreadLocalStorage<std::map<keyT, valT> >::get()))->find(key); }
typename std::map<keyT, valT>::iterator begin()
{ return ((std::map<keyT, valT>*)(ThreadLocalStorage<std::map<keyT, valT> >::get()))->begin(); }
typename std::map<keyT, valT>::const_iterator begin() const
{ return ((std::map<keyT, valT>*)(ThreadLocalStorage<std::map<keyT, valT> >::get()))->begin(); }
typename std::map<keyT, valT>::iterator end()
{ return ((std::map<keyT, valT>*)(ThreadLocalStorage<std::map<keyT, valT> >::get()))->end(); }
typename std::map<keyT, valT>::const_iterator end() const
{ return ((std::map<keyT, valT>*)(ThreadLocalStorage<std::map<keyT, valT> >::get()))->end(); }
// Content management
typename std::map<keyT, valT>::iterator erase(typename std::map<keyT, valT>::const_iterator position)
{ return ((std::map<keyT, valT>*)(ThreadLocalStorage<std::map<keyT, valT> >::get()))->erase(position); }
typename std::map<keyT, valT>::size_type erase(const keyT& k)
{ return ((std::map<keyT, valT>*)(ThreadLocalStorage<std::map<keyT, valT> >::get()))->erase(k); }
typename std::map<keyT, valT>::iterator erase(typename std::map<keyT, valT>::const_iterator first, typename std::map<keyT, valT>::const_iterator last)
{ return ((std::map<keyT, valT>*)(ThreadLocalStorage<std::map<keyT, valT> >::get()))->erase(first, last); }
}; // class ThreadLocalStorageMap
// ------------------------------------------------------------------
// ----- Specialized type of ThreadLocalStorage that is focused -----
// ----- on arrays of items of type std::vector. -----
// ------------------------------------------------------------------
template<typename valT>
class ThreadLocalStorageVector: public ThreadLocalStorage<std::vector<valT> > {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorageVector(const ThreadLocalStorageVector<valT>& that) {}
// Records whether the initial requested size of this vector was specified
bool initSizeKnown;
// The requested initial size of the vector
typename std::vector<valT>::size_type initSize;
// Records whether the initial value of this numeric variable was specified
bool initValKnown;
// If was, the initial value itself
valT initVal;
public:
ThreadLocalStorageVector(): initSizeKnown(false), initValKnown(false) {}
ThreadLocalStorageVector(typename std::vector<valT>::size_type initSize): initSizeKnown(true), initSize(initSize), initValKnown(false) {}
ThreadLocalStorageVector(typename std::vector<valT>::size_type initSize, const valT& initVal): initSize(initSize), initValKnown(true), initVal(initVal) {}
// Allocates a new instance of an object of type T and returns it.
std::vector<valT>* allocate() {
if(initSizeKnown) {
if(initValKnown)
return new std::vector<valT>(initSize, initVal);
else
return new std::vector<valT>(initSize);
} else
return new std::vector<valT>();
}
// Iteration methods
typename std::vector<valT>::iterator begin()
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->begin(); }
typename std::vector<valT>::const_iterator begin() const
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->begin(); }
typename std::vector<valT>::reverse_iterator rbegin()
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->rbegin(); }
typename std::vector<valT>::const_reverse_iterator rbegin() const
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->rbegin(); }
typename std::vector<valT>::iterator end()
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->end(); }
typename std::vector<valT>::const_iterator end() const
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->end(); }
typename std::vector<valT>::reverse_iterator rend()
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->rend(); }
typename std::vector<valT>::const_reverse_iterator rend() const
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->rend(); }
// Capacity
typename std::vector<valT>::size_type size()
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->size(); }
typename std::vector<valT>::size_type max_size()
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->max_size(); }
void resize(typename std::vector<valT>::size_type size, valT initVal=valT())
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->resize(size, initVal); }
bool empty()
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->empty(); }
void reserve(typename std::vector<valT>::size_type size)
{ return ((std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))->reserve(size); }
// Element Access
std::vector<valT>& operator=(const std::vector<valT>& that) {
(*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).assign(that.begin(), that.end());
return *ThreadLocalStorage<std::vector<valT> >::get();
}
valT& operator[](typename std::vector<valT>::size_type i)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))[i]; }
const valT& operator[](typename std::vector<valT>::size_type i) const
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get()))[i]; }
valT& at(typename std::vector<valT>::size_type i)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).at(i); }
const valT& at(typename std::vector<valT>::size_type i) const
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).at(i); }
valT& front()
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).front(); }
const valT& front() const
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).front(); }
valT& back()
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).back(); }
const valT& back() const
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).back(); }
// Modifiers
template <class InputIterator>
void assign(InputIterator first, InputIterator last)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).assign(first, last); }
void assign(typename std::vector<valT>::size_type n, const valT& val)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).assign(n, val); }
void push_back(const valT& val)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).push_back(val); }
void pop_back()
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).pop_back(); }
typename std::vector<valT>::iterator insert(typename std::vector<valT>::iterator position, const valT& val)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).insert(position, val); }
typename std::vector<valT>::iterator insert(typename std::vector<valT>::iterator position, typename std::vector<valT>::size_type n, const valT& val)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).insert(position, n, val); }
template <class InputIterator>
void insert (typename std::vector<valT>::iterator position, InputIterator first, InputIterator last)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).insert(position, first, last); }
typename std::vector<valT>::iterator erase(typename std::vector<valT>::iterator position)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).erase(position); }
typename std::vector<valT>::iterator erase(typename std::vector<valT>::iterator first, typename std::vector<valT>::iterator last)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).erase(first, last); }
void swap(std::vector<valT>& x)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).swap(x); }
void swap(ThreadLocalStorage<std::vector<valT> >& x)
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).swap(*x.get()); }
void clear()
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).clear(); }
// Allocator
typename std::vector<valT>::allocator_type get_allocator()
{ return (*(std::vector<valT>*)(ThreadLocalStorage<std::vector<valT> >::get())).get_allocator(); }
}; // class ThreadLocalStorageVector
// Comparison operators where the original type is compared to the TLS type
template<typename valT>
bool operator==(const std::vector<valT>& left, const ThreadLocalStorageVector<valT>& right)
{ return left == *right.get(); }
template<typename valT>
bool operator!=(const std::vector<valT>& left, const ThreadLocalStorageVector<valT>& right)
{ return left != *right.get(); }
template<typename valT>
bool operator<(const std::vector<valT>& left, const ThreadLocalStorageVector<valT>& right)
{ return left < *right.get(); }
template<typename valT>
bool operator<=(const std::vector<valT>& left, const ThreadLocalStorageVector<valT>& right)
{ return left <= *right.get(); }
template<typename valT>
bool operator>(const std::vector<valT>& left, const ThreadLocalStorageVector<valT>& right)
{ return left > *right.get(); }
template<typename valT>
bool operator>=(const std::vector<valT>& left, const ThreadLocalStorageVector<valT>& right)
{ return left >= *right.get(); }
// ------------------------------------------------------------------
// ----- Specialized type of ThreadLocalStorage that is focused -----
// ------ on arrays of items of type std::list. -----
// ------------------------------------------------------------------
template<typename valT>
class ThreadLocalStorageList: public ThreadLocalStorage<std::list<valT> > {
// Private copy constructor to ensure that callers cast TLS objects to T rather than make copies of them
private:
ThreadLocalStorageList(const ThreadLocalStorageList<valT>& that) {}
// Records whether the initial requested size of this list was specified
bool initSizeKnown;
// The requested initial size of the list
typename std::list<valT>::size_type initSize;
// Records whether the initial value of this numeric variable was specified
bool initValKnown;
// If was, the initial value itself
valT initVal;
public:
ThreadLocalStorageList(): initSizeKnown(false), initValKnown(false) {}
ThreadLocalStorageList(typename std::list<valT>::size_type initSize): initSizeKnown(true), initSize(initSize), initValKnown(false) {}
ThreadLocalStorageList(typename std::list<valT>::size_type initSize, const valT& initVal): initSize(initSize), initValKnown(true), initVal(initVal) {}
// Allocates a new instance of an object of type T and returns it.
std::list<valT>* allocate() {
if(initSizeKnown) {
if(initValKnown)
return new std::list<valT>(initSize, initVal);
else
return new std::list<valT>(initSize);
} else
return new std::list<valT>();
}
// Iteration methods
typename std::list<valT>::iterator begin()
{ return ((std::list<valT>*)(ThreadLocalStorage<std::list<valT> >::get()))->begin(); }
typename std::list<valT>::const_iterator begin() const
{ return ((std::list<valT>*)(ThreadLocalStorage<std::list<valT> >::get()))->begin(); }
typename std::list<valT>::reverse_iterator rbegin()
{ return ((std::list<valT>*)(ThreadLocalStorage<std::list<valT> >::get()))->rbegin(); }
typename std::list<valT>::const_reverse_iterator rbegin() const
{ return ((std::list<valT>*)(ThreadLocalStorage<std::list<valT> >::get()))->rbegin(); }
typename std::list<valT>::iterator end()
{ return ((std::list<valT>*)(ThreadLocalStorage<std::list<valT> >::get()))->end(); }
typename std::list<valT>::const_iterator end() const
{ return ((std::list<valT>*)(ThreadLocalStorage<std::list<valT> >::get()))->end(); }
typename std::list<valT>::reverse_iterator rend()
{ return ((std::list<valT>*)(ThreadLocalStorage<std::list<valT> >::get()))->rend(); }
typename std::list<valT>::const_reverse_iterator rend() const
{ return ((std::list<valT>*)(ThreadLocalStorage<std::list<valT> >::get()))->rend(); }
// Capacity
typename std::list<valT>::size_type size()
{ return ((std::list<valT>*)(ThreadLocalStorage<std::list<valT> >::get()))->size(); }
typename std::list<valT>::size_type max_size()
{ return ((std::list<valT>*)(ThreadLocalStorage<std::list<valT> >::get()))->max_size(); }