Shop OBEX P1 Docs P2 Docs Learn Events
Using locks from other objects — Parallax Forums

Using locks from other objects

oodesoodes Posts: 131
edited 2013-06-10 01:18 in Propeller 1
Hello,

I'm wondering is it possible to use a lock that i have declared in my top object folder but use from another lower object folder. I have my main cog executing and writes to a logfile events on an sd card and i have wrote another object that emails me the logfile when requested. But i need to put a lock on the logfile so 2 cogs dont access it at the same time. Works fine when I'm using other locks within the same object folder but when i try to use the same lock from different objects its causing problems.


Thanks Des

Comments

  • Alex.StanfieldAlex.Stanfield Posts: 198
    edited 2013-06-07 09:12
    You have to get the lock (LOCKNEW) only once. Then you have to inform the lock number to all the other routines/modules that should use it. Then you use it with LOCKSET/CLR. Have a look at the manual in page 122.

    Works great.

    Alex
  • Mark_TMark_T Posts: 1,981
    edited 2013-06-08 05:05
    Normally you would use the lock in relevant methods of the library that controls that resource,
    so just calling the methods from outside will be protected. So for instance an SPI library would
    use a lock to protect access to the bus it is handling, and you would only need to call methods
    on the instance to get the lock handling automatically.

    This of course just moves the problem to that of gaining a handle for the right instance in two places,

    There is a subtle issue of recursive access to locks - when code in the same cog is calling methods in
    several libraries and each instance is using the same lock then there is a risk of trying to claim a lock
    recursively (not supported by Prop locks). The way to avoid this is to only use a lock for one purpuse in
    one library, share the reference to the instance, not the lock, and write the library to not recursively claim
    the lock.
  • oodesoodes Posts: 131
    edited 2013-06-10 01:18
    Cheers lads

    Yes I was unsure if the resource was protected when I called from an outside object. I had a lock on the resource within the main object folder but i wasn't sure if this remained protected when i called from another object the same resource without using the lock in place.

    Thanks
    Des
Sign In or Register to comment.