[3ae31e9] | 1 | Mutli-Tasker Notes -- 1988-04-14 -- Rev 15 D.N. Lynx Crowe
|
---|
| 2 |
|
---|
| 3 | Summary
|
---|
| 4 | -------
|
---|
| 5 |
|
---|
| 6 | . Task scheduling:
|
---|
| 7 |
|
---|
| 8 | . Priority scheduler
|
---|
| 9 |
|
---|
| 10 | . Multiple priority levels, 0..65535 (0 is lowest)
|
---|
| 11 |
|
---|
| 12 | . Round-Robin scheduling within each level
|
---|
| 13 |
|
---|
| 14 | . Multi-level interrupt support
|
---|
| 15 |
|
---|
| 16 | . Time slicing optionally supported
|
---|
| 17 |
|
---|
| 18 | . Tasks are either stopped, waiting, ready, or running
|
---|
| 19 |
|
---|
| 20 | . Arbitrary number of Task Control Blocks (up to 65535)
|
---|
| 21 |
|
---|
| 22 | . Swapping rules designed to minimize chance of system lockup
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | . Inter-task communication:
|
---|
| 27 |
|
---|
| 28 | . Supports counting semaphores for event signalling or
|
---|
| 29 | control of exclusive access to shared resources
|
---|
| 30 |
|
---|
| 31 | . Supports inter-task mailboxes (message queues)
|
---|
| 32 | |
---|
| 33 |
|
---|
| 34 | Multitasker support functions
|
---|
| 35 | -----------------------------
|
---|
| 36 |
|
---|
| 37 | . Task functions
|
---|
| 38 |
|
---|
| 39 | void MTInit()
|
---|
| 40 | unsigned MTID()
|
---|
| 41 | short MTRun(tid, pri, stat, slice, pstack, func, par)
|
---|
| 42 | void MTSetT(tcp, tid, pri, stat, slice, pstack, func, par)
|
---|
| 43 | unsigned MTSetP(pri)
|
---|
| 44 | void MTSwap()
|
---|
| 45 | void MTNext()
|
---|
| 46 | short MTStop(tid)
|
---|
| 47 | short MTZap(tid)
|
---|
| 48 | void MTExit()
|
---|
| 49 | void MTQuit()
|
---|
| 50 | short MTStat(tid)
|
---|
| 51 | unsigned MTGetID()
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | . Semaphore functions
|
---|
| 55 |
|
---|
| 56 | void SMInit(psem, n)
|
---|
| 57 | short SMStat(psem)
|
---|
| 58 | void SMSig(psem)
|
---|
| 59 | void SMWait(psem)
|
---|
| 60 | short SMCSig(psem)
|
---|
| 61 | short SMCWait(psem)
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | . Mailbox functions
|
---|
| 65 |
|
---|
| 66 | void MBInit(pmbox)
|
---|
| 67 | void MBSend(pmbox, pmsg)
|
---|
| 68 | MSG * MBRecv(pmbox)
|
---|
| 69 | MSG * MBChek(pmbox)
|
---|
| 70 | short MBDel(pmbox, pmsg)
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | . System variables
|
---|
| 74 |
|
---|
| 75 | TCB *MT_TCBs pointer to TCB chain
|
---|
| 76 | TCB *MT_CurP pointer to current TCB
|
---|
| 77 | TCB *MT_RdyQ pointer to ready queue
|
---|
| 78 | unsigned MT_IDct task ID assignment counter
|
---|
| 79 | unsigned *_MT_Vc1 saved TRAP 8 vector (_MT_Swp)
|
---|
| 80 | unsigned *_MT_Vc2 saved TRAP 9 vector (_MT_Nxt)
|
---|
| 81 | long _MT_Stk[48] stack for _MT_Nil() (null task)
|
---|
| 82 | TCB _MT_TCB dummy TCB for shutdown
|
---|
| 83 | |
---|
| 84 |
|
---|
| 85 | Data structures
|
---|
| 86 | ---------------
|
---|
| 87 |
|
---|
| 88 | . SEM - Semaphore (counting type)
|
---|
| 89 |
|
---|
| 90 | typedef long SEM;
|
---|
| 91 |
|
---|
| 92 | . Single 32 bit word per semaphore used for both semaphore
|
---|
| 93 | counter and wait queue pointer
|
---|
| 94 |
|
---|
| 95 | . LSB indicates status of semaphore and meaning of MS bits
|
---|
| 96 |
|
---|
| 97 | . 1 = MS bits are the number of signals received and
|
---|
| 98 | not processed
|
---|
| 99 |
|
---|
| 100 | . 0 = MS bits are a pointer to the queue of tasks
|
---|
| 101 | waiting on the semaphore
|
---|
| 102 |
|
---|
| 103 | . Initialize to a count of 0 (value = 0) for event
|
---|
| 104 | signalling, or to a count of 1 (value = 3) for
|
---|
| 105 | controlling mutually exclusive access to a shared
|
---|
| 106 | resource
|
---|
| 107 | |
---|
| 108 |
|
---|
| 109 | . MSG - Message
|
---|
| 110 |
|
---|
| 111 | typedef struct MSG {
|
---|
| 112 |
|
---|
| 113 | MSG *next;
|
---|
| 114 | SEM reply; /* initialize to 0 */
|
---|
| 115 | long msize;
|
---|
| 116 | long mdata;
|
---|
| 117 | };
|
---|
| 118 |
|
---|
| 119 | . Consists of a queue pointer, a semaphore, a size variable,
|
---|
| 120 | and a data pointer
|
---|
| 121 |
|
---|
| 122 | . 'next' -- pointer -- points to the next message
|
---|
| 123 | in the message queue
|
---|
| 124 |
|
---|
| 125 | . 'reply' -- semaphore -- used to indicate reply status
|
---|
| 126 |
|
---|
| 127 | . 'msize' -- variable -- contains the size of the
|
---|
| 128 | message if 'mdata' contains a pointer, or 0 if 'mdata'
|
---|
| 129 | contains the data itself
|
---|
| 130 |
|
---|
| 131 | . 'mdata' -- variable -- points to the actual data,
|
---|
| 132 | or is the data itself
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | . MBOX - Mailbox (intertask message queue)
|
---|
| 136 |
|
---|
| 137 | typedef struct MBOX {
|
---|
| 138 |
|
---|
| 139 | SEM mail; /* initialize to zero */
|
---|
| 140 | SEM mutex; /* initialize to 1 */
|
---|
| 141 | MSG *head; /* initialize to NIL */
|
---|
| 142 | MSG *tail; /* initialize to NIL */
|
---|
| 143 | };
|
---|
| 144 |
|
---|
| 145 | . Managed by two semaphores and two pointers
|
---|
| 146 |
|
---|
| 147 | . 'mail' -- semaphore -- indicates messages present
|
---|
| 148 | in the message queue
|
---|
| 149 |
|
---|
| 150 | . 'mutex' -- semaphore -- enforces mutually exclusive
|
---|
| 151 | access to the message queue
|
---|
| 152 |
|
---|
| 153 | . 'head' -- pointer -- points to the first message
|
---|
| 154 | in the message queue, or NIL if the queue is empty
|
---|
| 155 |
|
---|
| 156 | . 'tail' -- pointer -- points to the last message
|
---|
| 157 | in the message queue, or NIL if the queue is empty
|
---|
| 158 | |
---|
| 159 |
|
---|
| 160 | . TCB - Task Control Block
|
---|
| 161 |
|
---|
| 162 | typedef struct TCB {
|
---|
| 163 |
|
---|
| 164 | TCB *next; 4 bytes
|
---|
| 165 | TCB *fwd; 4 bytes
|
---|
| 166 | unsigned tid; 2 bytes
|
---|
| 167 | unsigned pri; 2 bytes
|
---|
| 168 | long slice; 4 bytes
|
---|
| 169 | long reg[16]; 64 bytes
|
---|
| 170 | long sp; 4 bytes
|
---|
| 171 | long pc; 4 bytes
|
---|
| 172 | unsigned sr; 2 bytes
|
---|
| 173 | unsigned flags; 2 bytes
|
---|
| 174 | long tos; 4 bytes
|
---|
| 175 | }; --
|
---|
| 176 | 96 bytes total
|
---|
| 177 |
|
---|
| 178 | . 'next' points to the next task in the task queue or NIL
|
---|
| 179 |
|
---|
| 180 | . 'fwd' points to the next TCB in the TCB chain
|
---|
| 181 |
|
---|
| 182 | . 'tid' contains the task identifier (0..65536)
|
---|
| 183 |
|
---|
| 184 | . 'pri' contains the task priority (0..65535, 0 is lowest)
|
---|
| 185 |
|
---|
| 186 | . 'slice' contains the time slice (milliseconds) for the task,
|
---|
| 187 | or -1 for a task that isn't sliced
|
---|
| 188 |
|
---|
| 189 | . 'reg[]' contains the task register contents when
|
---|
| 190 | the task is not executing (D0-D7/A0-A7)
|
---|
| 191 |
|
---|
| 192 | . 'sp' contains the task stack pointer when the task
|
---|
| 193 | is not executing
|
---|
| 194 |
|
---|
| 195 | . 'pc' contains the task Program Counter contents when
|
---|
| 196 | the task is not executing
|
---|
| 197 |
|
---|
| 198 | . 'sr' contains the task Status Register contents when
|
---|
| 199 | the task is not executing
|
---|
| 200 |
|
---|
| 201 | . 'flags' contains the task control flags:
|
---|
| 202 |
|
---|
| 203 | MTF_OCC 0x8000 /* TCB occupied */
|
---|
| 204 | MTF_RDY 0x0001 /* task ready to run (on MT_RdyQ) */
|
---|
| 205 | MTF_SWT 0x0002 /* task waiting on a semaphore */
|
---|
| 206 | MTF_RUN 0x0004 /* task running (MT_CurP) */
|
---|
| 207 | MTF_STP 0x0008 /* task to be stopped (waiting) */
|
---|
| 208 |
|
---|
| 209 | . 'tos' points to the top of the task's stack
|
---|
| 210 | |
---|
| 211 |
|
---|
| 212 | Multitasker support function calling sequences
|
---|
| 213 | ----------------------------------------------
|
---|
| 214 |
|
---|
| 215 | . Initialize the multi-tasker
|
---|
| 216 |
|
---|
| 217 | void
|
---|
| 218 | MTInit()
|
---|
| 219 |
|
---|
| 220 | . Get next available task ID
|
---|
| 221 |
|
---|
| 222 | unsigned task ID
|
---|
| 223 | MTID()
|
---|
| 224 |
|
---|
| 225 | . Acquire and initialize a TCB and start a task
|
---|
| 226 |
|
---|
| 227 | short result code
|
---|
| 228 | MTRun(tid, pri, stat, slice, pstack, func, par)
|
---|
| 229 | unsigned tid; task ID
|
---|
| 230 | unsigned pri; initial task priority
|
---|
| 231 | unsigned stat; initial task status register
|
---|
| 232 | long slice; time slice, or -1
|
---|
| 233 | long pstack; top of stack address
|
---|
| 234 | short (*func)(); initial task program counter
|
---|
| 235 | long par; long parameter passed to task
|
---|
| 236 |
|
---|
| 237 | result code:
|
---|
| 238 |
|
---|
| 239 | SUCCESS TCB created and placed on ready queue
|
---|
| 240 | FAILURE no TCB available for task
|
---|
| 241 |
|
---|
| 242 | Note: acquires a free TCB from the current chain of known TCBs.
|
---|
| 243 |
|
---|
| 244 | . Initialize a specific TCB and start a task
|
---|
| 245 |
|
---|
| 246 | MTSetT(tcp, tid, pri, stat, slice, pstack, func, par)
|
---|
| 247 | TCB *tcp; TCB pointer
|
---|
| 248 | unsigned tid; task ID
|
---|
| 249 | unsigned pri; initial task priority
|
---|
| 250 | unsigned stat; initial task status register
|
---|
| 251 | long slice; time slice, or -1
|
---|
| 252 | long pstack; top of stack address
|
---|
| 253 | short (*func)(); initial task program counter
|
---|
| 254 | long par; long parameter passed to task
|
---|
| 255 |
|
---|
| 256 | Note: adds the TCB to the current chain of known TCBs.
|
---|
| 257 | |
---|
| 258 |
|
---|
| 259 | . Set the priority of the current task and swap to the highest
|
---|
| 260 | priority ready task
|
---|
| 261 |
|
---|
| 262 | unsigned old priority (0..65535)
|
---|
| 263 | MTSetP(pri)
|
---|
| 264 | unsigned pri; new priority (0..65535)
|
---|
| 265 |
|
---|
| 266 | . Swap to the highest priority ready task,
|
---|
| 267 | re-schedule the current task
|
---|
| 268 |
|
---|
| 269 | void
|
---|
| 270 | MTSwap();
|
---|
| 271 |
|
---|
| 272 | . Swap to the highest priority ready task,
|
---|
| 273 | don't re-schedule the current task
|
---|
| 274 |
|
---|
| 275 | void
|
---|
| 276 | MTNext();
|
---|
| 277 |
|
---|
| 278 | Note: this is an internal function, use with caution.
|
---|
| 279 |
|
---|
| 280 | . Stop a specific task
|
---|
| 281 |
|
---|
| 282 | short result code
|
---|
| 283 | MTStop(tid)
|
---|
| 284 | unsigned tid; task ID of task to stop
|
---|
| 285 |
|
---|
| 286 | result code:
|
---|
| 287 |
|
---|
| 288 | -3 - task not stopped (was the current task)
|
---|
| 289 | -2 - task not stopped (can't find it on ready queue)
|
---|
| 290 | -1 - task not stopped (can't find it at all)
|
---|
| 291 | 0 - task stopped (was ready to run)
|
---|
| 292 | 1 - task stopped (was waiting)
|
---|
| 293 | 2 - task already stopped (wasn't waiting or on ready queue)
|
---|
| 294 | 3 - task will be stopped (has MTF_STP set)
|
---|
| 295 |
|
---|
| 296 | . Delete a specific task, free its TCB
|
---|
| 297 |
|
---|
| 298 | short result code
|
---|
| 299 | MTZap(tid)
|
---|
| 300 | unsigned tid; task ID
|
---|
| 301 |
|
---|
| 302 | result code:
|
---|
| 303 |
|
---|
| 304 | -1 not deleted (was active or waiting)
|
---|
| 305 | 0 deleted
|
---|
| 306 | 1 not found
|
---|
| 307 | |
---|
| 308 |
|
---|
| 309 | . Terminate the current task, free its TCB
|
---|
| 310 |
|
---|
| 311 | void
|
---|
| 312 | MTExit()
|
---|
| 313 |
|
---|
| 314 | . Shut down the multi-tasker
|
---|
| 315 |
|
---|
| 316 | void
|
---|
| 317 | MTQuit();
|
---|
| 318 |
|
---|
| 319 | Note: this brings everything to a halt. Use with caution.
|
---|
| 320 |
|
---|
| 321 | . Get the task ID of the currently running task
|
---|
| 322 |
|
---|
| 323 | unsigned
|
---|
| 324 | MTGetID()
|
---|
| 325 |
|
---|
| 326 | . Check the state of a specific task
|
---|
| 327 |
|
---|
| 328 | short result code
|
---|
| 329 | MTStat(tid)
|
---|
| 330 | unsigned tid; task ID
|
---|
| 331 |
|
---|
| 332 | result code:
|
---|
| 333 |
|
---|
| 334 | -3 stopped, waiting on a sempahore
|
---|
| 335 | -2 stopped, not waiting
|
---|
| 336 | -1 not found
|
---|
| 337 | 0 running (current task)
|
---|
| 338 | 1 ready to run
|
---|
| 339 | 2 waiting on a sempahore
|
---|
| 340 |
|
---|
| 341 | |
---|
| 342 |
|
---|
| 343 | Semaphore Operation function calling sequences
|
---|
| 344 | ----------------------------------------------
|
---|
| 345 |
|
---|
| 346 | . Initialize a semaphore
|
---|
| 347 |
|
---|
| 348 | void
|
---|
| 349 | SMInit(psem, n)
|
---|
| 350 | SEM *psem; pointer to semaphore
|
---|
| 351 | long n; count to put in semaphore
|
---|
| 352 |
|
---|
| 353 | . Check the status of a semaphore
|
---|
| 354 |
|
---|
| 355 | short result code
|
---|
| 356 | SMStat(psem)
|
---|
| 357 | SEM *psem; pointer to semaphore
|
---|
| 358 |
|
---|
| 359 | result code:
|
---|
| 360 |
|
---|
| 361 | -1 awaited at least one process is waiting
|
---|
| 362 | 0 not signalled no signals, no waiting processes
|
---|
| 363 | 1 signalled at least one unreceived signal
|
---|
| 364 |
|
---|
| 365 | . Signal a semphore
|
---|
| 366 |
|
---|
| 367 | void
|
---|
| 368 | SMSig(psem)
|
---|
| 369 | SEM *psem; pointer to semaphore
|
---|
| 370 |
|
---|
| 371 | . Wait on a semaphore
|
---|
| 372 |
|
---|
| 373 | void
|
---|
| 374 | SMWait(psem)
|
---|
| 375 | SEM *psem; pointer to semaphore
|
---|
| 376 | |
---|
| 377 |
|
---|
| 378 | . Conditionally signal a semaphore
|
---|
| 379 |
|
---|
| 380 | short result code
|
---|
| 381 | SMCSig(psem)
|
---|
| 382 | SEM *psem; pointer to semaphore
|
---|
| 383 |
|
---|
| 384 | result code:
|
---|
| 385 |
|
---|
| 386 | -1 stopped task signalled
|
---|
| 387 | 0 nothing signalled
|
---|
| 388 | 1 waiting task signalled
|
---|
| 389 |
|
---|
| 390 | . Conditional wait on a semaphore
|
---|
| 391 |
|
---|
| 392 | short result code
|
---|
| 393 | SMCWait(psem)
|
---|
| 394 | SEM *psem; pointer to semaphore
|
---|
| 395 |
|
---|
| 396 | result code:
|
---|
| 397 |
|
---|
| 398 | TRUE semaphore was non-zero, and was decremented
|
---|
| 399 | FALSE semaphore was zero, or tasks were waiting for it
|
---|
| 400 | |
---|
| 401 |
|
---|
| 402 | Mailbox Operation function calling sequences
|
---|
| 403 | --------------------------------------------
|
---|
| 404 |
|
---|
| 405 | . Initialize a mailbox
|
---|
| 406 |
|
---|
| 407 | void
|
---|
| 408 | MBInit(pmbox)
|
---|
| 409 | MBOX *pmbox; pointer to mailbox
|
---|
| 410 |
|
---|
| 411 | . Send a message to a mailbox queue
|
---|
| 412 |
|
---|
| 413 | void
|
---|
| 414 | MBSend(pmbox, pmsg)
|
---|
| 415 | MBOX *pmbox; pointer to mailbox
|
---|
| 416 | MSG *pmsg; pointer to message
|
---|
| 417 |
|
---|
| 418 | . Receive a message from a mailbox queue (wait for message)
|
---|
| 419 |
|
---|
| 420 | MSG * pointer to message received
|
---|
| 421 | MBRecv(pmbox)
|
---|
| 422 | MBOX *pmbox; pointer to mailbox
|
---|
| 423 |
|
---|
| 424 | . Check for a message from a mailbox queue and conditionally receive it
|
---|
| 425 |
|
---|
| 426 | MSG * pointer to message, or NIL
|
---|
| 427 | MBChek(pmbox)
|
---|
| 428 | MBOX *pmbox; pointer to mailbox
|
---|
| 429 |
|
---|
| 430 | . Delete a message from a mailbox queue (cancel message)
|
---|
| 431 |
|
---|
| 432 | short result code
|
---|
| 433 | MBDel(pmbox, pmsg)
|
---|
| 434 | MBOX *pmbox; pointer to mailbox
|
---|
| 435 | MSG *pmsg; pointer to message
|
---|
| 436 |
|
---|
| 437 | result code:
|
---|
| 438 |
|
---|
| 439 | FAILURE unable to find the message
|
---|
| 440 | SUCCESS message found and deleted from mailbox
|
---|