Difference between revisions of "Unit Threads"

From Ultibo.org
Jump to: navigation, search
Line 5: Line 5:
 
----
 
----
  
''To be documented''
+
'''Locking Primitives'''
 +
 +
'''Spin''' -
 +
 +
A simple spin lock for fast mutually exclusive access to data. Threads "spin" while waiting to acquire the lock and do not yield the CPU unless pre-empted.
 +
 
 +
Non recursive (can only be acquired once by the same thread)
 +
 
 +
Includes IRQ/FIQ entry and exit routines to Save/Restore the IRQ/FIQ state.
 +
 
 +
Suitable for use by Interrupt handlers if non interrupt callers use the IRQ/FIQ entry and exit routines to Lock and Unlock.
 +
 
 +
Suitable for use on multiprocessor systems if the lock is allocated from shared memory (Determined during initialization).
 +
 
 +
Not recommended for long held operations or holding during I/O operations.
 +
 
 +
Access is not serialized, the next thread to try obtaining the lock when it is released will succeed even if that thread was not the first waiting.
 +
 
 +
Usage:
 +
             
 +
''Create/Destroy''
 +
       
 +
Spin locks are created using SpinCreate() and destroyed using SpinDestroy(), these functions should not be called from within an Interrupt handler. Lock/Unlock (Data accessed only by threads)
 +
       
 +
To synchronize data access between threads each thread should call SpinLock() before accessing the data and SpinUnlock() when finished accessing the data. These calls do not affect the state of IRQs or FIQs and therefore do not impact interrupt latency. Lock/Unlock (Data accessed by threads and interrupt handlers)
 +
       
 +
To use a Spin lock to synchronise data access between threads and an interrupt handler each thread should call SpinLockIRQ() or SpinLockFIQ(), depending on whether the interrupt handler is servicing IRQ or FIQ requests, before accessing the data and SpinUnlockIRQ() or SpinUnlockFIQ() when finished accessing the data. These calls disable interrupts before acquiring the lock (with deadlock protection) and restore interrupts after releaseing the lock and therefore should only be used to protect very short sections of code accessing shared data to minimize the impact on interrupt latency.
 +
       
 +
Interrupt handlers should call SpinLock() before accessing the data and SpinUnlock() when finished accessing the data. In a Uniprocessor system it is technically not necessary for interrupt handlers to call lock/unlock as the use of IRQ/FIQ disable/enable will prevent interrupt handlers from executing while a thread has the lock. On a Multiprocessor system however interrupt handlers can execute on one processor while a thread is executing on another (which will not deadlock), to correctly synchronize data access both threads and interrupt handlers should call the appropriate lock/unlock before and after access to the data.
 +
 
 +
'''Lock Hierachy'''
 +
       
 +
It is safe to acquire one lock using SpinLock() then another lock using SpinLock() and then release them in reverse order.
 +
       
 +
It is also safe to acquire one lock using SpinLock() then another lock using SpinLockIRQ(or FIQ)() and then release them in reverse order.
 +
       
 +
It is NOT safe to acquire one lock using SpinLockIRQ(or FIQ)() then another lock using SpinLock() and then release them in reverse order. This is because the first SpinLockIRQ(or FIQ)() can disable the scheduling and prevent thread preemption. If another thread is already holding the second lock then this sequence will deadlock (except on a multicore system where the other thread is running on a different CPU).
 +
       
 +
It is also safe to acquire one lock using SpinLockIRQ(or FIQ)() then another lock using SpinLockIRQ(or FIQ)() and then release them in reverse order. In this case you must ensure that any thread aquiring the second lock also calls SpinLockIRQ(or FIQ)() to thereby avoid the deadlock.
 +
       
 +
It is NOT safe to acquire one lock using SpinLockIRQ(or FIQ)() then another lock using SpinLockIRQ(or FIQ)() and then release them in the SAME order. If the situation absolutely requires this behavious then you must use SpinExchangeIRQ(or FIQ)() when holding both locks in order to reverse the order of the IRQ or FIQ renabling.
 +
       
 +
'''Mutex''' -
 +
 
 +
A mutually exlusive lock for controlling access to data. Threads yield while waiting to acquire the lock.
 +
Non recursive (can only be acquired once by the same thread)
 +
 
 +
Not suitable for use by Interrupt handlers.
 +
 
 +
Suitable for use on multiprocessor systems if the lock is allocated from shared memory (Determined during initialization).
 +
 
 +
Recommended for long held operations or holding during I/O operations.
 +
 
 +
Access is not serialized, the next thread to try obtaining the lock when it is released will succeed even if that thread was not the first waiting.
 +
 
 +
'''CriticalSection''' -
 +
 
 +
A mutually exlusive lock for serializing access to data. Threads are placed on a wait list while waiting to acquire the lock.
 +
 
 +
Recursive (can be acquired multiple times by the same thread)
 +
 
 +
Not suitable for use by Interrupt handlers.
 +
 
 +
Suitable for use on multiprocessor systems.
 +
 
 +
Recommended for long held operations or holding during I/O operations.
 +
 
 +
Access is serialized, the next thread to obtain the lock when it is released will be the thread that has been waiting longest.
 +
               
 +
'''Semaphore''' -
 +
 +
Suitable for use by Interrupt handlers for signaling only if created with SEMAPHORE_FLAG_IRQ or FIQ (Interrupt handlers must not call wait).
 +
 
 +
Suitable for use on multiprocessor systems.
 +
 
 +
Access is serialized, the next thread to acquire the semaphore when it is signaled will be the thread that has been waiting longest.
 +
 +
'''Synchronizer''' -
 +
 
 +
A reader/writer lock for serializing multiple concurrent reads and single writes to data. Threads are placed on a wait list while waiting to acquire the lock.
 +
 
 +
Recursive (reader lock can be acquired multiple times by the same thread or by other threads / writer lock can be acquired multiple times by the same thread)
 +
Not suitable for use by Interrupt handlers.
 +
 
 +
