[PATCH v4 3/4] windows.media.speech: Implement concurrency in IAsyncOperation.

Bernhard Kölbl besentv at gmail.com
Mon Apr 18 16:42:23 CDT 2022


Signed-off-by: Bernhard Kölbl <besentv at gmail.com>
---
 dlls/windows.media.speech/async.c        | 123 ++++++++++++++++++++++-
 dlls/windows.media.speech/private.h      |   4 +-
 dlls/windows.media.speech/recognizer.c   |   7 +-
 dlls/windows.media.speech/tests/speech.c |  22 ++--
 4 files changed, 141 insertions(+), 15 deletions(-)

diff --git a/dlls/windows.media.speech/async.c b/dlls/windows.media.speech/async.c
index cf0da23e493..66cba46d85b 100644
--- a/dlls/windows.media.speech/async.c
+++ b/dlls/windows.media.speech/async.c
@@ -40,7 +40,13 @@ struct async_operation
     LONG ref;
 
     IAsyncOperationCompletedHandler_IInspectable *handler;
+    IInspectable *result;
 
+    async_operation_worker worker;
+    TP_WORK *async_run_work;
+    IInspectable *invoker;
+
+    CRITICAL_SECTION cs;
     AsyncStatus status;
     HRESULT hr;
 };
@@ -95,9 +101,14 @@ static ULONG WINAPI async_operation_Release( IAsyncOperation_IInspectable *iface
     {
         IAsyncInfo_Close(&impl->IAsyncInfo_iface);
 
+        if (impl->invoker)
+            IInspectable_Release(impl->invoker);
         if (impl->handler && impl->handler != HANDLER_NOT_SET)
             IAsyncOperationCompletedHandler_IInspectable_Release(impl->handler);
+        if (impl->result)
+            IInspectable_Release(impl->result);
 
+        DeleteCriticalSection(&impl->cs);
         free(impl);
     }
 
@@ -130,6 +141,7 @@ static HRESULT WINAPI async_operation_put_Completed( IAsyncOperation_IInspectabl
 
     TRACE("iface %p, handler %p.\n", iface, handler);
 
+    EnterCriticalSection(&impl->cs);
     if (impl->handler == HANDLER_NOT_SET)
     {
         /*
@@ -140,19 +152,25 @@ static HRESULT WINAPI async_operation_put_Completed( IAsyncOperation_IInspectabl
 
         if (handler)
         {
-            if (impl->status != Started)
+            if (impl->status != Started && impl->status != Closed)
             {
                 IAsyncOperation_IInspectable *operation = &impl->IAsyncOperation_IInspectable_iface;
                 AsyncStatus status = impl->status;
 
                 IAsyncOperation_IInspectable_AddRef(operation);
                 IAsyncOperationCompletedHandler_IInspectable_AddRef(handler);
+                LeaveCriticalSection(&impl->cs);
 
                 IAsyncOperationCompletedHandler_IInspectable_Invoke(handler, operation, status);
 
                 IAsyncOperationCompletedHandler_IInspectable_Release(handler);
                 IAsyncOperation_IInspectable_Release(operation);
             }
+            else
+            {
+                IAsyncOperationCompletedHandler_IInspectable_AddRef((impl->handler = handler));
+                LeaveCriticalSection(&impl->cs);
+            }
         }
 
         return S_OK;
@@ -163,6 +181,7 @@ static HRESULT WINAPI async_operation_put_Completed( IAsyncOperation_IInspectabl
         hr = E_ILLEGAL_DELEGATE_ASSIGNMENT;
     else
         hr = E_UNEXPECTED;
+    LeaveCriticalSection(&impl->cs);
 
     return hr;
 }
@@ -175,10 +194,12 @@ static HRESULT WINAPI async_operation_get_Completed( IAsyncOperation_IInspectabl
 
     FIXME("iface %p, handler %p semi stub!\n", iface, handler);
 
+    EnterCriticalSection(&impl->cs);
     if (impl->handler != HANDLER_NOT_SET && impl->status == Closed)
         hr = E_ILLEGAL_METHOD_CALL;
     else
         hr = S_OK;
+    LeaveCriticalSection(&impl->cs);
 
     *handler = NULL; /* Hanlder *seems* to always be NULL from the tests. */
     return hr;
@@ -192,10 +213,18 @@ static HRESULT WINAPI async_operation_GetResults( IAsyncOperation_IInspectable *
 
     TRACE("iface %p, results %p.\n", iface, results);
 
+    EnterCriticalSection(&impl->cs);
     if (impl->status == Closed)
         hr = E_ILLEGAL_METHOD_CALL;
+    else if (!impl->result)
+        hr = E_UNEXPECTED;
     else
+    {
+        *results = impl->result;
+        impl->result = NULL; /* NOTE: AsyncOperation gives up it's reference to result here! */
         hr = S_OK;
+    }
+    LeaveCriticalSection(&impl->cs);
 
     return hr;
 }
@@ -237,12 +266,15 @@ static HRESULT WINAPI async_operation_info_get_Status( IAsyncInfo *iface, AsyncS
 
     TRACE("iface %p, status %p.\n", iface, status);
 
+    EnterCriticalSection(&impl->cs);
     if (impl->status == Closed)
         hr = E_ILLEGAL_METHOD_CALL;
     else
         hr = S_OK;
 
     *status = impl->status;
+    LeaveCriticalSection(&impl->cs);
+
     return hr;
 }
 
@@ -251,7 +283,10 @@ static HRESULT WINAPI async_operation_info_get_ErrorCode( IAsyncInfo *iface, HRE
     struct async_operation *impl = impl_from_IAsyncInfo(iface);
     TRACE("iface %p, error_code %p.\n", iface, error_code);
 
+    EnterCriticalSection(&impl->cs);
     *error_code = impl->hr;
+    LeaveCriticalSection(&impl->cs);
+
     return S_OK;
 }
 
@@ -263,10 +298,12 @@ static HRESULT WINAPI async_operation_info_Cancel( IAsyncInfo *iface )
     TRACE("iface %p.\n", iface);
     hr = S_OK;
 
+    EnterCriticalSection(&impl->cs);
     if (impl->status == Closed)
         hr = E_ILLEGAL_METHOD_CALL;
     else if (impl->status == Started)
         impl->status = Canceled;
+    LeaveCriticalSection(&impl->cs);
 
     return hr;
 }
@@ -277,7 +314,16 @@ static HRESULT WINAPI async_operation_info_Close( IAsyncInfo *iface )
 
     TRACE("iface %p.\n", iface);
 
+    EnterCriticalSection(&impl->cs);
+    if (impl->async_run_work)
+    {
+        CloseThreadpoolWork(impl->async_run_work);
+        impl->async_run_work = NULL;
+    }
+
     impl->status = Closed;
+    LeaveCriticalSection(&impl->cs);
+
     return S_OK;
 }
 
@@ -299,7 +345,65 @@ static const struct IAsyncInfoVtbl async_operation_info_vtbl =
     async_operation_info_Close
 };
 
