xref: /oneTBB/test/common/tls_limit.h (revision c21e688a)
1 /*
2     Copyright (c) 2005-2022 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB_test_common_tls_limit_H_
18 #define __TBB_test_common_tls_limit_H_
19 
20 #include "config.h"
21 
22 class LimitTLSKeysTo {
23 #if _WIN32 || _WIN64
24     #if __TBB_WIN8UI_SUPPORT && !defined(TLS_OUT_OF_INDEXES)
25         // for SDKs for Windows*8 Store Apps that did not redirect TLS to FLS
26         #define TlsAlloc() FlsAlloc(nullptr)
27         #define TlsFree FlsFree
28         #define TLS_OUT_OF_INDEXES FLS_OUT_OF_INDEXES
29     #endif
30     typedef DWORD handle;
31 #else // _WIN32 || _WIN64
32     typedef pthread_key_t handle;
33 #endif
34     // for platforms that not limit number of TLS keys, set artificial limit
35     static const int LIMIT = 16*1024;
36     handle handles[LIMIT];
37     int    lastUsedIdx;
38 public:
LimitTLSKeysTo(int keep_keys)39     LimitTLSKeysTo(int keep_keys) {
40         for (lastUsedIdx=0; lastUsedIdx<LIMIT; lastUsedIdx++) {
41 #if _WIN32 || _WIN64
42             handle h = TlsAlloc();
43             if (h==TLS_OUT_OF_INDEXES)
44 #else
45             int setspecific_dummy=10;
46             if (pthread_key_create(&handles[lastUsedIdx], nullptr)!=0)
47 #endif
48             {
49                 break;
50             }
51 #if _WIN32 || _WIN64
52             handles[lastUsedIdx] = h;
53 #else
54             pthread_setspecific(handles[lastUsedIdx], &setspecific_dummy);
55 #endif
56         }
57         lastUsedIdx--;
58         REQUIRE_MESSAGE(lastUsedIdx >= keep_keys-1, "Less TLS keys are available than requested");
59         for (; keep_keys>0; keep_keys--, lastUsedIdx--) {
60 #if _WIN32 || _WIN64
61             TlsFree(handles[lastUsedIdx]);
62 #else
63             int ret = pthread_key_delete(handles[lastUsedIdx]);
64             REQUIRE_MESSAGE(!ret, "Can't delete a key");
65 #endif
66         }
67         // REMARK("%d thread local objects allocated in advance\n", lastUsedIdx+1);
68     }
~LimitTLSKeysTo()69     ~LimitTLSKeysTo() {
70         for (int i=0; i<=lastUsedIdx; i++) {
71 #if _WIN32 || _WIN64
72             TlsFree(handles[i]);
73 #else
74             int ret = pthread_key_delete(handles[i]);
75             REQUIRE_MESSAGE(!ret, "Can't delete a key");
76 #endif
77         }
78         lastUsedIdx = 0;
79     }
80 };
81 #endif // __TBB_test_common_tls_limit_H_
82