Commit c6ab933b authored by Bernhard Kölbl's avatar Bernhard Kölbl Committed by Alexandre Julliard

windows.media.speech: Make IAsyncAction concurrent.

parent 1438b1b6
...@@ -40,6 +40,11 @@ struct async_void ...@@ -40,6 +40,11 @@ struct async_void
IAsyncActionCompletedHandler *handler; IAsyncActionCompletedHandler *handler;
async_action_callback callback;
TP_WORK *async_run_work;
IInspectable *invoker;
CRITICAL_SECTION cs;
AsyncStatus status; AsyncStatus status;
HRESULT hr; HRESULT hr;
}; };
...@@ -116,6 +121,7 @@ HRESULT WINAPI async_void_put_Completed( IAsyncAction *iface, IAsyncActionComple ...@@ -116,6 +121,7 @@ HRESULT WINAPI async_void_put_Completed( IAsyncAction *iface, IAsyncActionComple
TRACE("iface %p, handler %p.\n", iface, handler); TRACE("iface %p, handler %p.\n", iface, handler);
EnterCriticalSection(&impl->cs);
if (impl->status == Closed) if (impl->status == Closed)
hr = E_ILLEGAL_METHOD_CALL; hr = E_ILLEGAL_METHOD_CALL;
else if (impl->handler != HANDLER_NOT_SET) else if (impl->handler != HANDLER_NOT_SET)
...@@ -133,11 +139,15 @@ HRESULT WINAPI async_void_put_Completed( IAsyncAction *iface, IAsyncActionComple ...@@ -133,11 +139,15 @@ HRESULT WINAPI async_void_put_Completed( IAsyncAction *iface, IAsyncActionComple
IAsyncAction *action = &impl->IAsyncAction_iface; IAsyncAction *action = &impl->IAsyncAction_iface;
AsyncStatus status = impl->status; AsyncStatus status = impl->status;
impl->handler = NULL; /* Prevent concurrent invoke. */ impl->handler = NULL; /* Prevent concurrent invoke. */
LeaveCriticalSection(&impl->cs);
IAsyncActionCompletedHandler_Invoke(handler, action, status); IAsyncActionCompletedHandler_Invoke(handler, action, status);
IAsyncActionCompletedHandler_Release(handler); IAsyncActionCompletedHandler_Release(handler);
return S_OK;
} }
} }
LeaveCriticalSection(&impl->cs);
return hr; return hr;
} }
...@@ -149,9 +159,11 @@ HRESULT WINAPI async_void_get_Completed( IAsyncAction *iface, IAsyncActionComple ...@@ -149,9 +159,11 @@ HRESULT WINAPI async_void_get_Completed( IAsyncAction *iface, IAsyncActionComple
FIXME("iface %p, handler %p semi stub!\n", iface, handler); FIXME("iface %p, handler %p semi stub!\n", iface, handler);
EnterCriticalSection(&impl->cs);
if (impl->status == Closed) if (impl->status == Closed)
hr = E_ILLEGAL_METHOD_CALL; hr = E_ILLEGAL_METHOD_CALL;
*handler = (impl->handler != HANDLER_NOT_SET) ? impl->handler : NULL; *handler = (impl->handler != HANDLER_NOT_SET) ? impl->handler : NULL;
LeaveCriticalSection(&impl->cs);
return hr; return hr;
} }
...@@ -200,9 +212,11 @@ static HRESULT WINAPI async_void_info_get_Status( IAsyncInfo *iface, AsyncStatus ...@@ -200,9 +212,11 @@ static HRESULT WINAPI async_void_info_get_Status( IAsyncInfo *iface, AsyncStatus
TRACE("iface %p, status %p.\n", iface, status); TRACE("iface %p, status %p.\n", iface, status);
EnterCriticalSection(&impl->cs);
if (impl->status == Closed) if (impl->status == Closed)
hr = E_ILLEGAL_METHOD_CALL; hr = E_ILLEGAL_METHOD_CALL;
*status = impl->status; *status = impl->status;
LeaveCriticalSection(&impl->cs);
return hr; return hr;
} }
...@@ -214,10 +228,12 @@ static HRESULT WINAPI async_void_info_get_ErrorCode( IAsyncInfo *iface, HRESULT ...@@ -214,10 +228,12 @@ static HRESULT WINAPI async_void_info_get_ErrorCode( IAsyncInfo *iface, HRESULT
TRACE("iface %p, error_code %p.\n", iface, error_code); TRACE("iface %p, error_code %p.\n", iface, error_code);
EnterCriticalSection(&impl->cs);
if (impl->status == Closed) if (impl->status == Closed)
*error_code = hr = E_ILLEGAL_METHOD_CALL; *error_code = hr = E_ILLEGAL_METHOD_CALL;
else else
*error_code = impl->hr; *error_code = impl->hr;
LeaveCriticalSection(&impl->cs);
return hr; return hr;
} }
...@@ -229,10 +245,12 @@ static HRESULT WINAPI async_void_info_Cancel( IAsyncInfo *iface ) ...@@ -229,10 +245,12 @@ static HRESULT WINAPI async_void_info_Cancel( IAsyncInfo *iface )
TRACE("iface %p.\n", iface); TRACE("iface %p.\n", iface);
EnterCriticalSection(&impl->cs);
if (impl->status == Closed) if (impl->status == Closed)
hr = E_ILLEGAL_METHOD_CALL; hr = E_ILLEGAL_METHOD_CALL;
else if (impl->status == Started) else if (impl->status == Started)
impl->status = Canceled; impl->status = Canceled;
LeaveCriticalSection(&impl->cs);
return hr; return hr;
} }
...@@ -244,10 +262,16 @@ static HRESULT WINAPI async_void_info_Close( IAsyncInfo *iface ) ...@@ -244,10 +262,16 @@ static HRESULT WINAPI async_void_info_Close( IAsyncInfo *iface )
TRACE("iface %p.\n", iface); TRACE("iface %p.\n", iface);
EnterCriticalSection(&impl->cs);
if (impl->status == Started) if (impl->status == Started)
hr = E_ILLEGAL_STATE_CHANGE; hr = E_ILLEGAL_STATE_CHANGE;
else if (impl->status != Closed) else if (impl->status != Closed)
{
CloseThreadpoolWork(impl->async_run_work);
impl->async_run_work = NULL;
impl->status = Closed; impl->status = Closed;
}
LeaveCriticalSection(&impl->cs);
return hr; return hr;
} }
...@@ -270,11 +294,40 @@ static const struct IAsyncInfoVtbl async_void_info_vtbl = ...@@ -270,11 +294,40 @@ static const struct IAsyncInfoVtbl async_void_info_vtbl =
async_void_info_Close async_void_info_Close
}; };
HRESULT async_action_create( IAsyncAction **out ) static void CALLBACK async_void_run_cb(TP_CALLBACK_INSTANCE *instance, void *data, TP_WORK *work)
{
IAsyncAction *action = data;
struct async_void *impl = impl_from_IAsyncAction(action);
HRESULT hr;
hr = impl->callback(impl->invoker);
EnterCriticalSection(&impl->cs);
if (impl->status < Closed)
impl->status = FAILED(hr) ? Error : Completed;
impl->hr = hr;
if (impl->handler != NULL && impl->handler != HANDLER_NOT_SET)
{
IAsyncActionCompletedHandler*handler = impl->handler;
AsyncStatus status = impl->status;
impl->handler = NULL; /* Prevent concurrent invoke. */
LeaveCriticalSection(&impl->cs);
IAsyncActionCompletedHandler_Invoke(handler, action, status);
IAsyncActionCompletedHandler_Release(handler);
}
else LeaveCriticalSection(&impl->cs);
IAsyncAction_Release(action);
}
HRESULT async_action_create( IInspectable *invoker, async_action_callback callback, IAsyncAction **out )
{ {
struct async_void *impl; struct async_void *impl;
TRACE("out %p.\n", out); TRACE("invoker %p, callback %p, out %p.\n", invoker, callback, out);
if (!(impl = calloc(1, sizeof(*impl)))) if (!(impl = calloc(1, sizeof(*impl))))
{ {
...@@ -287,7 +340,23 @@ HRESULT async_action_create( IAsyncAction **out ) ...@@ -287,7 +340,23 @@ HRESULT async_action_create( IAsyncAction **out )
impl->ref = 1; impl->ref = 1;
impl->handler = HANDLER_NOT_SET; impl->handler = HANDLER_NOT_SET;
impl->status = Completed; impl->callback = callback;
impl->status = Started;
if (!(impl->async_run_work = CreateThreadpoolWork(async_void_run_cb, &impl->IAsyncAction_iface, NULL)))
{
free(impl);
return HRESULT_FROM_WIN32(GetLastError());
}
if (invoker) IInspectable_AddRef((impl->invoker = invoker));
InitializeCriticalSection(&impl->cs);
impl->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": async_action.cs");
/* AddRef to keep the obj alive in the callback. */
IAsyncAction_AddRef(&impl->IAsyncAction_iface);
SubmitThreadpoolWork(impl->async_run_work);
*out = &impl->IAsyncAction_iface; *out = &impl->IAsyncAction_iface;
TRACE("created %p\n", *out); TRACE("created %p\n", *out);
......
...@@ -69,9 +69,10 @@ struct vector_iids ...@@ -69,9 +69,10 @@ struct vector_iids
const GUID *view; const GUID *view;
}; };
typedef HRESULT (WINAPI *async_action_callback)( IInspectable *invoker );
typedef HRESULT (WINAPI *async_operation_inspectable_callback)( IInspectable *invoker, IInspectable **result ); typedef HRESULT (WINAPI *async_operation_inspectable_callback)( IInspectable *invoker, IInspectable **result );
HRESULT async_action_create( IAsyncAction **out ); HRESULT async_action_create( IInspectable *invoker, async_action_callback callback, IAsyncAction **out );
HRESULT async_operation_inspectable_create( const GUID *iid, IInspectable *invoker, async_operation_inspectable_callback callback, HRESULT async_operation_inspectable_create( const GUID *iid, IInspectable *invoker, async_operation_inspectable_callback callback,
IAsyncOperation_IInspectable **out ); IAsyncOperation_IInspectable **out );
......
...@@ -244,10 +244,15 @@ static HRESULT WINAPI session_set_AutoStopSilenceTimeout( ISpeechContinuousRecog ...@@ -244,10 +244,15 @@ static HRESULT WINAPI session_set_AutoStopSilenceTimeout( ISpeechContinuousRecog
return E_NOTIMPL; return E_NOTIMPL;
} }
static HRESULT WINAPI start_callback( IInspectable *invoker )
{
return S_OK;
}
static HRESULT WINAPI session_StartAsync( ISpeechContinuousRecognitionSession *iface, IAsyncAction **action ) static HRESULT WINAPI session_StartAsync( ISpeechContinuousRecognitionSession *iface, IAsyncAction **action )
{ {
FIXME("iface %p, action %p stub!\n", iface, action); FIXME("iface %p, action %p stub!\n", iface, action);
return async_action_create(action); return async_action_create(NULL, start_callback, action);
} }
static HRESULT WINAPI session_StartWithModeAsync( ISpeechContinuousRecognitionSession *iface, static HRESULT WINAPI session_StartWithModeAsync( ISpeechContinuousRecognitionSession *iface,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment