Defines | Functions
kern/lock.h File Reference

Locks. More...

Defines

#define LOCK_MAX_ACQUIRES   255
 Maximum number of recursive acquires.

Functions

void init_lock (struct lock *k, const char *name)
void acquire (struct lock *k)
int try_acquire (struct lock *k)
int is_held (struct lock *k)
void release (struct lock *k)
void smash (struct lock *k)

Detailed Description

JoyOS includes a locking mechanism, useful when writing multi-threaded code. Locks allow sections of code to be protected, requiring a thread to acquire the lock before continuing. This allows multiple threads to to share a single resource (such as a variable, or a sensor). All of the Happyboard hardware drivers are locked to ensure thread-safe operation


Function Documentation

void acquire ( struct lock *  k)

Acquire the lock 'k'. If another thread holds the lock yield until it is available, then take it. A thread can recursively acquire the same lock multiple times, but it must release once for every acquire.

Parameters:
kLock to acquire. Must be non-null.
void init_lock ( struct lock *  k,
const char *  name 
)

Initialize the lock 'k'. This routine must be called before lock 'k' is used.

Parameters:
kLock to initialize, must be non-null.
nameDebugging name for lock.
int is_held ( struct lock *  k)

Checks to see if lock 'k' is held by the current thread.

Parameters:
kLock to check. Must be non-null.
Returns:
Non-zero if lock is held by current thread, zero otherwise.
void release ( struct lock *  k)

Release the lock 'k'.

Parameters:
kLock to release. Must be non-null.
void smash ( struct lock *  k)

Smash the lock 'k', releasing it. Should only ever be called by the kernel.

Parameters:
kLock to release. Must be non-null.
int try_acquire ( struct lock *  k)

Try to acquire the lock 'k'. If another thread holds the lock, return 0. If the lock was acquired, return 1.

Parameters:
kLock to acquire. Must be non-null.