Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/rwlock_static_init.cc

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0 OR ISC
3
4 // This test attempts to setup a potential race condition that can cause the
5 // first use of an rwlock to fail when initialized using
6 // PTHREAD_RWLOCK_INITIALIZER.
7 // See: https://sourceforge.net/p/mingw-w64/bugs/883/
8
9 #include <openssl/rand.h>
10
11 #include <cstdint>
12 #include <iostream>
13 #include <thread>
14 #include <cassert>
15 #include <cstdlib>
16
17 static void thread_task_rand(bool *myFlag) {
18 uint8_t buf[16];
19 if(1 == RAND_bytes(buf, sizeof(buf))) {
20 *myFlag = true;
21 }
22 }
23
24 int main(int _argc, char** _argv) {
25 constexpr size_t kNumThreads = 16;
26 bool myFlags[kNumThreads] = {};
27 std::thread myThreads[kNumThreads];
28
29 for (size_t i = 0; i < kNumThreads; i++) {
30 bool* myFlag = &myFlags[i];
31 myThreads[i] = std::thread(thread_task_rand, myFlag);
32 }
33 for (size_t i = 0; i < kNumThreads; i++) {
34 myThreads[i].join();
35 if(!myFlags[i]) {
36 std::cerr << "Thread " << i << " failed." << std::endl;
37 exit(1);
38 return 1;
39 }
40 }
41 std::cout << "PASS" << std::endl;
42 return 0;
43 }