├── LICENSE ├── README.md ├── main.c ├── stdalign.h ├── stdatomic.c ├── stdatomic.h └── threads.h /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Implement several missing C11 standard libraries for MSVC in Visual Studio 2019 2 | Simple , and implementation for MSVC in Visual Studio 2019. 3 | 4 | MSVC in Visual Studio 2019 has already supported part of C11 standard features such as unicode, `_Alignof`, etc. However, there are some language features supported in MSVC but not as standard libraries. Here encapsulates some useful language features as C11 standard libraries. 5 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "stdatomic.h" 7 | #include "stdalign.h" 8 | #include "threads.h" 9 | 10 | static thread_local int sss = 0; 11 | 12 | int main(void) 13 | { 14 | int a = alignof(long double); 15 | printf("alignment of long double is: %d\n", a); 16 | 17 | static_assert(alignof(long double) > 4, "Nope!"); 18 | 19 | volatile atomic_uchar alignas(16) atomChar; 20 | atomic_init(&atomChar, 120); 21 | 22 | uint8_t value8 = atomic_fetch_add_explicit(&atomChar, -121, memory_order_relaxed); 23 | printf("value8 = %u, atomChar = %u\n", value8, atomic_load_explicit(&atomChar, memory_order_relaxed)); 24 | 25 | value8 = atomic_fetch_sub_explicit(&atomChar, 128, memory_order_relaxed); 26 | printf("value8 = %u, atomChar = %u\n", value8, atomic_load(&atomChar)); 27 | 28 | value8 = atomic_fetch_or_explicit(&atomChar, 0x80, memory_order_relaxed); 29 | printf("value8 = 0x%02x, atomChar = 0x%02x\n", value8, atomic_load(&atomChar)); 30 | 31 | value8 = atomic_fetch_xor_explicit(&atomChar, 0x88, memory_order_relaxed); 32 | printf("value8 = 0x%02x, atomChar = 0x%02x\n", value8, atomic_load(&atomChar)); 33 | 34 | atomic_store_explicit(&atomChar, 0x55, memory_order_relaxed); 35 | 36 | value8 = atomic_fetch_and_explicit(&atomChar, 0xa1, memory_order_relaxed); 37 | printf("value8 = 0x%02x, atomChar = 0x%02x\n", value8, atomic_load(&atomChar)); 38 | 39 | value8 = atomic_exchange_explicit(&atomChar, 255, memory_order_relaxed); 40 | printf("value8 = %u, atomChar = %u\n", value8, atomic_load(&atomChar)); 41 | 42 | value8 = atomic_load_explicit(&atomChar, memory_order_seq_cst); 43 | value8--; 44 | if (!atomic_compare_exchange_weak_explicit(&atomChar, &value8, 100, memory_order_acq_rel, memory_order_acquire)) 45 | printf("Failed! value8 = %u\n", value8); 46 | 47 | if (atomic_compare_exchange_weak_explicit(&atomChar, &value8, 100, memory_order_acq_rel, memory_order_acquire)) 48 | printf("Succeeded! atomChar = %u\n", atomChar); 49 | 50 | volatile atomic_char16_t alignas(16) atomU16; 51 | atomic_init(&atomU16, u'禅'); 52 | 53 | char16_t valueU16 = atomic_fetch_add(&atomU16, 30000); 54 | printf("\nvalueU16 is: %u, atomU16 = %u\n", valueU16, atomic_load(&atomU16)); 55 | 56 | valueU16 = atomic_fetch_sub(&atomU16, 61000); 57 | printf("valueU16 = %u, atomU16 = %u\n", valueU16, atomic_load(&atomU16)); 58 | 59 | valueU16 = atomic_fetch_or(&atomU16, 0x80); 60 | printf("valueU16 = 0x%02x, atomU16 = 0x%02x\n", valueU16, atomic_load(&atomU16)); 61 | 62 | valueU16 = atomic_fetch_xor(&atomU16, 0x88); 63 | printf("valueU16 = 0x%02x, atomU16 = 0x%02x\n", valueU16, atomic_load(&atomU16)); 64 | 65 | atomic_store(&atomU16, 0xaa55); 66 | 67 | valueU16 = atomic_fetch_and(&atomU16, 0x55a1); 68 | printf("valueU16 = 0x%04x, atomU16 = 0x%04x\n", valueU16, atomic_load(&atomU16)); 69 | 70 | valueU16 = atomic_exchange(&atomU16, 65535); 71 | printf("valueU16 = %u, atomU16 = %u\n", valueU16, atomic_load(&atomU16)); 72 | 73 | valueU16 = atomic_load(&atomU16); 74 | valueU16--; 75 | if (!atomic_compare_exchange_weak(&atomU16, &valueU16, 1000)) 76 | printf("Failed! valueU16 = %u\n", valueU16); 77 | 78 | if (atomic_compare_exchange_weak(&atomU16, &valueU16, 1000)) 79 | printf("Succeeded! atomU16 = %u\n", atomU16); 80 | } 81 | 82 | -------------------------------------------------------------------------------- /stdalign.h: -------------------------------------------------------------------------------- 1 | #ifndef __STDALIGN_H 2 | #define __STDALIGN_H 3 | 4 | #if defined(_MSC_VER) && !defined(__cplusplus) 5 | 6 | #define alignof _Alignof 7 | #define alignas(nBytes) __declspec(align(nBytes)) 8 | 9 | #endif 10 | 11 | #endif 12 | 13 | -------------------------------------------------------------------------------- /stdatomic.c: -------------------------------------------------------------------------------- 1 | #ifdef _MSC_VER 2 | 3 | #include 4 | #include 5 | 6 | #include "stdatomic.h" 7 | 8 | int8_t zenny_atomic_fetch_add8(volatile atomic_schar* object, int8_t operand) 9 | { 10 | int8_t expected = *object; 11 | int8_t desired; 12 | bool success; 13 | 14 | do 15 | { 16 | desired = expected + operand; 17 | } while ((success = zenny_atomic_compare_exchange8(object, &expected, desired)), 18 | (success ? (void)0 : _mm_pause()), 19 | !success); 20 | 21 | return expected; 22 | } 23 | 24 | int16_t zenny_atomic_fetch_add16(volatile atomic_short* object, int16_t operand) 25 | { 26 | int16_t expected = *object; 27 | int16_t desired; 28 | bool success; 29 | 30 | do 31 | { 32 | desired = expected + operand; 33 | } while ((success = zenny_atomic_compare_exchange16(object, &expected, desired)), 34 | (success ? (void)0 : _mm_pause()), 35 | !success); 36 | 37 | return expected; 38 | } 39 | 40 | int32_t zenny_atomic_fetch_add32(volatile atomic_long* object, int32_t operand) 41 | { 42 | int32_t expected = *object; 43 | int32_t desired; 44 | bool success; 45 | 46 | do 47 | { 48 | desired = expected + operand; 49 | } 50 | while ((success = zenny_atomic_compare_exchange32(object, &expected, desired)), 51 | (success? (void)0 : _mm_pause()), 52 | !success); 53 | 54 | return expected; 55 | } 56 | 57 | int64_t zenny_atomic_fetch_add64(volatile atomic_llong* object, int64_t operand) 58 | { 59 | int64_t expected = *object; 60 | int64_t desired; 61 | bool success; 62 | 63 | do 64 | { 65 | desired = expected + operand; 66 | } while ((success = zenny_atomic_compare_exchange64(object, &expected, desired)), 67 | (success ? (void)0 : _mm_pause()), 68 | !success); 69 | 70 | return expected; 71 | } 72 | 73 | int8_t zenny_atomic_fetch_sub8(volatile atomic_schar* object, int8_t operand) 74 | { 75 | int8_t expected = *object; 76 | int8_t desired; 77 | bool success; 78 | 79 | do 80 | { 81 | desired = expected - operand; 82 | } while ((success = zenny_atomic_compare_exchange8(object, &expected, desired)), 83 | (success ? (void)0 : _mm_pause()), 84 | !success); 85 | 86 | return expected; 87 | } 88 | 89 | int16_t zenny_atomic_fetch_sub16(volatile atomic_short* object, int16_t operand) 90 | { 91 | int16_t expected = *object; 92 | int16_t desired; 93 | bool success; 94 | 95 | do 96 | { 97 | desired = expected - operand; 98 | } while ((success = zenny_atomic_compare_exchange16(object, &expected, desired)), 99 | (success ? (void)0 : _mm_pause()), 100 | !success); 101 | 102 | return expected; 103 | } 104 | 105 | int32_t zenny_atomic_fetch_sub32(volatile atomic_long* object, int32_t operand) 106 | { 107 | int32_t expected = *object; 108 | int32_t desired; 109 | bool success; 110 | 111 | do 112 | { 113 | desired = expected - operand; 114 | } while ((success = zenny_atomic_compare_exchange32(object, &expected, desired)), 115 | (success ? (void)0 : _mm_pause()), 116 | !success); 117 | 118 | return expected; 119 | } 120 | 121 | int64_t zenny_atomic_fetch_sub64(volatile atomic_llong* object, int64_t operand) 122 | { 123 | int64_t expected = *object; 124 | int64_t desired; 125 | bool success; 126 | 127 | do 128 | { 129 | desired = expected - operand; 130 | } while ((success = zenny_atomic_compare_exchange64(object, &expected, desired)), 131 | (success ? (void)0 : _mm_pause()), 132 | !success); 133 | 134 | return expected; 135 | } 136 | 137 | int8_t zenny_atomic_fetch_or8(volatile atomic_schar* object, int8_t operand) 138 | { 139 | return _InterlockedOr8(object, operand); 140 | } 141 | 142 | int16_t zenny_atomic_fetch_or16(volatile atomic_short* object, int16_t operand) 143 | { 144 | return _InterlockedOr16(object, operand); 145 | } 146 | 147 | int32_t zenny_atomic_fetch_or32(volatile atomic_long* object, int32_t operand) 148 | { 149 | return _InterlockedOr(object, operand); 150 | } 151 | 152 | int64_t zenny_atomic_fetch_or64(volatile atomic_llong* object, int64_t operand) 153 | { 154 | return _InterlockedOr64(object, operand); 155 | } 156 | 157 | int8_t zenny_atomic_fetch_xor8(volatile atomic_schar* object, int8_t operand) 158 | { 159 | return _InterlockedXor8(object, operand); 160 | } 161 | 162 | int16_t zenny_atomic_fetch_xor16(volatile atomic_short* object, int16_t operand) 163 | { 164 | return _InterlockedXor16(object, operand); 165 | } 166 | 167 | int32_t zenny_atomic_fetch_xor32(volatile atomic_long* object, int32_t operand) 168 | { 169 | return _InterlockedXor(object, operand); 170 | } 171 | 172 | int64_t zenny_atomic_fetch_xor64(volatile atomic_llong* object, int64_t operand) 173 | { 174 | return _InterlockedXor64(object, operand); 175 | } 176 | 177 | int8_t zenny_atomic_fetch_and8(volatile atomic_schar* object, int8_t operand) 178 | { 179 | return _InterlockedAnd8(object, operand); 180 | } 181 | 182 | int16_t zenny_atomic_fetch_and16(volatile atomic_short* object, int16_t operand) 183 | { 184 | return _InterlockedAnd16(object, operand); 185 | } 186 | 187 | int32_t zenny_atomic_fetch_and32(volatile atomic_long* object, int32_t operand) 188 | { 189 | return _InterlockedAnd(object, operand); 190 | } 191 | 192 | int64_t zenny_atomic_fetch_and64(volatile atomic_llong* object, int64_t operand) 193 | { 194 | return _InterlockedAnd64(object, operand); 195 | } 196 | 197 | int8_t zenny_atomic_exchange8(volatile atomic_schar* object, int8_t desired) 198 | { 199 | return _InterlockedExchange8(object, desired); 200 | } 201 | 202 | int16_t zenny_atomic_exchange16(volatile atomic_short* object, int16_t desired) 203 | { 204 | return _InterlockedExchange16(object, desired); 205 | } 206 | 207 | int32_t zenny_atomic_exchange32(volatile atomic_long* object, int32_t desired) 208 | { 209 | return _InterlockedExchange(object, desired); 210 | } 211 | 212 | int64_t zenny_atomic_exchange64(volatile atomic_llong* object, int64_t desired) 213 | { 214 | return _InterlockedExchange64(object, desired); 215 | } 216 | 217 | bool zenny_atomic_compare_exchange8(volatile atomic_schar* object, int8_t* expected, int8_t desired) 218 | { 219 | int8_t comparand = *expected; 220 | const int8_t dstValue = _InterlockedCompareExchange8(object, desired, comparand); 221 | bool success = dstValue == comparand; 222 | if (!success) 223 | *expected = dstValue; 224 | 225 | return success; 226 | } 227 | 228 | bool zenny_atomic_compare_exchange16(volatile atomic_short* object, int16_t* expected, int16_t desired) 229 | { 230 | int16_t comparand = *expected; 231 | const int16_t dstValue = _InterlockedCompareExchange16(object, desired, comparand); 232 | bool success = dstValue == comparand; 233 | if (!success) 234 | * expected = dstValue; 235 | 236 | return success; 237 | } 238 | 239 | bool zenny_atomic_compare_exchange32(volatile atomic_long* object, int32_t* expected, int32_t desired) 240 | { 241 | int32_t comparand = *expected; 242 | int32_t dstValue = _InterlockedCompareExchange(object, desired, comparand); 243 | bool success = dstValue == comparand; 244 | if (!success) 245 | *expected = dstValue; 246 | 247 | return success; 248 | } 249 | 250 | bool zenny_atomic_compare_exchange64(volatile atomic_llong* object, int64_t* expected, int64_t desired) 251 | { 252 | int64_t comparand = *expected; 253 | int64_t dstValue = _InterlockedCompareExchange64(object, desired, comparand); 254 | bool success = dstValue == comparand; 255 | if (!success) 256 | *expected = dstValue; 257 | 258 | return success; 259 | } 260 | 261 | bool atomic_flag_test_and_set(volatile atomic_flag *flag) 262 | { 263 | return _InterlockedExchange8((volatile char*)flag, 1) == 1; 264 | } 265 | 266 | bool atomic_flag_test_and_set_explicit(volatile atomic_flag* object, memory_order order) 267 | { 268 | return atomic_flag_test_and_set(object); 269 | } 270 | 271 | void atomic_flag_clear(volatile atomic_flag *flag) 272 | { 273 | _InterlockedExchange8((volatile char*)flag, 0); 274 | } 275 | 276 | void atomic_flag_clear_explicit(volatile atomic_flag* object, memory_order order) 277 | { 278 | atomic_flag_clear(object); 279 | } 280 | 281 | #endif 282 | 283 | -------------------------------------------------------------------------------- /stdatomic.h: -------------------------------------------------------------------------------- 1 | #ifndef __MSVC_STDATOMIC_H 2 | #define __MSVC_STDATOMIC_H 3 | 4 | #if defined(_MSC_VER) && !defined(__cplusplus) 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #define ATOMIC_VAR_INIT(value) (value) 11 | #define atomic_init(object, value) (void)(*(object) = (value)) 12 | 13 | #ifdef __GNUC__ 14 | #define atomic_store(object, desired) (void)( *(volatile typeof(*(object)) *)(object) = (desired) ) 15 | #define atomic_load(object) *(volatile typeof(*(object)) *)(object) 16 | #else 17 | #define atomic_store(object, desired) (void)(*(object) = (desired)) 18 | #define atomic_load(object) *(object) 19 | #endif 20 | 21 | #define ATOMIC_FLAG_INIT { 0 } 22 | 23 | typedef enum memory_order { 24 | memory_order_relaxed, 25 | memory_order_consume, 26 | memory_order_acquire, 27 | memory_order_release, 28 | memory_order_acq_rel, 29 | memory_order_seq_cst 30 | } memory_order; 31 | 32 | #define atomic_load_explicit(object, order) atomic_load(object) 33 | #define atomic_store_explicit(object, desired, order) atomic_store((object), (desired)) 34 | 35 | 36 | typedef struct atomic_flag { bool _Value; } atomic_flag; 37 | 38 | typedef bool atomic_bool; 39 | typedef char atomic_char; 40 | typedef unsigned char atomic_uchar; 41 | typedef signed char atomic_schar; 42 | typedef short atomic_short; 43 | typedef unsigned short atomic_ushort; 44 | typedef int atomic_int; 45 | typedef unsigned atomic_uint; 46 | typedef long atomic_long; 47 | typedef unsigned long atomic_ulong; 48 | typedef long long atomic_llong; 49 | typedef unsigned long long atomic_ullong; 50 | typedef intptr_t atomic_intptr_t; 51 | typedef uintptr_t atomic_uintptr_t; 52 | typedef size_t atomic_size_t; 53 | typedef ptrdiff_t atomic_ptrdiff_t; 54 | typedef intmax_t atomic_intmax_t; 55 | typedef uintmax_t atomic_uintmax_t; 56 | typedef char16_t atomic_char16_t; 57 | typedef char32_t atomic_char32_t; 58 | typedef wchar_t atomic_wchar_t; 59 | 60 | 61 | extern bool atomic_flag_test_and_set(volatile atomic_flag * object); 62 | extern bool atomic_flag_test_and_set_explicit(volatile atomic_flag* object, memory_order order); 63 | 64 | extern void atomic_flag_clear(volatile atomic_flag* object); 65 | extern void atomic_flag_clear_explicit(volatile atomic_flag* object, memory_order order); 66 | 67 | 68 | #define atomic_fetch_add(object, operand) ((sizeof(*(object)) == 1) ? zenny_atomic_fetch_add8((volatile atomic_schar*)(object), (int8_t)(operand)) : \ 69 | ((sizeof(*(object)) == 2) ? zenny_atomic_fetch_add16((volatile atomic_short*)(object), (int16_t)(operand)) : \ 70 | ((sizeof(*(object)) == 4) ? zenny_atomic_fetch_add32((volatile atomic_long*)(object), (int32_t)(operand)) : \ 71 | zenny_atomic_fetch_add64((volatile atomic_llong*)(object), (int64_t)(operand)) ))) 72 | 73 | #define atomic_fetch_add_explicit(object, operand, order) atomic_fetch_add((object), (operand)) 74 | 75 | extern int8_t zenny_atomic_fetch_add8(volatile atomic_schar* object, int8_t operand); 76 | extern int16_t zenny_atomic_fetch_add16(volatile atomic_short* object, int16_t operand); 77 | extern int32_t zenny_atomic_fetch_add32(volatile atomic_long* object, int32_t operand); 78 | extern int64_t zenny_atomic_fetch_add64(volatile atomic_llong* object, int64_t operand); 79 | 80 | 81 | #define atomic_fetch_sub(object, operand) ((sizeof(*(object)) == 1) ? zenny_atomic_fetch_sub8((volatile atomic_schar*)(object), (int8_t)(operand)) : \ 82 | ((sizeof(*(object)) == 2) ? zenny_atomic_fetch_sub16((volatile atomic_short*)(object), (int16_t)(operand)) : \ 83 | ((sizeof(*(object)) == 4) ? zenny_atomic_fetch_sub32((volatile atomic_long*)(object), (int32_t)(operand)) : \ 84 | zenny_atomic_fetch_sub64((volatile atomic_llong*)(object), (int64_t)(operand)) ))) 85 | 86 | #define atomic_fetch_sub_explicit(object, operand, order) atomic_fetch_sub((object), (operand)) 87 | 88 | extern int8_t zenny_atomic_fetch_sub8(volatile atomic_schar* object, int8_t operand); 89 | extern int16_t zenny_atomic_fetch_sub16(volatile atomic_short* object, int16_t operand); 90 | extern int32_t zenny_atomic_fetch_sub32(volatile atomic_long* object, int32_t operand); 91 | extern int64_t zenny_atomic_fetch_sub64(volatile atomic_llong* object, int64_t operand); 92 | 93 | 94 | #define atomic_fetch_or(object, operand) ((sizeof(*(object)) == 1) ? zenny_atomic_fetch_or8((volatile atomic_schar*)(object), (int8_t)(operand)) : \ 95 | ((sizeof(*(object)) == 2) ? zenny_atomic_fetch_or16((volatile atomic_short*)(object), (int16_t)(operand)) : \ 96 | ((sizeof(*(object)) == 4) ? zenny_atomic_fetch_or32((volatile atomic_long*)(object), (int32_t)(operand)) : \ 97 | zenny_atomic_fetch_or64((volatile atomic_llong*)(object), (int64_t)(operand)) ))) 98 | 99 | #define atomic_fetch_or_explicit(object, operand, order) atomic_fetch_or((object), (operand)) 100 | 101 | extern int8_t zenny_atomic_fetch_or8(volatile atomic_schar* object, int8_t operand); 102 | extern int16_t zenny_atomic_fetch_or16(volatile atomic_short* object, int16_t operand); 103 | extern int32_t zenny_atomic_fetch_or32(volatile atomic_long* object, int32_t operand); 104 | extern int64_t zenny_atomic_fetch_or64(volatile atomic_llong* object, int64_t operand); 105 | 106 | 107 | #define atomic_fetch_xor(object, operand) ((sizeof(*(object)) == 1) ? zenny_atomic_fetch_xor8((volatile atomic_schar*)(object), (int8_t)(operand)) : \ 108 | ((sizeof(*(object)) == 2) ? zenny_atomic_fetch_xor16((volatile atomic_short*)(object), (int16_t)(operand)) : \ 109 | ((sizeof(*(object)) == 4) ? zenny_atomic_fetch_xor32((volatile atomic_long*)(object), (int32_t)(operand)) : \ 110 | zenny_atomic_fetch_xor64((volatile atomic_llong*)(object), (int64_t)(operand)) ))) 111 | 112 | #define atomic_fetch_xor_explicit(object, operand, order) atomic_fetch_xor((object), (operand)) 113 | 114 | extern int8_t zenny_atomic_fetch_xor8(volatile atomic_schar* object, int8_t operand); 115 | extern int16_t zenny_atomic_fetch_xor16(volatile atomic_short* object, int16_t operand); 116 | extern int32_t zenny_atomic_fetch_xor32(volatile atomic_long* object, int32_t operand); 117 | extern int64_t zenny_atomic_fetch_xor64(volatile atomic_llong* object, int64_t operand); 118 | 119 | 120 | #define atomic_fetch_and(object, operand) ((sizeof(*(object)) == 1) ? zenny_atomic_fetch_and8((volatile atomic_schar*)(object), (int8_t)(operand)) : \ 121 | ((sizeof(*(object)) == 2) ? zenny_atomic_fetch_and16((volatile atomic_short*)(object), (int16_t)(operand)) : \ 122 | ((sizeof(*(object)) == 4) ? zenny_atomic_fetch_and32((volatile atomic_long*)(object), (int32_t)(operand)) : \ 123 | zenny_atomic_fetch_and64((volatile atomic_llong*)(object), (int64_t)(operand)) ))) 124 | 125 | #define atomic_fetch_and_explicit(object, operand, order) atomic_fetch_and((object), (operand)) 126 | 127 | extern int8_t zenny_atomic_fetch_and8(volatile atomic_schar* object, int8_t operand); 128 | extern int16_t zenny_atomic_fetch_and16(volatile atomic_short* object, int16_t operand); 129 | extern int32_t zenny_atomic_fetch_and32(volatile atomic_long* object, int32_t operand); 130 | extern int64_t zenny_atomic_fetch_and64(volatile atomic_llong* object, int64_t operand); 131 | 132 | 133 | #define atomic_exchange(object, desired) ((sizeof(*(object)) == 1) ? zenny_atomic_exchange8((volatile atomic_schar*)(object), (int8_t)(desired)) : \ 134 | ((sizeof(*(object)) == 2) ? zenny_atomic_exchange16((volatile atomic_short*)(object), (int16_t)(desired)) : \ 135 | ((sizeof(*(object)) == 4) ? zenny_atomic_exchange32((volatile atomic_long*)(object), (int32_t)(desired)) : \ 136 | zenny_atomic_exchange64((volatile atomic_llong*)(object), (int64_t)(desired)) ))) 137 | 138 | #define atomic_exchange_explicit(object, desired, order) atomic_exchange((object), (desired)) 139 | 140 | extern int8_t zenny_atomic_exchange8(volatile atomic_schar* object, int8_t desired); 141 | extern int16_t zenny_atomic_exchange16(volatile atomic_short* object, int16_t desired); 142 | extern int32_t zenny_atomic_exchange32(volatile atomic_long* object, int32_t desired); 143 | extern int64_t zenny_atomic_exchange64(volatile atomic_llong* object, int64_t desired); 144 | 145 | 146 | #define atomic_compare_exchange_strong(object, expected, desired) (sizeof(*(object)) == 1 ? zenny_atomic_compare_exchange8((volatile atomic_schar*)(object), (int8_t*)(expected), (int8_t)(desired)) : \ 147 | (sizeof(*(object)) == 2 ? zenny_atomic_compare_exchange16((volatile atomic_short*)(object), (int16_t*)(expected), (int16_t)(desired)) : \ 148 | (sizeof(*(object)) == 4 ? zenny_atomic_compare_exchange32((volatile atomic_long *)(object), (int32_t*)(expected), (int32_t)(desired)) : \ 149 | zenny_atomic_compare_exchange64((volatile atomic_llong*)(object), (int64_t*)(expected), (int64_t)(desired)) ))) 150 | 151 | #define atomic_compare_exchange_weak atomic_compare_exchange_strong 152 | 153 | #define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure) atomic_compare_exchange_strong((object), (expected), (desired)) 154 | #define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure) atomic_compare_exchange_weak((object), (expected), (desired)) 155 | 156 | extern bool zenny_atomic_compare_exchange8(volatile atomic_schar *object, int8_t *expected, int8_t desired); 157 | extern bool zenny_atomic_compare_exchange16(volatile atomic_short *object, int16_t *expected, int16_t desired); 158 | extern bool zenny_atomic_compare_exchange32(volatile atomic_long *object, int32_t *expected, int32_t desired); 159 | extern bool zenny_atomic_compare_exchange64(volatile atomic_llong *object, int64_t *expected, int64_t desired); 160 | 161 | #endif // #if defined(_MSC_VER) && !defined(__cplusplus) 162 | 163 | #endif 164 | 165 | -------------------------------------------------------------------------------- /threads.h: -------------------------------------------------------------------------------- 1 | #ifndef __MSVC_THREADS_H 2 | #define __MSVC_THREADS_H 3 | 4 | #if defined(_MSC_VER) && !defined(__cplusplus) 5 | 6 | #define thread_local __declspec(thread) 7 | 8 | #endif 9 | 10 | #endif 11 | 12 | --------------------------------------------------------------------------------