-HRESULT async_operation_create( const GUID *iid, IAsyncOperation_IInspectable **out )
+static void CALLBACK async_run_cb(TP_CALLBACK_INSTANCE *instance, void *data, TP_WORK *work)
+{
+    IAsyncOperation_IInspectable *operation = data;
+    struct async_operation *impl = impl_from_IAsyncOperation_IInspectable(operation);
+    IInspectable *result = NULL, *invoker = NULL;
+    async_operation_worker worker;
+    HRESULT hr;
+
+    EnterCriticalSection(&impl->cs);
+    if (impl->status == Canceled)
+        goto abort;
+
+    impl->status = Started;
+
+    invoker = impl->invoker;
+    worker = impl->worker;
+    LeaveCriticalSection(&impl->cs);
+
+    hr = worker(invoker, &result);
+
+    EnterCriticalSection(&impl->cs);
+    if (FAILED(hr))
+        impl->status = Error;
+    else
+        impl->status = Completed;
+
+    impl->result = result;
+    impl->hr = hr;
+
+    if (impl->status == Canceled)
+        goto abort;
+
+    if (impl->handler != NULL && impl->handler != HANDLER_NOT_SET)
+    {
+        IAsyncOperationCompletedHandler_IInspectable *handler = impl->handler;
+        AsyncStatus status = impl->status;
+
+        IAsyncOperation_IInspectable_AddRef(operation);
+        IAsyncOperationCompletedHandler_IInspectable_AddRef(handler);
+        LeaveCriticalSection(&impl->cs);
+
+        IAsyncOperationCompletedHandler_IInspectable_Invoke(handler, operation, status);
+
+        IAsyncOperationCompletedHandler_IInspectable_Release(handler);
+        IAsyncOperation_IInspectable_Release(operation);
+    }
+    else
+        LeaveCriticalSection(&impl->cs);
+
+    IAsyncOperation_IInspectable_Release(operation);
+    return;
+
+abort:
+    impl->hr = E_FAIL;
+    LeaveCriticalSection(&impl->cs);
+    IAsyncOperation_IInspectable_Release(operation);
+}
+
+HRESULT async_operation_create( const GUID *iid, IInspectable *invoker, async_operation_worker worker, IAsyncOperation_IInspectable **out )
 {
     struct async_operation *impl;
     HRESULT hr;
@@ -318,8 +422,23 @@ HRESULT async_operation_create( const GUID *iid, IAsyncOperation_IInspectable **
     impl->ref = 1;
 
     impl->handler = HANDLER_NOT_SET;
+    impl->worker = worker;
     impl->status = Closed;
 
+    IAsyncOperation_IInspectable_AddRef(&impl->IAsyncOperation_IInspectable_iface); /* AddRef to keep the obj alive in the callback. */
+    if (!(impl->async_run_work = CreateThreadpoolWork(async_run_cb, &impl->IAsyncOperation_IInspectable_iface, NULL)))
+    {
+        hr = HRESULT_FROM_WIN32(GetLastError());
+        goto error;
+    }
+
+    if (invoker) IInspectable_AddRef((impl->invoker = invoker));
+
+    InitializeCriticalSection(&impl->cs);
+    impl->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": async_operation.cs");
+
+    SubmitThreadpoolWork(impl->async_run_work);
+
     *out = &impl->IAsyncOperation_IInspectable_iface;
     TRACE("created %p\n", *out);
     return S_OK;
diff --git a/dlls/windows.media.speech/private.h b/dlls/windows.media.speech/private.h
index 1cf61c51f1e..8eb0570c170 100644
--- a/dlls/windows.media.speech/private.h
+++ b/dlls/windows.media.speech/private.h
@@ -69,7 +69,9 @@ struct vector_iids
     const GUID *view;
 };
 