Suitable for use on multiprocessor systems.
 +
 
 +
Recommended for long held operations or holding during I/O operations.
 +
 
 +
Access is serialized, the next thread to obtain the lock when it is released will be the thread that has been waiting longest.
  
 
=== Constants ===
 
=== Constants ===

Revision as of 05:22, 30 November 2016

Return to Unit Reference


Description


Locking Primitives

Spin -

A simple spin lock for fast mutually exclusive access to data. Threads "spin" while waiting to acquire the lock and do not yield the CPU unless pre-empted.

Non recursive (can only be acquired once by the same thread)

Includes IRQ/FIQ entry and exit routines to Save/Restore the IRQ/FIQ state.

Suitable for use by Interrupt handlers if non interrupt callers use the IRQ/FIQ entry and exit routines to Lock and Unlock.

Suitable for use on multiprocessor systems if the lock is allocated from shared memory (Determined during initialization).

Not recommended for long held operations or holding during I/O operations.

Access is not serialized, the next thread to try obtaining the lock when it is released will succeed even if that thread was not the first waiting.

Usage:

Create/Destroy

Spin locks are created using SpinCreate() and destroyed using SpinDestroy(), these functions should not be called from within an Interrupt handler. Lock/Unlock (Data accessed only by threads)

To synchronize data access between threads each thread should call SpinLock() before accessing the data and SpinUnlock() when finished accessing the data. These calls do not affect the state of IRQs or FIQs and therefore do not impact interrupt latency. Lock/Unlock (Data accessed by threads and interrupt handlers)

To use a Spin lock to synchronise data access between threads and an interrupt handler each thread should call SpinLockIRQ() or SpinLockFIQ(), depending on whether the interrupt handler is servicing IRQ or FIQ requests, before accessing the data and SpinUnlockIRQ() or SpinUnlockFIQ() when finished accessing the data. These calls disable interrupts before acquiring the lock (with deadlock protection) and restore interrupts after releaseing the lock and therefore should only be used to protect very short sections of code accessing shared data to minimize the impact on interrupt latency.

Interrupt handlers should call SpinLock() before accessing the data and SpinUnlock() when finished accessing the data. In a Uniprocessor system it is technically not necessary for interrupt handlers to call lock/unlock as the use of IRQ/FIQ disable/enable will prevent interrupt handlers from executing while a thread has the lock. On a Multiprocessor system however interrupt handlers can execute on one processor while a thread is executing on another (which will not deadlock), to correctly synchronize data access both threads and interrupt handlers should call the appropriate lock/unlock before and after access to the data.

Lock Hierachy

It is safe to acquire one lock using SpinLock() then another lock using SpinLock() and then release them in reverse order.

It is also safe to acquire one lock using SpinLock() then another lock using SpinLockIRQ(or FIQ)() and then release them in reverse order.

It is NOT safe to acquire one lock using SpinLockIRQ(or FIQ)() then another lock using SpinLock() and then release them in reverse order. This is because the first SpinLockIRQ(or FIQ)() can disable the scheduling and prevent thread preemption. If another thread is already holding the second lock then this sequence will deadlock (except on a multicore system where the other thread is running on a different CPU).

It is also safe to acquire one lock using SpinLockIRQ(or FIQ)() then another lock using SpinLockIRQ(or FIQ)() and then release them in reverse order. In this case you must ensure that any thread aquiring the second lock also calls SpinLockIRQ(or FIQ)() to thereby avoid the deadlock.

It is NOT safe to acquire one lock using SpinLockIRQ(or FIQ)() then another lock using SpinLockIRQ(or FIQ)() and then release them in the SAME order. If the situation absolutely requires this behavious then you must use SpinExchangeIRQ(or FIQ)() when holding both locks in order to reverse the order of the IRQ or FIQ renabling.

Mutex -

A mutually exlusive lock for controlling access to data. Threads yield while waiting to acquire the lock. Non recursive (can only be acquired once by the same thread)

Not suitable for use by Interrupt handlers.

Suitable for use on multiprocessor systems if the lock is allocated from shared memory (Determined during initialization).

Recommended for long held operations or holding during I/O operations.

Access is not serialized, the next thread to try obtaining the lock when it is released will succeed even if that thread was not the first waiting.

CriticalSection -

A mutually exlusive lock for serializing access to data. Threads are placed on a wait list while waiting to acquire the lock.

Recursive (can be acquired multiple times by the same thread)

Not suitable for use by Interrupt handlers.

Suitable for use on multiprocessor systems.

Recommended for long held operations or holding during I/O operations.

Access is serialized, the next thread to obtain the lock when it is released will be the thread that has been waiting longest.

Semaphore -

Suitable for use by Interrupt handlers for signaling only if created with SEMAPHORE_FLAG_IRQ or FIQ (Interrupt handlers must not call wait).

Suitable for use on multiprocessor systems.

Access is serialized, the next thread to acquire the semaphore when it is signaled will be the thread that has been waiting longest.

Synchronizer -

A reader/writer lock for serializing multiple concurrent reads and single writes to data. Threads are placed on a wait list while waiting to acquire the lock.

Recursive (reader lock can be acquired multiple times by the same thread or by other threads / writer lock can be acquired multiple times by the same thread) Not suitable for use by Interrupt handlers.

Suitable for use on multiprocessor systems.

Recommended for long held operations or holding during I/O operations.

Access is serialized, the next thread to obtain the lock when it is released will be the thread that has been waiting longest.

Constants



[Expand]
Lock constants LOCK_FLAG_*


[Expand]
Spin constants SPIN_*


[Expand]
Spin state constants SPIN_STATE_*


[Expand]
Mutex constants MUTEX_*


[Expand]
Mutex state constants MUTEX_STATE_*


[Expand]
Mutex flags constants MUTEX_FLAG_*


[Expand]
Critical section constants CRITICAL_SECTION_*


[Expand]
Critical section state constants CRITICAL_SECTION_STATE_*


