Skip to content

Commit

Permalink
Merge pull request grpc#1894 from yang-g/create-thread-pool
Browse files Browse the repository at this point in the history
  Add ability to override default thread pool
  • Loading branch information
nicolasnoble committed Jun 4, 2015
2 parents aa152ef + 6f4fb3b commit 67a4851
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ cc_library(
"src/cpp/common/rpc_method.cc",
"src/cpp/proto/proto_utils.cc",
"src/cpp/server/async_generic_service.cc",
"src/cpp/server/create_default_thread_pool.cc",
"src/cpp/server/insecure_server_credentials.cc",
"src/cpp/server/server.cc",
"src/cpp/server/server_builder.cc",
Expand Down Expand Up @@ -662,6 +663,7 @@ cc_library(
"src/cpp/common/rpc_method.cc",
"src/cpp/proto/proto_utils.cc",
"src/cpp/server/async_generic_service.cc",
"src/cpp/server/create_default_thread_pool.cc",
"src/cpp/server/insecure_server_credentials.cc",
"src/cpp/server/server.cc",
"src/cpp/server/server_builder.cc",
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3332,6 +3332,7 @@ LIBGRPC++_SRC = \
src/cpp/common/rpc_method.cc \
src/cpp/proto/proto_utils.cc \
src/cpp/server/async_generic_service.cc \
src/cpp/server/create_default_thread_pool.cc \
src/cpp/server/insecure_server_credentials.cc \
src/cpp/server/server.cc \
src/cpp/server/server_builder.cc \
Expand Down Expand Up @@ -3619,6 +3620,7 @@ LIBGRPC++_UNSECURE_SRC = \
src/cpp/common/rpc_method.cc \
src/cpp/proto/proto_utils.cc \
src/cpp/server/async_generic_service.cc \
src/cpp/server/create_default_thread_pool.cc \
src/cpp/server/insecure_server_credentials.cc \
src/cpp/server/server.cc \
src/cpp/server/server_builder.cc \
Expand Down
1 change: 1 addition & 0 deletions build.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"src/cpp/common/rpc_method.cc",
"src/cpp/proto/proto_utils.cc",
"src/cpp/server/async_generic_service.cc",
"src/cpp/server/create_default_thread_pool.cc",
"src/cpp/server/insecure_server_credentials.cc",
"src/cpp/server/server.cc",
"src/cpp/server/server_builder.cc",
Expand Down
49 changes: 49 additions & 0 deletions src/cpp/server/create_default_thread_pool.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

#include <grpc/support/cpu.h>
#include "src/cpp/server/thread_pool.h"

#ifndef GRPC_CUSTOM_DEFAULT_THREAD_POOL

namespace grpc {

ThreadPoolInterface* CreateDefaultThreadPool() {
int cores = gpr_cpu_num_cores();
if (!cores) cores = 4;
return new ThreadPool(cores);
}

} // namespace grpc

#endif // !GRPC_CUSTOM_DEFAULT_THREAD_POOL
4 changes: 1 addition & 3 deletions src/cpp/server/server_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
return nullptr;
}
if (!thread_pool_ && !services_.empty()) {
int cores = gpr_cpu_num_cores();
if (!cores) cores = 4;
thread_pool_ = new ThreadPool(cores);
thread_pool_ = CreateDefaultThreadPool();
thread_pool_owned = true;
}
std::unique_ptr<Server> server(
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/server/thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class ThreadPool GRPC_FINAL : public ThreadPoolInterface {
void ThreadFunc();
};

ThreadPoolInterface* CreateDefaultThreadPool();

} // namespace grpc

#endif // GRPC_INTERNAL_CPP_SERVER_THREAD_POOL_H
2 changes: 1 addition & 1 deletion tools/doxygen/Doxyfile.c++.internal
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ WARN_LOGFILE =
# spaces.
# Note: If this tag is empty the current directory is searched.

INPUT = include/grpc++/async_generic_service.h include/grpc++/async_unary_call.h include/grpc++/byte_buffer.h include/grpc++/channel_arguments.h include/grpc++/channel_interface.h include/grpc++/client_context.h include/grpc++/completion_queue.h include/grpc++/config.h include/grpc++/create_channel.h include/grpc++/credentials.h include/grpc++/generic_stub.h include/grpc++/impl/call.h include/grpc++/impl/client_unary_call.h include/grpc++/impl/grpc_library.h include/grpc++/impl/internal_stub.h include/grpc++/impl/rpc_method.h include/grpc++/impl/rpc_service_method.h include/grpc++/impl/service_type.h include/grpc++/impl/sync.h include/grpc++/impl/sync_cxx11.h include/grpc++/impl/sync_no_cxx11.h include/grpc++/impl/thd.h include/grpc++/impl/thd_cxx11.h include/grpc++/impl/thd_no_cxx11.h include/grpc++/server.h include/grpc++/server_builder.h include/grpc++/server_context.h include/grpc++/server_credentials.h include/grpc++/slice.h include/grpc++/status.h include/grpc++/status_code_enum.h include/grpc++/stream.h include/grpc++/thread_pool_interface.h include/grpc++/time.h src/cpp/client/secure_credentials.h src/cpp/server/secure_server_credentials.h src/cpp/client/channel.h src/cpp/proto/proto_utils.h src/cpp/server/thread_pool.h src/cpp/client/secure_credentials.cc src/cpp/server/secure_server_credentials.cc src/cpp/client/channel.cc src/cpp/client/channel_arguments.cc src/cpp/client/client_context.cc src/cpp/client/client_unary_call.cc src/cpp/client/create_channel.cc src/cpp/client/credentials.cc src/cpp/client/generic_stub.cc src/cpp/client/insecure_credentials.cc src/cpp/client/internal_stub.cc src/cpp/common/call.cc src/cpp/common/completion_queue.cc src/cpp/common/rpc_method.cc src/cpp/proto/proto_utils.cc src/cpp/server/async_generic_service.cc src/cpp/server/insecure_server_credentials.cc src/cpp/server/server.cc src/cpp/server/server_builder.cc src/cpp/server/server_context.cc src/cpp/server/server_credentials.cc src/cpp/server/thread_pool.cc src/cpp/util/byte_buffer.cc src/cpp/util/slice.cc src/cpp/util/status.cc src/cpp/util/time.cc
INPUT = include/grpc++/async_generic_service.h include/grpc++/async_unary_call.h include/grpc++/byte_buffer.h include/grpc++/channel_arguments.h include/grpc++/channel_interface.h include/grpc++/client_context.h include/grpc++/completion_queue.h include/grpc++/config.h include/grpc++/create_channel.h include/grpc++/credentials.h include/grpc++/generic_stub.h include/grpc++/impl/call.h include/grpc++/impl/client_unary_call.h include/grpc++/impl/grpc_library.h include/grpc++/impl/internal_stub.h include/grpc++/impl/rpc_method.h include/grpc++/impl/rpc_service_method.h include/grpc++/impl/service_type.h include/grpc++/impl/sync.h include/grpc++/impl/sync_cxx11.h include/grpc++/impl/sync_no_cxx11.h include/grpc++/impl/thd.h include/grpc++/impl/thd_cxx11.h include/grpc++/impl/thd_no_cxx11.h include/grpc++/server.h include/grpc++/server_builder.h include/grpc++/server_context.h include/grpc++/server_credentials.h include/grpc++/slice.h include/grpc++/status.h include/grpc++/status_code_enum.h include/grpc++/stream.h include/grpc++/thread_pool_interface.h include/grpc++/time.h src/cpp/client/secure_credentials.h src/cpp/server/secure_server_credentials.h src/cpp/client/channel.h src/cpp/proto/proto_utils.h src/cpp/server/thread_pool.h src/cpp/client/secure_credentials.cc src/cpp/server/secure_server_credentials.cc src/cpp/client/channel.cc src/cpp/client/channel_arguments.cc src/cpp/client/client_context.cc src/cpp/client/client_unary_call.cc src/cpp/client/create_channel.cc src/cpp/client/credentials.cc src/cpp/client/generic_stub.cc src/cpp/client/insecure_credentials.cc src/cpp/client/internal_stub.cc src/cpp/common/call.cc src/cpp/common/completion_queue.cc src/cpp/common/rpc_method.cc src/cpp/proto/proto_utils.cc src/cpp/server/async_generic_service.cc src/cpp/server/create_default_thread_pool.cc src/cpp/server/insecure_server_credentials.cc src/cpp/server/server.cc src/cpp/server/server_builder.cc src/cpp/server/server_context.cc src/cpp/server/server_credentials.cc src/cpp/server/thread_pool.cc src/cpp/util/byte_buffer.cc src/cpp/util/slice.cc src/cpp/util/status.cc src/cpp/util/time.cc

# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
Expand Down
2 changes: 2 additions & 0 deletions vsprojects/grpc++/grpc++.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\async_generic_service.cc">
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\create_default_thread_pool.cc">
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\insecure_server_credentials.cc">
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\server.cc">
Expand Down
3 changes: 3 additions & 0 deletions vsprojects/grpc++/grpc++.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<ClCompile Include="..\..\src\cpp\server\async_generic_service.cc">
<Filter>src\cpp\server</Filter>
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\create_default_thread_pool.cc">
<Filter>src\cpp\server</Filter>
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\insecure_server_credentials.cc">
<Filter>src\cpp\server</Filter>
</ClCompile>
Expand Down
2 changes: 2 additions & 0 deletions vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\async_generic_service.cc">
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\create_default_thread_pool.cc">
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\insecure_server_credentials.cc">
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\server.cc">
Expand Down
3 changes: 3 additions & 0 deletions vsprojects/grpc++_unsecure/grpc++_unsecure.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<ClCompile Include="..\..\src\cpp\server\async_generic_service.cc">
<Filter>src\cpp\server</Filter>
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\create_default_thread_pool.cc">
<Filter>src\cpp\server</Filter>
</ClCompile>
<ClCompile Include="..\..\src\cpp\server\insecure_server_credentials.cc">
<Filter>src\cpp\server</Filter>
</ClCompile>
Expand Down

0 comments on commit 67a4851

Please sign in to comment.