Path: utzoo!utdoe!generic!pnet91!ericmcg From: ericmcg@pnet91.cts.com (Eric Mcgillicuddy) Newsgroups: comp.sys.apple2 Subject: Re: Multitasking on a II Message-ID: <313@generic.UUCP> Date: 23 Dec 90 00:50:04 GMT Sender: root@generic.UUCP Organization: People-Net [pnet91], Etobicoke, ON Lines: 55 >When the resource is initialized by the system: >resource_init ... ;blah, blah, blah > lda #1 ;could be $ff if you prefer to > sta flag ; grab resource with inc > ... ;blah, blah, blah > >Then for each process that wants a piece of the action: >your_code ... ;blah, blah, blah >loop dec flag ;try to grab resource > beq locked ;got it! > inc flag ;undo attempt > jmp loop ;block until available >locked ... ;Here you get exclusive use of the resource. >release inc flag ;undo resource lock > ... ;blah, blah, blah > >I think this should work for about 254 processes. With a slight >variation on this theme, you can also implement non-blocking resource >contention. > >Michael Ziober That will not work since you do not test the flag before modifying it. Suppose the Flag were 0, meaning that the resource is locked. You now decrement it and it is not zero meaning that it is unlocked. If you only grab a resource when it is free ($01 in flag) not just when is not locked (big difference logically), then it could work, but requires much more protection coding. Another problem is that your test loop will eventually DEC FLAG to $01 and on the next pass tell you that the resource is free. Even on a 65816 where FLAG is a 2 byte value, you will eventually luck out and grab a locked resource. Multiprocesses will compound the problem. Regardless of how you test a flag, you must not alter it unless you are claiming it, this is where TSB comes in. lda flag_mask w8_4_free tsb global_flag bne w8_4_free ......your code lda flag_mask trb global_flag ;clear busy bit when done with resource This should protect whatever resources capable of being claimed in a pre-emptive multitasking emvironment. The limited addressing modes available will cause problems in a 65816 system unless this is in a shared memory region of the system since the global semaphores must be in the same bank as the code that tests and claims them. An advantage is that a single byte/word can hold 8/16 flags as opposed to just a single flag using ASL or INC/DEC. Of course it is dead simple in a Toolbox based system such as the GS. UUCP: bkj386!pnet91!ericmcg INET: ericmcg@pnet91.cts.com