Skip to content

Commit

Permalink
fix #377 fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
zanzaben committed Dec 15, 2020
1 parent b18e9d3 commit d15206b
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/tests/select-test/select-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ OS_SockAddr_t s2_addr;
OS_SockAddr_t c_addr;
OS_SockAddr_t c2_addr;
osal_id_t bin_sem_id;
osal_id_t bin_sem_id2;

/* *************************************** MAIN ************************************** */

Expand All @@ -65,15 +66,24 @@ void BinSemSetup(void)
{
uint32 status;
OS_bin_sem_prop_t bin_sem_prop;
OS_bin_sem_prop_t bin_sem_prop2;

/*
** Create the binary semaphore
* Create the binary semaphore
* BinSem1 is used to control when the server can accept connections
* BinSem2 is used to make sure all sub task finish before the main task does.
*/
status = OS_BinSemCreate(&bin_sem_id, "BinSem1", 0, 0);
UtAssert_True(status == OS_SUCCESS, "BinSem1 create Id=%lx Rc=%d", OS_ObjectIdToInteger(bin_sem_id), (int)status);

status = OS_BinSemGetInfo(bin_sem_id, &bin_sem_prop);
UtAssert_True(status == OS_SUCCESS, "BinSem1 value=%d Rc=%d", (int)bin_sem_prop.value, (int)status);

status = OS_BinSemCreate(&bin_sem_id2, "BinSem2", 0, 0);
UtAssert_True(status == OS_SUCCESS, "BinSem2 create Id=%lx Rc=%d", OS_ObjectIdToInteger(bin_sem_id2), (int)status);

status = OS_BinSemGetInfo(bin_sem_id2, &bin_sem_prop2);
UtAssert_True(status == OS_SUCCESS, "BinSem2 value=%d Rc=%d", (int)bin_sem_prop2.value, (int)status);
}

void Setup_Server(void)
Expand Down Expand Up @@ -143,16 +153,22 @@ void Server_Fn(void)
{
osal_id_t connsock_id = OS_OBJECT_ID_UNDEFINED;
OS_SockAddr_t addr;
uint32 status;

/* Accept incoming connections */
OS_SocketAccept(s_socket_id, &connsock_id, &addr, OS_PEND);

uint32 status = OS_BinSemTake(bin_sem_id);
status = OS_BinSemTake(bin_sem_id);
UtAssert_True(status == OS_SUCCESS, "BinSem1 Server 1 take Rc=%d", (int)status);

OS_close(s_socket_id);
OS_close(connsock_id);
status = OS_close(s_socket_id);
UtAssert_True(status == OS_SUCCESS, "status after close s_socket_id = %d", (int)status);

status = OS_close(connsock_id);
UtAssert_True(status == OS_SUCCESS, "status after close connsock_id = %d", (int)status);

status = OS_BinSemGive(bin_sem_id2);
UtAssert_True(status == OS_SUCCESS, "BinSem2 Server 1 give Rc=%d", (int)status);
} /* end Server_Fn */

void Setup_Server2(void)
Expand Down Expand Up @@ -222,13 +238,16 @@ void Server_Fn2(void)
{
osal_id_t connsock_id = OS_OBJECT_ID_UNDEFINED;
OS_SockAddr_t addr;
uint32 status;

/* Accept incoming connections */
OS_SocketAccept(s2_socket_id, &connsock_id, &addr, OS_PEND);

OS_close(s2_socket_id);
OS_close(connsock_id);
status = OS_close(s2_socket_id);
UtAssert_True(status == OS_SUCCESS, "status after close s2_socket_id = %d", (int)status);

status = OS_close(connsock_id);
UtAssert_True(status == OS_SUCCESS, "status after close connsock_id = %d", (int)status);
} /* end Server_Fn */

void Setup_Single(void){
Expand All @@ -244,12 +263,22 @@ void Setup_Multi(void){
}

void Teardown_Single(void){
OS_BinSemDelete(bin_sem_id) ;
uint32 status;

OS_close(c_socket_id);
status = OS_BinSemTake(bin_sem_id2);
UtAssert_True(status == OS_SUCCESS, "BinSem2 Teardown single take Rc=%d", (int)status);

OS_BinSemDelete(bin_sem_id);
OS_BinSemDelete(bin_sem_id2);
}

void Teardown_Multi(void){
OS_BinSemFlush(bin_sem_id);
uint32 status;

status = OS_BinSemFlush(bin_sem_id);
UtAssert_True(status == OS_SUCCESS, "BinSem1 Teardown multi flush Rc=%d", (int)status);

OS_close(c2_socket_id);
Teardown_Single();
}
Expand Down

0 comments on commit d15206b

Please sign in to comment.