so if a locked scope processes another function call that also has a lock on it, thats a guarenteed deadlock? why not check if the ownership of the current mutex lock is of same ownership of the one being called in the other called function, letting it pass through like a normal single threaded sketch? i would assume that as being recursive, correct? why wouldnt this be useful?
If you are locking the same mutex then it's a deadlock.
Lets say I start a lock, I would then assume responsibility of all current and future calls, of other functions, regardless if they also include locks of their own, as long as I currently hold the MAIN lock status flag, I should be able to bypass other lock sections to process their code as well. Recursive would be more ideal then. The owner could basically be the thread id (id1) for example. And if thread(id2) tried to call a function with a lock, his access, recursive or not, will be queued until thread(id1) lock (recursive or not) is removed.
Some platforms have recursive mutexes. But it's an advanced topic. I'm not sure it's suitable for a simple platform like Teensy so I didn't implement them.
Some also have a type of lock called a semaphone that might work for something like this. It keeps track of the number of locks but doesn't actually block you. So if you lock, it adds 1 to the count. When you unlock it subtracts. Your code has to figure out what to do based on the count. I didn't implement this either because it's so simple, you can easily implement it in your own code.
thread1: im locked, i own all current and future code, any locks in sub functions will be ignored as I will run their code anyways! (Basically same concept as an everyday single threaded arduino sketch.
thread2: arg, thread1 owns the main lock, I will just wait since I need to aquire a lock.
thread3: gibbs on next queue! I'll be right after you!
if thread(id1) is locked, and a subfunction calls to lock a mutex, I could imagine HIS scope would exit the lock, but, wouldn't the library function just say:
thread(id1) is locked, i need to call foo();, oh! there is a scope lock in that function, im told to not set it as I already own the main lock, that way when it exits I dont remove the main lock, oh great, i returned to my scope, and ended the brace, release the main lock. (this would be sort of like a master lock in single thread mode).
isnt this safe?
All these things are possible, but they are a bit too specialized for particular purposes and scenarios to be included in a general purpose library. If you look at the code, it actually relatively simple to implement your own specialized locking logic.