AsyncJITCompileWorker.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using ILRuntime.CLR.Method;
  7. namespace ILRuntime.Runtime.Intepreter.RegisterVM
  8. {
  9. class AsyncJITCompileWorker
  10. {
  11. AutoResetEvent evt = new AutoResetEvent(false);
  12. Queue<ILMethod> jobs = new Queue<ILMethod>();
  13. bool exit;
  14. Thread thread;
  15. public AsyncJITCompileWorker()
  16. {
  17. thread = new Thread(DoJob);
  18. thread.Name = "ILRuntime JIT Worker";
  19. thread.Start();
  20. }
  21. public void QueueCompileJob(ILMethod method)
  22. {
  23. if (exit)
  24. throw new NotSupportedException("Already disposed");
  25. lock (jobs)
  26. jobs.Enqueue(method);
  27. evt.Set();
  28. }
  29. public void Dispose()
  30. {
  31. exit = true;
  32. evt.Set ();
  33. }
  34. void DoJob()
  35. {
  36. while (!exit)
  37. {
  38. evt.WaitOne();
  39. while (jobs.Count > 0)
  40. {
  41. ILMethod m;
  42. lock (jobs)
  43. m = jobs.Dequeue();
  44. try
  45. {
  46. m.InitCodeBody(true);
  47. }
  48. catch(Exception ex)
  49. {
  50. string str = string.Format("Compile {0} failed\r\n{1}", m, ex);
  51. #if UNITY_5_5_OR_NEWER
  52. UnityEngine.Debug.LogError(str);
  53. #else
  54. Console.WriteLine(str);
  55. #endif
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }