Mailbox.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using System;
  18. using System.Collections.Concurrent;
  19. namespace Ryujinx.Audio.Renderer.Utils
  20. {
  21. /// <summary>
  22. /// A simple generic message queue for unmanaged types.
  23. /// </summary>
  24. /// <typeparam name="T">The target unmanaged type used</typeparam>
  25. public class Mailbox<T> : IDisposable where T : unmanaged
  26. {
  27. private BlockingCollection<T> _messageQueue;
  28. private BlockingCollection<T> _responseQueue;
  29. public Mailbox()
  30. {
  31. _messageQueue = new BlockingCollection<T>(1);
  32. _responseQueue = new BlockingCollection<T>(1);
  33. }
  34. public void SendMessage(T data)
  35. {
  36. _messageQueue.Add(data);
  37. }
  38. public void SendResponse(T data)
  39. {
  40. _responseQueue.Add(data);
  41. }
  42. public T ReceiveMessage()
  43. {
  44. return _messageQueue.Take();
  45. }
  46. public T ReceiveResponse()
  47. {
  48. return _responseQueue.Take();
  49. }
  50. public void Dispose()
  51. {
  52. Dispose(true);
  53. }
  54. protected virtual void Dispose(bool disposing)
  55. {
  56. if (disposing)
  57. {
  58. _messageQueue.Dispose();
  59. _responseQueue.Dispose();
  60. }
  61. }
  62. }
  63. }