richards.lisp 12.5 KB
Newer Older
ram's avatar
ram committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
(in-package "USER")
#+declare-unsafe
(proclaim '(optimize speed (space 0) (safety 0) (compilation-speed 0)))

(defconstant deviceA 5)
(defconstant deviceB 6)
(defconstant devicePacketKind 1)
(defconstant handlerA 3)
(defconstant handlerB 4)
(defconstant idler 1)
(defconstant noWork nil)
(defconstant noTask nil)
(defconstant worker 2)
(defconstant workPacketKind 2)

(defvar taskList noTask)
(defvar currentTask nil)
(defvar currentTaskIdentity nil)
(defvar taskTable (make-array 6 :initial-element noTask))
(proclaim '(simple-vector taskTable))
(defvar tracing nil)
(defvar layout 0)
(defvar queuePacketCount 0)
(defvar holdCount 0)
(proclaim '(fixnum layout queuePacketCount holdCount))

(declaim (inline make-taskControlBlock make-packet make-deviceTaskDataRecord
		 make-handlerTaskDataRecord make-idleTaskDataRecord
		 make-workerTaskDataRecord wait))

(defstruct (taskControlBlock (:constructor make-taskControlBLock ()))
  packetPending taskWaiting taskHolding link identity
  (priority 0 :type fixnum)
  input state handle)

(defstruct (packet (:constructor make-packet ()))
  link identity
  (kind 0 :type fixnum)
  (datum 0 :type fixnum) 
  (data '#() :type simple-vector))

(defstruct (deviceTaskDataRecord (:constructor make-deviceTaskDataRecord ()))
  pending)

(defstruct (handlerTaskDataRecord (:constructor make-handlerTaskDataRecord ()))
  workIn deviceIn)

(defstruct (idleTaskDataRecord (:constructor make-idleTaskDataRecord ()))
  (control 0 :type fixnum)
  (count 0 :type fixnum))

(defstruct (workerTaskDataRecord (:constructor make-workerTaskDataRecord ()))
  (destination 0 :type fixnum)
  (count 0 :type fixnum))

(defun wait ()
  (setf (taskControlBlock-taskWaiting currentTask) t)
  currentTask)

#+cmu
(declaim (ext:freeze-type taskControlBlock packet deviceTaskDataRecord
			  handlerTaskDataRecord idleTaskDataRecord
			  workerTaskDataRecord))
#+cmu
(declaim (ext:start-block richards))

(defun deviceTaskDataRecord-run (self work)
  (let ((functionWork work))
    (if (eq noWork functionWork)
	(progn
	 (setq functionWork (deviceTaskDataRecord-pending self))
	 (if (eq noWork functionWork)
	     (wait)
	   (progn
	    (setf (deviceTaskDataRecord-pending self) noWork)
	    (queuePacket functionWork))))
      (progn
       (setf (deviceTaskDataRecord-pending self) functionWork)
       (if tracing (trace-it (packet-datum functionWork)))
       (holdSelf)))))

(defun handlerTaskDataRecord-run (self work)
  (if (eq noWork work)
      nil
    (if (= workPacketKind (packet-kind work))
	(workInAdd self work)
      (deviceInAdd self work)))
  (let ((workPacket (handlerTaskDataRecord-workIn self)))
    (if (eq noWork workPacket)
	(wait)
      (let ((count (packet-datum workPacket)))
	(if (> count 4)
	    (progn
	     (setf (handlerTaskDataRecord-workIn self)
		   (packet-link workPacket))
	     (queuePacket workPacket))
	  (let ((devicePacket (handlerTaskDataRecord-deviceIn self)))
	    (if (eq noWork devicePacket)
		(wait)
	      (progn
	       (setf (handlerTaskDataRecord-deviceIn self)
		     (packet-link devicePacket))
	       (setf (packet-datum devicePacket)
		     (svref (packet-data workPacket) (- count 1)))
	       (setf (packet-datum workPacket) (+ count 1))
	       (queuePacket devicePacket)))))))))

(defun idleTaskDataRecord-run (self work)
  (declare (ignore work))
  (setf (idleTaskDataRecord-count self)
	(- (idleTaskDataRecord-count self) 1))
  (if (= 0 (idleTaskDataRecord-count self))
      (holdSelf)
    (if (= 0 (logand (idleTaskDataRecord-control self) 1))
	(progn
	 (setf (idleTaskDataRecord-control self)
	       (floor (idleTaskDataRecord-control self) 2))
	 (release deviceA))
      (progn
       (setf (idleTaskDataRecord-control self)
	     (logxor (floor (idleTaskDataRecord-control self) 2)
		     53256))
       (release deviceB)))))

(defun workerTaskDataRecord-run (self work)
  (if (eq noWork work)
      (wait)
    (progn
     (setf (workerTaskDataRecord-destination self)
	   (if (= handlerA (workerTaskDataRecord-destination self))
	       handlerB
	     handlerA))
     (setf (packet-identity work) (workerTaskDataRecord-destination self))
     (setf (packet-datum work) 1)
     (do ((i 0 (+ i 1)))
	 ((> i 3) nil)
       (declare (fixnum i))
	 (setf (workerTaskDataRecord-count self)
	       (+ (workerTaskDataRecord-count self) 1))
	 (if (> (workerTaskDataRecord-count self) 256)
	     (setf (workerTaskDataRecord-count self) 1))
	 (setf (svref (packet-data work) i)
	       (the fixnum
		    (+ (char-code #\A)
		       (- (workerTaskDataRecord-count self) 1)))))
     (queuePacket work))))

(defun appendHead (packet queueHead)
  (setf (packet-link packet) noWork)
  (if (eq noWork queueHead)
      packet
    (let ((mouse queueHead))
      (let ((link (packet-link mouse)))
	(do ()
	    ((eq noWork link) nil)
	    (setq mouse link)
	    (setq link (packet-link mouse)))
	(setf (packet-link mouse) packet)
	queueHead))))

(defun initialize-globals ()
  (setq taskList noTask)
  (setq currentTask nil)
  (setq currentTaskIdentity nil)
  (setq taskTable (make-array 6 :initial-element noTask))
  (setq tracing nil)
  (setq layout 0)
  (setq queuePacketCount 0)
  (setq holdCount 0))

(defun richards ()
  (initialize-globals)

  (createIdler idler 0 noWork (running (make-taskControlBlock)))

  (let ((workQ))

    (setq workQ (createPacket noWork worker workPacketKind))
    (setq workQ (createPacket workQ worker workPacketKind))
    (createWorker worker 1000 workQ (waitingWithPacket))

    (setq workQ (createPacket noWork deviceA devicePacketKind))
    (setq workQ (createPacket workQ deviceA devicePacketKind))
    (setq workQ (createPacket workQ deviceA devicePacketKind))
    (createHandler handlerA 2000 workQ (waitingWithPacket))
    
    (setq workQ (createPacket noWork deviceB devicePacketKind))
    (setq workQ (createPacket workQ deviceB devicePacketKind))
    (setq workQ (createPacket workQ deviceB devicePacketKind))
    (createHandler handlerB 3000 workQ (waitingWithPacket))
    
    (createDevice deviceA 4000 noWork (waiting))
    (createDevice deviceB 5000 noWork (waiting))

    )

  (schedule)

  (if (not (and (= queuePacketCount 23246) (= holdCount 9297)))
      (error "richards results incorrect"))

  nil)

(defun schedule ()
  (setq currentTask taskList)
  (do ()
      ((eq noTask currentTask) nil)
    (if (isTaskHoldingOrWaiting currentTask)
	(setq currentTask (taskControlBlock-link currentTask))
	(progn
	 (setq currentTaskIdentity (taskControlBlock-identity currentTask))
	 (if tracing (trace-it currentTaskIdentity))
	 (setq currentTask (runTask currentTask))))))

(defun findTask (identity)
  (declare (fixnum identity))
  (let ((tk (svref taskTable (- identity 1))))
    (if (eq noTask tk) (error "findTask failed"))
    tk))

(defun holdSelf ()
  (setq holdCount (+ holdCount 1))
  (setf (taskControlBlock-taskHolding currentTask) t)
  (taskControlBlock-link currentTask))

(defun queuePacket (packet)
  (let ((tk (findTask (packet-identity packet))))
    (if (eq noTask tk)
	noTask
	(progn
	 (setq queuePacketCount (+ queuePacketCount 1))
	 (setf (packet-link packet) noWork)
	 (setf (packet-identity packet) currentTaskIdentity)
	 (addInput tk packet currentTask)))))

(defun release (identity)
  (let ((tk (findTask identity)))
    (if (eq noTask tk)
	noTask
	(progn
	 (setf (taskControlBlock-taskHolding tk) nil)
	 (if (> (taskControlBlock-priority tk)
		(taskControlBlock-priority currentTask))
	     tk
	     currentTask)))))

(defun trace-it (id)
  (setq layout (- layout 1))
  (if (>= 0 layout)
      (progn
       (format t "~%")
       (setq layout 30)))
  (format t "~a " id))

(defun createDevice (identity priority work state)
  (let ((data (create-deviceTaskDataRecord)))
    (createTask identity priority work state data)))

(defun createHandler (identity priority work state)
  (let ((data (create-handlerTaskDataRecord)))
    (createTask identity priority work state data)))

(defun createIdler (identity priority work state)
  (let ((data (create-idleTaskDataRecord)))
    (createTask identity priority work state data)))

(defun createWorker (identity priority work state)
  (let ((data (create-workerTaskDataRecord)))
    (createTask identity priority work state data)))

(defun createTask (identity priority work state data)
  (let ((tk (create-taskControlBlock
	     taskList identity priority work state data)))
    (setq taskList tk)
    (setf (svref taskTable (- identity 1)) tk)))

(defun createPacket (link identity kind)
  (create-packet link identity kind))

(defun running (tcb)
  (setf (taskControlBlock-packetPending tcb) nil)
  (setf (taskControlBlock-taskWaiting tcb) nil)
  (setf (taskControlBlock-taskHolding tcb) nil)
  tcb)

(defun waiting ()
  (let ((tcb (make-taskControlBlock)))
    (setf (taskControlBlock-packetPending tcb) nil)
    (setf (taskControlBlock-taskWaiting tcb) t)
    (setf (taskControlBlock-taskHolding tcb) nil)
    tcb))

(defun waitingWithPacket ()
  (let ((tcb (make-taskControlBlock)))
    (setf (taskControlBlock-packetPending tcb) t)
    (setf (taskControlBlock-taskWaiting tcb) t)
    (setf (taskControlBlock-taskHolding tcb) nil)
    tcb))

(defun isTaskHoldingOrWaiting (tcb)
  (or (taskControlBlock-taskHolding tcb)
      (and (not (taskControlBlock-packetPending tcb))
	   (taskControlBlock-taskWaiting tcb))))

(defun isWaitingWithPacket (tcb)
  (and (taskControlBlock-packetPending tcb)
       (and (taskControlBlock-taskWaiting tcb)
	    (not (taskControlBlock-taskHolding tcb)))))

(defun packetNowPending (tcb)
  (setf (taskControlBlock-packetPending tcb) t)
  (setf (taskControlBlock-taskWaiting tcb) nil)
  (setf (taskControlBlock-taskHolding tcb) nil)
  tcb)

(defun create-taskControlBlock
  (link identity priority initialWorkQueue initialState privateData)
  (let ((r (make-taskControlBlock)))
    (setf (taskControlBlock-link r) link)
    (setf (taskControlBlock-identity r) identity)
    (setf (taskControlBlock-priority r) priority)
    (setf (taskControlBlock-input r) initialWorkQueue)
    (setf (taskControlBlock-packetPending r)
	 (taskControlBlock-packetPending initialState))
    (setf (taskControlBlock-taskWaiting r)
	 (taskControlBlock-taskWaiting initialState))
    (setf (taskControlBlock-taskHolding r)
	 (taskControlBlock-taskHolding initialState))
    (setf (taskControlBlock-handle r) privateData)
    (setf (taskControlBlock-state r) nil)
    r))

(defun addInput (tcb packet oldTask)
  (if (eq noWork (taskControlBlock-input tcb))
      (progn
       (setf (taskControlBlock-input tcb) packet)
       (setf (taskControlBlock-packetPending tcb) t)
       (if (> (taskControlBlock-priority tcb)
	      (taskControlBlock-priority oldTask))
	   tcb
	   oldTask))
      (progn
       (setf (taskControlBlock-input tcb)
	    (appendHead packet (taskControlBlock-input tcb)))
       oldTask)))

(defun runTask (tcb)
  (let ((message nil))
    (if (isWaitingWithPacket tcb)
	(progn
	  (setq message (taskControlBlock-input tcb))
	  (setf (taskControlBlock-input tcb) (packet-link message))
	  (if (eq noWork (taskControlBlock-input tcb))
	      (running tcb)
	    (packetNowPending tcb)))
      (setq message noWork))
    (run (taskControlBlock-handle tcb) message)))

(defun run (self work)
  (typecase self
	    (deviceTaskDataRecord (deviceTaskDataRecord-run self work))
	    (handlerTaskDataRecord (handlerTaskDataRecord-run self work))
	    (idleTaskDataRecord (idleTaskDataRecord-run self work))
	    (workerTaskDataRecord (workerTaskDataRecord-run self work))))

(defun create-packet (link identity kind)
  (let ((p (make-packet)))
    (setf (packet-link p) link)
    (setf (packet-identity p) identity)
    (setf (packet-kind p) kind)
    (setf (packet-datum p) 1)
    (let ((v (make-array 4 :initial-element 0)))
      (setf (packet-data p) v))
    p))

(defun create-deviceTaskDataRecord ()
  (let ((tk (make-deviceTaskDataRecord)))
    (setf (deviceTaskDataRecord-pending tk) noWork)
    tk))

(defun create-handlerTaskDataRecord ()
  (let ((tk (make-handlerTaskDataRecord)))
    (setf (handlerTaskDataRecord-workIn tk) noWork)
    (setf (handlerTaskDataRecord-deviceIn tk) noWork)
    tk))

(defun deviceInAdd (tk packet)
  (setf (handlerTaskDataRecord-deviceIn tk)
       (appendHead packet (handlerTaskDataRecord-deviceIn tk)))
  tk)

(defun workInAdd (tk packet)
  (setf (handlerTaskDataRecord-workIn tk)
       (appendHead packet (handlerTaskDataRecord-workIn tk)))
  tk)

(defun create-idleTaskDataRecord ()
  (let ((tk (make-idleTaskDataRecord)))
    (setf (idleTaskDataRecord-control tk) 1)
    (setf (idleTaskDataRecord-count tk) 10000)
    tk))

(defun create-workerTaskDataRecord ()
  (let ((tk (make-workerTaskDataRecord)))
    (setf (workerTaskDataRecord-destination tk) handlerA)
    (setf (workerTaskDataRecord-count tk) 0)
    tk))