Skip to content

Commit 7f8465c

Browse files
author
invxp
committed
1:remove http_server
2:add threading_pool 3:optimize code
1 parent bdd61d3 commit 7f8465c

14 files changed

+124
-348
lines changed

brink.vcxproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
</ProjectConfiguration>
1212
</ItemGroup>
1313
<ItemGroup>
14-
<ClCompile Include="tcp\http\http_server.cpp" />
1514
<ClCompile Include="tcp\protobuf\include\pbuf.cc" />
1615
<ClCompile Include="tcp\protobuf\protobuf_server.cpp" />
1716
<ClCompile Include="tcp\tcp_socket.cpp" />
@@ -26,7 +25,6 @@
2625
<ClInclude Include="include\pool\pool.hpp" />
2726
<ClInclude Include="include\brink_handler.h" />
2827
<ClInclude Include="include\pool\thread.hpp" />
29-
<ClInclude Include="tcp\http\http_server.h" />
3028
<ClInclude Include="tcp\protobuf\include\pbuf.h" />
3129
<ClInclude Include="tcp\protobuf\protobuf_server.h" />
3230
<ClInclude Include="tcp\tcp_socket.h" />
@@ -94,11 +92,11 @@
9492
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
9593
<AdditionalIncludeDirectories>.\include;.\3rd\protobuf-2.6.0\include</AdditionalIncludeDirectories>
9694
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
97-
<DebugInformationFormat>None</DebugInformationFormat>
95+
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
9896
<AdditionalOptions>-D_WIN32_WINNT=0x0501 %(AdditionalOptions)</AdditionalOptions>
9997
</ClCompile>
10098
<Link>
101-
<GenerateDebugInformation>false</GenerateDebugInformation>
99+
<GenerateDebugInformation>true</GenerateDebugInformation>
102100
<EnableCOMDATFolding>true</EnableCOMDATFolding>
103101
<OptimizeReferences>true</OptimizeReferences>
104102
<AdditionalLibraryDirectories>.\3rd\protobuf-2.6.0\vsprojects\Release;</AdditionalLibraryDirectories>

brink.vcxproj.filters

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
<ClCompile Include="tcp\protobuf\include\pbuf.cc">
1515
<Filter>tcp\protobuf_server\include</Filter>
1616
</ClCompile>
17-
<ClCompile Include="tcp\http\http_server.cpp">
18-
<Filter>tcp\http_server</Filter>
19-
</ClCompile>
2017
</ItemGroup>
2118
<ItemGroup>
2219
<Filter Include="include">
@@ -31,15 +28,9 @@
3128
<Filter Include="tcp\protobuf_server">
3229
<UniqueIdentifier>{1192bbfd-4bf3-41ed-af22-e17b73978737}</UniqueIdentifier>
3330
</Filter>
34-
<Filter Include="tcp\http_server">
35-
<UniqueIdentifier>{235f06e1-9cba-4d67-b034-e5785efa9902}</UniqueIdentifier>
36-
</Filter>
3731
<Filter Include="tcp\protobuf_server\include">
3832
<UniqueIdentifier>{e794f17b-e8d2-4864-8f2d-b87cd4800a81}</UniqueIdentifier>
3933
</Filter>
40-
<Filter Include="tcp\http_server\include">
41-
<UniqueIdentifier>{23150904-5b35-402c-ac59-bda06dae60b8}</UniqueIdentifier>
42-
</Filter>
4334
</ItemGroup>
4435
<ItemGroup>
4536
<ClInclude Include="include\brink_defines.h">
@@ -63,9 +54,6 @@
6354
<ClInclude Include="tcp\protobuf\include\pbuf.h">
6455
<Filter>tcp\protobuf_server\include</Filter>
6556
</ClInclude>
66-
<ClInclude Include="tcp\http\http_server.h">
67-
<Filter>tcp\http_server</Filter>
68-
</ClInclude>
6957
<ClInclude Include="include\brink_param.h">
7058
<Filter>include</Filter>
7159
</ClInclude>

include/brink_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ namespace BrinK
9494
if (file_size == std::string::npos)
9595
return false;
9696

