CorePartition
Macros | Enumerations | Functions | Variables
CorePartition.c File Reference
#include "CorePartition.h"
#include <assert.h>
#include <stdlib.h>

Macros

#define NOTRACE   1 ? (void)0 : (void)printf
 
#define VERIFY(term, ret)
 
#define YYTRACE   printf
 
#define TRACE   NOTRACE
 
#define Cpx_SetBit(var, bitValue)   (var |= bitBValue)
 
#define Cpx_UnsetBit(var, bitValue)   (var &= (~bitValue))
 
#define Cpx_ToggleBit(var, bitValue)   (var ^= bitValue)
 
#define THREADL_ER_STACKOVFLW   1 /* Stack Overflow */
 
#define THREAD_FACTOR_MAXBYTES   8
 
#define THREAD_NAME_MAX   8
 
#define Cpx_SetState(nNewState)   pCurrentThread->nStatus = nNewState
 
#define Cpx_NowYield()
 
#define POLY   0x8408
 
#define _CPTHREAD(T)   pCpxThread[T]
 

Enumerations

enum  __cp_lock_type { CP_LOCK_NONE = 0 , CP_LOCK_SIMPLE , CP_LOCK_SHARED }
 
enum  __cpx_thread_controller_type { CPX_CTRL_TYPE_STATIC = 1 , CPX_CTRL_BROKER_STATIC = 2 }
 

Functions

uint32_t Cpx_GetCurrentTick (void)
 
void Cpx_SleepTicks (uint32_t nSleepTime)
 
void Cpx_StackOverflowHandler (void)
 
bool Cpx_IsCoreRunning (void)
 Report if there is any running thread. More...
 
void Cpx_InternalSetNice (uint32_t nNice)
 
void Cpx_InternalSetStatus (uint8_t nStatus)
 
uint16_t Cpx_CRC16 (const uint8_t *pData, size_t nSize, uint16_t nCRC)
 
bool Cpx_StaticStart (CpxThread **ppStaticThread, size_t nStaticThreadSize)
 Start CorePartition thread provisioning. More...
 
bool Cpx_Start (size_t nThreadPartitions)
 Start CorePartition thread provisioning. More...
 
bool Cpx_CreateThread (void(*pFunction)(void *), void *pValue, size_t nStackMaxSize, uint32_t nNice)
 Create a non-Isolated context Thread. More...
 
bool Cpx_CreateStaticThread (void(*pFunction)(void *), void *pValue, CpxStaticThread *pStaticThread, size_t nStaticThreadSize, uint32_t nNice)
 Create a Thread using a static Context + virtual stack page. More...
 
void Cpx_Join (void)
 This will start all the threads. More...
 
uint8_t Cpx_Yield (void)
 Function to be called inside a thread to change context. More...
 
void Cpx_SetSleep (uint32_t nDelayTickTime)
 Will set the thread sleep parameter before Yield. More...
 
size_t Cpx_GetID (void)
 Get Current Thread ID. More...
 
size_t Cpx_GetStackSizeByID (size_t nID)
 Get Current Thread ID. More...
 
size_t Cpx_GetMaxStackSizeByID (size_t nID)
 Get total size of stack context page for a Thread ID. More...
 
uint32_t Cpx_GetNiceByID (size_t nID)
 Current Thread Nice. More...
 
uint8_t Cpx_GetStatusByID (size_t nID)
 Get a thread status for a thread ID. More...
 
uint32_t Cpx_GetLastDutyCycleByID (size_t nID)
 Last Duty Cycle of the current Thread. More...
 
uint32_t Cpx_GetLastMomentumByID (size_t nID)
 Get Current Thread last momentum on switch back. More...
 
size_t Cpx_GetNumberOfActiveThreads (void)
 Get Number of total active Threads. More...
 
size_t Cpx_GetNumberOfThreads (void)
 Get Number of assigned Threads. More...
 
size_t Cpx_GetMaxNumberOfThreads (void)
 Get Max Number of total active Threads. More...
 
size_t Cpx_GetStructContextSize (void)
 Get Thread context size. More...
 
size_t Cpx_GetContextSizeByID (size_t nID)
 Get Full context size (Contex + stack + Broker) More...
 