[Expand]
Semaphore constants SEMAPHORE_*


[Expand]
Semaphore flag constants SEMAPHORE_FLAG_*


[Expand]
Synchronizer constants SYNCHRONIZER_*


[Expand]
Synchronizer state constants SYNCHRONIZER_STATE_*


[Expand]
Synchronizer flag constants SYNCHRONIZER_FLAG_*


[Expand]
List constants LIST_*


[Expand]
List type constants LIST_TYPE_*


[Expand]
List flag constants LIST_FLAG_*


[Expand]
Queue constants QUEUE_*


[Expand]
Queue type constants QUEUE_TYPE_*


[Expand]
Queue flag constants QUEUE_FLAG_*


[Expand]
Queue key constants QUEUE_KEY_*


[Expand]
Thread constants THREAD_*


[Expand]
Thread type constants THREAD_TYPE_*


[Expand]
Thread state constants THREAD_STATE_*


[Expand]
Thread priority constants THREAD_PRIORITY_*


[Expand]
Thread name constants THREAD_NAME_*


[Expand]
Thread priority constants *_THREAD_PRIORITY


[Expand]
Thread create constants THREAD_CREATE_*


[Expand]
Thread TLS constants THREAD_TLS_*


[Expand]
Thread TLS flag constants THREAD_TLS_FLAG_*


[Expand]
Thread wait constants THREAD_LISTS_*


[Expand]
Messageslot constants MESSAGESLOT_*


[Expand]
Messageslot flag constants MESSAGESLOT_FLAG_*


[Expand]
Mailslot constants MAILSLOT_*


[Expand]
Buffer constants BUFFER_*


[Expand]
Buffer flag constants BUFFER_FLAG_*


[Expand]
Event constants EVENT_*


[Expand]
Event state constants EVENT_STATE_*


[Expand]
Event flag constants EVENT_FLAG_*


[Expand]
Timer constants TIMER_*


[Expand]
Timer state constants TIMER_STATE_*


[Expand]
Timer flag constants TIMER_FLAG_*


[Expand]
Timer key constants TIMER_KEY_*


[Expand]
Worker constants WORKER_*


[Expand]
Worker flag constants WORKER_FLAG_*


[Expand]
Tasker task constants TASKER_TASK_*


[Expand]
Scheduler migration constants SCHEDULER_MIGRATION_*


[Expand]
Scheduler preempt constants SCHEDULER_PREEMPT_*


[Expand]
Scheduler allocation constants SCHEDULER_ALLOCATION_*


[Expand]
Scheduler mask constants SCHEDULER_MASK_*


[Expand]
Scheduler quantum constants SCHEDULER_QUANTUM_*


[Expand]
Thread logging constants THREAD_LOG_*


Type definitions


To be documented

Public variables


To be documented

Function declarations



Initialization functions

[Expand]
procedure LocksInit;
Description: Initialize Locks


[Expand]
procedure ThreadsInit;
Description: Initialize Threading


[Expand]
procedure PrimaryInit;
Description: Initialize the primary CPU


[Expand]
procedure SchedulerInit;
Description: Initialize the thread scheduler


[Expand]
procedure SchedulerStart(CPUID:LongWord);
Description: Initialize the thread scheduler for secondary CPUs (Where Applicable)


[Expand]
procedure SecondaryInit;
Description: Initialize the secondary CPUs (Where Applicable)


[Expand]
procedure SecondaryBoot(CPUID:LongWord);
Description: Boot the specified secondary CPU (Where Applicable)


[Expand]
procedure SecondaryStart(CPUID:LongWord);
Description: Startup procedure for secondary CPUs (Where Applicable)


[Expand]
function IRQExecute(Parameter:Pointer):PtrInt;
Description: To be documented


[Expand]
function FIQExecute(Parameter:Pointer):PtrInt;
Description: To be documented


[Expand]
function SWIExecute(Parameter:Pointer):PtrInt;
Description: To be documented


[Expand]
function IdleExecute(Parameter:Pointer):PtrInt;
Description: To be documented


[Expand]
function MainExecute(Parameter:Pointer):PtrInt;
Description: To be documented


[Expand]
function TimerExecute(Parameter:Pointer):PtrInt;
Description: To be documented


[Expand]
function WorkerExecute(Parameter:Pointer):PtrInt;
Description: To be documented


[Expand]
function IdleCalibrate:LongWord;
Description: Calibrate the idle thread loop by counting the number of loops in 100ms


Spin functions

[Expand]
function SpinCreate:TSpinHandle; {$IFDEF SPIN_INLINE}inline; {$ENDIF}
Description: Create and insert a new Spin entry


[Expand]
function SpinCreateEx(InitialOwner:Boolean):TSpinHandle;
Description: Create and insert a new Spin entry


[Expand]
function SpinDestroy(Spin:TSpinHandle):LongWord;
Description: Destroy and remove an existing Spin entry


[Expand]
function SpinOwner(Spin:TSpinHandle):TThreadHandle;
Description: Get the current owner of an existing Spin entry


[Expand]
function SpinLock(Spin:TSpinHandle):LongWord; {$IFDEF SPIN_INLINE}inline; {$ENDIF}
Description: Lock an existing Spin entry


[Expand]
function SpinUnlock(Spin:TSpinHandle):LongWord; {$IFDEF SPIN_INLINE}inline; {$ENDIF}
Description: Unlock an existing Spin entry


[Expand]
function SpinLockIRQ(Spin:TSpinHandle):LongWord; {$IFDEF SPIN_INLINE}inline; {$ENDIF}
Description: Lock an existing Spin entry, disable IRQ and save the previous IRQ state


[Expand]
function SpinUnlockIRQ(Spin:TSpinHandle):LongWord; {$IFDEF SPIN_INLINE}inline; {$ENDIF}
Description: Unlock an existing Spin entry and restore the previous IRQ state


