/*
 *  Copyright (c) 2017, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 *
 */

#pragma once

#include <atomic>
#include <string>
#include <thread>

#include <wangle/concurrent/ThreadFactory.h>
#include <folly/Conv.h>
#include <folly/Range.h>
#include <folly/ThreadName.h>

namespace wangle {

class NamedThreadFactory : public ThreadFactory {
 public:
  explicit NamedThreadFactory(folly::StringPiece prefix)
    : prefix_(prefix.str()), suffix_(0) {}

  std::thread newThread(folly::Func&& func) override {
    auto thread = std::thread([&](folly::Func&& funct) {
      folly::setThreadName(folly::to<std::string>(prefix_, suffix_++));
      funct();
    }, std::move(func));
    return thread;
  }

  void setNamePrefix(folly::StringPiece prefix) {
    prefix_ = prefix.str();
  }

  std::string getNamePrefix() {
    return prefix_;
  }

 private:
  std::string prefix_;
  std::atomic<uint64_t> suffix_;
};

} // namespace wangle

/*
 *  Copyright (c) 2017, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 *
 */

#pragma once

#include <atomic>
#include <string>
#include <thread>

#include <wangle/concurrent/ThreadFactory.h>
#include <folly/Conv.h>
#include <folly/Range.h>
#include <folly/ThreadName.h>

namespace wangle {

class NamedThreadFactory : public ThreadFactory {
 public:
  explicit NamedThreadFactory(folly::StringPiece prefix)
    : prefix_(prefix.str()), suffix_(0) {}

  std::thread newThread(folly::Func&& func) override {
    auto thread = std::thread(std::move(func));
    folly::setThreadName(
        thread.native_handle(),
        folly::to<std::string>(prefix_, suffix_++));
    return thread;
  }

  void setNamePrefix(folly::StringPiece prefix) {
    prefix_ = prefix.str();
  }

  std::string getNamePrefix() {
    return prefix_;
  }

 private:
  std::string prefix_;
  std::atomic<uint64_t> suffix_;
};

} // namespace wangle

--- 
+++ 
@@ -27,10 +27,10 @@
     : prefix_(prefix.str()), suffix_(0) {}
 
   std::thread newThread(folly::Func&& func) override {
-    auto thread = std::thread(std::move(func));
-    folly::setThreadName(
-        thread.native_handle(),
-        folly::to<std::string>(prefix_, suffix_++));
+    auto thread = std::thread([&](folly::Func&& funct) {
+      folly::setThreadName(folly::to<std::string>(prefix_, suffix_++));
+      funct();
+    }, std::move(func));
     return thread;
   }
 
Switch various calls to folly::setThreadName to set the current thread's name

Summary: This is in preparation for killing the pthread_t overload of folly::setThreadName.

Reviewed By: yfeldblum

Differential Revision: D5012627

fbshipit-source-id: a4e6e2c2cb5bd02b1ebea85c305eac59355a7d42
