Skip to content

Commit c246806

Browse files
committed
Refactored thread_pool from the utils sub-directory (and namespace) into concurrency.
1 parent 5f6cce5 commit c246806

File tree

11 files changed

+127
-61
lines changed

11 files changed

+127
-61
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,4 @@ message(STATUS " CPP-NETLIB_DISABLE_LOGGING: ${CPP-NETLIB_DISABLE_LOGGING}\t(
113113
add_subdirectory(uri)
114114
add_subdirectory(message)
115115
add_subdirectory(logging)
116+
add_subdirectory(concurrency)

concurrency/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) Glyn Matthews 2012.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# (See accompanying file LICENSE_1_0.txt or copy at
4+
# http://www.boost.org/LICENSE_1_0.txt)
5+
6+
add_subdirectory(src)
7+
8+
if(CPP-NETLIB_BUILD_TESTS)
9+
enable_testing()
10+
add_subdirectory(test)
11+
endif(CPP-NETLIB_BUILD_TESTS)

concurrency/src/CMakeLists.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) Glyn Matthews 2012.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# (See accompanying file LICENSE_1_0.txt or copy at
4+
# http://www.boost.org/LICENSE_1_0.txt)
5+
6+
include_directories(${CPP-NETLIB_SOURCE_DIR}/concurrency/src)
7+
8+
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
9+
if (HAVE_STD11)
10+
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11")
11+
elseif (HAVE_STD0X)
12+
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++0x")
13+
endif()
14+
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
15+
CHECK_CXX_COMPILER_FLAG(-std=c++11 HAVE_STD11)
16+
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11 -stdlib=libc++")
17+
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11 -stdlib=libc++")
18+
endif()
19+
20+
set(CPP-NETLIB_CONCURRENCY_SRCS thread_pool.cpp)
21+
add_library(cppnetlib-concurrency ${CPP-NETLIB_CONCURRENCY_SRCS})
22+
foreach (src_file ${CPP-NETLIB_CONCURRENCY_SRCS})
23+
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
24+
set_source_files_properties(${src_file}
25+
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
26+
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
27+
set_source_files_properties(${src_file}
28+
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
29+
endif()
30+
endforeach(src_file)

include/network/utils/thread_pool.hpp renamed to concurrency/src/network/concurrency/thread_pool.hpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Copyright 2010 Dean Michael Berris.
22
// Copyright 2012 Google, Inc.
3+
// Copyright (c) Glyn Matthews 2012.
34
// Distributed under the Boost Software License, Version 1.0.
45
// (See accompanying file LICENSE_1_0.txt or copy at
56
// http://www.boost.org/LICENSE_1_0.txt)
67

7-
#ifndef NETWORK_UTILS_THREAD_POOL_HPP_20101020
8-
#define NETWORK_UTILS_THREAD_POOL_HPP_20101020
8+
#ifndef NETWORK_CONCURRENCY_THREAD_POOL_HPP_20101020
9+
#define NETWORK_CONCURRENCY_THREAD_POOL_HPP_20101020
910

1011
#include <cstddef>
1112
#include <thread>
@@ -14,7 +15,7 @@
1415
#include <vector>
1516
#include <boost/asio/io_service.hpp>
1617

17-
namespace network { namespace utils {
18+
namespace network { namespace concurrency {
1819

1920
typedef std::shared_ptr<boost::asio::io_service> io_service_ptr;
2021
typedef std::shared_ptr<std::vector<std::thread>> worker_threads_ptr;
@@ -46,7 +47,7 @@ namespace network { namespace utils {
4647
l.swap(r);
4748
}
4849

49-
} // namespace utils
50+
} // namespace concurrency
5051
} // namespace network
5152

52-
#endif /* NETWORK_UTILS_THREAD_POOL_HPP_20101020 */
53+
#endif /* NETWORK_CONCURRENCY_THREAD_POOL_HPP_20101020 */

include/network/utils/thread_pool.ipp renamed to concurrency/src/network/concurrency/thread_pool.ipp

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
// Copyright 2011 Dean Michael Berris <[email protected]>.
22
// Copyright 2011 Google, Inc.
3+
// Copyright (c) Glyn Matthews 2012.
34
// Distributed under the Boost Software License, Version 1.0.
45
// (See accompanying file LICENSE_1_0.txt or copy at
56
// http://www.boost.org/LICENSE_1_0.txt)
67

7-
#ifndef NETWORK_UTILS_THREAD_POOL_IPP_20111021
8-
#define NETWORK_UTILS_THREAD_POOL_IPP_20111021
8+
#ifndef NETWORK_CONCURRENCY_THREAD_POOL_IPP_20111021
9+
#define NETWORK_CONCURRENCY_THREAD_POOL_IPP_20111021
910

1011
#include <vector>
1112
#include <thread>
12-
#include <network/utils/thread_pool.hpp>
13+
#include <network/concurrency/thread_pool.hpp>
1314
#include <boost/scope_exit.hpp>
1415

15-
namespace network { namespace utils {
16+
namespace network { namespace concurrency {
1617

1718
struct thread_pool_pimpl {
1819
thread_pool_pimpl(std::size_t threads = 1,
@@ -109,7 +110,7 @@ namespace network { namespace utils {
109110
delete pimpl;
110111
}
111112

112-
} // namespace utils
113+
} // namespace concurrency
113114
} // namespace network
114115

115-
#endif /* NETWORK_UTILS_THREAD_POOL_IPP_20111021 */
116+
#endif /* NETWORK_CONCURRENCY_THREAD_POOL_IPP_20111021 */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Glyn Matthews 2012.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
7+
#ifndef __NETWORK_UTILS_THREAD_POOL_INC__
8+
#define __NETWORK_UTILS_THREAD_POOL_INC__
9+
10+
#include <network/concurrency/thread_pool.hpp>
11+
12+
namespace network {
13+
namespace utils {
14+
typedef ::network::concurrency::thread_pool thread_pool;
15+
} // namespace utils
16+
} // namespace network
17+
18+
19+
#endif // __NETWORK_UTILS_THREAD_POOL_INC__
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Copyright 2011 Dean Michael Berris <[email protected]>.
22
// Copyright 2011 Google, Inc.
3+
// Copyright (c) Glyn Matthews 2012.
34
// Distributed under the Boost Software License, Version 1.0.
45
// (See accompanying file LICENSE_1_0.txt or copy at
56
// http://www.boost.org/LICENSE_1_0.txt)
67

7-
#include <network/utils/thread_pool.ipp>
8+
#include <network/concurrency/thread_pool.ipp>

concurrency/test/CMakeLists.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) Glyn Matthews 2012.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# (See accompanying file LICENSE_1_0.txt or copy at
4+
# http://www.boost.org/LICENSE_1_0.txt)
5+
6+
include_directories(${CPP-NETLIB_SOURCE_DIR}/concurrency/src)
7+
8+
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
9+
if (HAVE_STD11)
10+
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11")
11+
elseif (HAVE_STD0X)
12+
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++0x")
13+
endif()
14+
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
15+
CHECK_CXX_COMPILER_FLAG(-std=c++11 HAVE_STD11)
16+
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11 -stdlib=libc++")
17+
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11 -stdlib=libc++")
18+
endif()
19+
20+
set_source_files_properties(thread_pool_test.cpp
21+
PROPERTIES COMPILE_FLAGS "-Wall")
22+
add_executable(cpp-netlib-thread_pool_test thread_pool_test.cpp)
23+
target_link_libraries(cpp-netlib-thread_pool_test
24+
cppnetlib-concurrency
25+
${Boost_LIBRARIES}
26+
${CMAKE_THREAD_LIBS_INIT})
27+
set_target_properties(cpp-netlib-thread_pool_test
28+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
29+
add_test(cpp-netlib-thread_pool_test ${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-thread_pool_test)
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11

22
// Copyright 2010 Dean Michael Berris.
33
// Copyright 2012 Google, Inc.
4+
// Copyright (c) Glyn Matthews 2012.
45
// Distributed under the Boost Software License, Version 1.0.
56
// (See accompanying file LICENSE_1_0.txt or copy at
67
// http://www.boost.org/LICENSE_1_0.txt)
78

89
#ifdef BUILD_SHARED_LIBS
910
# define BOOST_TEST_DYN_LINK
1011
#endif
11-
#define BOOST_TEST_MODULE utils thread pool test
12+
#define BOOST_TEST_MODULE thread pool test
1213
#include <boost/config/warning_disable.hpp>
1314
#include <boost/test/unit_test.hpp>
14-
#include <network/utils/thread_pool.hpp>
15+
#include <network/concurrency/thread_pool.hpp>
1516
#include <boost/bind.hpp>
1617

17-
using namespace network;
18+
using network::concurrency::thread_pool;
1819

1920
// This test specifies the requirements for a thread pool interface. At the
2021
// very least any thread pool implementation should be able to pass the simple
@@ -25,29 +26,29 @@ using namespace network;
2526
//
2627

2728
BOOST_AUTO_TEST_CASE( default_constructor ) {
28-
utils::thread_pool pool;
29-
BOOST_CHECK_EQUAL(pool.thread_count(), std::size_t(1));
29+
thread_pool pool;
30+
BOOST_CHECK_EQUAL(pool.thread_count(), std::size_t(1));
3031
}
3132

3233
struct foo {
33-
foo() : val_(0) {}
34-
void bar(int val) {
35-
val_ += val;
36-
}
37-
int const val() const {
38-
return val_;
39-
}
34+
foo() : val_(0) {}
35+
void bar(int val) {
36+
val_ += val;
37+
}
38+
int const val() const {
39+
return val_;
40+
}
4041
protected:
41-
int val_;
42+
int val_;
4243
};
4344

4445
BOOST_AUTO_TEST_CASE( post_work ) {
45-
foo instance;
46-
{
47-
utils::thread_pool pool;
48-
BOOST_CHECK_NO_THROW(pool.post(boost::bind(&foo::bar, &instance, 1)));
49-
BOOST_CHECK_NO_THROW(pool.post(boost::bind(&foo::bar, &instance, 2)));
50-
// require that pool is destroyed here, RAII baby
51-
}
52-
BOOST_CHECK_EQUAL(instance.val(), 3);
46+
foo instance;
47+
{
48+
thread_pool pool;
49+
BOOST_CHECK_NO_THROW(pool.post(boost::bind(&foo::bar, &instance, 1)));
50+
BOOST_CHECK_NO_THROW(pool.post(boost::bind(&foo::bar, &instance, 2)));
51+
// require that pool is destroyed here, RAII baby
52+
}
53+
BOOST_CHECK_EQUAL(instance.val(), 3);
5354
}

libs/network/src/CMakeLists.txt

-12
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,6 @@ elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
172172
endif()
173173
endforeach(src_file)
174174

175-
set(CPP-NETLIB_UTILS_THREAD_POOL_SRCS utils/thread_pool.cpp)
176-
add_library(cppnetlib-utils-thread_pool ${CPP-NETLIB_UTILS_THREAD_POOL_SRCS})
177-
foreach (src_file ${CPP-NETLIB_UTILS_THREAD_POOL_SRCS})
178-
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
179-
set_source_files_properties(${src_file}
180-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
181-
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
182-
set_source_files_properties(${src_file}
183-
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
184-
endif()
185-
endforeach(src_file)
186-
187175
set(CPP-NETLIB_CONSTANTS_SRCS constants.cpp)
188176
add_library(cppnetlib-constants ${CPP-NETLIB_CONSTANTS_SRCS})
189177
foreach (src_file ${CPP-NETLIB_CONSTANTS_SRCS})

libs/network/test/CMakeLists.txt

-16
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,3 @@ if(CPP-NETLIB_BUILD_SHARED_LIBS)
1010
endif()
1111

1212
add_subdirectory(http)
13-
14-
if (Boost_FOUND)
15-
16-
set_source_files_properties(utils_thread_pool.cpp
17-
PROPERTIES COMPILE_FLAGS "-Wall")
18-
add_executable(cpp-netlib-utils_thread_pool utils_thread_pool.cpp)
19-
target_link_libraries(cpp-netlib-utils_thread_pool
20-
cppnetlib-utils-thread_pool
21-
${Boost_LIBRARIES}
22-
${CMAKE_THREAD_LIBS_INIT})
23-
set_target_properties(cpp-netlib-utils_thread_pool
24-
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
25-
add_test(cpp-netlib-utils_thread_pool ${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-utils_thread_pool)
26-
27-
endif()
28-

0 commit comments

Comments
 (0)