[Expand]
function SpinLockFIQ(Spin:TSpinHandle):LongWord; {$IFDEF SPIN_INLINE}inline; {$ENDIF}
Description: Lock an existing Spin entry, disable FIQ and save the previous FIQ state


[Expand]
function SpinUnlockFIQ(Spin:TSpinHandle):LongWord; {$IFDEF SPIN_INLINE}inline; {$ENDIF}
Description: Unlock an existing Spin entry and restore the previous FIQ state


[Expand]
function SpinLockIRQFIQ(Spin:TSpinHandle):LongWord; {$IFDEF SPIN_INLINE}inline; {$ENDIF}
Description: Lock an existing Spin entry, disable IRQ and FIQ and save the previous IRQ and FIQ state


[Expand]
function SpinUnlockIRQFIQ(Spin:TSpinHandle):LongWord; {$IFDEF SPIN_INLINE}inline; {$ENDIF}
Description: Unlock an existing Spin entry and restore the previous IRQ and FIQ state


[Expand]
function SpinCheckIRQ(Spin:TSpinHandle):Boolean;
Description: Check the mask that stores the previous IRQ state to determine if IRQ is enabled


[Expand]
function SpinCheckFIQ(Spin:TSpinHandle):Boolean;
Description: Check the mask that stores the previous FIQ state to determine if FIQ is enabled


[Expand]
function SpinExchangeIRQ(Spin1,Spin2:TSpinHandle):LongWord;
Description: Exchange the previous IRQ state between two Spin entries


[Expand]
function SpinExchangeFIQ(Spin1,Spin2:TSpinHandle):LongWord;
Description: Exchange the previous FIQ state between two Spin entries


[Expand]
function SpinLockDefault(SpinEntry:PSpinEntry):LongWord;
Description: Default version of SpinLock function used if no handler is registered


[Expand]
function SpinUnlockDefault(SpinEntry:PSpinEntry):LongWord;
Description: Default version of SpinUnlock function used if no handler is registered


[Expand]
function SpinLockIRQDefault(SpinEntry:PSpinEntry):LongWord;
Description: Default version of SpinLockIRQ function used if no handler is registered


[Expand]
function SpinUnlockIRQDefault(SpinEntry:PSpinEntry):LongWord;
Description: Default version of SpinUnlockIRQ function used if no handler is registered


[Expand]
function SpinLockFIQDefault(SpinEntry:PSpinEntry):LongWord;
Description: Default version of SpinLockFIQ function used if no handler is registered


[Expand]
function SpinUnlockFIQDefault(SpinEntry:PSpinEntry):LongWord;
Description: Default version of SpinUnlockFIQ function used if no handler is registered


[Expand]
function SpinLockIRQFIQDefault(SpinEntry:PSpinEntry):LongWord;
Description: Default version of SpinLockIRQFIQ function used if no handler is registered


Mutex functions

[Expand]
function MutexCreate:TMutexHandle; {$IFDEF MUTEX_INLINE}inline; {$ENDIF}
Description: Create and insert a new Mutex entry


[Expand]
function MutexCreateEx(InitialOwner:Boolean; SpinCount:LongWord; Flags:LongWord):TMutexHandle;
Description: Create and insert a new Mutex entry


[Expand]
function MutexDestroy(Mutex:TMutexHandle):LongWord;
Description: Destroy and remove an existing Mutex entry


[Expand]
function MutexFlags(Mutex:TMutexHandle):LongWord;
Description: Get the current flags of an existing Mutex entry


[Expand]
function MutexLock(Mutex:TMutexHandle):LongWord; {$IFDEF MUTEX_INLINE}inline; {$ENDIF}
Description: Lock an existing Mutex entry


[Expand]
function MutexUnlock(Mutex:TMutexHandle):LongWord; {$IFDEF MUTEX_INLINE}inline; {$ENDIF}
Description: Unlock an existing Mutex entry


[Expand]
function MutexTryLock(Mutex:TMutexHandle):LongWord; {$IFDEF MUTEX_INLINE}inline; {$ENDIF}
Description: Try to lock an existing Mutex entry. If the Mutex is not locked then lock it and mark the owner as the current thread. If the Mutex is already locked then return immediately with an error and do not wait for it to be unlocked.


[Expand]
function MutexCount(Mutex:TMutexHandle):LongWord;
Description: Get the current lock count of an existing Mutex entry


[Expand]
function MutexOwner(Mutex:TMutexHandle):TThreadHandle;
Description: Get the current owner of an existing Mutex entry


[Expand]
function MutexLockDefault(MutexEntry:PMutexEntry):LongWord;
Description: Default version of MutexLock function used if no handler is registered


[Expand]
function MutexUnlockDefault(MutexEntry:PMutexEntry):LongWord;
Description: Default version of MutexUnlock function used if no handler is registered


[Expand]
function MutexTryLockDefault(MutexEntry:PMutexEntry):LongWord;
Description: Default version of MutexTryLock function used if no handler is registered


Critical section functions

[Expand]
function CriticalSectionCreate:TCriticalSectionHandle;
Description: Create and insert a new CriticalSection entry


[Expand]
function CriticalSectionCreateEx(InitialOwner:Boolean; SpinCount:LongWord):TCriticalSectionHandle;
Description: Create and insert a new CriticalSection entry


[Expand]
function CriticalSectionDestroy(CriticalSection:TCriticalSectionHandle):LongWord;
Description: Destroy and remove an existing CriticalSection entry


[Expand]
function CriticalSectionCount(CriticalSection:TCriticalSectionHandle):LongWord;
Description: Get the current lock count of an existing CriticalSection entry


[Expand]
function CriticalSectionOwner(CriticalSection:TCriticalSectionHandle):TThreadHandle;
Description: Get the current owner of an existing CriticalSection entry


[Expand]
function CriticalSectionSetSpinCount(CriticalSection:TCriticalSectionHandle; SpinCount:LongWord):LongWord;
Description: Set the spin count of an existing CriticalSection entry


