@@ -16,7 +16,7 @@ struct BufferedChannel {
1616 return 0 ;
1717 }
1818
19- int TryRecv () {
19+ int Recv () {
2020 if (deq.empty ()) {
2121 return -1 ;
2222 }
@@ -32,13 +32,13 @@ struct BufferedChannel {
3232 return l->Send (std::get<0 >(*real_args));
3333 };
3434
35- method_t try_recv_func = [](BufferedChannel *l, void *args) -> int {
36- return l->TryRecv ();
35+ method_t recv_func = [](BufferedChannel *l, void *args) -> int {
36+ return l->Recv ();
3737 };
3838
3939 return std::map<std::string, method_t >{
4040 {" Send" , send_func},
41- {" TryRecv " , try_recv_func },
41+ {" Recv " , recv_func },
4242 };
4343 }
4444
@@ -67,7 +67,7 @@ struct BufferedChannel {
6767 non_atomic int Send (int v) {
6868 std::unique_lock lock{mutex_};
6969 while (!closed_ && full_) {
70- debug (stderr, " Waiting...\n " );
70+ debug (stderr, " Waiting in send ...\n " );
7171 send_side_cv_.wait (lock);
7272 }
7373 debug (stderr, " Send\n " );
@@ -76,15 +76,17 @@ struct BufferedChannel {
7676 sidx_ = (sidx_ + 1 ) % N;
7777 full_ = (sidx_ == ridx_);
7878 empty_ = false ;
79+ recv_side_cv_.notify_one ();
7980 return 0 ;
8081 }
8182
82- // TryRecv is not blocking otherwise it is dual structure
83- non_atomic int TryRecv () {
84- std::lock_guard lock{mutex_};
85- if (closed_ || empty_) {
86- return - 1 ;
83+ non_atomic int Recv () {
84+ std::unique_lock lock{mutex_};
85+ while (!closed_ && empty_) {
86+ debug (stderr, " Waiting in recv... \n " );
87+ recv_side_cv_. wait (lock) ;
8788 }
89+ debug (stderr, " Recv\n " );
8890 auto val = queue_[ridx_];
8991 ridx_ = (ridx_ + 1 ) % 5 ;
9092 empty_ = (sidx_ == ridx_);
@@ -93,15 +95,16 @@ struct BufferedChannel {
9395 return val;
9496 }
9597
96- non_atomic int Close () {
98+ int Close () {
9799 closed_.store (true );
98100 send_side_cv_.notify_all ();
101+ recv_side_cv_.notify_all ();
99102 return 0 ;
100103 }
101104
102105 std::mutex mutex_;
103- std::condition_variable send_side_cv_;
104- std::atomic_bool closed_{false };
106+ std::condition_variable send_side_cv_, recv_side_cv_ ;
107+ std::atomic< bool > closed_{false };
105108
106109 bool full_{false };
107110 bool empty_{true };
@@ -122,5 +125,4 @@ using spec_t =
122125LTEST_ENTRYPOINT_CONSTRAINT (spec_t , spec::BufferedChannelVerifier);
123126
124127target_method (generateInt, void , BufferedChannel, Send, int );
125- target_method (ltest::generators::genEmpty, int , BufferedChannel, TryRecv);
126- // target_method(ltest::generators::genEmpty, int, BufferedChannel, Close);
128+ target_method (ltest::generators::genEmpty, int , BufferedChannel, Recv);
0 commit comments