* Author:
* Dick Porter (dick@ximian.com)
*
- * (C) 2002 Ximian, Inc.
+ * (C) 2002-2006 Novell, Inc.
*/
#include <config.h>
#include <string.h>
#include <errno.h>
-#include <mono/os/gc_wrapper.h>
-
#include <mono/io-layer/wapi.h>
#include <mono/io-layer/handles-private.h>
#include <mono/io-layer/wapi-private.h>
-#include <mono/io-layer/mono-mutex.h>
#include <mono/io-layer/misc-private.h>
-#undef DEBUG
+#include <mono/utils/mono-mutex.h>
+
+#if 0
+#define DEBUG(...) g_message(__VA_ARGS__)
+#else
+#define DEBUG(...)
+#endif
static gboolean own_if_signalled(gpointer handle)
{
struct timespec abstime;
int thr_ret;
gboolean apc_pending = FALSE;
- gpointer current_thread = GetCurrentThread ();
+ gpointer current_thread = _wapi_thread_handle_from_id (pthread_self ());
+
+ if (current_thread == NULL) {
+ SetLastError (ERROR_INVALID_HANDLE);
+ return(WAIT_FAILED);
+ }
+
+ if (handle == _WAPI_THREAD_CURRENT) {
+ handle = _wapi_thread_handle_from_id (pthread_self ());
+ if (handle == NULL) {
+ SetLastError (ERROR_INVALID_HANDLE);
+ return(WAIT_FAILED);
+ }
+ }
+
+ if ((GPOINTER_TO_UINT (handle) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
+ SetLastError (ERROR_INVALID_HANDLE);
+ return(WAIT_FAILED);
+ }
if (_wapi_handle_test_capabilities (handle,
WAPI_HANDLE_CAP_WAIT) == FALSE) {
-#ifdef DEBUG
- g_message ("%s: handle %p can't be waited for", __func__,
+ DEBUG ("%s: handle %p can't be waited for", __func__,
handle);
-#endif
return(WAIT_FAILED);
}
_wapi_handle_ops_prewait (handle);
if (_wapi_handle_test_capabilities (handle, WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE) {
-#ifdef DEBUG
- g_message ("%s: handle %p has special wait", __func__, handle);
-#endif
+ DEBUG ("%s: handle %p has special wait", __func__, handle);
- ret = _wapi_handle_ops_special_wait (handle, timeout);
+ ret = _wapi_handle_ops_special_wait (handle, timeout, alertable);
if (alertable && _wapi_thread_apc_pending (current_thread)) {
apc_pending = TRUE;
}
-#ifdef DEBUG
- g_message ("%s: locking handle %p", __func__, handle);
-#endif
+ DEBUG ("%s: locking handle %p", __func__, handle);
pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
handle);
if (_wapi_handle_test_capabilities (handle,
WAPI_HANDLE_CAP_OWN) == TRUE) {
if (own_if_owned (handle) == TRUE) {
-#ifdef DEBUG
- g_message ("%s: handle %p already owned", __func__,
+ DEBUG ("%s: handle %p already owned", __func__,
handle);
-#endif
ret = WAIT_OBJECT_0;
goto done;
}
}
if (own_if_signalled (handle) == TRUE) {
-#ifdef DEBUG
- g_message ("%s: handle %p already signalled", __func__,
+ DEBUG ("%s: handle %p already signalled", __func__,
handle);
-#endif
ret=WAIT_OBJECT_0;
goto done;
_wapi_handle_ops_prewait (handle);
if (own_if_signalled (handle)) {
-#ifdef DEBUG
- g_message ("%s: handle %p signalled", __func__,
+ DEBUG ("%s: handle %p signalled", __func__,
handle);
-#endif
ret = WAIT_OBJECT_0;
goto done;
}
if (timeout == INFINITE) {
- waited = _wapi_handle_wait_signal_handle (handle);
+ waited = _wapi_handle_wait_signal_handle (handle, alertable);
} else {
- waited = _wapi_handle_timedwait_signal_handle (handle, &abstime);
+ waited = _wapi_handle_timedwait_signal_handle (handle, &abstime, alertable, FALSE);
}
if (alertable)
* if someone else got in before us.)
*/
if (own_if_signalled (handle)) {
-#ifdef DEBUG
- g_message ("%s: handle %p signalled", __func__,
+ DEBUG ("%s: handle %p signalled", __func__,
handle);
-#endif
ret=WAIT_OBJECT_0;
goto done;
} while(waited == 0 && !apc_pending);
/* Timeout or other error */
-#ifdef DEBUG
- g_message ("%s: wait on handle %p error: %s", __func__, handle,
+ DEBUG ("%s: wait on handle %p error: %s", __func__, handle,
strerror (waited));
-#endif
ret = WAIT_TIMEOUT;
done:
-#ifdef DEBUG
- g_message ("%s: unlocking handle %p", __func__, handle);
-#endif
+ DEBUG ("%s: unlocking handle %p", __func__, handle);
thr_ret = _wapi_handle_unlock_handle (handle);
g_assert (thr_ret == 0);
struct timespec abstime;
int thr_ret;
gboolean apc_pending = FALSE;
- gpointer current_thread = GetCurrentThread ();
+ gpointer current_thread = _wapi_thread_handle_from_id (pthread_self ());
+
+ if (current_thread == NULL) {
+ SetLastError (ERROR_INVALID_HANDLE);
+ return(WAIT_FAILED);
+ }
+
+ if (signal_handle == _WAPI_THREAD_CURRENT) {
+ signal_handle = _wapi_thread_handle_from_id (pthread_self ());
+ if (signal_handle == NULL) {
+ SetLastError (ERROR_INVALID_HANDLE);
+ return(WAIT_FAILED);
+ }
+ }
+
+ if (wait == _WAPI_THREAD_CURRENT) {
+ wait = _wapi_thread_handle_from_id (pthread_self ());
+ if (wait == NULL) {
+ SetLastError (ERROR_INVALID_HANDLE);
+ return(WAIT_FAILED);
+ }
+ }
+
+ if ((GPOINTER_TO_UINT (signal_handle) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
+ SetLastError (ERROR_INVALID_HANDLE);
+ return(WAIT_FAILED);
+ }
+
+ if ((GPOINTER_TO_UINT (wait) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
+ SetLastError (ERROR_INVALID_HANDLE);
+ return(WAIT_FAILED);
+ }
if (_wapi_handle_test_capabilities (signal_handle,
WAPI_HANDLE_CAP_SIGNAL)==FALSE) {
return (WAIT_FAILED);
}
-#ifdef DEBUG
- g_message ("%s: locking handle %p", __func__, wait);
-#endif
+ DEBUG ("%s: locking handle %p", __func__, wait);
pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
wait);
if (_wapi_handle_test_capabilities (wait, WAPI_HANDLE_CAP_OWN)==TRUE) {
if (own_if_owned (wait)) {
-#ifdef DEBUG
- g_message ("%s: handle %p already owned", __func__,
+ DEBUG ("%s: handle %p already owned", __func__,
wait);
-#endif
ret = WAIT_OBJECT_0;
goto done;
}
}
if (own_if_signalled (wait)) {
-#ifdef DEBUG
- g_message ("%s: handle %p already signalled", __func__, wait);
-#endif
+ DEBUG ("%s: handle %p already signalled", __func__, wait);
ret = WAIT_OBJECT_0;
goto done;
_wapi_handle_ops_prewait (wait);
if (own_if_signalled (wait)) {
-#ifdef DEBUG
- g_message ("%s: handle %p signalled", __func__, wait);
-#endif
+ DEBUG ("%s: handle %p signalled", __func__, wait);
ret = WAIT_OBJECT_0;
goto done;
}
if (timeout == INFINITE) {
- waited = _wapi_handle_wait_signal_handle (wait);
+ waited = _wapi_handle_wait_signal_handle (wait, alertable);
} else {
- waited = _wapi_handle_timedwait_signal_handle (wait, &abstime);
+ waited = _wapi_handle_timedwait_signal_handle (wait, &abstime, alertable, FALSE);
}
if (alertable) {
* if someone else got in before us.)
*/
if (own_if_signalled (wait)) {
-#ifdef DEBUG
- g_message ("%s: handle %p signalled", __func__,
+ DEBUG ("%s: handle %p signalled", __func__,
wait);
-#endif
ret = WAIT_OBJECT_0;
goto done;
} while(waited == 0 && !apc_pending);
/* Timeout or other error */
-#ifdef DEBUG
- g_message ("%s: wait on handle %p error: %s", __func__, wait,
+ DEBUG ("%s: wait on handle %p error: %s", __func__, wait,
strerror (ret));
-#endif
ret = WAIT_TIMEOUT;
done:
-#ifdef DEBUG
- g_message ("%s: unlocking handle %p", __func__, wait);
-#endif
+ DEBUG ("%s: unlocking handle %p", __func__, wait);
thr_ret = _wapi_handle_unlock_handle (wait);
g_assert (thr_ret == 0);
gboolean done;
int i;
-#ifdef DEBUG
- g_message ("%s: locking handles", __func__);
-#endif
+ DEBUG ("%s: locking handles", __func__);
cleanup_data.numobjects = numobjects;
cleanup_data.handles = handles;
}
}
-#ifdef DEBUG
- g_message ("%s: unlocking handles", __func__);
-#endif
+ DEBUG ("%s: unlocking handles", __func__);
/* calls the unlock function */
pthread_cleanup_pop (1);
return(done);
}
-
-
/**
* WaitForMultipleObjectsEx:
* @numobjects: The number of objects in @handles. The maximum allowed
gboolean waitall, guint32 timeout,
gboolean alertable)
{
- GHashTable *dups;
gboolean duplicate = FALSE, bogustype = FALSE, done;
guint32 count, lowest;
struct timespec abstime;
guint i;
guint32 ret;
int thr_ret;
- gpointer current_thread = GetCurrentThread ();
+ gpointer current_thread = _wapi_thread_handle_from_id (pthread_self ());
+ guint32 retval;
+ gboolean poll;
+ gpointer sorted_handles [MAXIMUM_WAIT_OBJECTS];
+
+ if (current_thread == NULL) {
+ SetLastError (ERROR_INVALID_HANDLE);
+ return(WAIT_FAILED);
+ }
if (numobjects > MAXIMUM_WAIT_OBJECTS) {
-#ifdef DEBUG
- g_message ("%s: Too many handles: %d", __func__, numobjects);
-#endif
+ DEBUG ("%s: Too many handles: %d", __func__, numobjects);
return(WAIT_FAILED);
}
}
/* Check for duplicates */
- dups = g_hash_table_new (g_direct_hash, g_direct_equal);
for (i = 0; i < numobjects; i++) {
- gpointer exists = g_hash_table_lookup (dups, handles[i]);
- if (exists != NULL) {
-#ifdef DEBUG
- g_message ("%s: Handle %p duplicated", __func__,
- handles[i]);
-#endif
+ if (handles[i] == _WAPI_THREAD_CURRENT) {
+ handles[i] = _wapi_thread_handle_from_id (pthread_self ());
+
+ if (handles[i] == NULL) {
+ DEBUG ("%s: Handle %d bogus", __func__, i);
- duplicate = TRUE;
+ bogustype = TRUE;
+ break;
+ }
+ }
+
+ if ((GPOINTER_TO_UINT (handles[i]) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
+ DEBUG ("%s: Handle %d pseudo process", __func__,
+ i);
+
+ bogustype = TRUE;
break;
}
if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_WAIT) == FALSE) {
-#ifdef DEBUG
- g_message ("%s: Handle %p can't be waited for",
+ DEBUG ("%s: Handle %p can't be waited for",
__func__, handles[i]);
-#endif
bogustype = TRUE;
+ break;
}
- g_hash_table_insert (dups, handles[i], handles[i]);
+ sorted_handles [i] = handles [i];
_wapi_handle_ops_prewait (handles[i]);
}
- g_hash_table_destroy (dups);
+
+ qsort (sorted_handles, numobjects, sizeof (gpointer), g_direct_equal);
+ for (i = 1; i < numobjects; i++) {
+ if (sorted_handles [i - 1] == sorted_handles [i]) {
+ duplicate = TRUE;
+ break;
+ }
+ }
if (duplicate == TRUE) {
-#ifdef DEBUG
- g_message ("%s: Returning due to duplicates", __func__);
-#endif
+ DEBUG ("%s: Returning due to duplicates", __func__);
return(WAIT_FAILED);
}
if (bogustype == TRUE) {
-#ifdef DEBUG
- g_message ("%s: Returning due to bogus type", __func__);
-#endif
+ DEBUG ("%s: Returning due to bogus type", __func__);
return(WAIT_FAILED);
}
+ poll = FALSE;
+ for (i = 0; i < numobjects; ++i)
+ if (_wapi_handle_type (handles [i]) == WAPI_HANDLE_PROCESS || _WAPI_SHARED_HANDLE (_wapi_handle_type (handles[i])))
+ /* Can't wait for a process handle + another handle without polling */
+ poll = TRUE;
+
done = test_and_own (numobjects, handles, waitall, &count, &lowest);
if (done == TRUE) {
return(WAIT_OBJECT_0+lowest);
return WAIT_IO_COMPLETION;
}
+ for (i = 0; i < numobjects; i++) {
+ /* Add a reference, as we need to ensure the handle wont
+ * disappear from under us while we're waiting in the loop
+ * (not lock, as we don't want exclusive access here)
+ */
+ _wapi_handle_ref (handles[i]);
+ }
+
while(1) {
/* Prod all handles with prewait methods and
* special-wait handles that aren't already signalled
_wapi_handle_ops_prewait (handles[i]);
if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE && _wapi_handle_issignalled (handles[i]) == FALSE) {
- _wapi_handle_ops_special_wait (handles[i], 0);
+ _wapi_handle_ops_special_wait (handles[i], 0, alertable);
}
}
-
- /* Check before waiting on the condition, just in case
- */
- done = test_and_own (numobjects, handles, waitall,
- &count, &lowest);
- if (done == TRUE) {
- return(WAIT_OBJECT_0 + lowest);
- }
-#ifdef DEBUG
- g_message ("%s: locking signal mutex", __func__);
-#endif
+ DEBUG ("%s: locking signal mutex", __func__);
pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_signal_mutex, NULL);
thr_ret = _wapi_handle_lock_signal_mutex ();
g_assert (thr_ret == 0);
+
+ /* Check the signalled state of handles inside the critical section */
+ if (waitall) {
+ done = TRUE;
+ for (i = 0; i < numobjects; i++)
+ if (!_wapi_handle_issignalled (handles [i]))
+ done = FALSE;
+ } else {
+ done = FALSE;
+ for (i = 0; i < numobjects; i++)
+ if (_wapi_handle_issignalled (handles [i]))
+ done = TRUE;
+ }
- if (timeout == INFINITE) {
- ret = _wapi_handle_wait_signal ();
+ if (!done) {
+ /* Enter the wait */
+ if (timeout == INFINITE) {
+ ret = _wapi_handle_wait_signal (poll);
+ } else {
+ ret = _wapi_handle_timedwait_signal (&abstime, poll);
+ }
} else {
- ret = _wapi_handle_timedwait_signal (&abstime);
+ /* No need to wait */
+ ret = 0;
}
-#ifdef DEBUG
- g_message ("%s: unlocking signal mutex", __func__);
-#endif
+ DEBUG ("%s: unlocking signal mutex", __func__);
thr_ret = _wapi_handle_unlock_signal_mutex (NULL);
g_assert (thr_ret == 0);
if (alertable && _wapi_thread_apc_pending (current_thread)) {
_wapi_thread_dispatch_apc_queue (current_thread);
- return WAIT_IO_COMPLETION;
+ retval = WAIT_IO_COMPLETION;
+ break;
}
/* Check if everything is signalled, as we can't
done = test_and_own (numobjects, handles, waitall,
&count, &lowest);
if (done == TRUE) {
- return(WAIT_OBJECT_0+lowest);
+ retval = WAIT_OBJECT_0+lowest;
+ break;
} else if (ret != 0) {
/* Didn't get all handles, and there was a
* timeout or other error
*/
-#ifdef DEBUG
- g_message ("%s: wait returned error: %s", __func__,
+ DEBUG ("%s: wait returned error: %s", __func__,
strerror (ret));
-#endif
if(ret==ETIMEDOUT) {
- return(WAIT_TIMEOUT);
+ retval = WAIT_TIMEOUT;
} else {
- return(WAIT_FAILED);
+ retval = WAIT_FAILED;
}
+ break;
}
}
+
+ for (i = 0; i < numobjects; i++) {
+ /* Unref everything we reffed above */
+ _wapi_handle_unref (handles[i]);
+ }
+
+ return retval;
}
guint32 WaitForMultipleObjects(guint32 numobjects, gpointer *handles,
{
return WaitForMultipleObjectsEx(numobjects, handles, waitall, timeout, FALSE);
}
+
+/**
+ * WaitForInputIdle:
+ * @handle: a handle to the process to wait for
+ * @timeout: the maximum time in milliseconds to wait for
+ *
+ * This function returns when either @handle process is waiting
+ * for input, or @timeout ms elapses. If @timeout is zero, the
+ * process state is tested and the function returns immediately.
+ * If @timeout is %INFINITE, the function waits forever.
+ *
+ * Return value: 0 - @handle process is waiting for input.
+ * %WAIT_TIMEOUT - The @timeout interval elapsed and
+ * @handle process is not waiting for input. %WAIT_FAILED - an error
+ * occurred.
+ */
+guint32 WaitForInputIdle(gpointer handle, guint32 timeout)
+{
+ /*TODO: Not implemented*/
+ return WAIT_TIMEOUT;
+}
+