static void init_sdp_parser(void) { if (is_initialized != 0) return; pj_enter_critical_section(); if (is_initialized != 0) { pj_leave_critical_section(); return; } pj_cis_buf_init(&cis_buf); pj_cis_init(&cis_buf, &cs_token); pj_cis_add_alpha(&cs_token); pj_cis_add_num(&cs_token); pj_cis_add_str(&cs_token, TOKEN); pj_cis_init(&cis_buf, &cs_digit); pj_cis_add_num(&cs_digit); is_initialized = 1; pj_leave_critical_section(); }
PJ_DEF(pj_status_t) pj_grp_lock_add_ref_dbg(pj_grp_lock_t *glock, const char *file, int line) { grp_lock_ref *ref; pj_status_t status; pj_enter_critical_section(); if (!pj_list_empty(&glock->ref_free_list)) { ref = glock->ref_free_list.next; pj_list_erase(ref); } else { ref = PJ_POOL_ALLOC_T(glock->pool, grp_lock_ref); } ref->file = file; ref->line = line; pj_list_push_back(&glock->ref_list, ref); pj_leave_critical_section(); status = grp_lock_add_ref(glock); if (status != PJ_SUCCESS) { pj_enter_critical_section(); pj_list_erase(ref); pj_list_push_back(&glock->ref_free_list, ref); pj_leave_critical_section(); } return status; }
PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str) { char *p, *end; PJ_CHECK_STACK(); if (guid_chars[0] == '\0') { pj_enter_critical_section(); if (guid_chars[0] == '\0') { init_guid_chars(); } pj_leave_critical_section(); } /* This would only work if PJ_GUID_STRING_LENGTH is multiple of 2 bytes */ pj_assert(PJ_GUID_STRING_LENGTH % 2 == 0); for (p=str->ptr, end=p+PJ_GUID_STRING_LENGTH; p<end; ) { /* Assumes rand() only has 16bit randomness */ unsigned short val = pj_rand(); *p++ = guid_chars[(val >> 8) & 63]; *p++ = guid_chars[(val & 0xFF) & 63]; } str->slen = PJ_GUID_STRING_LENGTH; return str; }
PJ_DEF(pj_status_t) pj_grp_lock_dec_ref_dbg(pj_grp_lock_t *glock, const char *file, int line) { grp_lock_ref *ref; pj_enter_critical_section(); /* Find the same source file */ ref = glock->ref_list.next; while (ref != &glock->ref_list) { if (strcmp(ref->file, file) == 0) { pj_list_erase(ref); pj_list_push_back(&glock->ref_free_list, ref); break; } ref = ref->next; } pj_leave_critical_section(); if (ref == &glock->ref_list) { PJ_LOG(2,(THIS_FILE, "pj_grp_lock_dec_ref_dbg() could not find " "matching ref for %s", file)); } return grp_lock_dec_ref(glock); }
PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str) { char *p, *end; PJ_CHECK_STACK(); if (guid_chars[0] == '\0') { pj_enter_critical_section(); if (guid_chars[0] == '\0') { init_guid_chars(); } pj_leave_critical_section(); } /* This would only work if PJ_GUID_STRING_LENGTH is multiple of 2 bytes */ pj_assert(PJ_GUID_STRING_LENGTH % 2 == 0); for (p=str->ptr, end=p+PJ_GUID_STRING_LENGTH; p<end; ) { pj_uint32_t rand_val = pj_rand(); pj_uint32_t rand_idx = RAND_MAX; for ( ; rand_idx>0 && p<end; rand_idx>>=8, rand_val>>=8, p++) { *p = guid_chars[(rand_val & 0xFF) & 63]; } } str->slen = PJ_GUID_STRING_LENGTH; return str; }
pj_status_t pj_gettimeofday(pj_time_val *tv) { #ifdef WINCE_TIME pj_timestamp tick; pj_uint64_t msec_elapsed; #else SYSTEMTIME st; FILETIME ft; LARGE_INTEGER li; #endif pj_status_t status; if (base_time.QuadPart == 0) { status = get_base_time(); if (status != PJ_SUCCESS) return status; } #ifdef WINCE_TIME do { status = pj_get_timestamp(&tick); if (status != PJ_SUCCESS) return status; if (tick.u64 - g_last_update.u64 >= g_update_period) { pj_enter_critical_section(); if (tick.u64 - g_last_update.u64 >= g_update_period) { g_last_update.u64 = tick.u64; check_system_time(tick.u64 - g_start_tick.u64); } pj_leave_critical_section(); } else { break; } } while (1); msec_elapsed = pj_elapsed_msec64(&g_start_tick, &tick); tv->sec = (long)(g_start_time.QuadPart + msec_elapsed/1000); tv->msec = (long)(msec_elapsed % 1000); #else /* Standard Win32 GetLocalTime */ GetLocalTime(&st); SystemTimeToFileTime(&st, &ft); li.LowPart = ft.dwLowDateTime; li.HighPart = ft.dwHighDateTime; li.QuadPart /= SECS_TO_FT_MULT; li.QuadPart -= base_time.QuadPart; tv->sec = li.LowPart; tv->msec = st.wMilliseconds; #endif /* WINCE_TIME */ return PJ_SUCCESS; }
PJ_DEF(pj_status_t) pj_exception_id_alloc( const char *name, pj_exception_id_t *id) { unsigned i; pj_enter_critical_section(); /* * Start from 1 (not 0)!!! * Exception 0 is reserved for normal path of setjmp()!!! */ for (i=1; i<PJ_MAX_EXCEPTION_ID; ++i) { if (exception_id_names[i] == NULL) { exception_id_names[i] = name; *id = i; pj_leave_critical_section(); return PJ_SUCCESS; } } pj_leave_critical_section(); return PJ_ETOOMANY; }
PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id ) { /* * Start from 1 (not 0)!!! * Exception 0 is reserved for normal path of setjmp()!!! */ PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, PJ_EINVAL); pj_enter_critical_section(); exception_id_names[id] = NULL; pj_leave_critical_section(); return PJ_SUCCESS; }
PJ_DEF(void) pj_grp_lock_t::pj_grp_lock_dump() { #if PJ_GRP_LOCK_DEBUG grp_lock_ref *ref = grp_lock->ref_list.next; char info_buf[1000]; pj_str_t info; info.ptr = info_buf; info.slen = 0; pj_grp_lock_acquire(grp_lock); pj_enter_critical_section(); while (ref != &grp_lock->ref_list && info.slen < sizeof(info_buf)) { char *start = info.ptr + info.slen; int max_len = sizeof(info_buf) - info.slen; int len; len = pj_ansi_snprintf(start, max_len, "%s:%d ", ref->file, ref->line); if (len < 1 || len >= max_len) { len = strlen(ref->file); if (len > max_len - 1) len = max_len - 1; memcpy(start, ref->file, len); start[len++] = ' '; } info.slen += len; ref = ref->next; } if (ref != &grp_lock->ref_list) { int i; for (i=0; i<4; ++i) info_buf[sizeof(info_buf)-i-1] = '.'; } info.ptr[info.slen-1] = '\0'; pj_leave_critical_section(); pj_grp_lock_release(grp_lock); PJ_LOG(4,(THIS_FILE, "Group lock %p, ref_cnt=%d. Reference holders: %s", grp_lock, pj_grp_lock_get_ref(grp_lock), info.ptr)); #endif }
PJ_DEF(pj_status_t) pjmedia_endpt_atexit( pjmedia_endpt *endpt, pjmedia_endpt_exit_callback func) { exit_cb *new_cb; PJ_ASSERT_RETURN(endpt && func, PJ_EINVAL); if (endpt->quit_flag) return PJ_EINVALIDOP; new_cb = PJ_POOL_ZALLOC_T(endpt->pool, exit_cb); new_cb->func = func; pj_enter_critical_section(); pj_list_push_back(&endpt->exit_cb_list, new_cb); pj_leave_critical_section(); return PJ_SUCCESS; }
// Find 1st Jan 1970 as a FILETIME static pj_status_t get_base_time(void) { SYSTEMTIME st; FILETIME ft; pj_status_t status = PJ_SUCCESS; memset(&st,0,sizeof(st)); st.wYear=1970; st.wMonth=1; st.wDay=1; SystemTimeToFileTime(&st, &ft); base_time.LowPart = ft.dwLowDateTime; base_time.HighPart = ft.dwHighDateTime; base_time.QuadPart /= SECS_TO_FT_MULT; #ifdef WINCE_TIME pj_enter_critical_section(); status = init_start_time(); pj_leave_critical_section(); #endif return status; }
static int timer_initialize() { pj_status_t rc; pj_mutex_t* temp_mutex; rc = pj_mutex_create_simple(timer_pool, "zrtp_timer", &temp_mutex); if (rc != PJ_SUCCESS) { return rc; } pj_enter_critical_section(); if (timer_mutex == NULL) timer_mutex = temp_mutex; else pj_mutex_destroy(temp_mutex); pj_leave_critical_section(); pj_mutex_lock(timer_mutex); if (timer_initialized) { pj_mutex_unlock(timer_mutex); return PJ_SUCCESS; } rc = pj_timer_heap_create(timer_pool, 4, &timer); if (rc != PJ_SUCCESS) { goto ERROR; } rc = pj_sem_create(timer_pool, "zrtp_timer", 0, 1, &timer_sem); if (rc != PJ_SUCCESS) { goto ERROR; } rc = pj_thread_create(timer_pool, "zrtp_timer", &timer_thread_run, NULL, PJ_THREAD_DEFAULT_STACK_SIZE, 0, &thread_run); if (rc != PJ_SUCCESS) { goto ERROR; } timer_initialized = 1; pj_mutex_unlock(timer_mutex); return PJ_SUCCESS; ERROR: if (timer != NULL) { pj_timer_heap_destroy(timer); timer = NULL; } if (timer_sem != NULL) { pj_sem_destroy(timer_sem); timer_sem = NULL; } if (timer_mutex != NULL) { pj_mutex_unlock(timer_mutex); pj_mutex_destroy(timer_mutex); timer_mutex = NULL; } return rc; }