static thread_storage* get_thread_storage(JNIEnv* env) { thread_storage* tls = (thread_storage *)TLS_GET(tls_thread_data_key); if (tls == NULL) { tls = (thread_storage*)malloc(sizeof(thread_storage)); if (!tls) { throwByName(env, EOutOfMemory, "JNA: Can't allocate thread storage"); } else { snprintf(tls->name, sizeof(tls->name), "<uninitialized thread name>"); tls->jvm_thread = JNI_TRUE; tls->last_error = 0; tls->termination_flag = NULL; if ((*env)->GetJavaVM(env, &tls->jvm) != JNI_OK) { free(tls); throwByName(env, EIllegalState, "JNA: Could not get JavaVM"); tls = NULL; } else if (!TLS_SET(tls_thread_data_key, tls)) { free(tls); throwByName(env, EOutOfMemory, "JNA: Internal TLS error"); tls = NULL; } } } return tls; }
void INTERNAL hazardous_ptr(unsigned int which, void *ptr) {/*{{{*/ uintptr_t *hzptrs = TLS_GET(ts_hazard_ptrs); if (hzptrs == NULL) { { qthread_worker_t *wkr = qthread_internal_getworker(); if (wkr == NULL) { hzptrs = calloc(sizeof(uintptr_t), HAZARD_PTRS_PER_SHEP + 1); assert(hzptrs); do { hzptrs[HAZARD_PTRS_PER_SHEP] = (uintptr_t)QTHREAD_CASLOCK_READ(hzptr_list); } while (QT_CAS(hzptr_list, hzptrs[HAZARD_PTRS_PER_SHEP], hzptrs) != (void *)hzptrs[HAZARD_PTRS_PER_SHEP]); (void)qthread_incr(&hzptr_list_len, 1); } else { hzptrs = wkr->hazard_ptrs; } } TLS_SET(ts_hazard_ptrs, hzptrs); } assert(hzptrs); assert(which < HAZARD_PTRS_PER_SHEP); hzptrs[which] = (uintptr_t)ptr; }/*}}}*/