int get_vol_mute(long* vol, int* mute) { snd_mixer_t* handle; int open_code = snd_mixer_open(&handle, 0); if (open_code != 0) return open_code; int attach_code = snd_mixer_attach(handle, "default"); if (attach_code != 0) { snd_mixer_close(handle); return attach_code; } int register_code = snd_mixer_selem_register(handle, NULL, NULL); if (register_code != 0) { cleanup_handle(handle); return register_code; } int load_code = snd_mixer_load(handle); if (load_code != 0) { cleanup_handle(handle); return load_code; } snd_mixer_selem_id_t *sid; snd_mixer_selem_id_alloca(&sid); snd_mixer_selem_id_set_index(sid, 0); snd_mixer_selem_id_set_name(sid, "Master"); snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid); if (elem == 0) { cleanup_handle(handle); return 1; } int vol_code = snd_mixer_selem_get_playback_dB(elem, SND_MIXER_SCHN_UNKNOWN, vol); if (vol_code != 0) { cleanup_handle(handle); return vol_code; } int switch_code = snd_mixer_selem_get_playback_switch(elem, SND_MIXER_SCHN_UNKNOWN, mute); if (switch_code != 0) { cleanup_handle(handle); return switch_code; } /* snd_mixer_selem_id_free(sid); */ snd_mixer_detach(handle, "default"); snd_mixer_close(handle); return 0; }
static ERL_NIF_TERM re2_compile(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ErlNifBinary pdata; if (enif_inspect_iolist_as_binary(env, argv[0], &pdata)) { const re2::StringPiece p((const char*)pdata.data, pdata.size); re2_handle* handle = (re2_handle*)enif_alloc_resource( re2_resource_type, sizeof(re2_handle)); handle->re = NULL; re2::RE2::Options re2opts; re2opts.set_log_errors(false); if (argc == 2 && !parse_compile_options(env, argv[1], re2opts)) { cleanup_handle(handle); enif_release_resource(handle); return enif_make_badarg(env); } re2::RE2 *re2 = (re2::RE2*)enif_alloc(sizeof(re2::RE2)); if (re2 == NULL) { cleanup_handle(handle); enif_release_resource(handle); return error(env, a_err_enif_alloc); } handle->re = new (re2) re2::RE2(p, re2opts); // placement new if (!handle->re->ok()) { ERL_NIF_TERM error = re2error(env, handle->re); cleanup_handle(handle); enif_release_resource(handle); return error; } ERL_NIF_TERM result = enif_make_resource(env, handle); enif_release_resource(handle); return enif_make_tuple2(env, a_ok, result); } else { return enif_make_badarg(env); } }
/* Thread launcher function responsible for registering * cleanup handlers and calling pthread_exit, if not done * in thread function. */ static void* thread_launcher(void* arg) { DWORD res = 1; void* rc = NULL; WINPR_THREAD* thread = (WINPR_THREAD*) arg; typedef void* (*fkt_t)(void*); fkt_t fkt; if (!thread) { WLog_ERR(TAG, "Called with invalid argument %p", arg); goto exit; } if (!(fkt = (fkt_t)thread->lpStartAddress)) { WLog_ERR(TAG, "Thread function argument is %p", fkt); goto exit; } if (pthread_mutex_lock(&thread->threadIsReadyMutex)) goto exit; if (!ListDictionary_Contains(thread_list, &thread->thread)) { if (pthread_cond_wait(&thread->threadIsReady, &thread->threadIsReadyMutex) != 0) { WLog_ERR(TAG, "The thread could not be made ready"); pthread_mutex_unlock(&thread->threadIsReadyMutex); goto exit; } } if (pthread_mutex_unlock(&thread->threadIsReadyMutex)) goto exit; assert(ListDictionary_Contains(thread_list, &thread->thread)); rc = fkt(thread->lpParameter); exit: if (thread) { if (!thread->exited) thread->dwExitCode = (DWORD)(size_t)rc; set_event(thread); res = thread->dwExitCode; if (thread->detached || !thread->started) cleanup_handle(thread); } return rc; }
VOID ExitThread(DWORD dwExitCode) { DWORD rc; pthread_t tid = pthread_self(); if (!thread_list) { WLog_ERR(TAG, "function called without existing thread list!"); #if defined(WITH_DEBUG_THREADS) DumpThreadHandles(); #endif pthread_exit(0); } else if (!ListDictionary_Contains(thread_list, &tid)) { WLog_ERR(TAG, "function called, but no matching entry in thread list!"); #if defined(WITH_DEBUG_THREADS) DumpThreadHandles(); #endif pthread_exit(0); } else { WINPR_THREAD* thread; ListDictionary_Lock(thread_list); thread = ListDictionary_GetItemValue(thread_list, &tid); assert(thread); thread->exited = TRUE; thread->dwExitCode = dwExitCode; #if defined(WITH_DEBUG_THREADS) thread->exit_stack = winpr_backtrace(20); #endif ListDictionary_Unlock(thread_list); set_event(thread); rc = thread->dwExitCode; if (thread->detached || !thread->started) cleanup_handle(thread); pthread_exit((void*) (size_t) rc); } }
BOOL ThreadCloseHandle(HANDLE handle) { WINPR_THREAD* thread = (WINPR_THREAD*) handle; if (!thread_list) { WLog_ERR(TAG, "Thread list does not exist, check call!"); dump_thread(thread); } else if (!ListDictionary_Contains(thread_list, &thread->thread)) { WLog_ERR(TAG, "Thread list does not contain this thread! check call!"); dump_thread(thread); } else { ListDictionary_Lock(thread_list); dump_thread(thread); if ((thread->started) && (WaitForSingleObject(thread, 0) != WAIT_OBJECT_0)) { WLog_ERR(TAG, "Thread running, setting to detached state!"); thread->detached = TRUE; pthread_detach(thread->thread); } else { cleanup_handle(thread); } ListDictionary_Unlock(thread_list); if (ListDictionary_Count(thread_list) < 1) { ListDictionary_Free(thread_list); thread_list = NULL; } } return TRUE; }
/* Thread launcher function responsible for registering * cleanup handlers and calling pthread_exit, if not done * in thread function. */ static void* thread_launcher(void* arg) { DWORD res = -1; void* rc = NULL; WINPR_THREAD* thread = (WINPR_THREAD*) arg; if (!thread) { WLog_ERR(TAG, "Called with invalid argument %p", arg); goto exit; } else { void *(*fkt)(void*) = (void*) thread->lpStartAddress; if (!fkt) { WLog_ERR(TAG, "Thread function argument is %p", fkt); goto exit; } rc = fkt(thread->lpParameter); } exit: if (thread) { if (!thread->exited) thread->dwExitCode = (DWORD)(size_t)rc; set_event(thread); res = thread->dwExitCode; if (thread->detached || !thread->started) cleanup_handle(thread); } pthread_exit((void*) (size_t) res); return rc; }
static void re2_resource_cleanup(ErlNifEnv*, void* arg) { // Delete any dynamically allocated memory stored in re2_handle re2_handle* handle = (re2_handle*)arg; cleanup_handle(handle); }