-HRESULT async_operation_create( const GUID *iid, IAsyncOperation_IInspectable **out );
+typedef HRESULT (WINAPI *async_operation_worker)( IInspectable *invoker, IInspectable **result );
+
+HRESULT async_operation_create( const GUID *iid, IInspectable *invoker, async_operation_worker worker, IAsyncOperation_IInspectable **out );
 
 HRESULT typed_event_handlers_append( struct list *list, ITypedEventHandler_IInspectable_IInspectable *handler, EventRegistrationToken *token );
 HRESULT typed_event_handlers_remove( struct list *list, EventRegistrationToken *token );
diff --git a/dlls/windows.media.speech/recognizer.c b/dlls/windows.media.speech/recognizer.c
index b16ecb24641..5cffe1172e3 100644
--- a/dlls/windows.media.speech/recognizer.c
+++ b/dlls/windows.media.speech/recognizer.c
@@ -348,12 +348,17 @@ static HRESULT WINAPI recognizer_get_UIOptions( ISpeechRecognizer *iface, ISpeec
     return E_NOTIMPL;
 }
 
+static HRESULT WINAPI compile_worker( IInspectable *invoker, IInspectable **result )
+{
+    return S_OK;
+}
+
 static HRESULT WINAPI recognizer_CompileConstraintsAsync( ISpeechRecognizer *iface,
                                                           IAsyncOperation_SpeechRecognitionCompilationResult **operation )
 {
     IAsyncOperation_IInspectable **value = (IAsyncOperation_IInspectable **)operation;
     FIXME("iface %p, operation %p semi-stub!\n", iface, operation);
-    return async_operation_create(&IID_IAsyncOperation_SpeechRecognitionCompilationResult, value);
+    return async_operation_create(&IID_IAsyncOperation_SpeechRecognitionCompilationResult, NULL, compile_worker, value);
 }
 
 static HRESULT WINAPI recognizer_RecognizeAsync( ISpeechRecognizer *iface,
diff --git a/dlls/windows.media.speech/tests/speech.c b/dlls/windows.media.speech/tests/speech.c
index 03608842cd7..7b05bcdcd7c 100644
--- a/dlls/windows.media.speech/tests/speech.c
+++ b/dlls/windows.media.speech/tests/speech.c
@@ -1009,11 +1009,11 @@ static void test_SpeechRecognizer(void)
         CloseHandle(compilation_handler.event_finished);
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_put_Completed(operation, NULL);
-        todo_wine ok(hr == E_ILLEGAL_DELEGATE_ASSIGNMENT, "Got unexpected hr %#lx.\n", hr);
+        ok(hr == E_ILLEGAL_DELEGATE_ASSIGNMENT, "Got unexpected hr %#lx.\n", hr);
 
         handler = (void*)0xdeadbeef;
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_get_Completed(operation, &handler);
-        todo_wine ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
+        ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
         ok(handler == NULL || broken(handler != NULL), "Handler had value %p.\n", handler); /* Broken on Win10 1507 x32... */
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_GetResults(operation, &compilation_result);
@@ -1032,7 +1032,7 @@ static void test_SpeechRecognizer(void)
         }
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_GetResults(operation, &compilation_result);
-        todo_wine ok(hr == E_UNEXPECTED, "Got unexpected hr %#lx.\n", hr);
+        ok(hr == E_UNEXPECTED, "Got unexpected hr %#lx.\n", hr);
 
         hr = IAsyncOperation_SpeechRecognitionCompilationResult_QueryInterface(operation, &IID_IAsyncInfo, (void **)&info);
         ok(hr == S_OK, "Got unexpected hr %#lx.\n", hr);