void Cpx_SetNice (uint32_t nNice)
 Set Current Thread Nice. More...
 
bool Cpx_IsThreadStaticByID (size_t nID)
 Return if a Threads was statically initiated. More...
 
bool Cpx_IsBrokerStaticByID (size_t nID)
 Return if a Thread's Broker interface was statically initiated. More...
 
bool Cpx_EnableStaticBroker (void *pUserContext, CpxStaticBroker *pStaticBroker, size_t nStaticBrokerSize, TopicCallback callback)
 Enable Broker for the current thread. More...
 
bool Cpx_EnableBroker (void *pUserContext, uint16_t nMaxTopics, TopicCallback callback)
 Enable Broker for the current thread. More...
 
bool Cpx_IsSubscribed (const char *pszTopic, size_t length)
 Check if the current thread already subscribe to a topic. More...
 
bool Cpx_SubscribeTopic (const char *pszTopic, size_t length)
 Subscribe for a specific topic. More...
 
bool Cpx_PublishTopic (const char *pszTopic, size_t length, size_t nAttribute, uint64_t nValue)
 Public a tuple Param and Value. More...
 
bool Cpx_WaitMessage (const char *pszTag, size_t nTagLength, CpxMsgPayload *payload)
 Wait for a specific notification from a given TAG and payload. More...
 
bool Cpx_Wait (const char *pszTag, size_t nTagLength)
 Wait for a specific notification from a given TAG. More...
 
bool Cpx_SetTopicID (const char *pszTag, size_t nTagLength, uint32_t *pnTopicID)
 
bool Cpx_NotifyOne (const char *pszTag, size_t nTagLength)
 Notify ONE TAG assigned as waiting thread. More...
 
bool Cpx_NotifyMessageOne (const char *pszTag, size_t nTagLength, size_t nAttribute, uint64_t nValue)
 Notify ONE TAGs assigned as waiting thread with a Message payload. More...
 
bool Cpx_NotifyAll (const char *pszTag, size_t nTagLength)
 Notify ALL TAGs assigned as waiting thread. More...
 
bool Cpx_NotifyMessageAll (const char *pszTag, size_t nTagLength, size_t nAttribute, uint64_t nValue)
 Notify ALL TAGs assigned as waiting thread with a Message payload. More...
 
bool Cpx_WaitVariableLock (void *pnLockID, size_t *pnStatus)
 Wait for a Variable Locks notification. More...
 
size_t Cpx_NotifyVariableLock (void *pnLockID, size_t nStatus, bool bOneOnly)
 Notify all/one Variable lock waiting for notification. More...
 
size_t Cpx_WaitingVariableLock (void *pnLockID)
 Return how much threads are locked waiting for a variable notification. More...
 
bool Cpx_LockInit (CpxSmartLock *pLock)
 Init SmartLock variable. More...
 
bool Cpx_TryLock (CpxSmartLock *pLock)
 Like Lock() but only locks in case it is unlocked. More...
 
bool Cpx_Lock (CpxSmartLock *pLock)
 Do a exclusive Lock and set to Simple lock. More...
 
bool Cpx_SharedLock (CpxSmartLock *pLock)
 Can act as multiple locks. More...
 
bool Cpx_SharedUnlock (CpxSmartLock *pLock)
 Unlock shared locks. More...
 
bool Cpx_Unlock (CpxSmartLock *pLock)
 Unlock exclusive locks. More...
 
void * Cpx_GetLockID ()
 
void * Cpx_GetLockIDByID (size_t nID)
 

Variables

CpxThread ** pCpxThread = NULL
 
CpxThreadpCurrentThread = NULL
 
void * pStartStck = NULL
 
jmp_buf jmpJoinPointer
 

Macro Definition Documentation

◆ _CPTHREAD

#define _CPTHREAD (   T)    pCpxThread[T]

◆ Cpx_NowYield

#define Cpx_NowYield ( )
Value:
{ \
Cpx_SetState (THREADL_NOW); \
Cpx_Yield (); \
}
@ THREADL_NOW
Definition: CorePartition.h:71

◆ Cpx_SetBit

#define Cpx_SetBit (   var,
  bitValue 
)    (var |= bitBValue)

