What exactly do locks do and how do they work?

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
gerrykurz
XCore Addict
Posts: 204
Joined: Sun Jun 01, 2014 10:25 pm

What exactly do locks do and how do they work?

Post by gerrykurz »

I am trying to figure out what locks do. There are lots of example of how to get and set them, and an api to do the same.

The documentation says they are used to share memory or resources between cores/tasks.

What what do they actually do?

How do they actually work?


User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Post by Bianco »

The are thread synchronisation mechanisms just like semaphores and the like.
Once a lock is acquired it cannot be acquired by another thread until the lock is released (if properly programmed).
In shared memory configurations one could use them to ensure that only one thread will make changes to the shared memory at a time (mutual exclusion).

XMOS advocates to use channels for synchronisation, so you don't see the locks very often used, but it's there.
User avatar
JohnWilson
Active Member
Posts: 38
Joined: Fri Oct 28, 2011 10:53 pm

Post by JohnWilson »

Is there a non-dogmatic reason why use of locks isn't encouraged? The XMOS implementation of locks is absolutely beautiful.

In answer to the original question: locks don't exactly *do* anything, but the idea is that all users of a shared data structure agree that they'll touch it only while holding a particular lock.
User avatar
gerrykurz
XCore Addict
Posts: 204
Joined: Sun Jun 01, 2014 10:25 pm

Post by gerrykurz »

Could someone post a short programming example of how they work?
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Post by Bianco »

Did you take a look at https://github.com/xmos/lib_locks
User avatar
gerrykurz
XCore Addict
Posts: 204
Joined: Sun Jun 01, 2014 10:25 pm

Post by gerrykurz »

Thanks, that helps.