/xbmc/visualizations/Vortex/angelscript/docs/doxygen/source/doc_adv_timeout.h

http://github.com/xbmc/xbmc · C++ Header · 101 lines · 0 code · 0 blank · 101 comment · 0 complexity · d468e0006054987b449d958b22dc97b1 MD5 · raw file

  1. /**
  2. \page doc_adv_timeout Timeout long running scripts
  3. To prevent long running scripts to freeze the application it may be necessary
  4. to add a way to timeout the execution. This article presents two different
  5. ways to do so.
  6. \section doc_adv_timeout_1 With the line callback
  7. The line callback feature can be used to perform some special treatment
  8. during execution of the scripts. The callback is called for every script
  9. statement, which for example makes it possible to verify if the script has
  10. executed for too long time and if so suspend the execution to be resumed at
  11. a later time.
  12. Before calling the context's \ref asIScriptContext::Execute "Execute" method,
  13. set the callback function like so:
  14. \code
  15. int ExecuteScriptWithTimeOut(asIScriptContext *ctx)
  16. {
  17. // Define the timeout as 1 second
  18. DWORD timeOut = timeGetTime() + 1000;
  19. // Set up the line callback that will timout the script
  20. ctx->SetLineCallback(asFUNCTION(LineCallback), &timeOut, asCALL_CDECL);
  21. // Execute the script
  22. int status = ctx->Execute();
  23. // If the status is asEXECUTION_SUSPENDED the script can
  24. // be resumed by calling this function again.
  25. return status;
  26. }
  27. // The line callback function is called by the VM for each statement that is executed
  28. void LineCallback(asIScriptContext *ctx, DWORD *timeOut)
  29. {
  30. // If the time out is reached we suspend the script
  31. if( *timeOut < timeGetTime() )
  32. ctx->Suspend();
  33. }
  34. \endcode
  35. Take a look at the sample \ref doc_samples_events to
  36. see this working.
  37. \section doc_adv_timeout_2 With a secondary thread
  38. A second thread can be set up to suspend the execution after the timeout. This thread
  39. can then be put to sleep so that it doesn't impact performance during the execution.
  40. When the thread wakes up it should call the context's \ref asIScriptContext::Suspend "Suspend" method.
  41. The below shows some possible code for doing this. Note that the code for setting up
  42. the thread is fictive, as this is different for each target OS.
  43. \code
  44. // The variables that are shared between the threads
  45. asIScriptContext *threadCtx;
  46. int threadId;
  47. // This function will be executed in the secondary thread
  48. void SuspendThread()
  49. {
  50. // Put the thread to sleep until the timeout (1 second)
  51. Sleep(1000);
  52. // When we wake-up we call the context's Suspend method
  53. ctx->Suspend();
  54. }
  55. // This function sets up the timeout thread and executes the script
  56. int ExecuteScriptWithTimeOut(asIScriptContext *ctx)
  57. {
  58. // Set the shared context pointer before creating the thread
  59. threadCtx = ctx;
  60. // Create the thread that will immediately go to sleep
  61. threadId = CreateThread(SuspendThread);
  62. // Execute the script
  63. int status = ctx->Execute();
  64. // Destroy the secondary thread before releasing the context
  65. DestroyThread(threadId);
  66. // Clear the global variables
  67. threadId = 0;
  68. threadCtx = 0;
  69. // If the status is asEXECUTION_SUSPENDED the script can
  70. // be resumed by calling this function again.
  71. return status;
  72. }
  73. \endcode
  74. Observe that this way of doing it is safe even if the AngelScript library has been
  75. built without \ref doc_adv_multithread "multithread support".
  76. */