pte_dspbios.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>PTE on DSP/BIOS</title></head><body><br><big><big><big><span style="font-weight: bold;">PTE on DSP/BIOS</span></big></big></big><meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8"><title></title><meta name="GENERATOR" content="OpenOffice.org 2.3 (Linux)">
  3. <style type="text/css">
  4. <!--
  5. @page { size: 8.5in 11in; margin: 0.79in }
  6. P { margin-bottom: 0.08in }
  7. H1 { margin-bottom: 0.08in }
  8. H1.western { font-family: "Helvetica"; font-size: 16pt }
  9. H1.cjk { font-family: "AR PL ShanHeiSun Uni"; font-size: 16pt }
  10. H1.ctl { font-family: "Tahoma"; font-size: 16pt }
  11. H2 { margin-bottom: 0.08in }
  12. H2.western { font-family: "Helvetica"; font-size: 14pt; font-style: italic }
  13. H2.cjk { font-family: "AR PL ShanHeiSun Uni"; font-size: 14pt; font-style: italic }
  14. H2.ctl { font-size: 14pt; font-style: italic }
  15. -->
  16. </style>
  17. <p style="margin-bottom: 0in;"><br>
  18. </p>
  19. <p style="margin-bottom: 0in;">The DSP/BIOS OSAL is relatively
  20. straightforward and maps relatively cleanly to the DSP/BIOS API. For
  21. instance, most mutex and semaphore operations are supported directly
  22. using the LCK and SEM API's. Specific details and exceptions are
  23. described below.</p>
  24. <h1 class="western">Threads</h1>
  25. <h2 class="western">Thread creation</h2>
  26. <p>OsThreadCreate allocates a dspbiosThreadData structure that
  27. contains semaphores used for joining to a thread, cancelling a thread
  28. and the initial priority requested by the user. A pointer to this
  29. structure is stored as a TLS value.
  30. </p>
  31. <p>OsThreadCreate must create threads in a suspended state. In
  32. DSP/BIOS this is done by setting the initial priority to zero. The
  33. initial priority is stored in the per thread control data. When
  34. OsThreadStart is called, it retrieves the initial priority from the
  35. control data attached to the thread and then sets the priority of the
  36. thread, which starts execution.</p>
  37. <h2 class="western">Thread termination</h2>
  38. <p>In order for pthread_join to wait for a thread, OsThreadWaitForEnd
  39. is called. Since DSP/BIOS doesn't have a explicit system call for
  40. this, we emulate it using a semaphore that is posted to when the
  41. thread exits. This semaphore is saved in the per thread control
  42. structure.</p>
  43. <h2 class="western">Thread cleanup</h2>
  44. <p style="margin-bottom: 0in;"><br>
  45. </p>
  46. <p style="margin-bottom: 0in;">The PTE library calls <font face="Courier New, monospace">OsThreadDelete</font>
  47. to signal to the OSAL that the resources for a task can be freed.
  48. For DSP/BIOS, this means a call to TSK_delete (task resources are <i>not</i><span style="font-style: normal;">
  49. automatically freed by DSP/BIOS when the thread exits).</span></p>
  50. <p style="margin-bottom: 0in; font-style: normal;"><br>
  51. </p>
  52. <p style="margin-bottom: 0in;"><font face="Courier New, monospace"><span style="font-style: normal;">OsThreadDelete</span></font><span style="font-style: normal;">
  53. will be called at a number of points. For attached threads, it will
  54. be called when </span><font face="Courier New, monospace"><span style="font-style: normal;">pthread_join</span></font><span style="font-style: normal;">
  55. is called and returns or when </span><font face="Courier New, monospace"><span style="font-style: normal;">pthread_detach</span></font><span style="font-style: normal;">
  56. is called for a terminated thread. For detached threads
  57. </span><font face="Courier New, monospace"><span style="font-style: normal;">OsThreadDelete</span></font><span style="font-style: normal;">
  58. will be called when the thread exits.</span></p>
  59. <p style="margin-bottom: 0in; font-style: normal;"><br>
  60. </p>
  61. <p style="margin-bottom: 0in;"><span style="font-style: normal;">The
  62. problem is that </span><font face="Courier New, monospace"><span style="font-style: normal;">TSK_delete</span></font><span style="font-style: normal;">
  63. naturally can not be called from the same context as the thread that
  64. we're trying to free. For the first two cases (</span><font face="Courier New, monospace"><span style="font-style: normal;">pthread_join</span></font><span style="font-style: normal;">
  65. and </span><font face="Courier New, monospace"><span style="font-style: normal;">pthread_detach</span></font><span style="font-style: normal;">)
  66. this is not a problem as these will always be called from another
  67. thread. However, for detached threads </span><font face="Courier New, monospace"><span style="font-style: normal;">OsThreadDelete</span></font><span style="font-style: normal;">
  68. will be called from the same context.</span></p>
  69. <p style="margin-bottom: 0in; font-style: normal;"><br>
  70. </p>
  71. <p style="margin-bottom: 0in;"><span style="font-style: normal;">To
  72. work around it, the DSP/BIOS OSAL includes a low priority that will
  73. clean up detached threads. If </span><font face="Courier New, monospace"><span style="font-style: normal;">OsThreadDelete</span></font><span style="font-style: normal;">
  74. detects that it is being called from the same context, it will post a
  75. message to a mailbox that the garbage collector thread is waiting on.
  76. The garbage collector thread will then call </span><font face="Courier New, monospace"><span style="font-style: normal;">TSK_delete</span></font><span style="font-style: normal;">
  77. for that thread, as well as to clean up any resources that were
  78. allocated when the thread was created.</span></p>
  79. <p style="margin-bottom: 0in;"><br>
  80. </p>
  81. <h1 class="western">Thread Local Storage</h1>
  82. <p style="margin-bottom: 0in;"><br>
  83. </p>
  84. <p style="margin-bottom: 0in;">TLS is implemented using the
  85. &#8220;environment&#8221; of DSP/BIOS threads. This allows a single value to
  86. be associated with a thread. The TLS helper routines are used to
  87. provide full TLS functionality; these routines allocate a structure
  88. that holds multiple TLS keys and values. The DSP/BIOS environment is
  89. used to hold a pointer to this structure.</p>
  90. <p style="margin-bottom: 0in;"><br>
  91. </p>
  92. <h1 class="western">Thread Cancellation</h1>
  93. <p style="margin-bottom: 0in;"><br>
  94. </p>
  95. <p style="margin-bottom: 0in;">Since DSP/BIOS does not natively
  96. provide a way to break out of blocked operations, this functionality
  97. is emulated using a cancellation semaphore that is stored in the per
  98. thread control data. When the user requests that a thread be
  99. canceled (i.e. <font face="Courier New, monospace">OsThreadCancel</font>
  100. is called) this semaphore is posted to. In
  101. <font face="Courier New, monospace">OsSemaphoreCancellablePend</font>,
  102. the cancellation semaphore as well as the user semaphore are polled
  103. (rather than blocking) and the routine returns if the cancellation
  104. semaphore is posted to.</p>
  105. </body></html>