Chapter 6
Synchronization Interfaces

6.1 oskit_lock: Thread-safe lock interface

The oskit_lock COM interface allows components to protect data structures from concurrent access by multiple threads. The interface is intended to be generic so that components do not need to know the specifics of any particular thread system. The user of a lock should be prepared for the possibility that the thread will be put to sleep if the lock cannot be granted. There are two variants supported; a regular lock and a critical lock. A critical lock differs only in that interrupts are blocked while the lock is held. The oskit_lock COM interface inherits from oskit_iunknown, and has the following additional methods:

lock:
Lock a lock.
unlock:
Unlock a lock.

6.1.1 lock: Lock a lock

SYNOPSIS

#include <oskit/com/lock.h>

OSKIT_COMDECL oskit_lock_lock(oskit_lock_t *lock);

DESCRIPTION

This method attempts to lock lock. If the lock cannot be immediately granted, the current thread is put to sleep until the lock can be granted.

PARAMETERS
lock:
The oskit_lock COM interface for the lock.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

6.1.2 lock: Unlock a lock

SYNOPSIS

#include <oskit/com/lock.h>

OSKIT_COMDECL oskit_lock_unlock(oskit_lock_t *lock);

DESCRIPTION

This method unlocks lock. If there are any threads waiting for the lock, one will be woken up and granted the lock.

PARAMETERS
lock:
The oskit_lock COM interface for the lock.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

6.2 oskit_condvar: Condition variable interface

The oskit_condvar COM interface allows components to wait for conditions. The interface is intended to be generic so that components do not need to know the specifics of any particular thread system. A condition is typically combined with an oskit_lock object to facilitate building monitor type objects. Attempting to wait on a condition without supplying a locked oskit_lock object results in undefined behavior. The oskit_lock COM interface inherits from oskit_iunknown, and has the following additional methods:

wait:
Wait on a condition variable.
signal:
Signal a condition variable.
broadcast:
Broadcast a condition variable.

6.2.1 wait: Wait on a condition variable

SYNOPSIS

#include <oskit/com/condvar.h>

OSKIT_COMDECL oskit_condvar_wait(oskit_condvar_t *condvar, oskit_lock_t *lock);

DESCRIPTION

This method causes the current thread is to wait until the condition variable is signaled or broadcast. The oskit_lock object must be locked when called. The lock is released prior to waiting, and reacquired before returning.

PARAMETERS
condvar:
The oskit_condvar COM interface for the condition variable.
lock:
The oskit_lock COM interface for the lock.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

6.2.2 signal: Signal a condition variable

SYNOPSIS

#include <oskit/com/condvar.h>

OSKIT_COMDECL oskit_condvar_signal(oskit_condvar_t *condvar);

DESCRIPTION

Wake up exactly one thread waiting on the condition variable object condvar.

PARAMETERS
condvar:
The oskit_condvar COM interface for the condition variable.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

6.2.3 broadcast: Broadcast a condition variable

SYNOPSIS

#include <oskit/com/condvar.h>

OSKIT_COMDECL oskit_condvar_broadcast(oskit_condvar_t *condvar);

DESCRIPTION

Wake up all threads waiting on the condition variable object condvar.

PARAMETERS
condvar:
The oskit_condvar COM interface for the condition variable.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

6.3 oskit_lock_mgr: Lock manager: Interface for creating locks and condition variables

The lock manager is registered in the services registry (See Section 5.1) when the threading system (if it is included) is initialized. Components that need to protect data structures can query the services registry for the lock manager. Since the lock manager will only be registered by the threading system, the client can assume that the absence of a lock manager implies a single threaded system (locks are unnecessary). The oskit_lock_mgr COM interface inherits from oskit_iunknown, and has the following additional methods:

allocate_lock:
Allocate a lock object.
allocate_critical_lock:
Allocate a critical lock object.

6.3.1 allocate_lock: Allocate a thread-safe lock

SYNOPSIS

#include <oskit/com/lock_mgr.h>

OSKIT_COMDECL oskit_lock_mgr_allocate_lock(oskit_lock_mgr_t *lmgr, [out] oskit_lock_t *out_lock);

DESCRIPTION

This method returns an oskit_lock_t COM interface in out_lock.

PARAMETERS
lmgr:
The lock manager COM interface.
out_lock:
The oskit_lock COM interface for the new lock.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

6.3.2 allocate_critical_lock: Allocate a critical thread-safe lock

SYNOPSIS

#include <oskit/com/lock_mgr.h>

OSKIT_COMDECL oskit_lock_mgr_allocate_critical_lock(oskit_lock_mgr_t *lmgr, [out] oskit_lock_t *out_lock);

DESCRIPTION

This method returns an oskit_lock_t COM interface in out_lock. The lock is flagged as critical so that interrupts are blocked while the lock is held.

PARAMETERS
lmgr:
The lock manager COM interface.
out_lock:
The oskit_lock COM interface for the new lock.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.

6.3.3 allocate_condvar: Allocate a condition variable

SYNOPSIS

#include <oskit/com/lock_mgr.h>

OSKIT_COMDECL oskit_lock_mgr_allocate_condvar(oskit_lock_mgr_t *lmgr, [out] oskit_condvar_t *out_condvar);

DESCRIPTION

This method returns an oskit_condvar_t COM interface in out_condvar. Condition variables may be used in conjunction with locks to form monitors.

PARAMETERS
lmgr:
The lock manager COM interface.
out_condvar:
The oskit_condvar COM interface for the new condition variable.
RETURNS

Returns 0 on success, or an error code specified in <oskit/error.h>, on error.