◆ Cpx_SetState

#define Cpx_SetState (   nNewState)    pCurrentThread->nStatus = nNewState

◆ Cpx_ToggleBit

#define Cpx_ToggleBit (   var,
  bitValue 
)    (var ^= bitValue)

◆ Cpx_UnsetBit

#define Cpx_UnsetBit (   var,
  bitValue 
)    (var &= (~bitValue))

◆ NOTRACE

#define NOTRACE   1 ? (void)0 : (void)printf

◆ POLY

#define POLY   0x8408

◆ THREAD_FACTOR_MAXBYTES

#define THREAD_FACTOR_MAXBYTES   8

◆ THREAD_NAME_MAX

#define THREAD_NAME_MAX   8

◆ THREADL_ER_STACKOVFLW

#define THREADL_ER_STACKOVFLW   1 /* Stack Overflow */

◆ TRACE

#define TRACE   NOTRACE

◆ VERIFY

#define VERIFY (   term,
  ret 
)
Value:
if (!(term)) \
{ \
return ret; \
}

◆ YYTRACE

#define YYTRACE   printf

Enumeration Type Documentation

◆ __cp_lock_type

Enumerator
CP_LOCK_NONE 
CP_LOCK_SIMPLE 
CP_LOCK_SHARED 

◆ __cpx_thread_controller_type

Enumerator
CPX_CTRL_TYPE_STATIC 
CPX_CTRL_BROKER_STATIC 

Function Documentation

◆ Cpx_CRC16()

uint16_t Cpx_CRC16 ( const uint8_t *  pData,
size_t  nSize,
uint16_t  nCRC 
)

◆ Cpx_CreateStaticThread()

bool Cpx_CreateStaticThread ( void(*)(void *)  pFunction,
void *  pValue,
CpxStaticThread pStaticThread,
size_t  nStaticThreadSize,
uint32_t  nNice 
)

Create a Thread using a static Context + virtual stack page.

Parameters
pFunctionFunction (void Function (void* dataPointer)) as thread main
pValuedata that will be injected on Thread creation
pStaticThreadThe Static context + virtual stack pointer
nStaticThreadSizeSize of the Static Thread in bytes
nNiceWhen in time it is good to be used
Note
No memory will be created.
Returns
false In case of parameter error

◆ Cpx_CreateThread()

bool Cpx_CreateThread ( void(*)(void *)  pFunction,
void *  pValue,
size_t  nStackMaxSize,
uint32_t  nNice 
)

Create a non-Isolated context Thread.

Parameters
pFunctionFunction (void Function (void* dataPointer)) as thread main
pValuedata that will be injected on Thread creation
nStackMaxSizeSize of the Stack to be used
nNiceWhen in time it is good to be used
Returns
false fails on more provisioned threads or no more memory to create it
Note
All threads will be create with the size of stack plus context size (~100 bytes)

◆ Cpx_EnableBroker()

bool Cpx_EnableBroker ( void *  pUserContext,
uint16_t  nMaxTopics,
TopicCallback  callback 
)

Enable Broker for the current thread.

Parameters
pUserContextThe default context to be ejected if needed
nMaxTopicsMax topics to be handled by the current thread
callbackCall back to be used to process thread Synchronously
Returns
false failed to create the broker context for the current thread
Note
The default context must not be part of the thread§ stack, or it will be invalid on callback time, please use global variables or from heap (new or malloc memory), AGAIN: NEVER USE A LOCAL FUNCTION VARIABLE AS CONTEXT, USE A GLOBAL VARIABLE OR A ALLOCATED MEMORY.

◆ Cpx_EnableStaticBroker()

bool Cpx_EnableStaticBroker ( void *  pUserContext,
CpxStaticBroker pStaticBroker,
size_t  nStaticBrokerSize,
TopicCallback  callback 
)

Enable Broker for the current thread.

Parameters
pUserContextThe default context to be ejected if needed
pStaticBrokerStatic Broker CpxSubscriptions pointer
nStaticBrokerSizeStatic Broker CpxSubscriptions pointer size in bytes
callbackCall back to be used to process thread Synchronously
Returns
false failed to create the broker context for the current thread
Note
The default context must not be part of the thread stack, or it will be invalid on callback time, please use global variables or from heap (new or malloc memory), AGAIN: NEVER USE A LOCAL FUNCTION VARIABLE AS CONTEXT, USE A GLOBAL VARIABLE OR A ALLOCATED MEMORY.

