Using a chanend in both directions
Posted: Mon Apr 17, 2017 10:28 am
I have discovered hat I may use a chanend bidirectionally:
I have also discussed correct usage to avoid deadlock, and shown that the run-time will catch wrong usage causing a deadlock; here:
http://www.teigfam.net/oyvind/home/tech ... directions
(Standard disclaimer: no money, ads, gift etc.)
Comments welcome!
But then, if I can save two chanends by reuse, why doesn't the tool do this automatically?
Code: Select all
#define DEBUG_PRINT_XC_TEST_2 1
#define debug_printf(fmt, ...) do { if(DEBUG_PRINT_XC_TEST_2) printf(fmt, __VA_ARGS__); } while (0)
void My_Server (chanend c_in, chanend c_out) {
int value;
while(1) {
c_in :> value; // Input
debug_printf (" My_Server _ %u\n", value);
value++;
c_out <: value; // Output
debug_printf (" My_Server X %u\n", value);
}
}
void My_PingPong (chanend c_in_out) {
int value;
while(1) {
c_in_out :> value; // Input
debug_printf (" My_PingPong _ %u\n", value);
value++;
c_in_out <: value; // Output
debug_printf (" My_PingPong Y %u\n", value);
}
}
void My_Client (chanend c_out, chanend c_in, chanend c_out_in) {
int value = 0;
while(1) {
debug_printf ("My_Client X %u\n", value);
c_out <: value; // Output
c_in :> value; // Input
debug_printf (" My_Client Y %u\n", value);
c_out_in <: value; // Output
c_out_in :> value; // Input (Change dir: deadlock caught by run-time)
debug_printf (" My_Client Z %u\n", value);
}
}
int main(void) {
chan c_over;
chan c_back;
chan c_pingpong;
par {
My_Server (c_over, c_back);
My_PingPong (c_pingpong);
My_Client (c_over, c_back, c_pingpong);
}
return 0;
}
/* Simulator
My_Client X 0
My_Server _ 0
My_Client Y 1
My_Server X 1
My_PingPong _ 1
My_Client Z 2
My_PingPong Y 2
My_Client X 2
My_Server _ 2
My_Client Y 3
My_Server X 3
My_PingPong _ 3
My_Client Z 4
My_PingPong Y 4
My_Client X 4
My_Server _ 4
My_Client Y 5
My_Server X 5
My_PingPong _ 5
My_Client Z 6
My_PingPong Y 6
My_Client X 6
My_Server _ 6
My_Client Y 7
My_Server X 7
My_PingPong _ 7
My_Client Z 8
My_PingPong Y 8
My_Client X 8
My_Server _ 8
My_Client Y 9
My_Server X 9
My_PingPong _ 9
My_Client Z 10
My_PingPong Y 10
My_Client X 10
My_Server _ 10
http://www.teigfam.net/oyvind/home/tech ... directions
(Standard disclaimer: no money, ads, gift etc.)
Comments welcome!
But then, if I can save two chanends by reuse, why doesn't the tool do this automatically?