97-
std::unique_ptr <char[]> read_buf = std::make_unique < char[] >(file_size + sizeof(char));
97+
std::unique_ptr < char[] > read_buf = std::make_unique < char[] >(file_size + sizeof(char));
9898

9999
ifs.seekg(0);
100100

include/pool/pool.hpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace BrinK
3838
busy_list_.clear();
3939
free_list_.clear();
4040
}
41+
4142
public:
4243
void get(const std::function < void(const T& t) >& op = [](const T&){})
4344
{
@@ -47,8 +48,9 @@ namespace BrinK
4748
op(*busy_list_.emplace(new_()).first);
4849
else
4950
{
50-
op(*busy_list_.emplace(*free_list_.begin()).first);
51+
auto p = busy_list_.emplace(*std::move(free_list_.begin()));
5152
free_list_.erase(free_list_.begin());
53+
op(*p.first);
5254
}
5355
}
5456

@@ -57,53 +59,21 @@ namespace BrinK
5759
std::lock_guard < std::mutex > lock(mutex_);
5860

5961
auto it = busy_list_.find(t);
60-
if (it != busy_list_.end())
61-
{
62-
free_list_.emplace(t);
63-
busy_list_.erase(it);
64-
op(t);
65-
}
66-
}
67-
68-
public:
69-
size_t busy_size()
70-
{
71-
std::lock_guard < std::mutex > lock(mutex_);
72-
73-
return busy_list_.size();
74-
}
75-
76-
size_t free_size()
77-
{
78-
std::lock_guard < std::mutex > lock(mutex_);
62+
if (it == busy_list_.end())
63+
return;
7964

80-
return free_list_.size();
65+
free_list_.emplace(t);
66+
busy_list_.erase(it);
67+
op(t);
8168
}
8269

83-
public:
8470
void each(const std::function < void(const T& t) >& op = [](const T&){})
8571
{
8672
std::lock_guard < std::mutex > lock(mutex_);
8773

8874
std::for_each(busy_list_.begin(), busy_list_.end(), [&op](const T& t){ op(t); });
8975
}
9076

91-
void each_free(const std::function < void(const T& t) >& op = [](const T&){})
92-
{
93-
std::lock_guard < std::mutex > lock(mutex_);
94-
95-
std::for_each(free_list_.begin(), free_list_.end(), [&op](const T& t){ op(t); });
96-
}
97-
98-
void clear(const std::function < void(const T& t) >& op = [](const T&){})
99-
{
100-
std::lock_guard < std::mutex > lock(mutex_);
101-
102-
std::for_each(busy_list_.begin(), busy_list_.end(), [&op](const T& t){ op(t); });
103-
std::copy(busy_list_.begin(), busy_list_.end(), std::back_inserter(free_list_));
104-
busy_list_.clear();
105-
}
106-
10777
private:
10878
std::mutex mutex_;
10979
std::set< T > busy_list_;

include/pool/thread.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ namespace BrinK
5656
return wait_(wait_one_mutex_, wait_one_condition_);
5757
}
5858

59-
bool start(const unsigned int& pool_size = std::thread::hardware_concurrency())
59+
bool start(const unsigned int& thread_count = std::thread::hardware_concurrency())
6060
{
6161
std::lock_guard < std::mutex > lock(mutex_);
6262

6363
if (started_)
6464
return false;
6565

66-
create_threads_(pool_size);
66+
create_threads_(thread_count);
6767

6868
started_ = true;
6969

@@ -73,14 +73,17 @@ namespace BrinK
7373
bool stop()
7474
{
7575
std::lock_guard < std::mutex > lock(mutex_);
76-
std::unique_lock < std::mutex > lock_task(tasks_mutex_);
7776

78-
if (!started_)
79-
return false;
77+
{
78+
std::unique_lock < std::mutex > lock_task(tasks_mutex_);
8079

81-
stop_ = true;
80+
if (!started_)
81+
return false;
82+
83+
stop_ = true;
8284

83-
awake_condition(lock_task, tasks_condition_);
85+
awake_condition(lock_task, tasks_condition_);
86+
}
8487

8588
remove_threads_();
8689

tcp/http/http_server.cpp

Lines changed: 0 additions & 166 deletions
This file was deleted.

0 commit comments

Comments
 (0)