CorePartition
CorePartition.h
Go to the documentation of this file.
1 /*
2  CoreParitionOS Initiative
3 
4  MIT License
5 
6  Copyright (c) 2021 Gustavo Campos
7 
8  Permission is hereby granted, free of charge, to any person obtaining a copy
9  of this software and associated documentation files (the "Software"), to deal
10  in the Software without restriction, including without limitation the rights
11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  copies of the Software, and to permit persons to whom the Software is
13  furnished to do so, subject to the following conditions:
14 
15  The above copyright notice and this permission notice shall be included in all
16  copies or substantial portions of the Software.
17 
18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  SOFTWARE.
25 */
26 
27 
28 #ifndef Cpx_hpp
29 #define Cpx_hpp
30 
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #else
35 #if 0
36 #ifndef bool
37 #define bool uint8_t
38 #define false 0
39 #define true (!false)
40 #endif
41 #endif
42 #endif
43 
44 #include <setjmp.h>
45 #include <stdbool.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <string.h>
49 
50  /*
51  * IMPORTANT!
52  * KEEP RUNNING STATES ALWAYS
53  * ABOVE RUNNING AND BLOCKING
54  * STATES BELOW RUNNING.
55  */
57  {
58  /*
59  * BLocking
60  */
65 
66  /*
67  * Running
68  */
72 
73  THREADL_SLEEP = 200
74  };
75 
76  typedef struct
77  {
78  size_t nThreadID;
79  size_t nAttribute;
80  uint64_t nValue;
81  } CpxMsgPayload;
82 
83  typedef struct
84  {
87  } CpxSmartLock;
88 
89  /*
90  * TOPIC Callback for broker implemenation
91  */
92  typedef void (*TopicCallback) (void* pContext, const char* pszTopic, size_t nSize, CpxMsgPayload payLoad);
93 
94  /*
95  * Defining subscription structure
96  */
97  typedef struct CpxSubscriptions CpxSubscriptions;
99  {
101  void* pContext;
102  uint16_t nMaxTopics;
103  uint16_t nTopicCount;
104  uint32_t nTopicList;
105  };
106 
107  /*
108  * Global definition for CpxThread
109  */
110  typedef struct
111  {
112  void* pLastStack;
114 
115  union
116  {
118 
119  struct
120  {
121  void (*pFunction) (void* pStart);
122  void* pValue;
123  } func;
124  } mem;
125 
127  size_t nStackSize;
128 
129  uint32_t nNice;
130  uint32_t nLastMomentun;
131  uint32_t nExecTime;
132 
133  /*
134  * This will be, also, used as
135  * buffer for sleep.
136  */
137  uint32_t nNotifyUID;
138 
139  union
140  {
141  uint32_t nSleepTime;
143  CpxMsgPayload payload; /* data */
144  } control;
145 
147  uint8_t nStatus;
148  uint8_t stackPage;
149  } CpxThread;
150 
151  /* Static Memory types */
152  typedef uint8_t CpxStaticThread;
153  typedef uint8_t CpxStaticBroker;
154 
155  /* External prototypes for functions overloading */
156  extern uint32_t Cpx_GetCurrentTick (void);
157  extern void Cpx_SleepTicks (uint32_t);
158  extern void Cpx_StackOverflowHandler (void);
159 
160  /* Official version */
161  static const char CpxVersionCode[]="2.7.0";
162  static const char CpxVersion[] = "V2.7.0 from " __TIMESTAMP__;
163 
171  bool Cpx_Start (size_t nThreadPartitions);
172 
181  bool Cpx_StaticStart (CpxThread** ppStaticThread, size_t nStaticThreadSize);
182 
195  bool Cpx_CreateThread (void (*pFunction) (void*), void* pValue, size_t nStackMaxSize, uint32_t nNice);
196 
204 #define Cpx_GetStaticContextSize(nStaticThreadSize) (nStaticThreadSize >= sizeof (CpxThread) ? (nStaticThreadSize - sizeof (CpxThread)) : 0)
205 
213 #define Cpx_GetStaticThreadSize(nStackMaxSize) (sizeof (CpxThread) + nStackMaxSize)
214 
229  bool Cpx_CreateStaticThread (void (*pFunction) (void*), void* pValue, CpxStaticThread* pStaticThread, size_t nStaticThreadSize, uint32_t nNice);
230 
236  void Cpx_Join (void);
237 
247  uint8_t Cpx_Yield (void);
248 
257 #define Cpx_Sleep(nDelayTickTime) \
258  { \
259  Cpx_SetSleep (nDelayTickTime); \
260  Cpx_Yield (); \
261  }
262 
271  void Cpx_SetSleep (uint32_t nDelayTickTime);
272 
278  size_t Cpx_GetID (void);
279 
289  size_t Cpx_GetStackSizeByID (size_t nID);
290 
294 #define Cpx_GetStackSize() Cpx_GetStackSizeByID (Cpx_GetID ())
295 
303  size_t Cpx_GetMaxStackSizeByID (size_t nID);
304 
308 #define Cpx_GetMaxStackSize() Cpx_GetMaxStackSizeByID (Cpx_GetID ())
309 
315  size_t Cpx_GetStructContextSize (void);
316 
322  size_t Cpx_GetContextSizeByID (size_t nID);
323 
327 #define Cpx_GetContexSize() (Cpx_GetContextSizeByID (Cpx_GetID ()))
328 
336  uint8_t Cpx_GetStatusByID (size_t nID);
337 
341 #define Cpx_GetStatus() Cpx_GetStatusByID (Cpx_GetID ())
342 
353  uint32_t Cpx_GetNiceByID (size_t nID);
354 
358 #define Cpx_GetNice() Cpx_GetNiceByID (Cpx_GetID ())
359 
365  void Cpx_SetNice (uint32_t nNice);
366 
377  uint32_t Cpx_GetLastMomentumByID (size_t nID);
378 
382 #define Cpx_GetLastMomentum() Cpx_GetLastMomentumByID (Cpx_GetID ())
383 
394  uint32_t Cpx_GetLastDutyCycleByID (size_t nID);
395 
399 #define Cpx_GetLastDutyCycle() Cpx_GetLastDutyCycleByID (Cpx_GetID ())
400 
406  size_t Cpx_GetNumberOfActiveThreads (void);
407 
413  size_t Cpx_GetNumberOfThreads (void);
414 
420  size_t Cpx_GetMaxNumberOfThreads (void);
421 
429  char Cpx_IsSecureByID (size_t nID);
430 
436  bool Cpx_IsCoreRunning (void);
437 
445  bool Cpx_IsThreadStaticByID (size_t nID);
446 
454 #define Cpx_IsThreadStatic() Cpx_IsThreadStaticByID (Cpx_GetID ())
455 
463  bool Cpx_IsBrokerStaticByID (size_t nID);
464 
473 #define Cpx_IsBrokerStatic() Cpx_IsThreadStaticByID (Cpx_GetID ())
474 
489  bool Cpx_EnableBroker (void* pUserContext, uint16_t nMaxTopics, TopicCallback callback);
490 
498 #define Cpx_GetStaticBrokerSize(nMaxTopics) \
499  (sizeof (CpxSubscriptions) + (sizeof (uint32_t) * ((nMaxTopics <= 1) ? sizeof (uint32_t) : nMaxTopics - 1)))
500 
501  /*
502  * Calculate how much subscription a broker size can do.
503  */
504 #define Cpx_GetStaticBrokerMaxTopics(nStaticSubsSize) \
505  (nStaticSubsSize <= sizeof (CpxSubscriptions) ? 0 : ((nStaticSubsSize - sizeof (CpxSubscriptions)) / sizeof (uint32_t)) + 1)
506 
522  bool Cpx_EnableStaticBroker (void* pUserContext, CpxStaticBroker* pStaticBroker, size_t nStaticBrokerSize, TopicCallback callback);
523 
532  bool Cpx_SubscribeTopic (const char* pszTopic, size_t length);
533 
545  bool Cpx_PublishTopic (const char* pszTopic, size_t length, size_t nAttribute, uint64_t nValue);
546 
555  bool Cpx_IsSubscribed (const char* pszTopic, size_t length);
556 
567  bool Cpx_NotifyOne (const char* pszTag, size_t nTagLength);
568 
581  bool Cpx_NotifyMessageOne (const char* pszTag, size_t nTagLength, size_t nAttribute, uint64_t nValue);
582 
593  bool Cpx_NotifyAll (const char* pszTag, size_t nTagLength);
594 
607  bool Cpx_NotifyMessageAll (const char* pszTag, size_t nTagLength, size_t nAttribute, uint64_t nValue);
608 
617  bool Cpx_Wait (const char* pszTag, size_t nTagLength);
618 
632  bool Cpx_WaitMessage (const char* pszTag, size_t nTagLength, CpxMsgPayload* payload);
633 
643  bool Cpx_LockInit (CpxSmartLock* pLock);
644 
656  bool Cpx_Lock (CpxSmartLock* pLock);
657 
669  bool Cpx_TryLock (CpxSmartLock* pLock);
670 
682  bool Cpx_SharedLock (CpxSmartLock* pLock);
683 
691  bool Cpx_Unlock (CpxSmartLock* pLock);
692 
700  bool Cpx_SharedUnlock (CpxSmartLock* pLock);
701 
710  bool Cpx_WaitVariableLock (void* nLockID, size_t* pnStatus);
711 
719  size_t Cpx_WaitingVariableLock (void* nLockID);
720 
730  size_t Cpx_NotifyVariableLock (void* nLockID, size_t nStatus, bool bOneOnly);
731 
741 #define Cpx_NotifyVariableLockOne(nLockID, nStatus) Cpx_NotifyVariableLock (nLockID, nStatus, true)
742 
752 #define Cpx_NotifyVariableLockAll(nLockID, nStatus) Cpx_NotifyVariableLock (nLockID, nStatus, false)
753 
754  void* Cpx_GetLockID (void);
755 
756  void* Cpx_GetLockIDByID (size_t nID);
757 
758 #ifdef __cplusplus
759 }
760 #endif
761 
762 #endif /* Cpx_hpp */
bool Cpx_IsSubscribed(const char *pszTopic, size_t length)
Check if the current thread already subscribe to a topic.
Definition: CorePartition.c:756
bool Cpx_Start(size_t nThreadPartitions)
Start CorePartition thread provisioning.
Definition: CorePartition.c:248
size_t Cpx_GetMaxNumberOfThreads(void)
Get Max Number of total active Threads.
Definition: CorePartition.c:638
size_t Cpx_GetStackSizeByID(size_t nID)
Get Current Thread ID.
Definition: CorePartition.c:581
__THREADL_TYPES
Definition: CorePartition.h:57
@ THREADL_STOPPED
Definition: CorePartition.h:62
@ THREADL_WAITTAG
Definition: CorePartition.h:64
@ THREADL_START
Definition: CorePartition.h:70
@ THREADL_SLEEP
Definition: CorePartition.h:73
@ THREADL_NONE
Definition: CorePartition.h:61
@ THREADL_RUNNING
Definition: CorePartition.h:69
@ THREADL_NOW
Definition: CorePartition.h:71
@ THREADL_LOCK
Definition: CorePartition.h:63
bool Cpx_SharedLock(CpxSmartLock *pLock)
Can act as multiple locks.
Definition: CorePartition.c:1104
bool Cpx_WaitVariableLock(void *nLockID, size_t *pnStatus)
Wait for a Variable Locks notification.
Definition: CorePartition.c:972
size_t Cpx_GetContextSizeByID(size_t nID)
Get Full context size (Contex + stack + Broker)
Definition: CorePartition.c:650
uint32_t Cpx_GetLastDutyCycleByID(size_t nID)
Last Duty Cycle of the current Thread.
Definition: CorePartition.c:610
uint8_t Cpx_GetStatusByID(size_t nID)
Get a thread status for a thread ID.
Definition: CorePartition.c:603
uint8_t CpxStaticBroker
Definition: CorePartition.h:153
size_t Cpx_GetNumberOfThreads(void)
Get Number of assigned Threads.
Definition: CorePartition.c:631
bool Cpx_Wait(const char *pszTag, size_t nTagLength)
Wait for a specific notification from a given TAG.
Definition: CorePartition.c:848
bool Cpx_Lock(CpxSmartLock *pLock)
Do a exclusive Lock and set to Simple lock.
Definition: CorePartition.c:1086
void(* TopicCallback)(void *pContext, const char *pszTopic, size_t nSize, CpxMsgPayload payLoad)
Definition: CorePartition.h:92
size_t Cpx_GetMaxStackSizeByID(size_t nID)
Get total size of stack context page for a Thread ID.
Definition: CorePartition.c:589
bool Cpx_StaticStart(CpxThread **ppStaticThread, size_t nStaticThreadSize)
Start CorePartition thread provisioning.
Definition: CorePartition.c:241
bool Cpx_IsBrokerStaticByID(size_t nID)
Return if a Thread's Broker interface was statically initiated.
Definition: CorePartition.c:683
bool Cpx_TryLock(CpxSmartLock *pLock)
Like Lock() but only locks in case it is unlocked.
Definition: CorePartition.c:1073
bool Cpx_EnableStaticBroker(void *pUserContext, CpxStaticBroker *pStaticBroker, size_t nStaticBrokerSize, TopicCallback callback)
Enable Broker for the current thread.
Definition: CorePartition.c:736
bool Cpx_PublishTopic(const char *pszTopic, size_t length, size_t nAttribute, uint64_t nValue)
Public a tuple Param and Value.
Definition: CorePartition.c:796
size_t Cpx_GetID(void)
Get Current Thread ID.
Definition: CorePartition.c:574
uint32_t Cpx_GetNiceByID(size_t nID)
Current Thread Nice.
Definition: CorePartition.c:596
void * Cpx_GetLockID(void)
Definition: CorePartition.c:1155
bool Cpx_WaitMessage(const char *pszTag, size_t nTagLength, CpxMsgPayload *payload)
Wait for a specific notification from a given TAG and payload.
Definition: CorePartition.c:826
void Cpx_SetNice(uint32_t nNice)
Set Current Thread Nice.
Definition: CorePartition.c:669
bool Cpx_Unlock(CpxSmartLock *pLock)
Unlock exclusive locks.
Definition: CorePartition.c:1136
void * Cpx_GetLockIDByID(size_t nID)
Definition: CorePartition.c:1162
uint8_t CpxStaticThread
Definition: CorePartition.h:152
char Cpx_IsSecureByID(size_t nID)
Return the Secure Status for a thread ID.
bool Cpx_NotifyAll(const char *pszTag, size_t nTagLength)
Notify ALL TAGs assigned as waiting thread.
Definition: CorePartition.c:940
size_t Cpx_GetStructContextSize(void)
Get Thread context size.
Definition: CorePartition.c:645
uint32_t Cpx_GetCurrentTick(void)
Definition: CorePartition.c:118
uint32_t Cpx_GetLastMomentumByID(size_t nID)
Get Current Thread last momentum on switch back.
Definition: CorePartition.c:617
size_t Cpx_WaitingVariableLock(void *nLockID)
Return how much threads are locked waiting for a variable notification.
Definition: CorePartition.c:1038
void Cpx_StackOverflowHandler(void)
Definition: CorePartition.c:135
bool Cpx_IsCoreRunning(void)
Report if there is any running thread.
Definition: CorePartition.c:146
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.
Definition: CorePartition.c:927
bool Cpx_SharedUnlock(CpxSmartLock *pLock)
Unlock shared locks.
Definition: CorePartition.c:1119
size_t Cpx_GetNumberOfActiveThreads(void)
Get Number of total active Threads.
Definition: CorePartition.c:624
void Cpx_SleepTicks(uint32_t)
Definition: CorePartition.c:125
bool Cpx_IsThreadStaticByID(size_t nID)
Return if a Threads was statically initiated.
Definition: CorePartition.c:676
bool Cpx_SubscribeTopic(const char *pszTopic, size_t length)
Subscribe for a specific topic.
Definition: CorePartition.c:778
bool Cpx_CreateThread(void(*pFunction)(void *), void *pValue, size_t nStackMaxSize, uint32_t nNice)
Create a non-Isolated context Thread.
Definition: CorePartition.c:324
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.
Definition: CorePartition.c:329
void Cpx_Join(void)
This will start all the threads.
Definition: CorePartition.c:435
void Cpx_SetSleep(uint32_t nDelayTickTime)
Will set the thread sleep parameter before Yield.
Definition: CorePartition.c:559
bool Cpx_EnableBroker(void *pUserContext, uint16_t nMaxTopics, TopicCallback callback)
Enable Broker for the current thread.
Definition: CorePartition.c:744
uint8_t Cpx_Yield(void)
Function to be called inside a thread to change context.
Definition: CorePartition.c:520
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.
Definition: CorePartition.c:953
size_t Cpx_NotifyVariableLock(void *nLockID, size_t nStatus, bool bOneOnly)
Notify all/one Variable lock waiting for notification.
Definition: CorePartition.c:1003
bool Cpx_NotifyOne(const char *pszTag, size_t nTagLength)
Notify ONE TAG assigned as waiting thread.
Definition: CorePartition.c:914
bool Cpx_LockInit(CpxSmartLock *pLock)
Init SmartLock variable.
Definition: CorePartition.c:1063
Definition: CorePartition.h:77
size_t nThreadID
Definition: CorePartition.h:78
size_t nAttribute
Definition: CorePartition.h:79
uint64_t nValue
Definition: CorePartition.h:80
Definition: CorePartition.h:84
size_t nSharedLockCount
Definition: CorePartition.h:85
bool bExclusiveLock
Definition: CorePartition.h:86
Definition: CorePartition.h:99
uint16_t nTopicCount
Definition: CorePartition.h:103
void * pContext
Definition: CorePartition.h:101
TopicCallback callback
Definition: CorePartition.h:100
uint16_t nMaxTopics
Definition: CorePartition.h:102
uint32_t nTopicList
Definition: CorePartition.h:104
Definition: CorePartition.h:111
void * pValue
Definition: CorePartition.h:122
uint8_t nThreadController
Definition: CorePartition.h:146
void * pnVariableLockID
Definition: CorePartition.h:142
uint32_t nLastMomentun
Definition: CorePartition.h:130
uint32_t nNice
Definition: CorePartition.h:129
jmp_buf jmpRegisterBuffer
Definition: CorePartition.h:117
uint32_t nSleepTime
Definition: CorePartition.h:141
uint32_t nNotifyUID
Definition: CorePartition.h:137
CpxSubscriptions * pSubscriptions
Definition: CorePartition.h:113
CpxMsgPayload payload
Definition: CorePartition.h:143
size_t nStackMaxSize
Definition: CorePartition.h:126
uint8_t nStatus
Definition: CorePartition.h:147
uint32_t nExecTime
Definition: CorePartition.h:131
size_t nStackSize
Definition: CorePartition.h:127
uint8_t stackPage
Definition: CorePartition.h:148
void * pLastStack
Definition: CorePartition.h:112