Skip to content

Commit

Permalink
Add initializer flag for the main context
Browse files Browse the repository at this point in the history
  • Loading branch information
disa6302 committed Sep 21, 2023
1 parent 04008a6 commit a6e0a12
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/source/PeerConnection/PeerConnection.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@ PWebRtcClientContext getWebRtcClientInstance()
PStunIpAddrContext getStunIpContext()
{
PWebRtcClientContext pWebRtcClientContext = getWebRtcClientInstance();
if (!pWebRtcClientContext->isContextInitialized) {
return NULL;
}
return pWebRtcClientContext->pStunIpAddrCtx;
}

STATUS createWebRtcClientInstance()
{
PWebRtcClientContext pWebRtcClientContext = getWebRtcClientInstance();
STATUS retStatus = STATUS_SUCCESS;
CHK_WARN(!pWebRtcClientContext->isContextInitialized, retStatus, "WebRtc client context already initialized, nothing to do");

CHK_WARN(pWebRtcClientContext->pStunIpAddrCtx == NULL, STATUS_INVALID_OPERATION, "STUN object already allocated");
pWebRtcClientContext->pStunIpAddrCtx = (PStunIpAddrContext) MEMCALLOC(1, SIZEOF(StunIpAddrContext));
CHK_ERR(pWebRtcClientContext->pStunIpAddrCtx != NULL, STATUS_NULL_ARG, "Memory allocation for WebRtc client object failed");
pWebRtcClientContext->pStunIpAddrCtx->lock = MUTEX_CREATE(FALSE);
pWebRtcClientContext->pStunIpAddrCtx->expirationDuration = 2 * HUNDREDS_OF_NANOS_IN_AN_HOUR;

pWebRtcClientContext->isContextInitialized = TRUE;
CleanUp:
return retStatus;
}
Expand Down Expand Up @@ -743,15 +748,13 @@ STATUS getAddrAsync(PStunIpAddrContext pStunIpAddrCtx)
if (!resolved) {
retStatus = STATUS_RESOLVE_HOSTNAME_FAILED;
}
CleanUp:
return retStatus;
}

STATUS onSetStunServerIp(UINT64 customData, PCHAR url, PKvsIpAddress pIpAddr)
{
UNUSED_PARAM(customData);
STATUS retStatus = STATUS_SUCCESS;
CHAR addressResolved[KVS_IP_ADDRESS_STRING_BUFFER_LEN + 1] = {'\0'};
PStunIpAddrContext pStunIpAddrCtx = getStunIpContext();
CHK_ERR(pStunIpAddrCtx != NULL, STATUS_NULL_ARG, "WebRTC Client object could not be created");
UINT64 currentTime = GETTIME();
Expand Down Expand Up @@ -1579,6 +1582,7 @@ STATUS cleanupWebRtcClientContext()
// Stun object cleanup
PWebRtcClientContext pWebRtcClientContext = getWebRtcClientInstance();

CHK_WARN(pWebRtcClientContext->isContextInitialized, STATUS_INVALID_OPERATION, "WebRtc context not initialized, nothing to clean up");
CHK_WARN(pWebRtcClientContext->pStunIpAddrCtx != NULL, STATUS_NULL_ARG, "Destroying STUN object without setting up");

/* Start of handling STUN object */
Expand All @@ -1596,6 +1600,7 @@ STATUS cleanupWebRtcClientContext()
/* End of handling STUN object */

DLOGI("Destroyed WebRtc client context");
pWebRtcClientContext->isContextInitialized = FALSE;
CleanUp:
return retStatus;
}
Expand All @@ -1614,9 +1619,9 @@ STATUS deinitKvsWebRtc(VOID)

ATOMIC_STORE_BOOL(&gKvsWebRtcInitialized, FALSE);
#ifdef ENABLE_KVS_THREADPOOL
destroyThreadPoolContext();
DLOGI("Destroyed threadpool");
cleanupWebRtcClientContext();
DLOGI("Destroyed threadpool");
destroyThreadPoolContext();
#endif

CleanUp:
Expand Down
1 change: 1 addition & 0 deletions src/source/PeerConnection/PeerConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ typedef struct {
// Members of the singleton are responsible for their own sync mechanisms.
typedef struct {
PStunIpAddrContext pStunIpAddrCtx;
BOOL isContextInitialized;
} WebRtcClientContext, *PWebRtcClientContext;

STATUS onFrameReadyFunc(UINT64, UINT16, UINT16, UINT32);
Expand Down
7 changes: 3 additions & 4 deletions src/source/Threadpool/ThreadPoolContext.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ STATUS createThreadPoolContext()
UINT32 minThreads, maxThreads;
PThreadPoolContext pThreadPoolContext = getThreadContextInstance();

if (!IS_VALID_MUTEX_VALUE(pThreadPoolContext->threadpoolContextLock)) {
pThreadPoolContext->threadpoolContextLock = MUTEX_CREATE(FALSE);
}

if (NULL == (pMinThreads = GETENV(WEBRTC_THREADPOOL_MIN_THREADS_ENV_VAR)) || STATUS_SUCCESS != STRTOUI32(pMinThreads, NULL, 10, &minThreads)) {
minThreads = THREADPOOL_MIN_THREADS;
}
Expand All @@ -29,6 +25,9 @@ STATUS createThreadPoolContext()

// Protecting this section to ensure we are not pushing threads / destroying the pool
// when it is being created.
if (!IS_VALID_MUTEX_VALUE(pThreadPoolContext->threadpoolContextLock)) {
pThreadPoolContext->threadpoolContextLock = MUTEX_CREATE(FALSE);
}
MUTEX_LOCK(pThreadPoolContext->threadpoolContextLock);
locked = TRUE;
CHK_WARN(!pThreadPoolContext->isInitialized, retStatus, "Threadpool already set up. Nothing to do");
Expand Down

0 comments on commit a6e0a12

Please sign in to comment.