Skip to content

Commit f5d6212

Browse files
committed
Improvements in the code class Event
1 parent 2ce9315 commit f5d6212

File tree

2 files changed

+59
-40
lines changed

2 files changed

+59
-40
lines changed

src/Event.cpp

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,73 +10,92 @@ namespace HttpServer
1010

1111
void Event::wait()
1212
{
13-
std::unique_lock<std::mutex> lck(mtx);
14-
15-
while (false == signaled)
13+
if (false == this->signaled.load() )
1614
{
17-
cv.wait(lck);
15+
std::unique_lock<std::mutex> lck(this->mtx);
16+
17+
do
18+
{
19+
this->cv.wait(lck);
20+
}
21+
while (false == this->signaled.load() );
1822
}
1923

20-
if (false == manually)
24+
if (false == this->manually)
2125
{
22-
signaled = false;
26+
this->signaled.store(false);
2327
}
2428
}
2529

2630
bool Event::wait_for(const std::chrono::milliseconds &ms)
2731
{
28-
std::unique_lock<std::mutex> lck(mtx);
32+
bool is_timeout = false;
2933

30-
auto const status = cv.wait_for(lck, ms);
34+
if (false == this->signaled.load() )
35+
{
36+
std::unique_lock<std::mutex> lck(this->mtx);
37+
38+
is_timeout = false == this->cv.wait_for(lck, ms, [this] { return this->notifed(); } );
39+
}
3140

32-
if (false == manually)
41+
if (false == this->manually)
3342
{
34-
signaled = false;
43+
this->signaled.store(false);
3544
}
3645

37-
return std::cv_status::timeout == status;
46+
return is_timeout;
3847
}
3948

4049
bool Event::wait_until(const std::chrono::high_resolution_clock::time_point &tp)
4150
{
42-
std::unique_lock<std::mutex> lck(mtx);
51+
bool is_timeout = false;
52+
53+
if (false == this->signaled.load() )
54+
{
55+
std::unique_lock<std::mutex> lck(this->mtx);
4356

44-
auto const status = cv.wait_until(lck, tp);
57+
do
58+
{
59+
if (std::cv_status::timeout == this->cv.wait_until(lck, tp) )
60+
{
61+
is_timeout = true;
62+
break;
63+
}
64+
}
65+
while (false == this->signaled.load() );
66+
}
4567

46-
if (false == manually)
68+
if (false == this->manually)
4769
{
48-
signaled = false;
70+
this->signaled.store(false);
4971
}
5072

51-
return std::cv_status::timeout == status;
73+
return is_timeout;
5274
}
5375

5476
void Event::notify()
5577
{
56-
signaled = true;
57-
cv.notify_all();
78+
this->signaled.store(true);
79+
this->cv.notify_all();
5880
}
5981

6082
void Event::notify(const size_t threadsCount)
6183
{
62-
if (threadsCount)
63-
{
64-
signaled = true;
84+
this->signaled.store(true);
6585

66-
for (size_t i = 0; i < threadsCount; ++i)
67-
{
68-
cv.notify_one();
69-
}
86+
for (size_t i = 0; i < threadsCount; ++i)
87+
{
88+
this->cv.notify_one();
7089
}
7190
}
7291

7392
void Event::reset()
7493
{
75-
signaled = false;
94+
this->signaled.store(false);
7695
}
7796

7897
bool Event::notifed() const
7998
{
80-
return signaled;
99+
return this->signaled.load();
81100
}
82101
};

src/Server.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ namespace HttpServer
941941
{
942942
while (true)
943943
{
944-
Socket clientSocket;
944+
Socket sock;
945945
struct sockaddr_in addr;
946946

947947
eventThreadCycle.wait();
@@ -955,7 +955,7 @@ namespace HttpServer
955955

956956
if (sockets.size() )
957957
{
958-
std::tie(clientSocket, addr) = sockets.front();
958+
std::tie(sock, addr) = sockets.front();
959959

960960
sockets.pop();
961961
}
@@ -969,39 +969,39 @@ namespace HttpServer
969969

970970
this->sockets_queue_mtx.unlock();
971971

972-
if (clientSocket.is_open() )
972+
if (sock.is_open() )
973973
{
974974
++this->threads_working_count;
975975

976-
struct ::sockaddr_in p;
977-
::socklen_t p_len = sizeof(p);
976+
struct ::sockaddr_in sock_addr;
977+
::socklen_t sock_addr_len = sizeof(sock_addr);
978978

979-
::getsockname(clientSocket.get_handle(), reinterpret_cast<struct sockaddr *>(&p), &p_len);
979+
::getsockname(sock.get_handle(), reinterpret_cast<struct sockaddr *>(&sock_addr), &sock_addr_len);
980980

981-
const int port = ntohs(p.sin_port);
981+
const int port = ntohs(sock_addr.sin_port);
982982

983983
auto const it = this->tls_data.find(port);
984984

985985
if (this->tls_data.cend() != it) // if TLS connection
986986
{
987987
const std::tuple<gnutls_certificate_credentials_t, gnutls_priority_t> &data = it->second;
988988

989-
SocketAdapterTls sock(
990-
clientSocket,
989+
SocketAdapterTls socket_adapter(
990+
sock,
991991
std::get<gnutls_priority_t>(data),
992992
std::get<gnutls_certificate_credentials_t>(data)
993993
);
994994

995-
if (sock.handshake() )
995+
if (socket_adapter.handshake() )
996996
{
997-
this->threadRequestProc(sock, addr);
997+
this->threadRequestProc(socket_adapter, addr);
998998
}
999999
}
10001000
else
10011001
{
1002-
SocketAdapterDefault sock(clientSocket);
1002+
SocketAdapterDefault socket_adapter(sock);
10031003

1004-
this->threadRequestProc(sock, addr);
1004+
this->threadRequestProc(socket_adapter, addr);
10051005
}
10061006

10071007
--this->threads_working_count;

0 commit comments

Comments
 (0)