{"action":"created","comment":{"url":"https://api.github.com/repos/apple/swift/pulls/comments/169806600","pull_request_review_id":98394711,"id":169806600,"diff_hunk":"@@ -21,127 +21,73 @@\n #include <functional>\n #include <sys/mman.h>\n #include <vector>\n-#include <pthread.h>\n-\n-#if !defined(_POSIX_BARRIERS) || _POSIX_BARRIERS < 0\n-// Implement pthread_barrier_* for platforms that don't implement them (Darwin)\n-\n-#define PTHREAD_BARRIER_SERIAL_THREAD 1\n-struct pthread_barrier_t {\n-  pthread_mutex_t mutex;\n-  pthread_cond_t cond;\n-\n-  unsigned count;\n-  unsigned numThreadsWaiting;\n-};\n-typedef void *pthread_barrierattr_t;\n-\n-static int pthread_barrier_init(pthread_barrier_t *barrier,\n-                                pthread_barrierattr_t*, unsigned count) {\n-  if (count == 0) {\n-    errno = EINVAL;\n-    return -1;\n-  }\n-  if (pthread_mutex_init(&barrier->mutex, nullptr) != 0) {\n-    return -1;\n-  }\n-  if (pthread_cond_init(&barrier->cond, nullptr) != 0) {\n-    pthread_mutex_destroy(&barrier->mutex);\n-    return -1;\n-  }\n-  barrier->count = count;\n-  barrier->numThreadsWaiting = 0;\n-  return 0;\n-}\n-\n-static int pthread_barrier_destroy(pthread_barrier_t *barrier) {\n-  // want to destroy both even if destroying one fails.\n-  int ret = 0;\n-  if (pthread_cond_destroy(&barrier->cond) != 0) {\n-    ret = -1;\n-  }\n-  if (pthread_mutex_destroy(&barrier->mutex) != 0) {\n-    ret = -1;\n-  }\n-  return ret;\n-}\n-\n-static int pthread_barrier_wait(pthread_barrier_t *barrier) {\n-  if (pthread_mutex_lock(&barrier->mutex) != 0) {\n-    return -1;\n-  }\n-  ++barrier->numThreadsWaiting;\n-  if (barrier->numThreadsWaiting < barrier->count) {\n-    // Put the thread to sleep.\n-    if (pthread_cond_wait(&barrier->cond, &barrier->mutex) != 0) {\n-      return -1;\n-    }\n-    if (pthread_mutex_unlock(&barrier->mutex) != 0) {\n-      return -1;\n-    }\n-    return 0;\n-  } else {\n-    // Reset thread count.\n-    barrier->numThreadsWaiting = 0;\n-\n-    // Wake up all threads.\n-    if (pthread_cond_broadcast(&barrier->cond) != 0) {\n-      return -1;\n-    }\n-    if (pthread_mutex_unlock(&barrier->mutex) != 0) {\n-      return -1;\n-    }\n-    return PTHREAD_BARRIER_SERIAL_THREAD;\n-  }\n-}\n-#endif\n+#include <thread>\n+#include <condition_variable>\n \n using namespace swift;\n \n // Race testing.\n \n template <typename T>\n-struct RaceArgs {\n+struct RaceThreadContext {\n   std::function<T()> code;\n-  pthread_barrier_t *go;\n+  T result;\n+\n+  unsigned numThreads;\n+  unsigned &numThreadsReady;\n+  std::mutex &sharedMutex;\n+  std::condition_variable &start_condition;\n };\n \n-void *RaceThunk(void *vargs) {\n-  RaceArgs<void*> *args = static_cast<RaceArgs<void*> *>(vargs);\n-  // Signal ready. Wait for go.\n-  pthread_barrier_wait(args->go);\n-  return args->code();\n+template <typename T>\n+void RaceThunk(RaceThreadContext<T> ctx) {\n+  // update shared state\n+  std::unique_lock<std::mutex> lk(ctx.sharedMutex);\n+  ++ctx.numThreadsReady;\n+  bool isLastThread = ctx.numThreadsReady == ctx.numThreads;\n+\n+  // wait until the rest of the thunks are ready\n+  ctx.start_condition.wait(lk, [&ctx]{ // waiting releases the lock\n+    return ctx.numThreadsReady == ctx.numThreads;\n+  });\n+  lk.unlock();\n+\n+  // The last thread will signal the condition_variable to kick off the rest\n+  // of the waiting threads to start.\n+  if (isLastThread) ctx.start_condition.notify_all();\n+\n+  ctx.result = ctx.code();\n }\n \n /// RaceTest(code) runs code in many threads simultaneously, \n /// and returns a vector of all returned results.\n-template <typename T, int NumThreads = 64>\n-std::vector<T> \n-RaceTest(std::function<T()> code)\n-{\n-  const unsigned threadCount = NumThreads;\n-\n-  pthread_barrier_t go;\n-  pthread_barrier_init(&go, nullptr, threadCount);\n-\n-  // Create the threads.\n-  pthread_t threads[threadCount];\n-  std::vector<RaceArgs<T>> args(threadCount, {code, &go});\n-\n-  for (unsigned i = 0; i < threadCount; i++) {\n-    pthread_create(&threads[i], nullptr, &RaceThunk, &args[i]);\n+template <typename T, unsigned NumThreads = 64>\n+std::vector<T> RaceTest(std::function<T()> code) {\n+  unsigned numThreadsReady = 0;\n+  std::mutex sharedMutex;\n+  std::condition_variable start_condition;\n+\n+  // Create the vector + contexts.\n+  std::vector<std::thread> threads(NumThreads);\n+  std::vector<RaceThreadContext<T>> contexts(NumThreads, {\n+\t\t  /*code*/ code,\n+\t\t  /*result*/ nullptr,\n+\t\t  NumThreads,\n+\t\t  numThreadsReady,\n+\t\t  sharedMutex,\n+\t\t  start_condition});\n+\n+  // Create all the threads\n+  for (unsigned i = 0; i < NumThreads; i++) {\n+    threads.emplace_back(std::thread(RaceThunk<T>, contexts[i]));","path":"unittests/runtime/Metadata.cpp","position":158,"original_position":158,"commit_id":"5401cfc2fb2aee759971f1a52255fdd25f311588","original_commit_id":"5401cfc2fb2aee759971f1a52255fdd25f311588","user":{"login":"AndrewSB","id":3814772,"avatar_url":"https://avatars1.githubusercontent.com/u/3814772?v=4","gravatar_id":"","url":"https://api.github.com/users/AndrewSB","html_url":"https://github.com/AndrewSB","followers_url":"https://api.github.com/users/AndrewSB/followers","following_url":"https://api.github.com/users/AndrewSB/following{/other_user}","gists_url":"https://api.github.com/users/AndrewSB/gists{/gist_id}","starred_url":"https://api.github.com/users/AndrewSB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AndrewSB/subscriptions","organizations_url":"https://api.github.com/users/AndrewSB/orgs","repos_url":"https://api.github.com/users/AndrewSB/repos","events_url":"https://api.github.com/users/AndrewSB/events{/privacy}","received_events_url":"https://api.github.com/users/AndrewSB/received_events","type":"User","site_admin":false},"body":"I'm sorry! I'm still learning cpp, my bad 😬\r\nHow should I update the vector initializer to not fill in default values? I looked at http://en.cppreference.com/w/cpp/container/vector/vector but it doesn't seem easy to have the `Allocator` not insert values. Should I switch from `emplace_back`ing to assigning to `threads[i]`?","created_at":"2018-02-21T23:02:54Z","updated_at":"2018-02-21T23:02:54Z","html_url":"https://github.com/apple/swift/pull/14598#discussion_r169806600","pull_request_url":"https://api.github.com/repos/apple/swift/pulls/14598","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/apple/swift/pulls/comments/169806600"},"html":{"href":"https://github.com/apple/swift/pull/14598#discussion_r169806600"},"pull_request":{"href":"https://api.github.com/repos/apple/swift/pulls/14598"}},"in_reply_to_id":169804749},"pull_request":{"url":"https://api.github.com/repos/apple/swift/pulls/14598","id":168941453,"html_url":"https://github.com/apple/swift/pull/14598","diff_url":"https://github.com/apple/swift/pull/14598.diff","patch_url":"https://github.com/apple/swift/pull/14598.patch","issue_url":"https://api.github.com/repos/apple/swift/issues/14598","number":14598,"state":"open","locked":false,"title":"unittests/runtime: Migrate tests from pthreads to std::thread","user":{"login":"AndrewSB","id":3814772,"avatar_url":"https://avatars1.githubusercontent.com/u/3814772?v=4","gravatar_id":"","url":"https://api.github.com/users/AndrewSB","html_url":"https://github.com/AndrewSB","followers_url":"https://api.github.com/users/AndrewSB/followers","following_url":"https://api.github.com/users/AndrewSB/following{/other_user}","gists_url":"https://api.github.com/users/AndrewSB/gists{/gist_id}","starred_url":"https://api.github.com/users/AndrewSB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AndrewSB/subscriptions","organizations_url":"https://api.github.com/users/AndrewSB/orgs","repos_url":"https://api.github.com/users/AndrewSB/repos","events_url":"https://api.github.com/users/AndrewSB/events{/privacy}","received_events_url":"https://api.github.com/users/AndrewSB/received_events","type":"User","site_admin":false},"body":"This is required for Windows, which has std::threads, but no pthreads.\r\nThis is probably completely broken, working on running the tests locally right now.\r\n\r\nIf a member could ask @swift-ci to test, that would be much appreciated ❤️ ","created_at":"2018-02-13T19:59:00Z","updated_at":"2018-02-21T23:02:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d6e1237d2a4100f281f871fb3f443f240970a0a2","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/apple/swift/pulls/14598/commits","review_comments_url":"https://api.github.com/repos/apple/swift/pulls/14598/comments","review_comment_url":"https://api.github.com/repos/apple/swift/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apple/swift/issues/14598/comments","statuses_url":"https://api.github.com/repos/apple/swift/statuses/5401cfc2fb2aee759971f1a52255fdd25f311588","head":{"label":"AndrewSB:pthreads=😥","ref":"pthreads=😥","sha":"5401cfc2fb2aee759971f1a52255fdd25f311588","user":{"login":"AndrewSB","id":3814772,"avatar_url":"https://avatars1.githubusercontent.com/u/3814772?v=4","gravatar_id":"","url":"https://api.github.com/users/AndrewSB","html_url":"https://github.com/AndrewSB","followers_url":"https://api.github.com/users/AndrewSB/followers","following_url":"https://api.github.com/users/AndrewSB/following{/other_user}","gists_url":"https://api.github.com/users/AndrewSB/gists{/gist_id}","starred_url":"https://api.github.com/users/AndrewSB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AndrewSB/subscriptions","organizations_url":"https://api.github.com/users/AndrewSB/orgs","repos_url":"https://api.github.com/users/AndrewSB/repos","events_url":"https://api.github.com/users/AndrewSB/events{/privacy}","received_events_url":"https://api.github.com/users/AndrewSB/received_events","type":"User","site_admin":false},"repo":{"id":118685602,"name":"swift","full_name":"AndrewSB/swift","owner":{"login":"AndrewSB","id":3814772,"avatar_url":"https://avatars1.githubusercontent.com/u/3814772?v=4","gravatar_id":"","url":"https://api.github.com/users/AndrewSB","html_url":"https://github.com/AndrewSB","followers_url":"https://api.github.com/users/AndrewSB/followers","following_url":"https://api.github.com/users/AndrewSB/following{/other_user}","gists_url":"https://api.github.com/users/AndrewSB/gists{/gist_id}","starred_url":"https://api.github.com/users/AndrewSB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AndrewSB/subscriptions","organizations_url":"https://api.github.com/users/AndrewSB/orgs","repos_url":"https://api.github.com/users/AndrewSB/repos","events_url":"https://api.github.com/users/AndrewSB/events{/privacy}","received_events_url":"https://api.github.com/users/AndrewSB/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/AndrewSB/swift","description":"The Swift Programming Language","fork":true,"url":"https://api.github.com/repos/AndrewSB/swift","forks_url":"https://api.github.com/repos/AndrewSB/swift/forks","keys_url":"https://api.github.com/repos/AndrewSB/swift/keys{/key_id}","collaborators_url":"https://api.github.com/repos/AndrewSB/swift/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/AndrewSB/swift/teams","hooks_url":"https://api.github.com/repos/AndrewSB/swift/hooks","issue_events_url":"https://api.github.com/repos/AndrewSB/swift/issues/events{/number}","events_url":"https://api.github.com/repos/AndrewSB/swift/events","assignees_url":"https://api.github.com/repos/AndrewSB/swift/assignees{/user}","branches_url":"https://api.github.com/repos/AndrewSB/swift/branches{/branch}","tags_url":"https://api.github.com/repos/AndrewSB/swift/tags","blobs_url":"https://api.github.com/repos/AndrewSB/swift/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/AndrewSB/swift/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/AndrewSB/swift/git/refs{/sha}","trees_url":"https://api.github.com/repos/AndrewSB/swift/git/trees{/sha}","statuses_url":"https://api.github.com/repos/AndrewSB/swift/statuses/{sha}","languages_url":"https://api.github.com/repos/AndrewSB/swift/languages","stargazers_url":"https://api.github.com/repos/AndrewSB/swift/stargazers","contributors_url":"https://api.github.com/repos/AndrewSB/swift/contributors","subscribers_url":"https://api.github.com/repos/AndrewSB/swift/subscribers","subscription_url":"https://api.github.com/repos/AndrewSB/swift/subscription","commits_url":"https://api.github.com/repos/AndrewSB/swift/commits{/sha}","git_commits_url":"https://api.github.com/repos/AndrewSB/swift/git/commits{/sha}","comments_url":"https://api.github.com/repos/AndrewSB/swift/comments{/number}","issue_comment_url":"https://api.github.com/repos/AndrewSB/swift/issues/comments{/number}","contents_url":"https://api.github.com/repos/AndrewSB/swift/contents/{+path}","compare_url":"https://api.github.com/repos/AndrewSB/swift/compare/{base}...{head}","merges_url":"https://api.github.com/repos/AndrewSB/swift/merges","archive_url":"https://api.github.com/repos/AndrewSB/swift/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/AndrewSB/swift/downloads","issues_url":"https://api.github.com/repos/AndrewSB/swift/issues{/number}","pulls_url":"https://api.github.com/repos/AndrewSB/swift/pulls{/number}","milestones_url":"https://api.github.com/repos/AndrewSB/swift/milestones{/number}","notifications_url":"https://api.github.com/repos/AndrewSB/swift/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/AndrewSB/swift/labels{/name}","releases_url":"https://api.github.com/repos/AndrewSB/swift/releases{/id}","deployments_url":"https://api.github.com/repos/AndrewSB/swift/deployments","created_at":"2018-01-23T23:47:08Z","updated_at":"2018-01-23T23:47:41Z","pushed_at":"2018-02-21T22:18:35Z","git_url":"git://github.com/AndrewSB/swift.git","ssh_url":"git@github.com:AndrewSB/swift.git","clone_url":"https://github.com/AndrewSB/swift.git","svn_url":"https://github.com/AndrewSB/swift","homepage":"https://swift.org/","size":264641,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apple:master","ref":"master","sha":"03590d1fe12ea1a991def4f098d05bd98a144def","user":{"login":"apple","id":10639145,"avatar_url":"https://avatars0.githubusercontent.com/u/10639145?v=4","gravatar_id":"","url":"https://api.github.com/users/apple","html_url":"https://github.com/apple","followers_url":"https://api.github.com/users/apple/followers","following_url":"https://api.github.com/users/apple/following{/other_user}","gists_url":"https://api.github.com/users/apple/gists{/gist_id}","starred_url":"https://api.github.com/users/apple/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apple/subscriptions","organizations_url":"https://api.github.com/users/apple/orgs","repos_url":"https://api.github.com/users/apple/repos","events_url":"https://api.github.com/users/apple/events{/privacy}","received_events_url":"https://api.github.com/users/apple/received_events","type":"Organization","site_admin":false},"repo":{"id":44838949,"name":"swift","full_name":"apple/swift","owner":{"login":"apple","id":10639145,"avatar_url":"https://avatars0.githubusercontent.com/u/10639145?v=4","gravatar_id":"","url":"https://api.github.com/users/apple","html_url":"https://github.com/apple","followers_url":"https://api.github.com/users/apple/followers","following_url":"https://api.github.com/users/apple/following{/other_user}","gists_url":"https://api.github.com/users/apple/gists{/gist_id}","starred_url":"https://api.github.com/users/apple/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apple/subscriptions","organizations_url":"https://api.github.com/users/apple/orgs","repos_url":"https://api.github.com/users/apple/repos","events_url":"https://api.github.com/users/apple/events{/privacy}","received_events_url":"https://api.github.com/users/apple/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apple/swift","description":"The Swift Programming Language","fork":false,"url":"https://api.github.com/repos/apple/swift","forks_url":"https://api.github.com/repos/apple/swift/forks","keys_url":"https://api.github.com/repos/apple/swift/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apple/swift/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apple/swift/teams","hooks_url":"https://api.github.com/repos/apple/swift/hooks","issue_events_url":"https://api.github.com/repos/apple/swift/issues/events{/number}","events_url":"https://api.github.com/repos/apple/swift/events","assignees_url":"https://api.github.com/repos/apple/swift/assignees{/user}","branches_url":"https://api.github.com/repos/apple/swift/branches{/branch}","tags_url":"https://api.github.com/repos/apple/swift/tags","blobs_url":"https://api.github.com/repos/apple/swift/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apple/swift/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apple/swift/git/refs{/sha}","trees_url":"https://api.github.com/repos/apple/swift/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apple/swift/statuses/{sha}","languages_url":"https://api.github.com/repos/apple/swift/languages","stargazers_url":"https://api.github.com/repos/apple/swift/stargazers","contributors_url":"https://api.github.com/repos/apple/swift/contributors","subscribers_url":"https://api.github.com/repos/apple/swift/subscribers","subscription_url":"https://api.github.com/repos/apple/swift/subscription","commits_url":"https://api.github.com/repos/apple/swift/commits{/sha}","git_commits_url":"https://api.github.com/repos/apple/swift/git/commits{/sha}","comments_url":"https://api.github.com/repos/apple/swift/comments{/number}","issue_comment_url":"https://api.github.com/repos/apple/swift/issues/comments{/number}","contents_url":"https://api.github.com/repos/apple/swift/contents/{+path}","compare_url":"https://api.github.com/repos/apple/swift/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apple/swift/merges","archive_url":"https://api.github.com/repos/apple/swift/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apple/swift/downloads","issues_url":"https://api.github.com/repos/apple/swift/issues{/number}","pulls_url":"https://api.github.com/repos/apple/swift/pulls{/number}","milestones_url":"https://api.github.com/repos/apple/swift/milestones{/number}","notifications_url":"https://api.github.com/repos/apple/swift/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apple/swift/labels{/name}","releases_url":"https://api.github.com/repos/apple/swift/releases{/id}","deployments_url":"https://api.github.com/repos/apple/swift/deployments","created_at":"2015-10-23T21:15:07Z","updated_at":"2018-02-21T21:32:19Z","pushed_at":"2018-02-21T22:57:05Z","git_url":"git://github.com/apple/swift.git","ssh_url":"git@github.com:apple/swift.git","clone_url":"https://github.com/apple/swift.git","svn_url":"https://github.com/apple/swift","homepage":"https://swift.org/","size":266036,"stargazers_count":42699,"watchers_count":42699,"language":"C++","has_issues":false,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":6705,"mirror_url":null,"archived":false,"open_issues_count":292,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":6705,"open_issues":292,"watchers":42699,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apple/swift/pulls/14598"},"html":{"href":"https://github.com/apple/swift/pull/14598"},"issue":{"href":"https://api.github.com/repos/apple/swift/issues/14598"},"comments":{"href":"https://api.github.com/repos/apple/swift/issues/14598/comments"},"review_comments":{"href":"https://api.github.com/repos/apple/swift/pulls/14598/comments"},"review_comment":{"href":"https://api.github.com/repos/apple/swift/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apple/swift/pulls/14598/commits"},"statuses":{"href":"https://api.github.com/repos/apple/swift/statuses/5401cfc2fb2aee759971f1a52255fdd25f311588"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"AndrewSB"}}