[Expand]
function CriticalSectionLock(CriticalSection:TCriticalSectionHandle):LongWord;
Description: Lock an existing CriticalSection entry


[Expand]
function CriticalSectionLockEx(CriticalSection:TCriticalSectionHandle; Timeout:LongWord):LongWord;
Description: Lock an existing CriticalSection entry


[Expand]
function CriticalSectionUnlock(CriticalSection:TCriticalSectionHandle):LongWord;
Description: Unlock an existing CriticalSection entry


[Expand]
function CriticalSectionTryLock(CriticalSection:TCriticalSectionHandle):LongWord;
Description: Try to lock an existing CriticalSection entry


Semaphore functions

[Expand]
function SemaphoreCreate(Count:LongWord):TSemaphoreHandle;
Description: Create and insert a new Semaphore entry


[Expand]
function SemaphoreCreateEx(Count,Maximum:LongWord; Flags:LongWord):TSemaphoreHandle;
Description: Create and insert a new Semaphore entry


[Expand]
function SemaphoreDestroy(Semaphore:TSemaphoreHandle):LongWord;
Description: Destroy and remove an existing Semaphore entry


[Expand]
function SemaphoreCount(Semaphore:TSemaphoreHandle):LongWord;
Description: Get the current count of an existing Semaphore entry


[Expand]
function SemaphoreWait(Semaphore:TSemaphoreHandle):LongWord;
Description: Wait on an existing Semaphore entry


[Expand]
function SemaphoreWaitEx(Semaphore:TSemaphoreHandle; Timeout:LongWord):LongWord;
Description: Wait on an existing Semaphore entry


[Expand]
function SemaphoreSignal(Semaphore:TSemaphoreHandle):LongWord;
Description: Signal an existing Semaphore entry


[Expand]
function SemaphoreSignalEx(Semaphore:TSemaphoreHandle; Count:LongWord; Previous:PLongWord):LongWord;
Description: Signal an existing Semaphore entry one or more times


Synchronizer functions

[Expand]
function SynchronizerCreate:TSynchronizerHandle;
Description: Create and insert a new Synchronizer entry


[Expand]
function SynchronizerCreateEx(InitialReader,InitialWriter:Boolean):TSynchronizerHandle;
Description: Create and insert a new Synchronizer entry


[Expand]
function SynchronizerDestroy(Synchronizer:TSynchronizerHandle):LongWord;
Description: Destroy and remove an existing Synchronizer entry


[Expand]
function SynchronizerReaderCount(Synchronizer:TSynchronizerHandle):LongWord;
Description: Get the current reader count of an existing Synchronizer entry


[Expand]
function SynchronizerReaderLast(Synchronizer:TSynchronizerHandle):TThreadHandle;
Description: Get the last reader thread of an existing Synchronizer entry


[Expand]
function SynchronizerReaderLock(Synchronizer:TSynchronizerHandle):LongWord;
Description: Lock an existing Synchronizer entry for reading


[Expand]
function SynchronizerReaderLockEx(Synchronizer:TSynchronizerHandle; Timeout:LongWord):LongWord;
Description: Lock an existing Synchronizer entry for reading


[Expand]
function SynchronizerReaderUnlock(Synchronizer:TSynchronizerHandle):LongWord;
Description: Unlock an existing Synchronizer entry


[Expand]
function SynchronizerReaderConvert(Synchronizer:TSynchronizerHandle):LongWord;
Description: Convert a reader lock on an existing Synchronizer entry to a writer lock


[Expand]
function SynchronizerReaderConvertEx(Synchronizer:TSynchronizerHandle; Timeout:LongWord):LongWord;
Description: Convert a reader lock on an existing Synchronizer entry to a writer lock


[Expand]
function SynchronizerWriterCount(Synchronizer:TSynchronizerHandle):LongWord;
Description: Get the current writer count of an existing Synchronizer entry


[Expand]
function SynchronizerWriterOwner(Synchronizer:TSynchronizerHandle):TThreadHandle;
Description: Get the current writer owner of an existing Synchronizer entry


[Expand]
function SynchronizerWriterLock(Synchronizer:TSynchronizerHandle):LongWord;
Description: Lock an existing Synchronizer entry for writing


[Expand]
function SynchronizerWriterLockEx(Synchronizer:TSynchronizerHandle; Timeout:LongWord):LongWord;
Description: Lock an existing Synchronizer entry for writing


[Expand]
function SynchronizerWriterUnlock(Synchronizer:TSynchronizerHandle):LongWord;
Description: Unlock an existing Synchronizer entry


[Expand]
function SynchronizerWriterConvert(Synchronizer:TSynchronizerHandle):LongWord;
Description: Convert a writer lock on an existing Synchronizer entry to a reader lock


List functions

[Expand]
function ListCreate:TListHandle; {$IFDEF LIST_INLINE}inline; {$ENDIF}
Description: Create and insert a new List entry


[Expand]
function ListCreateEx(ListType:LongWord; Flags:LongWord):TListHandle;
Description: Create and insert a new List entry


[Expand]
function ListDestroy(List:TListHandle):LongWord;
Description: Destroy and remove an existing List entry


[Expand]
function ListCount(List:TListHandle):LongWord;
Description: Get the current count from the supplied list


[Expand]
function ListAddFirst(List:TListHandle; Element:PListElement):LongWord;
Description: Add the supplied element as the first item in the List


[Expand]
function ListAddLast(List:TListHandle; Element:PListElement):LongWord;
Description: Add the supplied element as the last item in the List


[Expand]
function ListGetThread(List:TListHandle; Thread:TThreadHandle):PListElement;
Description: Find the supplied thread in the List and return its element


[Expand]
function ListGetFirst(List:TListHandle):PListElement;
Description: Get the first element from the List


[Expand]
function ListGetFirstEx(List:TListHandle; Remove:Boolean):PListElement;
Description: Get the first element from the List


