static void namedmutex_abandon (gpointer handle, pid_t pid, pthread_t tid) { struct _WapiHandle_namedmutex *mutex_handle; gboolean ok; int thr_ret; ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX, (gpointer *)&mutex_handle); if (ok == FALSE) { g_warning ("%s: error looking up named mutex handle %p", __func__, handle); return; } thr_ret = _wapi_handle_lock_shared_handles (); g_assert (thr_ret == 0); if (mutex_handle->pid == pid && pthread_equal (mutex_handle->tid, tid)) { DEBUG ("%s: Mutex handle %p abandoned!", __func__, handle); mutex_handle->recursion = 0; mutex_handle->pid = 0; mutex_handle->tid = 0; _wapi_shared_handle_set_signal_state (handle, TRUE); } _wapi_handle_unlock_shared_handles (); }
/* The shared state is not locked when prewait methods are called */ static void namedmutex_prewait (gpointer handle) { /* If the mutex is not currently owned, do nothing and let the * usual wait carry on. If it is owned, check that the owner * is still alive; if it isn't we override the previous owner * and assume that process exited abnormally and failed to * clean up. */ struct _WapiHandle_namedmutex *namedmutex_handle; gboolean ok; ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDMUTEX, (gpointer *)&namedmutex_handle); if (ok == FALSE) { g_warning ("%s: error looking up named mutex handle %p", __func__, handle); return; } DEBUG ("%s: Checking ownership of named mutex handle %p", __func__, handle); if (namedmutex_handle->recursion == 0) { DEBUG ("%s: Named mutex handle %p not owned", __func__, handle); } else if (namedmutex_handle->pid == _wapi_getpid ()) { DEBUG ("%s: Named mutex handle %p owned by this process", __func__, handle); } else { int thr_ret; gpointer proc_handle; DEBUG ("%s: Named mutex handle %p owned by another process", __func__, handle); proc_handle = OpenProcess (0, 0, namedmutex_handle->pid); if (proc_handle == NULL) { /* Didn't find the process that this handle * was owned by, overriding it */ DEBUG ("%s: overriding old owner of named mutex handle %p", __func__, handle); thr_ret = _wapi_handle_lock_shared_handles (); g_assert (thr_ret == 0); namedmutex_handle->pid = 0; namedmutex_handle->tid = 0; namedmutex_handle->recursion = 0; _wapi_shared_handle_set_signal_state (handle, TRUE); _wapi_handle_unlock_shared_handles (); } else { DEBUG ("%s: Found active pid %d for named mutex handle %p", __func__, namedmutex_handle->pid, handle); } if (proc_handle != NULL) CloseProcess (proc_handle); } }
static gboolean own_if_owned(gpointer handle) { gboolean ret = FALSE; if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) { if (_wapi_handle_trylock_shared_handles () == EBUSY) { return (FALSE); } } if (_wapi_handle_ops_isowned (handle)) { _wapi_handle_ops_own (handle); ret = TRUE; } if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) { _wapi_handle_unlock_shared_handles (); } return(ret); }