AkSyncCaller.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. // AkSyncLoader.h
  21. /// \file
  22. /// Class for synchronous calls of asynchronous models
  23. #ifndef _AK_SYNC_CALLER_H_
  24. #define _AK_SYNC_CALLER_H_
  25. #include <AK/Tools/Common/AkPlatformFuncs.h>
  26. namespace AK
  27. {
  28. namespace SoundEngine
  29. {
  30. /// AkSyncLoader: Init to create a sync event, call the asynchronous method, passing
  31. /// it the address of this object as the cookie, then call Wait.
  32. class AkSyncCaller
  33. {
  34. public:
  35. #if defined(AK_SUPPORT_THREADS)
  36. /// Initialize.
  37. AKRESULT Init()
  38. {
  39. if ( AKPLATFORM::AkCreateEvent( m_hEvent ) != AK_Success )
  40. {
  41. AKASSERT( !"Could not create synchronization event" );
  42. return AK_Fail;
  43. }
  44. return AK_Success;
  45. }
  46. /// Wait until the async function calls its callback.
  47. AKRESULT Wait( AKRESULT in_eResult )
  48. {
  49. if ( in_eResult != AK_Success )
  50. {
  51. AKPLATFORM::AkDestroyEvent( m_hEvent );
  52. return in_eResult;
  53. }
  54. // Task queueing successful. Block until completion.
  55. AKPLATFORM::AkWaitForEvent( m_hEvent );
  56. AKPLATFORM::AkDestroyEvent( m_hEvent );
  57. return m_eResult;
  58. }
  59. /// Call this from callback to release blocked thread.
  60. inline void Done() { AKPLATFORM::AkSignalEvent( m_hEvent ); }
  61. #else
  62. AKRESULT Init() { m_bIsDone = false; return AK_Success;
  63. }
  64. inline bool IsDone() { return m_bIsDone; }
  65. inline void Done() { m_bIsDone = true; }
  66. #endif
  67. AKRESULT m_eResult; ///< Operation result
  68. private:
  69. #if defined(AK_SUPPORT_THREADS)
  70. AkEvent m_hEvent; ///< Sync event
  71. #else
  72. bool m_bIsDone;
  73. #endif
  74. };
  75. }
  76. }
  77. #endif // _AK_SYNC_CALLER_H_