◆ Cpx_GetContextSizeByID()

size_t Cpx_GetContextSizeByID ( size_t  nID)

Get Full context size (Contex + stack + Broker)

Returns
size_t

◆ Cpx_GetCurrentTick()

uint32_t Cpx_GetCurrentTick ( void  )

◆ Cpx_GetID()

size_t Cpx_GetID ( void  )

Get Current Thread ID.

Returns
size_t Thread ID

◆ Cpx_GetLastDutyCycleByID()

uint32_t Cpx_GetLastDutyCycleByID ( size_t  nID)

Last Duty Cycle of the current Thread.

Parameters
nIDThread ID
Returns
uint32_t time in Tick
Note
Tick will represent the overridden time interface otherwise it will be a single context switch to each.

◆ Cpx_GetLastMomentumByID()

uint32_t Cpx_GetLastMomentumByID ( size_t  nID)

Get Current Thread last momentum on switch back.

Parameters
nIDThread ID
Returns
uint32_t the LastMomentum in Tick
Note
Tick will represent the overridden time interface otherwise it will be a single context switch to each.

◆ Cpx_GetLockID()

void* Cpx_GetLockID ( void  )

◆ Cpx_GetLockIDByID()

void* Cpx_GetLockIDByID ( size_t  nID)

◆ Cpx_GetMaxNumberOfThreads()

size_t Cpx_GetMaxNumberOfThreads ( void  )

Get Max Number of total active Threads.

Returns
size_t number of threads

◆ Cpx_GetMaxStackSizeByID()

size_t Cpx_GetMaxStackSizeByID ( size_t  nID)

Get total size of stack context page for a Thread ID.

Parameters
nIDThread ID
Returns
size_t total size of stack context page

◆ Cpx_GetNiceByID()

uint32_t Cpx_GetNiceByID ( size_t  nID)

Current Thread Nice.

Parameters
nIDThread ID
Returns
uint32_t Nice representing tick
Note
Tick will represent the overridden time interface otherwise it will be a single context switch to each

◆ Cpx_GetNumberOfActiveThreads()

size_t Cpx_GetNumberOfActiveThreads ( void  )

Get Number of total active Threads.

Returns
size_t number of threads

◆ Cpx_GetNumberOfThreads()

size_t Cpx_GetNumberOfThreads ( void  )

Get Number of assigned Threads.

Returns
size_t number of threads

◆ Cpx_GetStackSizeByID()

size_t Cpx_GetStackSizeByID ( size_t  nID)

Get Current Thread ID.

Parameters
nIDA valid ID
Returns
size_t Thread ID
Note
if a non valid ID is provided it will return 0

◆ Cpx_GetStatusByID()

uint8_t Cpx_GetStatusByID ( size_t  nID)

Get a thread status for a thread ID.

Parameters
nIDThread ID
Returns
uint8_t Actual thread context

◆ Cpx_GetStructContextSize()

size_t Cpx_GetStructContextSize ( void  )

Get Thread context size.

Returns
size_t total size of the thread context

◆ Cpx_InternalSetNice()

void Cpx_InternalSetNice ( uint32_t  nNice)

◆ Cpx_InternalSetStatus()

void Cpx_InternalSetStatus ( uint8_t  nStatus)

◆ Cpx_IsBrokerStaticByID()

bool Cpx_IsBrokerStaticByID ( size_t  nID)

Return if a Thread's Broker interface was statically initiated.

Parameters
nIDThread ID
Returns
false if it was not statically initiated

◆ Cpx_IsCoreRunning()

bool Cpx_IsCoreRunning ( void  )

Report if there is any running thread.

Returns
false in case there is none running

◆ Cpx_IsSubscribed()

bool Cpx_IsSubscribed ( const char *  pszTopic,
size_t  length 
)

Check if the current thread already subscribe to a topic.

Parameters
pszTopicThe topic for information
lengthThe size of the topic string
Returns
false if it was not subscribed

