Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
1d2dd9c4
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
1d2dd9c4
编写于
2月 04, 2018
作者:
A
Abhinav Arora
提交者:
GitHub
2月 04, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Close buffered channel should unblock the blocked senders and receivers (#8109)
上级
b60da672
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
106 addition
and
7 deletion
+106
-7
paddle/framework/channel_test.cc
paddle/framework/channel_test.cc
+106
-7
未找到文件。
paddle/framework/channel_test.cc
浏览文件 @
1d2dd9c4
...
@@ -48,12 +48,12 @@ TEST(Channel, SufficientBufferSizeDoesntBlock) {
...
@@ -48,12 +48,12 @@ TEST(Channel, SufficientBufferSizeDoesntBlock) {
const
size_t
buffer_size
=
10
;
const
size_t
buffer_size
=
10
;
auto
ch
=
MakeChannel
<
size_t
>
(
buffer_size
);
auto
ch
=
MakeChannel
<
size_t
>
(
buffer_size
);
for
(
size_t
i
=
0
;
i
<
buffer_size
;
++
i
)
{
for
(
size_t
i
=
0
;
i
<
buffer_size
;
++
i
)
{
ch
->
Send
(
&
i
);
// should not block
EXPECT_EQ
(
ch
->
Send
(
&
i
),
true
);
// should not block
}
}
size_t
out
;
size_t
out
;
for
(
size_t
i
=
0
;
i
<
buffer_size
;
++
i
)
{
for
(
size_t
i
=
0
;
i
<
buffer_size
;
++
i
)
{
ch
->
Receive
(
&
out
);
// should not block
EXPECT_EQ
(
ch
->
Receive
(
&
out
),
true
);
// should not block
EXPECT_EQ
(
out
,
i
);
EXPECT_EQ
(
out
,
i
);
}
}
CloseChannel
(
ch
);
CloseChannel
(
ch
);
...
@@ -67,7 +67,10 @@ TEST(Channel, ConcurrentSendNonConcurrentReceiveWithSufficientBufferSize) {
...
@@ -67,7 +67,10 @@ TEST(Channel, ConcurrentSendNonConcurrentReceiveWithSufficientBufferSize) {
std
::
thread
t
([
&
]()
{
std
::
thread
t
([
&
]()
{
// Try to write more than buffer size.
// Try to write more than buffer size.
for
(
size_t
i
=
0
;
i
<
2
*
buffer_size
;
++
i
)
{
for
(
size_t
i
=
0
;
i
<
2
*
buffer_size
;
++
i
)
{
ch
->
Send
(
&
i
);
// should block after 10 iterations
if
(
i
<
buffer_size
)
EXPECT_EQ
(
ch
->
Send
(
&
i
),
true
);
// should block after 10 iterations
else
EXPECT_EQ
(
ch
->
Send
(
&
i
),
false
);
sum
+=
i
;
sum
+=
i
;
}
}
});
});
...
@@ -84,13 +87,13 @@ TEST(Channel, SimpleUnbufferedChannelTest) {
...
@@ -84,13 +87,13 @@ TEST(Channel, SimpleUnbufferedChannelTest) {
unsigned
sum_send
=
0
;
unsigned
sum_send
=
0
;
std
::
thread
t
([
&
]()
{
std
::
thread
t
([
&
]()
{
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
ch
->
Send
(
&
i
);
EXPECT_EQ
(
ch
->
Send
(
&
i
),
true
);
sum_send
+=
i
;
sum_send
+=
i
;
}
}
});
});
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
int
recv
;
int
recv
;
ch
->
Receive
(
&
recv
);
EXPECT_EQ
(
ch
->
Receive
(
&
recv
),
true
);
EXPECT_EQ
(
recv
,
i
);
EXPECT_EQ
(
recv
,
i
);
}
}
...
@@ -100,6 +103,102 @@ TEST(Channel, SimpleUnbufferedChannelTest) {
...
@@ -100,6 +103,102 @@ TEST(Channel, SimpleUnbufferedChannelTest) {
delete
ch
;
delete
ch
;
}
}
// This tests that closing a buffered channel also unblocks
// any receivers waiting on the channel
TEST
(
Channel
,
BufferedChannelCloseUnblocksReceiversTest
)
{
auto
ch
=
MakeChannel
<
int
>
(
1
);
size_t
num_threads
=
5
;
std
::
thread
t
[
num_threads
];
bool
thread_ended
[
num_threads
];
// Launches threads that try to read and are blocked because of no writers
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
thread_ended
[
i
]
=
false
;
t
[
i
]
=
std
::
thread
(
[
&
](
bool
*
p
)
{
int
data
;
// All reads should return false
EXPECT_EQ
(
ch
->
Receive
(
&
data
),
false
);
*
p
=
true
;
},
&
thread_ended
[
i
]);
}
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
100
));
// wait
// Verify that all threads are blocked
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
EXPECT_EQ
(
thread_ended
[
i
],
false
);
}
// Explicitly close the channel
// This should unblock all receivers
CloseChannel
(
ch
);
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
200
));
// wait
// Verify that all threads got unblocked
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
EXPECT_EQ
(
thread_ended
[
i
],
true
);
}
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
t
[
i
].
join
();
delete
ch
;
}
// This tests that closing a buffered channel also unblocks
// any senders waiting for channel to have write space
TEST
(
Channel
,
BufferedChannelCloseUnblocksSendersTest
)
{
auto
ch
=
MakeChannel
<
int
>
(
1
);
size_t
num_threads
=
5
;
std
::
thread
t
[
num_threads
];
bool
thread_ended
[
num_threads
];
bool
send_success
[
num_threads
];
// Launches threads that try to write and are blocked because of no readers
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
thread_ended
[
i
]
=
false
;
send_success
[
i
]
=
false
;
t
[
i
]
=
std
::
thread
(
[
&
](
bool
*
ended
,
bool
*
success
)
{
int
data
=
10
;
*
success
=
ch
->
Send
(
&
data
);
*
ended
=
true
;
},
&
thread_ended
[
i
],
&
send_success
[
i
]);
}
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
100
));
// wait
// Verify that atleast 4 threads are blocked
int
ct
=
0
;
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
if
(
thread_ended
[
i
]
==
false
)
ct
++
;
}
// Atleast 4 threads must be blocked
EXPECT_GE
(
ct
,
4
);
// Explicitly close the thread
// This should unblock all senders
CloseChannel
(
ch
);
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
200
));
// wait
// Verify that all threads got unblocked
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
EXPECT_EQ
(
thread_ended
[
i
],
true
);
}
// Verify that only 1 send was successful
ct
=
0
;
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
{
if
(
send_success
[
i
])
ct
++
;
}
// Only 1 send must be successful
EXPECT_EQ
(
ct
,
1
);
for
(
size_t
i
=
0
;
i
<
num_threads
;
i
++
)
t
[
i
].
join
();
delete
ch
;
}
// This tests that closing an unbuffered channel also unblocks
// This tests that closing an unbuffered channel also unblocks
// unblocks any receivers waiting for senders
// unblocks any receivers waiting for senders
TEST
(
Channel
,
UnbufferedChannelCloseUnblocksReceiversTest
)
{
TEST
(
Channel
,
UnbufferedChannelCloseUnblocksReceiversTest
)
{
...
@@ -114,7 +213,7 @@ TEST(Channel, UnbufferedChannelCloseUnblocksReceiversTest) {
...
@@ -114,7 +213,7 @@ TEST(Channel, UnbufferedChannelCloseUnblocksReceiversTest) {
t
[
i
]
=
std
::
thread
(
t
[
i
]
=
std
::
thread
(
[
&
](
bool
*
p
)
{
[
&
](
bool
*
p
)
{
int
data
;
int
data
;
ch
->
Receive
(
&
data
);
EXPECT_EQ
(
ch
->
Receive
(
&
data
),
false
);
*
p
=
true
;
*
p
=
true
;
},
},
&
thread_ended
[
i
]);
&
thread_ended
[
i
]);
...
@@ -155,7 +254,7 @@ TEST(Channel, UnbufferedChannelCloseUnblocksSendersTest) {
...
@@ -155,7 +254,7 @@ TEST(Channel, UnbufferedChannelCloseUnblocksSendersTest) {
t
[
i
]
=
std
::
thread
(
t
[
i
]
=
std
::
thread
(
[
&
](
bool
*
p
)
{
[
&
](
bool
*
p
)
{
int
data
=
10
;
int
data
=
10
;
ch
->
Send
(
&
data
);
EXPECT_EQ
(
ch
->
Send
(
&
data
),
false
);
*
p
=
true
;
*
p
=
true
;
},
},
&
thread_ended
[
i
]);
&
thread_ended
[
i
]);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录