Example #1
0
/* NB, always called with the shared handle lock held */
static gboolean namedevent_own (gpointer handle)
{
    struct _WapiHandle_namedevent *namedevent_handle;
    gboolean ok;

    DEBUG ("%s: owning named event handle %p", __func__, handle);

    ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDEVENT,
                              (gpointer *)&namedevent_handle);
    if (ok == FALSE) {
        g_warning ("%s: error looking up named event handle %p",
                   __func__, handle);
        return(FALSE);
    }

    if (namedevent_handle->manual == FALSE) {
        g_assert (namedevent_handle->set_count > 0);

        if (--namedevent_handle->set_count == 0) {
            _wapi_shared_handle_set_signal_state (handle, FALSE);
        }
    }

    return (TRUE);
}
Example #2
0
/* NB, always called with the shared handle lock held */
static gboolean namedsema_own (gpointer handle)
{
	struct _WapiHandle_namedsem *namedsem_handle;
	gboolean ok;
	
	DEBUG ("%s: owning named sem handle %p", __func__, handle);

	ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDSEM,
				  (gpointer *)&namedsem_handle);
	if (ok == FALSE) {
		g_warning ("%s: error looking up named sem handle %p",
			   __func__, handle);
		return (FALSE);
	}
	
	namedsem_handle->val--;
	
	DEBUG ("%s: named sem %p val now %d", __func__, handle,
		   namedsem_handle->val);

	if (namedsem_handle->val == 0) {
		_wapi_shared_handle_set_signal_state (handle, FALSE);
	}
	
	return (TRUE);
}
Example #3
0
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 ();
}
Example #4
0
/* NB, always called with the shared handle lock held */
static gboolean namedmutex_own (gpointer handle)
{
	struct _WapiHandle_namedmutex *namedmutex_handle;
	gboolean ok;
	
	DEBUG ("%s: owning named mutex handle %p", __func__, handle);
	
	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(FALSE);
	}

	_wapi_thread_own_mutex (handle);

	namedmutex_handle->pid = _wapi_getpid ();
	namedmutex_handle->tid = pthread_self ();
	namedmutex_handle->recursion++;

	_wapi_shared_handle_set_signal_state (handle, FALSE);

	DEBUG ("%s: mutex handle %p locked %d times by %d:%ld", __func__,
		   handle, namedmutex_handle->recursion,
		   namedmutex_handle->pid, namedmutex_handle->tid);
	
	return(TRUE);
}
Example #5
0
/* 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);
	}
}