[Expand]
function ListGetLast(List:TListHandle):PListElement;
Description: Get the last element from the List


[Expand]
function ListGetLastEx(List:TListHandle; Remove:Boolean):PListElement;
Description: Get the last element from the List


[Expand]
function ListInsert(List:TListHandle; Previous,Element:PListElement):LongWord;
Description: Insert a new element in the List


[Expand]
function ListRemove(List:TListHandle; Element:PListElement):LongWord;
Description: Remove an element from the List


[Expand]
function ListIsEmpty(List:TListHandle):Boolean;
Description: Check if the supplied List is empty


[Expand]
function ListNotEmpty(List:TListHandle):Boolean;
Description: Check if the supplied List is empty


[Expand]
function ListLock(List:TListHandle):LongWord;
Description: Lock the supplied List


[Expand]
function ListUnlock(List:TListHandle):LongWord;
Description: Unlock the supplied List


[Expand]
function ListLock(List:TListHandle):LongWord; {$IFDEF LIST_INLINE}inline; {$ENDIF}
Description: Lock the supplied List


[Expand]
function ListUnlock(List:TListHandle):LongWord; {$IFDEF LIST_INLINE}inline; {$ENDIF}
Description: Unlock the supplied List


Queue functions

[Expand]
function QueueCreate:TQueueHandle; {$IFDEF QUEUE_INLINE}inline; {$ENDIF}
Description: Create and insert a new Queue entry


[Expand]
function QueueCreateEx(QueueType:LongWord; Flags:LongWord):TQueueHandle;
Description: Create and insert a new Queue entry}


[Expand]
function QueueDestroy(Queue:TQueueHandle):LongWord;
Description: Destroy and remove an existing Queue entry


[Expand]
function QueueCount(Queue:TQueueHandle):LongWord;
Description: Get the current count from the supplied queue


[Expand]
function QueueEnqueue(Queue:TQueueHandle; Thread:TThreadHandle):LongWord;
Description: Add the supplied thread as the last item in the Queue


[Expand]
function QueueDequeue(Queue:TQueueHandle):TThreadHandle;
Description: Get and remove the first thread from the Queue


[Expand]
function QueueFirstKey(Queue:TQueueHandle):Integer;
Description: Get the first Key value from the Queue


[Expand]
function QueueLastKey(Queue:TQueueHandle):Integer;
Description: Get the last Key value from the Queue


[Expand]
function QueueInsertKey(Queue:TQueueHandle; Thread:TThreadHandle; Key:Integer):LongWord;
Description: Insert the supplied thread in the Queue ordered based on Key and the flags of the Queue


[Expand]
function QueueDeleteKey(Queue:TQueueHandle; Thread:TThreadHandle):LongWord;
Description: Delete the supplied thread from the Queue based on the flags of the Queue


[Expand]
function QueueIncrementKey(Queue:TQueueHandle):Integer;
Description: Increment the first Key value in the Queue


[Expand]
function QueueDecrementKey(Queue:TQueueHandle):Integer;
Description: Decrement the first Key value in the Queue


[Expand]
function QueueIsEmpty(Queue:TQueueHandle):Boolean;
Description: Check if the supplied Queue is empty


[Expand]
function QueueNotEmpty(Queue:TQueueHandle):Boolean;
Description: Check if the supplied Queue is not empty


[Expand]
function QueueLock(Queue:TQueueHandle):LongWord; {$IFDEF QUEUE_INLINE}inline; {$ENDIF}
Description: Lock the supplied Queue


[Expand]
function QueueUnlock(Queue:TQueueHandle):LongWord; {$IFDEF QUEUE_INLINE}inline; {$ENDIF}
Description: Unlock the supplied Queue


Thread functions

[Expand]
function ThreadCreate(StartProc:TThreadStart; StackSize,Priority:LongWord; Name:PChar; Parameter:Pointer):TThreadHandle;
Description: Create and insert a new Thread entry


[Expand]
function ThreadCreateEx(StartProc:TThreadStart; StackSize,Priority,Affinity,CPU:LongWord; Name:PChar; Parameter:Pointer):TThreadHandle;
Description: Create and insert a new Thread entry


[Expand]
function ThreadDestroy(Thread:TThreadHandle):LongWord;
Description: Destroy and remove an existing Thread entry


[Expand]
function ThreadGetCurrent:TThreadHandle; inline;
Description: Get the Handle of currently executing thread


[Expand]
function ThreadSetCurrent(Thread:TThreadHandle):LongWord; inline;
Description: Set the Handle of currently executing thread


[Expand]
function ThreadGetName(Thread:TThreadHandle):String;
Description: Get the name of a Thread}


[Expand]
function ThreadSetName(Thread:TThreadHandle; const Name:String):LongWord;
Description: Set the name of a Thread


[Expand]
function ThreadGetCPU(Thread:TThreadHandle):LongWord;
Description: Get the current CPU of a thread (eg CPU_ID_0)


[Expand]
function ThreadSetCPU(Thread:TThreadHandle; CPU:LongWord):LongWord;
Description: Set the current CPU of a thread (eg CPU_ID_0)


[Expand]
function ThreadGetState(Thread:TThreadHandle):LongWord;
Description: Get the current state of a thread (eg THREAD_STATE_SUSPENDED)


[Expand]
function ThreadGetLocale(Thread:TThreadHandle):LCID;
Description: Get the current locale of a thread


[Expand]
function ThreadSetLocale(Thread:TThreadHandle; Locale:LCID):LongWord;
Description: Set the locale of a thread


[Expand]
function ThreadGetTimes(Thread:TThreadHandle; var CreateTime,ExitTime,KernelTime:Int64):LongWord;
Description: Get the current times of a thread


[Expand]
function ThreadGetSwitchCount(Thread:TThreadHandle; var SwitchCount:Int64):LongWord;
Description: Get the current context switch count of a thread (How many times the thread has been scheduled)


[Expand]
function ThreadGetStackSize(Thread:TThreadHandle):LongWord;
Description: Get the current stack size of a thread