◆ Cpx_IsThreadStaticByID()

bool Cpx_IsThreadStaticByID ( size_t  nID)

Return if a Threads was statically initiated.

Parameters
nIDThread ID
Returns
false if it was not statically initiated

◆ Cpx_Join()

void Cpx_Join ( void  )

This will start all the threads.

Note
At least ONE thread must be defines before using this function

◆ Cpx_Lock()

bool Cpx_Lock ( CpxSmartLock pLock)

Do a exclusive Lock and set to Simple lock.

Parameters
pLockThe Lock variable
Returns
false the lock is null
Note
Wait till Lock is unlocked (type none) and lock it set to type Simple and lock, SharedLock will wait till it is unlocked.

◆ Cpx_LockInit()

bool Cpx_LockInit ( CpxSmartLock pLock)

Init SmartLock variable.

Parameters
pLockThe Lock variable
Returns
false the lock is null
Note
If you re initialise it will unlock all locks

◆ Cpx_NotifyAll()

bool Cpx_NotifyAll ( const char *  pszTag,
size_t  nTagLength 
)

Notify ALL TAGs assigned as waiting thread.

Parameters
pszTagThe Tag string value
nTagLengthThe length of the tag
Returns
true At least one thread will be notified;
Note
Please note that any notification triggers a context switch yield

◆ Cpx_NotifyMessageAll()

bool Cpx_NotifyMessageAll ( const char *  pszTag,
size_t  nTagLength,
size_t  nAttribute,
uint64_t  nValue 
)

Notify ALL TAGs assigned as waiting thread with a Message payload.

Parameters
pszTagThe TAG string value
nTagLengthThe length of the tag
nAttributeThe Attribute Value to be sent
nValueThe Value of the Attribute to be sent
Returns
true At least one thread will be notified;
Note
Please note that any notification triggers a context switch yield

◆ Cpx_NotifyMessageOne()

bool Cpx_NotifyMessageOne ( const char *  pszTag,
size_t  nTagLength,
size_t  nAttribute,
uint64_t  nValue 
)

Notify ONE TAGs assigned as waiting thread with a Message payload.

Parameters
pszTagThe Tag string value
nTagLengthThe length of the tag
nAttributeThe Attribute Value to be sent
nValueThe Value of the Attribute to be sent
Returns
true At least one thread will be notified;
Note
Please note that any notification triggers a context switch yield

◆ Cpx_NotifyOne()

bool Cpx_NotifyOne ( const char *  pszTag,
size_t  nTagLength 
)

Notify ONE TAG assigned as waiting thread.

Parameters
pszTagThe Tag string value
nTagLengthThe length of the tag
Returns
true At least one thread will be notified;
Note
Please note that any notification triggers a context switch yield

◆ Cpx_NotifyVariableLock()

size_t Cpx_NotifyVariableLock ( void *  nLockID,
size_t  nStatus,
bool  bOneOnly 
)

Notify all/one Variable lock waiting for notification.

Parameters
nLockIDVariable address
nStatusPayload to be sent, a size_t
bOneOnlyIf true only one is notified
Returns
false if LockID is invalid (== 0) or no data

◆ Cpx_PublishTopic()

bool Cpx_PublishTopic ( const char *  pszTopic,
size_t  length,
size_t  nAttribute,
uint64_t  nValue 
)

Public a tuple Param and Value.

Parameters
pszTopicTopic name to publish
lengthThe size of the topic string
nAttributeA attribute to be use to identify the value
nValueA value for the attribute (tuple)
Returns
true If at least one subscriber received the data.

◆ Cpx_SetNice()

void Cpx_SetNice ( uint32_t  nNice)

Set Current Thread Nice.

Parameters
nNiceNice to be used

◆ Cpx_SetSleep()

void Cpx_SetSleep ( uint32_t  nDelayTickTime)

Will set the thread sleep parameter before Yield.

Parameters
nDelayTickTimeHow much ticks to sleep
Note
if Time has been overridden it tick will correspond to the time frame used by sleep overridden function

◆ Cpx_SetTopicID()

bool Cpx_SetTopicID ( const char *  pszTag,
size_t  nTagLength,
uint32_t *  pnTopicID 
)

