{"action":"created","comment":{"url":"https://api.github.com/repos/facebook/folly/pulls/comments/35788639","id":35788639,"diff_hunk":"@@ -26,16 +26,33 @@\n #include <folly/File.h>\n #include <folly/FileUtil.h>\n \n+#ifdef _MSC_VER\n+#include <wincrypt.h>\n+#endif\n+\n namespace folly {\n \n namespace {\n \n void readRandomDevice(void* data, size_t size) {\n+#ifdef _MSC_VER\n+  static bool initialized = false;","path":"folly/Random.cpp","position":14,"original_position":14,"commit_id":"681a16de27c879657381d8f5a74e0a6eecf9f3b4","original_commit_id":"681a16de27c879657381d8f5a74e0a6eecf9f3b4","user":{"login":"sgolemon","id":812538,"avatar_url":"https://avatars.githubusercontent.com/u/812538?v=3","gravatar_id":"","url":"https://api.github.com/users/sgolemon","html_url":"https://github.com/sgolemon","followers_url":"https://api.github.com/users/sgolemon/followers","following_url":"https://api.github.com/users/sgolemon/following{/other_user}","gists_url":"https://api.github.com/users/sgolemon/gists{/gist_id}","starred_url":"https://api.github.com/users/sgolemon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sgolemon/subscriptions","organizations_url":"https://api.github.com/users/sgolemon/orgs","repos_url":"https://api.github.com/users/sgolemon/repos","events_url":"https://api.github.com/users/sgolemon/events{/privacy}","received_events_url":"https://api.github.com/users/sgolemon/received_events","type":"User","site_admin":false},"body":"(From @yfeldblum) This is not threadsafe. Two threads that simultaneously call readRandomDevice can overwrite each other's data. Using std::once_flag should help.\r\n\r\n```\r\n#include <mutex>\r\n\r\nstatic std::once_flag flag;\r\nstatic HCRYPTPROV provider;\r\nstd::call_once(flag, [&] }\r\n  PCHECK(CryptAcquireContext(&provider, nullptr, nullptr, PROV_RSA_FULL, 0));\r\n});\r\nCHECK(size <= std::numeric_limits<DWORD>::max()); // not PCHECK\r\nPCHECK(CryptGenRandom(provider, (DWORD)size, (BYTE*)data);\r\n```\r\n\r\nThe other possibility is to use ThreadLocal.","created_at":"2015-07-29T17:42:10Z","updated_at":"2015-07-29T17:42:10Z","html_url":"https://github.com/facebook/folly/pull/250#discussion_r35788639","pull_request_url":"https://api.github.com/repos/facebook/folly/pulls/250","_links":{"self":{"href":"https://api.github.com/repos/facebook/folly/pulls/comments/35788639"},"html":{"href":"https://github.com/facebook/folly/pull/250#discussion_r35788639"},"pull_request":{"href":"https://api.github.com/repos/facebook/folly/pulls/250"}}},"pull_request":{"url":"https://api.github.com/repos/facebook/folly/pulls/250","id":40734422,"html_url":"https://github.com/facebook/folly/pull/250","diff_url":"https://github.com/facebook/folly/pull/250.diff","patch_url":"https://github.com/facebook/folly/pull/250.patch","issue_url":"https://api.github.com/repos/facebook/folly/issues/250","number":250,"state":"open","locked":false,"title":"Implement Random.cpp for MSVC","user":{"login":"Orvid","id":709247,"avatar_url":"https://avatars.githubusercontent.com/u/709247?v=3","gravatar_id":"","url":"https://api.github.com/users/Orvid","html_url":"https://github.com/Orvid","followers_url":"https://api.github.com/users/Orvid/followers","following_url":"https://api.github.com/users/Orvid/following{/other_user}","gists_url":"https://api.github.com/users/Orvid/gists{/gist_id}","starred_url":"https://api.github.com/users/Orvid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Orvid/subscriptions","organizations_url":"https://api.github.com/users/Orvid/orgs","repos_url":"https://api.github.com/users/Orvid/repos","events_url":"https://api.github.com/users/Orvid/events{/privacy}","received_events_url":"https://api.github.com/users/Orvid/received_events","type":"User","site_admin":false},"body":"This uses `<random>` to implement it, because there is no `/dev/urandom` on Windows.","created_at":"2015-07-24T01:07:55Z","updated_at":"2015-07-29T17:42:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c5cc824aacf719e764e3d236afe276b1a48e4cd6","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/facebook/folly/pulls/250/commits","review_comments_url":"https://api.github.com/repos/facebook/folly/pulls/250/comments","review_comment_url":"https://api.github.com/repos/facebook/folly/pulls/comments{/number}","comments_url":"https://api.github.com/repos/facebook/folly/issues/250/comments","statuses_url":"https://api.github.com/repos/facebook/folly/statuses/681a16de27c879657381d8f5a74e0a6eecf9f3b4","head":{"label":"Orvid:msvc-random-impl","ref":"msvc-random-impl","sha":"681a16de27c879657381d8f5a74e0a6eecf9f3b4","user":{"login":"Orvid","id":709247,"avatar_url":"https://avatars.githubusercontent.com/u/709247?v=3","gravatar_id":"","url":"https://api.github.com/users/Orvid","html_url":"https://github.com/Orvid","followers_url":"https://api.github.com/users/Orvid/followers","following_url":"https://api.github.com/users/Orvid/following{/other_user}","gists_url":"https://api.github.com/users/Orvid/gists{/gist_id}","starred_url":"https://api.github.com/users/Orvid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Orvid/subscriptions","organizations_url":"https://api.github.com/users/Orvid/orgs","repos_url":"https://api.github.com/users/Orvid/repos","events_url":"https://api.github.com/users/Orvid/events{/privacy}","received_events_url":"https://api.github.com/users/Orvid/received_events","type":"User","site_admin":false},"repo":{"id":36701771,"name":"folly","full_name":"Orvid/folly","owner":{"login":"Orvid","id":709247,"avatar_url":"https://avatars.githubusercontent.com/u/709247?v=3","gravatar_id":"","url":"https://api.github.com/users/Orvid","html_url":"https://github.com/Orvid","followers_url":"https://api.github.com/users/Orvid/followers","following_url":"https://api.github.com/users/Orvid/following{/other_user}","gists_url":"https://api.github.com/users/Orvid/gists{/gist_id}","starred_url":"https://api.github.com/users/Orvid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Orvid/subscriptions","organizations_url":"https://api.github.com/users/Orvid/orgs","repos_url":"https://api.github.com/users/Orvid/repos","events_url":"https://api.github.com/users/Orvid/events{/privacy}","received_events_url":"https://api.github.com/users/Orvid/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Orvid/folly","description":"An open-source C++ library developed and used at Facebook.","fork":true,"url":"https://api.github.com/repos/Orvid/folly","forks_url":"https://api.github.com/repos/Orvid/folly/forks","keys_url":"https://api.github.com/repos/Orvid/folly/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Orvid/folly/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Orvid/folly/teams","hooks_url":"https://api.github.com/repos/Orvid/folly/hooks","issue_events_url":"https://api.github.com/repos/Orvid/folly/issues/events{/number}","events_url":"https://api.github.com/repos/Orvid/folly/events","assignees_url":"https://api.github.com/repos/Orvid/folly/assignees{/user}","branches_url":"https://api.github.com/repos/Orvid/folly/branches{/branch}","tags_url":"https://api.github.com/repos/Orvid/folly/tags","blobs_url":"https://api.github.com/repos/Orvid/folly/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Orvid/folly/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Orvid/folly/git/refs{/sha}","trees_url":"https://api.github.com/repos/Orvid/folly/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Orvid/folly/statuses/{sha}","languages_url":"https://api.github.com/repos/Orvid/folly/languages","stargazers_url":"https://api.github.com/repos/Orvid/folly/stargazers","contributors_url":"https://api.github.com/repos/Orvid/folly/contributors","subscribers_url":"https://api.github.com/repos/Orvid/folly/subscribers","subscription_url":"https://api.github.com/repos/Orvid/folly/subscription","commits_url":"https://api.github.com/repos/Orvid/folly/commits{/sha}","git_commits_url":"https://api.github.com/repos/Orvid/folly/git/commits{/sha}","comments_url":"https://api.github.com/repos/Orvid/folly/comments{/number}","issue_comment_url":"https://api.github.com/repos/Orvid/folly/issues/comments{/number}","contents_url":"https://api.github.com/repos/Orvid/folly/contents/{+path}","compare_url":"https://api.github.com/repos/Orvid/folly/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Orvid/folly/merges","archive_url":"https://api.github.com/repos/Orvid/folly/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Orvid/folly/downloads","issues_url":"https://api.github.com/repos/Orvid/folly/issues{/number}","pulls_url":"https://api.github.com/repos/Orvid/folly/pulls{/number}","milestones_url":"https://api.github.com/repos/Orvid/folly/milestones{/number}","notifications_url":"https://api.github.com/repos/Orvid/folly/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Orvid/folly/labels{/name}","releases_url":"https://api.github.com/repos/Orvid/folly/releases{/id}","created_at":"2015-06-02T02:10:56Z","updated_at":"2015-06-02T02:10:59Z","pushed_at":"2015-07-28T00:30:18Z","git_url":"git://github.com/Orvid/folly.git","ssh_url":"git@github.com:Orvid/folly.git","clone_url":"https://github.com/Orvid/folly.git","svn_url":"https://github.com/Orvid/folly","homepage":"https://groups.google.com/forum/?fromgroups#!forum/facebook-folly","size":6777,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"facebook:master","ref":"master","sha":"5532f19f7b604a9b12c79a1d0a4fe3b0ff74f3d1","user":{"login":"facebook","id":69631,"avatar_url":"https://avatars.githubusercontent.com/u/69631?v=3","gravatar_id":"","url":"https://api.github.com/users/facebook","html_url":"https://github.com/facebook","followers_url":"https://api.github.com/users/facebook/followers","following_url":"https://api.github.com/users/facebook/following{/other_user}","gists_url":"https://api.github.com/users/facebook/gists{/gist_id}","starred_url":"https://api.github.com/users/facebook/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/facebook/subscriptions","organizations_url":"https://api.github.com/users/facebook/orgs","repos_url":"https://api.github.com/users/facebook/repos","events_url":"https://api.github.com/users/facebook/events{/privacy}","received_events_url":"https://api.github.com/users/facebook/received_events","type":"Organization","site_admin":false},"repo":{"id":4524181,"name":"folly","full_name":"facebook/folly","owner":{"login":"facebook","id":69631,"avatar_url":"https://avatars.githubusercontent.com/u/69631?v=3","gravatar_id":"","url":"https://api.github.com/users/facebook","html_url":"https://github.com/facebook","followers_url":"https://api.github.com/users/facebook/followers","following_url":"https://api.github.com/users/facebook/following{/other_user}","gists_url":"https://api.github.com/users/facebook/gists{/gist_id}","starred_url":"https://api.github.com/users/facebook/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/facebook/subscriptions","organizations_url":"https://api.github.com/users/facebook/orgs","repos_url":"https://api.github.com/users/facebook/repos","events_url":"https://api.github.com/users/facebook/events{/privacy}","received_events_url":"https://api.github.com/users/facebook/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/facebook/folly","description":"An open-source C++ library developed and used at Facebook.","fork":false,"url":"https://api.github.com/repos/facebook/folly","forks_url":"https://api.github.com/repos/facebook/folly/forks","keys_url":"https://api.github.com/repos/facebook/folly/keys{/key_id}","collaborators_url":"https://api.github.com/repos/facebook/folly/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/facebook/folly/teams","hooks_url":"https://api.github.com/repos/facebook/folly/hooks","issue_events_url":"https://api.github.com/repos/facebook/folly/issues/events{/number}","events_url":"https://api.github.com/repos/facebook/folly/events","assignees_url":"https://api.github.com/repos/facebook/folly/assignees{/user}","branches_url":"https://api.github.com/repos/facebook/folly/branches{/branch}","tags_url":"https://api.github.com/repos/facebook/folly/tags","blobs_url":"https://api.github.com/repos/facebook/folly/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/facebook/folly/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/facebook/folly/git/refs{/sha}","trees_url":"https://api.github.com/repos/facebook/folly/git/trees{/sha}","statuses_url":"https://api.github.com/repos/facebook/folly/statuses/{sha}","languages_url":"https://api.github.com/repos/facebook/folly/languages","stargazers_url":"https://api.github.com/repos/facebook/folly/stargazers","contributors_url":"https://api.github.com/repos/facebook/folly/contributors","subscribers_url":"https://api.github.com/repos/facebook/folly/subscribers","subscription_url":"https://api.github.com/repos/facebook/folly/subscription","commits_url":"https://api.github.com/repos/facebook/folly/commits{/sha}","git_commits_url":"https://api.github.com/repos/facebook/folly/git/commits{/sha}","comments_url":"https://api.github.com/repos/facebook/folly/comments{/number}","issue_comment_url":"https://api.github.com/repos/facebook/folly/issues/comments{/number}","contents_url":"https://api.github.com/repos/facebook/folly/contents/{+path}","compare_url":"https://api.github.com/repos/facebook/folly/compare/{base}...{head}","merges_url":"https://api.github.com/repos/facebook/folly/merges","archive_url":"https://api.github.com/repos/facebook/folly/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/facebook/folly/downloads","issues_url":"https://api.github.com/repos/facebook/folly/issues{/number}","pulls_url":"https://api.github.com/repos/facebook/folly/pulls{/number}","milestones_url":"https://api.github.com/repos/facebook/folly/milestones{/number}","notifications_url":"https://api.github.com/repos/facebook/folly/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/facebook/folly/labels{/name}","releases_url":"https://api.github.com/repos/facebook/folly/releases{/id}","created_at":"2012-06-01T20:49:04Z","updated_at":"2015-07-29T06:12:04Z","pushed_at":"2015-07-29T16:22:13Z","git_url":"git://github.com/facebook/folly.git","ssh_url":"git@github.com:facebook/folly.git","clone_url":"https://github.com/facebook/folly.git","svn_url":"https://github.com/facebook/folly","homepage":"https://groups.google.com/forum/?fromgroups#!forum/facebook-folly","size":9967,"stargazers_count":5109,"watchers_count":5109,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1117,"mirror_url":null,"open_issues_count":57,"forks":1117,"open_issues":57,"watchers":5109,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/facebook/folly/pulls/250"},"html":{"href":"https://github.com/facebook/folly/pull/250"},"issue":{"href":"https://api.github.com/repos/facebook/folly/issues/250"},"comments":{"href":"https://api.github.com/repos/facebook/folly/issues/250/comments"},"review_comments":{"href":"https://api.github.com/repos/facebook/folly/pulls/250/comments"},"review_comment":{"href":"https://api.github.com/repos/facebook/folly/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/facebook/folly/pulls/250/commits"},"statuses":{"href":"https://api.github.com/repos/facebook/folly/statuses/681a16de27c879657381d8f5a74e0a6eecf9f3b4"}}}}