[Expand]
function ThreadGetStackBase(Thread:TThreadHandle):PtrUInt;
Description: Get the current stack base of a thread


[Expand]
function ThreadSetStackBase(Thread:TThreadHandle; StackBase:PtrUInt):LongWord;
Description: Set the current stack base of a thread


[Expand]
function ThreadGetStackPointer(Thread:TThreadHandle):PtrUInt;
Description: Get the current stack pointer of a thread


[Expand]
function ThreadGetExitCode(Thread:TThreadHandle):LongWord;
Description: Get the exit code of a Thread


[Expand]
function ThreadGetAffinity(Thread:TThreadHandle):LongWord;
Description: Get the scheduling affinity of a Thread


[Expand]
function ThreadSetAffinity(Thread:TThreadHandle; Affinity:LongWord):LongWord;
Description: Set the scheduling affinity of a Thread


[Expand]
function ThreadGetPriority(Thread:TThreadHandle):LongWord;
Description: Get the scheduling priority of a Thread


[Expand]
function ThreadSetPriority(Thread:TThreadHandle; Priority:LongWord):LongWord;
Description: Set the scheduling priority of a Thread


[Expand]
function ThreadGetLastError:LongWord;
Description: Get the last error value for the current Thread


[Expand]
function ThreadSetLastError(LastError:LongWord):LongWord;
Description: Set the last error value for the current Thread


[Expand]
function ThreadGetWaitResult:LongWord;
Description: Get the result of the last wait timeout for the current Thread


[Expand]
function ThreadGetReceiveResult:LongWord;
Description: Get the result of the last receive timeout for the current Thread


[Expand]
function ThreadAllocTlsIndex:LongWord;
Description: Allocate a TLS index in the TLS index table


[Expand]
function ThreadAllocTlsIndexEx(Flags:LongWord):LongWord;
Description: Allocate a TLS index in the TLS index table


[Expand]
function ThreadReleaseTlsIndex(TlsIndex:LongWord):LongWord;
Description: Deallocate a TLS index from the TLS index table


[Expand]
function ThreadGetTlsValue(TlsIndex:LongWord):Pointer;
Description: Get the pointer associated with the TLS index for the current thread


[Expand]
function ThreadSetTlsValue(TlsIndex:LongWord; TlsValue:Pointer):LongWord;
Description: Set the pointer associated with the TLS index for the current thread


[Expand]
function ThreadGetTlsPointer(Thread:TThreadHandle):Pointer;
Description: Get the RTL TLS (Thread Local Storage) pointer of a Thread


[Expand]
function ThreadSetTlsPointer(Thread:TThreadHandle; TlsPointer:Pointer):LongWord;
Description: Set the RTL TLS (Thread Local Storage) pointer of a Thread


[Expand]
function ThreadReady(Thread:TThreadHandle; Reschedule:Boolean):LongWord;
Description: Place the supplied Thread on the ready queue


[Expand]
function ThreadTimeout(Thread:TThreadHandle):LongWord;
Description: Place the supplied Thread on the ready queue after a timeout waiting on a resource


[Expand]
function ThreadWake(Thread:TThreadHandle):LongWord;
Description: Remove a thread prematurely from the sleep or timeout queues


[Expand]
function ThreadMigrate(Thread:TThreadHandle; CPU:LongWord):LongWord;
Description: Migrate a thread to a new CPU


[Expand]
procedure ThreadEnd(ExitCode:LongWord);
Description: Terminate the current Thread


[Expand]
function ThreadHalt(ExitCode:LongWord):LongWord;
Description: Halt the current thread so it will never be rescheduled


[Expand]
function ThreadTerminate(Thread:TThreadHandle; ExitCode:LongWord):LongWord;
Description: Terminate but do not destroy the supplied Thread


[Expand]
function ThreadYield:LongWord;
Description: Make the current thread yield the processor (Same as ThreadSleep(0))


[Expand]
function ThreadSleep(Milliseconds:LongWord):LongWord;
Description: Place the current thread on the sleep queue for a specified number of milliseconds


[Expand]
function ThreadWait(List:TListHandle; Lock:TSpinHandle; Flags:LongWord):LongWord;
Description: Put the current thread into a wait state on the supplied list


[Expand]
function ThreadWaitEx(List:TListHandle; Lock:TSpinHandle; Flags,Timeout:LongWord):LongWord;
Description: Put the current thread into a wait state with timeout on the supplied list


[Expand]
function ThreadRelease(List:TListHandle):LongWord;
Description: Release the first thread waiting on the supplied list


[Expand]
function ThreadAbandon(List:TListHandle):LongWord;
Description: Release the first thread waiting on the supplied list and return with WAIT_ABANDONED


[Expand]
function ThreadWaitTerminate(Thread:TThreadHandle; Timeout:LongWord):LongWord;
Description: Make the current thread wait until the specified thread has terminated


[Expand]
function ThreadSuspend(Thread:TThreadHandle):LongWord;
Description: Suspend a thread, placing it in hibernation


[Expand]
function ThreadResume(Thread:TThreadHandle):LongWord;
Description: Resume a suspended thread, making it ready


[Expand]
function ThreadWaitMessage:LongWord;
Description: Make the current thread wait until a message is received (indefinately)


[Expand]
function ThreadSendMessage(Thread:TThreadHandle; const Message:TMessage):LongWord;
Description: Send a message to another thread


[Expand]
function ThreadReceiveMessage(var Message:TMessage):LongWord;
Description: Make the current thread wait to receive a message (indefinately)


[Expand]
function ThreadReceiveMessageEx(var Message:TMessage; Timeout:LongWord; Remove:Boolean):LongWord;
Description: Make the current thread wait to receive a message


[Expand]
function ThreadAbandonMessage(Thread:TThreadHandle):LongWord;
Description: Tell another thread to abandon waiting for a message