◆ Cpx_SharedLock()

bool Cpx_SharedLock ( CpxSmartLock pLock)

Can act as multiple locks.

Parameters
pLockThe Lock variable
Returns
false If lock is null
Note
Can acquire multiples locks and lock() will wait till all multiples locks has been unlocked to lock exclusively

◆ Cpx_SharedUnlock()

bool Cpx_SharedUnlock ( CpxSmartLock pLock)

Unlock shared locks.

Parameters
pLockThe Lock variable
Returns
false If lock is null

◆ Cpx_SleepTicks()

void Cpx_SleepTicks ( uint32_t  nSleepTime)

◆ Cpx_StackOverflowHandler()

void Cpx_StackOverflowHandler ( void  )

◆ Cpx_Start()

bool Cpx_Start ( size_t  nThreadPartitions)

Start CorePartition thread provisioning.

Parameters
nThreadPartitionsNumber of threads to be provisioned
Returns
true true if successfully created all provisioned threads

◆ Cpx_StaticStart()

bool Cpx_StaticStart ( CpxThread **  ppStaticThread,
size_t  nStaticThreadSize 
)

Start CorePartition thread provisioning.

Parameters
ppStaticThreadStatic Thread pointer type CpxThread**
nStaticThreadSizeStatic Thread size in bytes
Returns
true true if successfully created all provisioned threads

◆ Cpx_SubscribeTopic()

bool Cpx_SubscribeTopic ( const char *  pszTopic,
size_t  length 
)

Subscribe for a specific topic.

Parameters
pszTopicThe topic to listen for information
lengthThe size of the topic string
Returns
false if there is no more room for a new subscription

◆ Cpx_TryLock()

bool Cpx_TryLock ( CpxSmartLock pLock)

Like Lock() but only locks in case it is unlocked.

Parameters
pLockThe Lock variable
Returns
false If lock is null or lock is locked
Note
Wait till exclusive Lock is unlocked (type none) and lock it set to type Simple and lock, SharedLock will wait till it is unlocked.

◆ Cpx_Unlock()

bool Cpx_Unlock ( CpxSmartLock pLock)

Unlock exclusive locks.

Parameters
pLockThe Lock variable
Returns
false If lock is null

◆ Cpx_Wait()

bool Cpx_Wait ( const char *  pszTag,
size_t  nTagLength 
)

Wait for a specific notification from a given TAG.

Parameters
pszTagThe Tag string value
nTagLengthThe length of the tag
Returns
true For success on receiving notification

◆ Cpx_WaitingVariableLock()

size_t Cpx_WaitingVariableLock ( void *  nLockID)

Return how much threads are locked waiting for a variable notification.

Parameters
nLockIDVariable address
Returns
size_t How much active waiting for a variable

◆ Cpx_WaitMessage()

bool Cpx_WaitMessage ( const char *  pszTag,
size_t  nTagLength,
CpxMsgPayload payload 
)

Wait for a specific notification from a given TAG and payload.

Parameters
pszTagThe Tag string value
nTagLengthThe length of the tag
payloadThe payload with the information sent by other thread
Returns
false if an error occurred
Note
if a Tag was notified using NotifyOne or NotifyAll the thread will receive 0 otherwise will receive the same value sent.

◆ Cpx_WaitVariableLock()

bool Cpx_WaitVariableLock ( void *  nLockID,
size_t *  pnStatus 
)

Wait for a Variable Locks notification.

Parameters
nLockIDVariable address
pnStatusPayload to be sent, a size_t
Returns
false if LockID is invalid (== 0) or no data

◆ Cpx_Yield()

uint8_t Cpx_Yield ( void  )

Function to be called inside a thread to change context.

Returns
true always return true while the thread is valid
Note
Cooperative state yield, should not be used with preemption for speed reasons, that will not complay with LockKernel, for this use PreemptionYield.

Variable Documentation

◆ jmpJoinPointer

jmp_buf jmpJoinPointer

◆ pCpxThread

CpxThread** pCpxThread = NULL

◆ pCurrentThread

CpxThread* pCurrentThread = NULL

◆ pStartStck

void* pStartStck = NULL