@@ -1052,16 +1052,16 @@ static void test_SpeechRecognizer(void)
 
         async_status = 0xdeadbeef;
         hr = IAsyncInfo_get_Status(info, &async_status);
-        todo_wine ok(hr == S_OK, "IAsyncInfo_get_Status failed, hr %#lx.\n", hr);
-        todo_wine ok(async_status == Completed, "Status was %#x.\n", async_status);
+        ok(hr == S_OK, "IAsyncInfo_get_Status failed, hr %#lx.\n", hr);
+        ok(async_status == Completed, "Status was %#x.\n", async_status);
 
         hr = IAsyncInfo_Cancel(info);
-        todo_wine ok(hr == S_OK, "IAsyncInfo_Cancel failed, hr %#lx.\n", hr);
+        ok(hr == S_OK, "IAsyncInfo_Cancel failed, hr %#lx.\n", hr);
 
         async_status = 0xdeadbeef;
         hr = IAsyncInfo_get_Status(info, &async_status);
-        todo_wine ok(hr == S_OK, "IAsyncInfo_get_Status failed, hr %#lx.\n", hr);
-        todo_wine ok(async_status == Completed, "Status was %#x.\n", async_status);
+        ok(hr == S_OK, "IAsyncInfo_get_Status failed, hr %#lx.\n", hr);
+        ok(async_status == Completed, "Status was %#x.\n", async_status);
 
         hr = IAsyncInfo_Close(info);
         ok(hr == S_OK, "IAsyncInfo_Close failed, hr %#lx.\n", hr);
@@ -1107,11 +1107,11 @@ static void test_SpeechRecognizer(void)
 
         async_status = 0xdeadbeef;
         hr = IAsyncInfo_get_Status(info, &async_status);
-        todo_wine ok(hr == S_OK, "IAsyncInfo_get_Status failed, hr %#lx.\n", hr);
-        todo_wine ok(async_status == Completed, "Status was %#x.\n", async_status);
+        ok(hr == S_OK, "IAsyncInfo_get_Status failed, hr %#lx.\n", hr);
+        ok(async_status == Completed, "Status was %#x.\n", async_status);
 
         ref = IAsyncInfo_Release(info);
-        ok(ref == 1, "Got unexpected ref %lu.\n", ref);
+        ok(ref <= 1, "Got unexpected ref %lu.\n", ref);
         ref = IAsyncOperation_SpeechRecognitionCompilationResult_Release(operation);
         ok(!ref, "Got unexpected ref %lu.\n", ref);
 
-- 
2.35.1




More information about the wine-devel mailing list