From 759520a6f180a638027fdc7be58e12772247fe47 Mon Sep 17 00:00:00 2001 From: Nanang Izzuddin Date: Thu, 22 Mar 2012 11:29:20 +0000 Subject: Close #1466 (using PJLIB outside PJSUA-LIB context): - static reference counter for PJLIB init/shutdown. - implemented atexit() in PJMEDIA and PJSIP level: pjmedia_endpt_atexit() & pjsip_endpt_atexit(). - updated pjmedia/transport_srtp.c, pjsip/sip_timer.c, and pjsip/sip_replaces.c to use the new atexit() functions. - API change: pjmedia_srtp_init_lib() now requires 'pjmedia_endpt' param. git-svn-id: http://svn.pjsip.org/repos/pjproject/branches/1.x@3986 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib/src/pj/os_core_symbian.cpp | 20 ++++++++++++++++++-- pjlib/src/pj/os_core_unix.c | 20 ++++++++++++++++++++ pjlib/src/pj/os_core_win32.c | 20 ++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) (limited to 'pjlib/src') diff --git a/pjlib/src/pj/os_core_symbian.cpp b/pjlib/src/pj/os_core_symbian.cpp index abf01b85..52733560 100644 --- a/pjlib/src/pj/os_core_symbian.cpp +++ b/pjlib/src/pj/os_core_symbian.cpp @@ -75,6 +75,9 @@ struct pj_sem_t int max; }; +/* Flag and reference counter for PJLIB instance */ +static int initialized; + /* Flags to indicate which TLS variables have been used */ static int tls_vars[PJ_MAX_TLS]; @@ -83,8 +86,6 @@ static unsigned atexit_count; static void (*atexit_func[32])(void); - - ///////////////////////////////////////////////////////////////////////////// // // CPjTimeoutTimer implementation @@ -335,6 +336,12 @@ PJ_DEF(pj_status_t) pj_init(void) char stack_ptr; pj_status_t status; + /* Check if PJLIB have been initialized */ + if (initialized) { + ++initialized; + return PJ_SUCCESS; + } + pj_ansi_strcpy(main_thread.obj_name, "pjthread"); // Init main thread @@ -368,6 +375,10 @@ PJ_DEF(pj_status_t) pj_init(void) stack_ptr = '\0'; #endif + /* Flag PJLIB as initialized */ + ++initialized; + pj_assert(initialized == 1); + PJ_LOG(5,(THIS_FILE, "PJLIB initialized.")); return PJ_SUCCESS; @@ -390,6 +401,11 @@ PJ_DEF(pj_status_t) pj_atexit(pj_exit_callback func) PJ_DEF(void) pj_shutdown(void) { + /* Only perform shutdown operation when 'initialized' reaches zero */ + pj_assert(initialized > 0); + if (--initialized != 0) + return; + /* Call atexit() functions */ while (atexit_count > 0) { (*atexit_func[atexit_count-1])(); diff --git a/pjlib/src/pj/os_core_unix.c b/pjlib/src/pj/os_core_unix.c index 3fcfa565..00087b64 100644 --- a/pjlib/src/pj/os_core_unix.c +++ b/pjlib/src/pj/os_core_unix.c @@ -102,6 +102,11 @@ struct pj_event_t #endif /* PJ_HAS_EVENT_OBJ */ +/* + * Flag and reference counter for PJLIB instance. + */ +static int initialized; + #if PJ_HAS_THREADS static pj_thread_t main_thread; static long thread_tls_id; @@ -127,6 +132,12 @@ PJ_DEF(pj_status_t) pj_init(void) pj_str_t guid; pj_status_t rc; + /* Check if PJLIB have been initialized */ + if (initialized) { + ++initialized; + return PJ_SUCCESS; + } + #if PJ_HAS_THREADS /* Init this thread's TLS. */ if ((rc=pj_thread_init()) != 0) { @@ -167,6 +178,10 @@ PJ_DEF(pj_status_t) pj_init(void) } #endif + /* Flag PJLIB as initialized */ + ++initialized; + pj_assert(initialized == 1); + PJ_LOG(4,(THIS_FILE, "pjlib %s for POSIX initialized", PJ_VERSION)); @@ -192,6 +207,11 @@ PJ_DEF(void) pj_shutdown() { int i; + /* Only perform shutdown operation when 'initialized' reaches zero */ + pj_assert(initialized > 0); + if (--initialized != 0) + return; + /* Call atexit() functions */ for (i=atexit_count-1; i>=0; --i) { (*atexit_func[i])(); diff --git a/pjlib/src/pj/os_core_win32.c b/pjlib/src/pj/os_core_win32.c index 2b7c2c91..55d7b1b9 100644 --- a/pjlib/src/pj/os_core_win32.c +++ b/pjlib/src/pj/os_core_win32.c @@ -116,6 +116,11 @@ struct pj_atomic_t long value; }; +/* + * Flag and reference counter for PJLIB instance. + */ +static int initialized; + /* * Static global variables. */ @@ -142,6 +147,12 @@ PJ_DEF(pj_status_t) pj_init(void) pj_str_t guid; pj_status_t rc; + /* Check if PJLIB have been initialized */ + if (initialized) { + ++initialized; + return PJ_SUCCESS; + } + /* Init Winsock.. */ if (WSAStartup(MAKEWORD(2,0), &wsa) != 0) { return PJ_RETURN_OS_ERROR(WSAGetLastError()); @@ -187,6 +198,10 @@ PJ_DEF(pj_status_t) pj_init(void) } #endif + /* Flag PJLIB as initialized */ + ++initialized; + pj_assert(initialized == 1); + PJ_LOG(4,(THIS_FILE, "pjlib %s for win32 initialized", PJ_VERSION)); @@ -213,6 +228,11 @@ PJ_DEF(void) pj_shutdown() { int i; + /* Only perform shutdown operation when 'initialized' reaches zero */ + pj_assert(initialized > 0); + if (--initialized != 0) + return; + /* Display stack usage */ #if defined(PJ_OS_HAS_CHECK_STACK) && PJ_OS_HAS_CHECK_STACK!=0 { -- cgit v1.2.3