home *** CD-ROM | disk | FTP | other *** search
/ C/C++ User's Journal & Wi…eveloper's Journal Tools / C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso / sysembed / thread.edh < prev    next >
Encoding:
Text File  |  1995-03-30  |  11.9 KB  |  348 lines

  1. [ 5. Real-Time Scheduling]
  2.                     EMBEDDED DOS THREAD SCHEDULING
  3. ════════════════════════════════════════════════════════════════════════
  4. The basic unit of asynchronous execution in the Embedded DOS system is
  5. the thread, and is implemented in the kernel with the thread object.
  6. Threads may be allocated and deallocated at any time, including interrupt
  7. time.  The kernel schedules the execution of a thread whenever the CPU
  8. becomes free to execute a thread, and a thread is executable.  Threads
  9. may voluntarily release control over the processor when they wait on
  10. events or mutexes (see semaphores).  They also involuntarily release
  11. control of the processor when a timer interrupt occurs and the kernel
  12. decides to give CPU cycles to another thread.
  13.  
  14. A thread's operating context consists simply of an initial stack segment
  15. with room to save the entire programmable register set of the CPU.  For
  16. 8088, 8086, 80188, 80186, 80288, and 80286 processors, this includes the
  17. following registers:
  18.  
  19.      AX, BX, CX, DX, SI, DI, DS, ES, BP, CS, IP, SS, SP, FL
  20.  
  21. One of the more typical uses of the thread object in the system is as a
  22. source of task-time control in an interrupt service routine (ISR).  For
  23. example, the disk driver's BPB aging timer expiration routine receives
  24. control from the kernel at interrupt time, schedules a thread for
  25. execution to handle the timeout, and then returns control to the caller
  26. in parallel.
  27.  
  28. 1.  AllocateThread
  29.  
  30. A thread object is created with the AllocateThread kernel                                              AllocateThread
  31. function.  Once the thread is allocated, it is automatically and
  32. independently scheduled for execution by the kernel until it is
  33. deallocated or aborted.
  34.  
  35. Thread allocation is a lightweight operation.  It is acceptable
  36. to create a thread at interrupt time to delay the bulk of the
  37. work to task time.  The kernel immediately schedules the new
  38. thread for execution, so ISRs must first perform any interrupt
  39. cleanup operations before allocating the new thread.  Failure to
  40. re-arm the 8259 before allocating a new thread in interrupt
  41. context could result in an interval of up to 55ms during which
  42. interrupts would be masked (this interval is based on a hardware
  43. timer timebase of 18.2 ticks/second for the typical PC
  44. implementation; the actual number may vary, depending on the OEM
  45. adaptation).
  46.  
  47. The parent's DS and ES general purpose register set context is
  48. copied to the newly-allocated thread's context, so that the new
  49. thread starts running with the same values in its general
  50. register set as the parent.  Naturally, the kernel allocates a
  51. different stack for the new thread, so the SS and SP registers
  52. are different.
  53.  
  54. This short form thread allocator automatically allocates some
  55. kernel pool for the thread's stack, and initializes the thread's
  56. priority to THREAD_PRIORITY_DEFAULT, or 16384.
  57.  
  58. Upon return, the macro instruction call clears the carry flag if
  59. the operation was successful, and sets it if the operation was
  60. not performed.
  61.  
  62. Assembly Language Format:
  63.  
  64.      mov  ax, OFFSET CGROUP:TargetLabel
  65.      mov  cx, CGROUP
  66.      mov  dl, SYS_ALLOCATE_THREAD
  67.      int  2dh                   ; (AX) = new thread handle.
  68.      jc   Failure
  69.  
  70. Macro Instruction Format:
  71.  
  72. label     AllocateThread <TargetLabel>
  73.  
  74. Portable Request Format:
  75.  
  76. STATUS AllocateThread(
  77.      IN VOID far (*TargetLabel)()
  78.      );
  79.  
  80. Parameters:
  81.  
  82.      TargetLabel - Specifies far address of the label or function               ___________
  83.           that the new thread will begin executing at.  For the
  84.           Macro Instruction format only, the label is assumed to
  85.           be relative to the parent's CS register.  The C-language
  86.           interface requires a FAR pointer to a function.
  87.  
  88. 2.  AllocateThreadLong
  89.  
  90. A higher degree of control over thread allocation is accomplished
  91. with the long form kernel function, AllocateThreadLong.  This                                              AllocateThrea
  92. function allows the caller to specify the priority at which the
  93. thread is to start running, and an optional stack segment to be
  94. used for the thread.  The stack segment must be at least 1Kb in
  95. size.  Once the thread is allocated, it is automatically and
  96. independently scheduled for execution by the kernel until it is
  97. deallocated or aborted.
  98.  
  99. Upon return, the macro instruction call clears the carry flag if
  100. the operation was successful, and sets it if the operation was
  101. not performed.
  102.  
  103. Assembly Language Format:
  104.  
  105.      mov  ax, OFFSET CGROUP:TargetLabel
  106.      mov  cx, CGROUP
  107.      mov  bx, <Priority>        ; (BX) = thread's initial priority.
  108.      mov  es, SEG Stack         ; (ES) = stack segment to use.
  109.      mov  dl, SYS_ALLOCATE_THREAD_LONG
  110.      int  2dh                   ; (AX) = new thread handle.
  111.      jc   Failure
  112.  
  113. Macro Instruction Format:
  114.  
  115.      none.
  116.  
  117. Portable Request Format:
  118.  
  119. STATUS AllocateThreadLong(
  120.      IN VOID far (*TargetLabel)(),
  121.      IN USHORT StackSegment,
  122.      IN USHORT Priority
  123.      );
  124.  
  125. Parameters:
  126.  
  127.      TargetLabel - Specifies the far address of the label or
  128.           function at which the new thread will begin executing.
  129.  
  130.      StackSegment - A 16-bit segment value that specifies the
  131.           segment address of a 1K stack to be used by the thread.
  132.           If the specified value is zero, then a stack will be
  133.           automatically allocated from the kernel's memory pool,
  134.           as is the case with AllocateThread.
  135.  
  136.      Priority - A 16-bit value that specifies the initial
  137.           scheduling priority for the new thread.  Higher values
  138.           have more urgent priority than lower values.  By
  139.           definition, The AllocateThread allocator creates
  140.           threads with a priority equal to THREAD_PRIORITY_DEFAULT.
  141.           The following priority values are prearchitected:
  142.  
  143.           THREAD_PRIORITY_LOW - Lowest possible priority.
  144.           THREAD_PRIORITY_DEFAULT - The default priority.
  145.           THREAD_PRIORITY_HI - The highest priority.
  146.           THREAD_PRIORITY_RESERVED - The lowest reserved priority.
  147.  
  148. 3.  DeallocateThread
  149.  
  150. A thread object is removed from the system with the
  151. DeallocateThread kernel function.  Once the thread is
  152. deallocated, its object is returned to the system to be recycled,
  153. and another thread is scheduled for execution.  If no other
  154. threads in the system are available for execution, then the
  155. scheduler executes the idle loop until an interrupt routine is
  156. executed which causes a new thread to be allocated or a blocked
  157. thread to be released.
  158.  
  159. A thread may only remove itself from the system with this
  160. function.  A thread may remove another thread in the system
  161. through the AbortThread function, although this function must be                      AbortThread
  162. used with care as it does not clean-up acquired resources.
  163.  
  164. Assembly Language Format:
  165.  
  166.      mov  dl, SYS_DEALLOCATE_THREAD
  167.      int  2dh                   ; never returns (current thread dies).
  168.  
  169. Macro Instruction Format:
  170.  
  171. label     DeallocateThread
  172.  
  173. Portable Request Format:
  174.  
  175. STATUS DeallocateThread();
  176.  
  177. Parameters:
  178.  
  179.      none.
  180.  
  181. 4.  AbortThread
  182.  
  183. A thread may remove another thread object in the system with the
  184. AbortThread kernel function.  The kernel does not perform cleanup
  185. with regard to resources held by the target thread, including but
  186. not limited to pool, events, mutexes, timers, or spinlocks.
  187.  
  188. A thread may remove itself from the system with this function by
  189. specifying a thread handle equal to zero.  This form has
  190. identical results to calling the DeallocateThread function.
  191.  
  192. Upon return, the macro instruction call clears the carry flag if
  193. the operation was successful, and sets it if the operation was
  194. not performed.
  195.  
  196. Assembly Language Format:
  197.  
  198.      mov  dl, SYS_ABORT_THREAD
  199.      mov  ax, <ThreadHandle>    ; (AX) = handle of thread to abort.
  200.      int  2dh                   ; destroys target thread.
  201.      jc   Failure
  202.  
  203. Macro Instruction Format:
  204.  
  205.      none.
  206.  
  207. Portable Request Format:
  208.  
  209. STATUS AbortThread(
  210.      IN HANDLE ThreadHandle
  211.      );
  212.  
  213. Parameters:
  214.  
  215.      ThreadHandle - A 16-bit handle to a thread object as
  216.           returned by the AllocateThread or AllocateThreadLong
  217.           kernel functions.  If the specified value is zero, then
  218.           the current thread aborts itself.
  219.  
  220. 5.  PrioritizeThread
  221.  
  222. A thread may change its priority with the PrioritizeThread kernel
  223. function.  If a thread lowers its priority, then the kernel
  224. automatically performs a rescheduling to allow other threads with
  225. potentially higher priorities to run.
  226.  
  227. Assembly Language Format:
  228.  
  229.      mov  dl, SYS_PRIORITIZE_THREAD
  230.      mov  ax, <ThreadHandle>    ; (AX) = thread handle.
  231.      mov  cx, <Priority>        ; (CX) = new thread priority.
  232.      int  2dh                   ; change the priority.
  233.      jc   Failure
  234.  
  235. Macro Instruction Format:
  236.  
  237.      none.
  238.  
  239. Portable Request Format:
  240.  
  241. STATUS PrioritizeThread(
  242.      IN HANDLE ThreadHandle,
  243.      IN USHORT Priority
  244.      );
  245.  
  246. Parameters:
  247.  
  248.      ThreadHandle - A 16-bit handle to a thread object as
  249.           returned by the AllocateThread or AllocateThreadLong
  250.           kernel functions.  If the specified value is zero, then
  251.           the current thread's priority is adjusted.
  252.  
  253.      Priority - A 16-bit handle to a thread object as returned by
  254.           the AllocateThread or AllocateThreadLong kernel
  255.           functions.  If the specified value is zero, then the
  256.           current thread's priority is adjusted.
  257.  
  258. 6.  EnterCriticalSection
  259.  
  260. A thread may disable context switching with the
  261. EnterCriticalSection kernel function.  The kernel allows nesting
  262. of EnterCriticalSection calls and counts them as the number of
  263. reasons why round-robin reschedulings within a priority should
  264. not take place.  The LeaveCriticalSection decrements this counter.
  265.  
  266. After the EnterCriticalSection function returns, context
  267. switching is disabled.  This is useful when manipulating data or
  268. hardware that cannot be shared among tasks, but that is under
  269. both task-time control and interrupt-time control.
  270.  
  271. Assembly Language Format:
  272.  
  273.      mov  dl, SYS_ENTER_CRITICAL_SECTION
  274.      int  2dh                   ; go non-preemptive.
  275.  
  276. Macro Instruction Format:
  277.  
  278.      none.
  279.  
  280. Portable Request Format:
  281.  
  282. STATUS EnterCriticalSection();
  283.  
  284. Parameters:
  285.  
  286.      none.
  287.  
  288. 7.  LeaveCriticalSection
  289.  
  290. A thread may reenable context switching with the
  291. EnterCriticalSection kernel function.  The kernel allows nesting
  292. of EnterCriticalSection calls and counts them as the number of
  293. reasons why round-robin reschedulings within a priority should
  294. not take place.
  295.  
  296. The LeaveCriticalSection decrements this counter, keeping context
  297. switching disabled until the EnterCriticalSection nesting level
  298. is zero.  When this happens, a forced rescheduling occurs.
  299.  
  300. Assembly Language Format:
  301.  
  302.      mov  dl, SYS_LEAVE_CRITICAL_SECTION
  303.      int  2dh                   ; allow preemption.
  304.  
  305. Macro Instruction Format:
  306.  
  307.      none.
  308.  
  309. Portable Request Format:
  310.  
  311. STATUS LeaveCriticalSection();
  312.  
  313. Parameters:
  314.  
  315.      none.
  316.  
  317. 8.  PassTimeSlice
  318.  
  319. A thread may cause a forced rescheduling to occur with the
  320. PassTimeSlice kernel function.  Regardless of the critical
  321. section level, this function immediately saves the state of the
  322. current thread, and looks for the next highest priority thread in
  323. the system, which may be the same thread.
  324.  
  325. The reader should be advised that calling PassTimeSlice while the
  326. critical section level is non-zero will possibly result in
  327. another thread gaining control with preemption disabled.  If the
  328. newly-executing thread is not prepared to decrement the critical
  329. section level on behalf of the first thread, or if it is not
  330. prepared to issue its own call to PassTimeSlice, the original
  331. thread might not again receive control.
  332.  
  333. Assembly Language Format:
  334.  
  335.      mov  dl, SYS_PASS_TIME_SLICE
  336.      int  2dh                   ; force context switch.
  337.  
  338. Macro Instruction Format:
  339.  
  340.      none.
  341.  
  342. Portable Request Format:
  343.  
  344. STATUS PassTimeSlice();
  345.  
  346. Parameters:
  347.  
  348.      none.