[Expand]
function ThreadLock(Thread:TThreadHandle):LongWord;
Description: Lock a thread allowing access to internal structures such as the thread stack


[Expand]
function ThreadUnlock(Thread:TThreadHandle):LongWord;
Description: Unlock a thread that was locked by ThreadLock


Scheduler functions

[Expand]
function SchedulerCheck(CPUID:LongWord):LongWord;
Description: Check if the sleep queue is empty, if not then decrement the first key. Note: Then check if the timeout queue is empty, if not then decrement the first key. If either key reaches zero, return success to indicate there are threads to be woken or threads whose timeout has expired. Finally check if the termination queue is empty, if not then decrement the first key. Items will be removed from the termination queue by SchedulerReschedule.


[Expand]
function SchedulerWakeup(CPUID:LongWord):LongWord;
Description: Remove all threads from the sleep queue that have no more time to sleep. Note: Threads will be placed back on the ready queue for rescheduling.


[Expand]
function SchedulerExpire(CPUID:LongWord):LongWord;
Description: Remove all threads from the timeout queue that have no more time to wait. Note: Threads will be placed back on the ready queue for rescheduling but will return with an error indicating the timeout expired.


[Expand]
function SchedulerSwitch(CPUID:LongWord; Thread:TThreadHandle):TThreadHandle;
Description: Perform a preemptive thread switch operation under an interrupt handler. Note: The next thread to run will be selected based on remaining quantum of the current thread, ready threads at higher priority levels and scheduler priority quantum for fair scheduling of lower priority threads.


[Expand]
function SchedulerSelect(CPUID:LongWord; Thread:TThreadHandle; Yield:Boolean):TThreadHandle;
Description: Select the next thread to be run based on state, yield, quantum and priority


[Expand]
function SchedulerReschedule(Yield:Boolean):LongWord;
Description: Perform a thread switch operation when a thread yields, sleeps or waits. Note: The next thread to run will be selected based on whether the current thread is yielding or no longer ready, remaining quantum of the current thread, ready threads at higher priority levels and scheduler priority quantum for fair scheduling of lower priority threads.


[Expand]
function SchedulerMigrationEnable:LongWord;
Description: Enable scheduler thread migration


[Expand]
function SchedulerMigrationDisable:LongWord;
Description: Disable scheduler thread migration


[Expand]
function SchedulerPreemptEnable(CPUID:LongWord):LongWord;
Description: Enable thread preemption for the specified CPU


[Expand]
function SchedulerPreemptDisable(CPUID:LongWord):LongWord;
Description: Disable thread preemption for the specified CPU


[Expand]
function SchedulerAllocationEnable(CPUID:LongWord):LongWord;
Description: Enable thread allocation for the specified CPU


[Expand]
function SchedulerAllocationDisable(CPUID:LongWord):LongWord;
Description: Disable thread allocation for the specified CPU


Messageslot functions

[Expand]
function MessageslotCreate:TMessageslotHandle;
Description: Create and insert a new Messageslot entry


[Expand]
function MessageslotCreateEx(Maximum:LongWord; Flags:LongWord):TMessageslotHandle;
Description: Create and insert a new Messageslot entry


[Expand]
function MessageslotDestroy(Messageslot:TMessageslotHandle):LongWord;
Description: Destroy and remove an existing Messageslot entry


[Expand]
function MessageslotCount(Messageslot:TMessageslotHandle):LongWord;
Description: Get the number of available messages in a Messageslot entry


[Expand]
function MessageslotSend(Messageslot:TMessageslotHandle; const Message:TMessage):LongWord;
Description: Send a message to a Messageslot


[Expand]
function MessageslotReceive(Messageslot:TMessageslotHandle; var Message:TMessage):LongWord;
Description: Receive a message from a Messageslot


[Expand]
function MessageslotReceiveEx(Messageslot:TMessageslotHandle; var Message:TMessage; Timeout:LongWord):LongWord;
Description: Receive a message from a Messageslot


Mailslot functions

[Expand]
function MailslotCreate(Maximum:LongWord):TMailslotHandle;
Description: Create and insert a new Mailslot entry


[Expand]
function MailslotDestroy(Mailslot:TMailslotHandle):LongWord;
Description: Destroy and remove an existing Mailslot entry


[Expand]
function MailslotCount(Mailslot:TMailslotHandle):LongWord;
Description: Get the number of available messages in a Mailslot entry


[Expand]
function MailslotSend(Mailslot:TMailslotHandle; Data:Integer):LongWord;
Description: Send a message to a Mailslot


[Expand]
function MailslotSendEx(Mailslot:TMailslotHandle; Data:Integer; Timeout:LongWord):LongWord;
Description: Send a message to a Mailslot


[Expand]
function MailslotReceive(Mailslot:TMailslotHandle):Integer;
Description: Receive a message from a Mailslot


Tasker functions

[Expand]
function TaskerThreadSendMessage(Thread:TThreadHandle; const Message:TMessage):LongWord;
Description: Perform a ThreadSendMessage() function call using the tasker list


[Expand]
function TaskerMessageslotSend(Messageslot:TMessageslotHandle; const Message:TMessage):LongWord;
Description: Perform a MessageslotSend() function call using the tasker list


[Expand]
function TaskerSemaphoreSignal(Semaphore:TSemaphoreHandle; Count:LongWord):LongWord;
Description: Perform a SemaphoreSignal() function call using the tasker list


[Expand]
function TaskerEnqueue(Task:PTaskerTask):LongWord;
Description: Add the supplied task to the end of the Tasker list


[Expand]
function TaskerDequeue:PTaskerTask;
Description: Get and remove the first task from the Tasker list


[Expand]
function TaskerCheck:LongWord;
Description: Check if the tasker list is empty or contains tasks


[Expand]
function TaskerTrigger:LongWord;
Description: Dequeue all tasks in the tasker list and perform the requested task for each


[Expand]
function TaskerGetCount:LongWord; inline;
Description: Get the current tasker count


Return to Unit Reference