77
88#include <ocre/ocre.h>
99#include "ocre_core_external.h"
10- #include <zephyr/kernel.h>
11- #include <zephyr/logging/log.h>
12- #include <zephyr/sys/mem_manage.h>
13- #include <zephyr/drivers/mm/system_mm.h>
14- #include <zephyr/sys/slist.h>
1510#include <stdlib.h>
1611#include <string.h>
12+ #include <stdint.h>
13+ #include <stdbool.h>
14+ #include <errno.h>
15+ #include <stddef.h>
16+
1717LOG_MODULE_DECLARE (ocre_cs_component , OCRE_LOG_LEVEL );
18- #include <zephyr/autoconf.h>
19- #include "../../../../../wasm-micro-runtime/core/iwasm/include/lib_export.h"
20- #include "bh_log.h"
21- #include "../ocre_timers/ocre_timer.h"
18+
19+ #ifdef CONFIG_OCRE_GPIO
2220#include "../ocre_gpio/ocre_gpio.h"
21+ #endif
22+
23+ #ifdef CONFIG_OCRE_CONTAINER_MESSAGING
2324#include "../ocre_messaging/ocre_messaging.h"
24- #include "ocre_common.h"
25- #include <zephyr/sys/ring_buffer.h>
26- // Place queue buffer in a dedicated section with alignments
27- #define SIZE_OCRE_EVENT_BUFFER 32
28- __attribute__((section (".noinit.ocre_event_queue" ),
29- aligned (8 ))) char ocre_event_queue_buffer [SIZE_OCRE_EVENT_BUFFER * sizeof (ocre_event_t )];
30- char * ocre_event_queue_buffer_ptr = ocre_event_queue_buffer ; // Pointer for validation
25+ #endif
3126
32- K_MSGQ_DEFINE (ocre_event_queue , sizeof (ocre_event_t ), SIZE_OCRE_EVENT_BUFFER , 4 );
33- bool ocre_event_queue_initialized = false;
34- struct k_spinlock ocre_event_queue_lock ;
27+ #include "ocre_common.h"
3528
3629typedef struct module_node {
3730 ocre_module_context_t ctx ;
38- sys_snode_t node ;
31+ core_snode_t node ;
3932} module_node_t ;
4033
41- static sys_slist_t module_registry ;
42- static struct k_mutex registry_mutex ;
34+ static core_slist_t module_registry ;
35+ static core_mutex_t registry_mutex ;
36+
37+ #define SIZE_OCRE_EVENT_BUFFER 32
38+
39+ /* Unified event queue implementation */
40+ core_eventq_t ocre_event_queue ;
41+ bool ocre_event_queue_initialized = false;
42+ core_spinlock_t ocre_event_queue_lock ;
4343
4444static struct cleanup_handler {
4545 ocre_resource_type_t type ;
@@ -95,22 +95,24 @@ int ocre_get_event(wasm_exec_env_t exec_env, uint32_t type_offset, uint32_t id_o
9595 }
9696
9797 ocre_event_t event ;
98- k_spinlock_key_t key = k_spin_lock (& ocre_event_queue_lock );
99- int ret = k_msgq_peek (& ocre_event_queue , & event );
98+ int ret ;
99+
100+ /* Generic event queue implementation for both platforms */
101+ core_spinlock_key_t key = core_spinlock_lock (& ocre_event_queue_lock );
102+ ret = core_eventq_peek (& ocre_event_queue , & event );
100103 if (ret != 0 ) {
101- // k_msg_peek returns either 0, or -ENOMSG if empty
102- k_spin_unlock (& ocre_event_queue_lock , key );
104+ core_spinlock_unlock (& ocre_event_queue_lock , key );
103105 return - ENOMSG ;
104106 }
105107
106108 if (event .owner != module_inst ) {
107- k_spin_unlock (& ocre_event_queue_lock , key );
109+ core_spinlock_unlock (& ocre_event_queue_lock , key );
108110 return - EPERM ;
109111 }
110112
111- ret = k_msgq_get (& ocre_event_queue , & event , K_FOREVER );
113+ ret = core_eventq_get (& ocre_event_queue , & event );
112114 if (ret != 0 ) {
113- k_spin_unlock (& ocre_event_queue_lock , key );
115+ core_spinlock_unlock (& ocre_event_queue_lock , key );
114116 return - ENOENT ;
115117 }
116118
@@ -163,12 +165,12 @@ int ocre_get_event(wasm_exec_env_t exec_env, uint32_t type_offset, uint32_t id_o
163165 =================================
164166 */
165167 default : {
166- k_spin_unlock (& ocre_event_queue_lock , key );
168+ core_spinlock_unlock (& ocre_event_queue_lock , key );
167169 LOG_ERR ("Invalid event type: %u" , event .type );
168170 return - EINVAL ;
169171 }
170172 }
171- k_spin_unlock (& ocre_event_queue_lock , key );
173+ core_spinlock_unlock (& ocre_event_queue_lock , key );
172174 return 0 ;
173175}
174176
@@ -178,21 +180,28 @@ int ocre_common_init(void) {
178180 LOG_INF ("Common system already initialized" );
179181 return 0 ;
180182 }
181- k_mutex_init ( & registry_mutex );
182- sys_slist_init ( & module_registry );
183- if (( uintptr_t ) ocre_event_queue_buffer_ptr % 4 != 0 ) {
184- LOG_ERR ( "ocre_event_queue_buffer misaligned: %p" , ( void * ) ocre_event_queue_buffer_ptr );
185- return - EINVAL ;
186- }
187- k_msgq_init ( & ocre_event_queue , ocre_event_queue_buffer , sizeof ( ocre_event_t ), 64 );
183+
184+ core_mutex_init ( & registry_mutex );
185+ core_slist_init ( & module_registry );
186+
187+ core_eventq_init ( & ocre_event_queue , sizeof ( ocre_event_t ), SIZE_OCRE_EVENT_BUFFER ) ;
188+
189+ /* Purge any stale events from queue */
188190 ocre_event_t dummy ;
189- while (k_msgq_get (& ocre_event_queue , & dummy , K_NO_WAIT ) == 0 ) {
191+ while (core_eventq_get (& ocre_event_queue , & dummy ) == 0 ) {
190192 LOG_INF ("Purged stale event from queue" );
191193 }
194+
195+ /* Temporary platform-specific initialization */
196+ #ifdef __ZEPHYR__
197+ /* No additional Zephyr-specific initialization needed */
198+ #else /* POSIX */
199+ pthread_mutex_init (& ocre_event_queue_lock .mutex , NULL );
200+ #endif
201+
192202 ocre_event_queue_initialized = true;
193203#if EVENT_THREAD_POOL_SIZE > 0
194- LOG_INF ("ocre_event_queue initialized at %p, size=%d, buffer=%p" , (void * )& ocre_event_queue , sizeof (ocre_event_t ),
195- (void * )ocre_event_queue_buffer_ptr );
204+ LOG_INF ("ocre_event_queue initialized at %p, size=%d" , (void * )& ocre_event_queue , sizeof (ocre_event_t ));
196205 for (int i = 0 ; i < EVENT_THREAD_POOL_SIZE ; i ++ ) {
197206 event_args [i ].index = i ;
198207 char thread_name [16 ];
@@ -208,6 +217,7 @@ int ocre_common_init(void) {
208217#endif
209218 initialized = true;
210219 common_initialized = true;
220+
211221 LOG_INF ("OCRE common initialized successfully" );
212222 return 0 ;
213223}
@@ -241,7 +251,7 @@ int ocre_register_module(wasm_module_inst_t module_inst) {
241251 LOG_ERR ("Null module instance" );
242252 return - EINVAL ;
243253 }
244- module_node_t * node = k_malloc (sizeof (module_node_t ));
254+ module_node_t * node = core_malloc (sizeof (module_node_t ));
245255 if (!node ) {
246256 LOG_ERR ("Failed to allocate module node" );
247257 return - ENOMEM ;
@@ -250,16 +260,18 @@ int ocre_register_module(wasm_module_inst_t module_inst) {
250260 node -> ctx .exec_env = wasm_runtime_create_exec_env (module_inst , OCRE_WASM_STACK_SIZE );
251261 if (!node -> ctx .exec_env ) {
252262 LOG_ERR ("Failed to create exec env for module %p" , (void * )module_inst );
253- k_free (node );
263+ core_free (node );
254264 return - ENOMEM ;
255265 }
256266 node -> ctx .in_use = true;
257- node -> ctx .last_activity = k_uptime_get_32 ();
267+ node -> ctx .last_activity = core_uptime_get ();
258268 memset (node -> ctx .resource_count , 0 , sizeof (node -> ctx .resource_count ));
259269 memset (node -> ctx .dispatchers , 0 , sizeof (node -> ctx .dispatchers ));
260- k_mutex_lock (& registry_mutex , K_FOREVER );
261- sys_slist_append (& module_registry , & node -> node );
262- k_mutex_unlock (& registry_mutex );
270+
271+ core_mutex_lock (& registry_mutex );
272+ core_slist_append (& module_registry , & node -> node );
273+ core_mutex_unlock (& registry_mutex );
274+
263275 LOG_INF ("Module registered: %p" , (void * )module_inst );
264276 return 0 ;
265277}
@@ -269,38 +281,41 @@ void ocre_unregister_module(wasm_module_inst_t module_inst) {
269281 LOG_ERR ("Null module instance" );
270282 return ;
271283 }
272- k_mutex_lock (& registry_mutex , K_FOREVER );
284+
285+ core_mutex_lock (& registry_mutex );
273286 module_node_t * node , * tmp ;
274- SYS_SLIST_FOR_EACH_CONTAINER_SAFE (& module_registry , node , tmp , node ) {
287+ module_node_t * prev = NULL ;
288+ CORE_SLIST_FOR_EACH_CONTAINER_SAFE (& module_registry , node , tmp , node ) {
275289 if (node -> ctx .inst == module_inst ) {
276290 ocre_cleanup_module_resources (module_inst );
277291 if (node -> ctx .exec_env ) {
278292 wasm_runtime_destroy_exec_env (node -> ctx .exec_env );
279293 }
280- sys_slist_remove (& module_registry , NULL , & node -> node );
281- k_free (node );
294+ core_slist_remove (& module_registry , prev ? & prev -> node : NULL , & node -> node );
295+ core_free (node );
282296 LOG_INF ("Module unregistered: %p" , (void * )module_inst );
283297 break ;
284298 }
299+ prev = node ;
285300 }
286- k_mutex_unlock (& registry_mutex );
301+ core_mutex_unlock (& registry_mutex );
287302}
288303
289304ocre_module_context_t * ocre_get_module_context (wasm_module_inst_t module_inst ) {
290305 if (!module_inst ) {
291306 LOG_ERR ("Null module instance" );
292307 return NULL ;
293308 }
294- k_mutex_lock (& registry_mutex , K_FOREVER );
295- module_node_t * node ;
296- SYS_SLIST_FOR_EACH_CONTAINER ( & module_registry , node , node ) {
309+ core_mutex_lock (& registry_mutex );
310+ for ( core_snode_t * current = module_registry . head ; current != NULL ; current = current -> next ) {
311+ module_node_t * node = ( module_node_t * )(( char * ) current - offsetof( module_node_t , node ));
297312 if (node -> ctx .inst == module_inst ) {
298- node -> ctx .last_activity = k_uptime_get_32 ();
299- k_mutex_unlock (& registry_mutex );
313+ node -> ctx .last_activity = core_uptime_get ();
314+ core_mutex_unlock (& registry_mutex );
300315 return & node -> ctx ;
301316 }
302317 }
303- k_mutex_unlock (& registry_mutex );
318+ core_mutex_unlock (& registry_mutex );
304319 LOG_ERR ("Module context not found for %p" , (void * )module_inst );
305320 return NULL ;
306321}
@@ -311,7 +326,9 @@ int ocre_register_dispatcher(wasm_exec_env_t exec_env, ocre_resource_type_t type
311326 function_name ? function_name : "null" );
312327 return - EINVAL ;
313328 }
329+
314330 wasm_module_inst_t module_inst = current_module_tls ? * current_module_tls : wasm_runtime_get_module_inst (exec_env );
331+
315332 if (!module_inst ) {
316333 LOG_ERR ("No module instance for event type %d" , type );
317334 return - EINVAL ;
@@ -327,9 +344,9 @@ int ocre_register_dispatcher(wasm_exec_env_t exec_env, ocre_resource_type_t type
327344 LOG_ERR ("Function %s not found in module %p" , function_name , (void * )module_inst );
328345 return - EINVAL ;
329346 }
330- k_mutex_lock (& registry_mutex , K_FOREVER );
347+ core_mutex_lock (& registry_mutex );
331348 ctx -> dispatchers [type ] = func ;
332- k_mutex_unlock (& registry_mutex );
349+ core_mutex_unlock (& registry_mutex );
333350 LOG_INF ("Registered dispatcher for type %d: %s" , type , function_name );
334351 return 0 ;
335352}
@@ -342,19 +359,19 @@ uint32_t ocre_get_resource_count(wasm_module_inst_t module_inst, ocre_resource_t
342359void ocre_increment_resource_count (wasm_module_inst_t module_inst , ocre_resource_type_t type ) {
343360 ocre_module_context_t * ctx = ocre_get_module_context (module_inst );
344361 if (ctx && type < OCRE_RESOURCE_TYPE_COUNT ) {
345- k_mutex_lock (& registry_mutex , K_FOREVER );
362+ core_mutex_lock (& registry_mutex );
346363 ctx -> resource_count [type ]++ ;
347- k_mutex_unlock (& registry_mutex );
364+ core_mutex_unlock (& registry_mutex );
348365 LOG_INF ("Incremented resource count: type=%d, count=%d" , type , ctx -> resource_count [type ]);
349366 }
350367}
351368
352369void ocre_decrement_resource_count (wasm_module_inst_t module_inst , ocre_resource_type_t type ) {
353370 ocre_module_context_t * ctx = ocre_get_module_context (module_inst );
354371 if (ctx && type < OCRE_RESOURCE_TYPE_COUNT && ctx -> resource_count [type ] > 0 ) {
355- k_mutex_lock (& registry_mutex , K_FOREVER );
372+ core_mutex_lock (& registry_mutex );
356373 ctx -> resource_count [type ]-- ;
357- k_mutex_unlock (& registry_mutex );
374+ core_mutex_unlock (& registry_mutex );
358375 LOG_INF ("Decremented resource count: type=%d, count=%d" , type , ctx -> resource_count [type ]);
359376 }
360377}
0 commit comments