ECRTData::ECRTData() { mlockall(MCL_CURRENT|MCL_FUTURE); rt_mutex_create(&(this->mutex), NULL); rt_mutex_create(&(this->mutexBuffer), NULL); rt_cond_create(&(this->freeCond), NULL); rt_task_create(&(this->ecThread), NULL, 0, 99, T_JOINABLE); rt_task_create(&(this->frThread), NULL, 0, 99, T_JOINABLE); rt_task_create(&(this->statusThread), NULL, 0, 99, T_JOINABLE); rt_task_create(&(this->supervisorThread), NULL, 0, 99, T_JOINABLE); }
/** * Init Mutex, Semaphores and Condition variable */ void TimerInit(void) { int ret = 0; char taskname[32]; // lock process in to RAM //mlockall(MCL_CURRENT | MCL_FUTURE); snprintf(taskname, sizeof(taskname), "S1-%d", current->pid); rt_sem_create(&CanFestival_mutex, taskname, 1, S_FIFO); snprintf(taskname, sizeof(taskname), "S2-%d", current->pid); rt_sem_create(&control_task, taskname, 0, S_FIFO); snprintf(taskname, sizeof(taskname), "M1-%d", current->pid); rt_mutex_create(&condition_mutex, taskname); snprintf(taskname, sizeof(taskname), "C1-%d", current->pid); rt_cond_create(&timer_set, taskname); }
int main(void) { unsigned long long before; RT_ALARM nalrm; RT_BUFFER nbuf; RT_COND ncond; RT_EVENT nevt; RT_HEAP nheap; RT_MUTEX nmtx; RT_PIPE npipe; RT_QUEUE nq; RT_SEM nsem; RT_TASK ntsk; int failed = 0; mlockall(MCL_CURRENT|MCL_FUTURE); rt_print_auto_init(1); rt_fprintf(stderr, "Checking for leaks in native skin services\n"); before = get_used(); check_native(rt_alarm_create(&nalrm, NULL)); check_native(rt_alarm_delete(&nalrm)); check_used("alarm", before, failed); before = get_used(); check_native(rt_buffer_create(&nbuf, NULL, 16384, B_PRIO)); check_native(rt_buffer_delete(&nbuf)); check_used("buffer", before, failed); before = get_used(); check_native(rt_cond_create(&ncond, NULL)); check_native(rt_cond_delete(&ncond)); check_used("cond", before, failed); before = get_used(); check_native(rt_event_create(&nevt, NULL, 0, EV_PRIO)); check_native(rt_event_delete(&nevt)); check_used("event", before, failed); before = get_used(); check_native(rt_heap_create(&nheap, "heap", 16384, H_PRIO | H_SHARED)); check_native(rt_heap_delete(&nheap)); check_used("heap", before, failed); before = get_used(); check_native(rt_mutex_create(&nmtx, NULL)); check_native(rt_mutex_delete(&nmtx)); check_used("mutex", before, failed); before = get_used(); check_native(rt_pipe_create(&npipe, NULL, P_MINOR_AUTO, 0)); check_native(rt_pipe_delete(&npipe)); check_used("pipe", before, failed); before = get_used(); check_native(rt_queue_create(&nq, "queue", 16384, Q_UNLIMITED, Q_PRIO)); check_native(rt_queue_delete(&nq)); check_used("queue", before, failed); before = get_used(); check_native(rt_sem_create(&nsem, NULL, 0, S_PRIO)); check_native(rt_sem_delete(&nsem)); check_used("sem", before, failed); before = get_used(); check_native(rt_task_spawn(&ntsk, NULL, 0, 1, T_JOINABLE, empty, NULL)); check_native(rt_task_join(&ntsk)); sleep(1); /* Leave some time for xnheap * deferred free */ check_used("task", before, failed); return failed ? EXIT_FAILURE : EXIT_SUCCESS; }
int cond_wait_until(cond_t *cond, mutex_t *mutex, unsigned long long date) { struct timespec ts = { .tv_sec = date / NS_PER_S, .tv_nsec = date % NS_PER_S, }; return -pthread_cond_timedwait(cond, mutex, &ts); } #define cond_destroy(cond) (-pthread_cond_destroy(cond)) int thread_msleep(unsigned ms) { struct timespec ts = { .tv_sec = (ms * NS_PER_MS) / NS_PER_S, .tv_nsec = (ms * NS_PER_MS) % NS_PER_S, }; return -nanosleep(&ts, NULL); } int thread_spawn(thread_t *thread, int prio, void *(*handler)(void *cookie), void *cookie) { struct sched_param param; pthread_attr_t tattr; int err; pthread_attr_init(&tattr); pthread_attr_setinheritsched(&tattr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&tattr, SCHED_FIFO); param.sched_priority = prio; pthread_attr_setschedparam(&tattr, ¶m); pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE); pthread_attr_setstacksize(&tattr, xeno_stacksize(0)); err = pthread_create(thread, &tattr, handler, cookie); pthread_attr_destroy(&tattr); return -err; } #define thread_yield() sched_yield() #define thread_kill(thread, sig) (-__real_pthread_kill(thread, sig)) #define thread_self() pthread_self() #define thread_join(thread) (-pthread_join(thread, NULL)) #else /* __NATIVE_SKIN__ */ typedef RT_MUTEX mutex_t; typedef RT_TASK *thread_t; typedef RT_COND cond_t; #define timer_read() rt_timer_read() int __mutex_init(mutex_t *mutex, const char *name, int type, int pi) { if (type == PTHREAD_MUTEX_ERRORCHECK) return -EINVAL; (void)(pi); return -rt_mutex_create(mutex, name); } #define mutex_init(mutex, type, pi) __mutex_init(mutex, #mutex, type, pi) #define mutex_destroy(mutex) rt_mutex_delete(mutex) #define mutex_lock(mutex) rt_mutex_acquire(mutex, TM_INFINITE) #define mutex_unlock(mutex) rt_mutex_release(mutex) int __cond_init(cond_t *cond, const char *name, int absolute) { (void)(absolute); return rt_cond_create(cond, name); } #define cond_init(cond, absolute) __cond_init(cond, #cond, absolute) #define cond_signal(cond) rt_cond_signal(cond) #define cond_wait(cond, mutex, ns) rt_cond_wait(cond, mutex, (RTIME)ns) #define cond_wait_until(cond, mutex, ns) \ rt_cond_wait_until(cond, mutex, (RTIME)ns) #define cond_destroy(cond) rt_cond_delete(cond) #define thread_self() rt_task_self() #define thread_msleep(ms) rt_task_sleep((RTIME)ms * NS_PER_MS) int thread_spawn_inner(thread_t *thread, const char *name, int prio, void *(*handler)(void *), void *cookie) { thread_t tcb; int err; tcb = malloc(sizeof(*tcb)); if (!tcb) return -ENOSPC; err = rt_task_spawn(tcb, name, 0, prio, T_JOINABLE, (void (*)(void *))handler, cookie); if (!err) *thread = tcb; return err; }
void __po_hi_gqueue_init (__po_hi_task_id id, __po_hi_uint8_t nb_ports, __po_hi_port_t queue[], __po_hi_int8_t sizes[], __po_hi_uint8_t first[], __po_hi_uint8_t offsets[], __po_hi_uint8_t woffsets[], __po_hi_uint8_t n_dest[], __po_hi_port_t* destinations[], __po_hi_uint8_t used_size[], __po_hi_local_port_t history[], __po_hi_request_t recent[], __po_hi_uint8_t empties[], __po_hi_uint16_t total_fifo_size) { __po_hi_uint8_t tmp; __po_hi_uint16_t off; __po_hi_request_t* request; int err; #if defined (RTEMS_PURE) rtems_status_code ret; #elif defined (XENO_NATIVE) int ret; #endif __po_hi_gqueues_global_history_woffset[id] = 0; __po_hi_gqueues_global_history_offset[id] = 0; __po_hi_gqueues_n_empty[id] = nb_ports; __po_hi_gqueues[id] = queue; __po_hi_gqueues_most_recent_values[id] = recent; __po_hi_gqueues_global_history[id] = history; __po_hi_gqueues_woffsets[id] = woffsets; __po_hi_gqueues_port_is_empty[id] = empties; __po_hi_gqueues_nb_ports[id] = nb_ports; __po_hi_gqueues_sizes[id] = sizes; __po_hi_gqueues_first[id] = first; __po_hi_gqueues_used_size[id] = used_size; __po_hi_gqueues_offsets[id] = offsets; __po_hi_gqueues_n_destinations[id] = n_dest; __po_hi_gqueues_destinations[id] = destinations; __po_hi_gqueues_total_fifo_size[id] = total_fifo_size; __po_hi_gqueues_queue_is_empty[id] = 1; #if defined (RTEMS_POSIX) || defined (POSIX) || defined (XENO_POSIX) err = pthread_mutexattr_init (&__po_hi_gqueues_mutexes_attr[id]); __DEBUGMSG("MUTEX_INIT %d %d\n", id, err); err = pthread_condattr_init (&__po_hi_gqueues_conds_attr[id]); __DEBUGMSG("MUTEX_INIT %d %d\n", id, err); #if defined (POSIX) || defined (XENO_POSIX) // XXX disabled for OS X #ifndef __MACH__ // OS X bugs on this attribute err = pthread_mutexattr_setpshared(&__po_hi_gqueues_mutexes_attr[id],PTHREAD_PROCESS_SHARED); #endif __DEBUGMSG("MUTEX_INIT %d\n", err); #endif err = pthread_mutex_init (&__po_hi_gqueues_mutexes[id], &__po_hi_gqueues_mutexes_attr[id]); __DEBUGMSG("MUTEX_INIT %d %d\n", id, err); err = pthread_cond_init (&__po_hi_gqueues_conds[id], &__po_hi_gqueues_conds_attr[id]); __DEBUGMSG("COND_INIT %d %d\n", id, err); #endif #ifdef RTEMS_PURE __PO_HI_DEBUG_INFO ("[GQUEUE] Create semaphore for queue of task %d\n", id); ret = rtems_semaphore_create (rtems_build_name ('G', 'S', 'E' , 'A' + (char) id), 1, RTEMS_BINARY_SEMAPHORE, __PO_HI_DEFAULT_PRIORITY, &(__po_hi_gqueues_semaphores[id])); if (ret != RTEMS_SUCCESSFUL) { __PO_HI_DEBUG_CRITICAL ("[GQUEUE] Cannot create semaphore, error code=%d\n", ret); } __PO_HI_DEBUG_INFO ("[GQUEUE] Create barrier for queue of task %d\n", id); ret = rtems_barrier_create (rtems_build_name ('G', 'S', 'I' , 'A' + (char) id),RTEMS_BARRIER_AUTOMATIC_RELEASE , 10, &(__po_hi_gqueues_barriers[id])); if (ret != RTEMS_SUCCESSFUL) { __PO_HI_DEBUG_CRITICAL ("[GQUEUE] Cannot create barrier, error code=%d\n", ret); } #endif #ifdef XENO_NATIVE ret = rt_mutex_create (&__po_hi_gqueues_mutexes[id], NULL); if (ret != 0) { __PO_HI_DEBUG_CRITICAL ("[GQUEUE] Cannot create mutex code=%d\n", ret); } ret = rt_cond_create (&__po_hi_gqueues_conds[id], NULL); if (ret != 0) { __PO_HI_DEBUG_CRITICAL ("[GQUEUE] Cannot create cond code=%d\n", ret); } #endif #ifdef _WIN32 __po_hi_gqueues_events[id] = CreateEvent (NULL, FALSE, FALSE, NULL); if (__po_hi_gqueues_events[id] == NULL) { __PO_HI_DEBUG_CRITICAL ("CreateEvent failed (%d)\n", GetLastError()); return; } InitializeCriticalSection (&__po_hi_gqueues_cs[id]); #endif off = 0; for (tmp=0;tmp<nb_ports;tmp++) { __po_hi_gqueues_used_size[id][tmp] = 0; if ( (sizes[tmp] != __PO_HI_GQUEUE_FIFO_INDATA) && (sizes[tmp] != __PO_HI_GQUEUE_FIFO_OUT)) { __po_hi_gqueues_first[id][tmp]=off; off += __po_hi_gqueues_sizes[id][tmp]; __po_hi_gqueues_offsets[id][tmp] = 0; __po_hi_gqueues_woffsets[id][tmp] = 0; __po_hi_gqueues_port_is_empty[id][tmp] = 1; } /* Set invalid all recent values */ request = (__po_hi_request_t*)&__po_hi_gqueues_most_recent_values[id][tmp]; request->port = __PO_HI_GQUEUE_INVALID_PORT; } #ifdef __PO_HI_DEBUG __DEBUGMSG("Initialize global queue for task-id %d ... ", id); for (tmp=0;tmp<nb_ports;tmp++) { __DEBUGMSG("port %d (used_size=%d,first=%d) ", tmp, __po_hi_gqueues_used_size[id][tmp], __po_hi_gqueues_first[id][tmp]); } __DEBUGMSG(" ... done\n"); #endif }
int __po_hi_initialize_early () { #if defined (XENO_POSIX) || defined (XENO_NATIVE) /* * Once initialization has been done, we avoid ALL * potential paging operations that can introduce * some indeterministic timing behavior. */ #include <sys/mman.h> mlockall (MCL_CURRENT|MCL_FUTURE); #endif #if defined (XENO_NATIVE) main_task_id = rt_task_self (); __po_hi_nb_tasks_to_init--; /* * If we are using the XENO_NATIVE skin, we need * to differentiate the main task (that is non real-time) * from the others since the main task cannot use * the services and operates on resources of real-time tasks. * In addition, we decrement the amount of tasks to * initialize since the main task does not wait * for the initialization of the other tasks. */ #endif #if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX) pthread_mutexattr_t mutex_attr; if (pthread_mutexattr_init (&mutex_attr) != 0) { __DEBUGMSG ("[MAIN] Unable to init mutex attributes\n"); } #ifdef RTEMS_POSIX if (pthread_mutexattr_setprioceiling (&mutex_attr, 50) != 0) { __DEBUGMSG ("[MAIN] Unable to set priority ceiling on mutex\n"); } #endif if (pthread_mutex_init (&mutex_init, &mutex_attr) != 0 ) { __DEBUGMSG ("[MAIN] Unable to init pthread_mutex\n"); return (__PO_HI_ERROR_PTHREAD_MUTEX); } __DEBUGMSG ("[MAIN] Have %d tasks to init\n", __po_hi_nb_tasks_to_init); if (pthread_cond_init (&cond_init, NULL) != 0) { return (__PO_HI_ERROR_PTHREAD_COND); } #endif #if defined (XENO_NATIVE) if (rt_cond_create (&cond_init, NULL)) { __DEBUGMSG ("[MAIN] Unable to init the initialization condition variable \n"); return (__PO_HI_ERROR_PTHREAD_MUTEX); } if (rt_mutex_create (&mutex_init, NULL) != 0) { __DEBUGMSG ("[MAIN] Unable to init the initialization mutex variable \n"); return (__PO_HI_ERROR_PTHREAD_COND); } #endif #if defined (RTEMS_POSIX) || defined (__PO_HI_RTEMS_CLASSIC_API) rtems_status_code ret; rtems_time_of_day time; time.year = 1988; time.month = 12; time.day = 31; time.hour = 9; time.minute = 1; time.second = 10; time.ticks = 0; ret = rtems_clock_set( &time ); if (ret != RTEMS_SUCCESSFUL) { __DEBUGMSG ("[MAIN] Cannot set the clock\n"); return __PO_HI_ERROR_CLOCK; } #endif #ifdef __PO_HI_RTEMS_CLASSIC_API __DEBUGMSG ("[MAIN] Create a barrier that wait for %d tasks\n", __po_hi_nb_tasks_to_init); ret = rtems_barrier_create (rtems_build_name ('B', 'A', 'R', 'M'), RTEMS_BARRIER_AUTOMATIC_RELEASE, __po_hi_nb_tasks_to_init, &__po_hi_main_initialization_barrier); if (ret != RTEMS_SUCCESSFUL) { __DEBUGMSG ("[MAIN] Cannot create the main barrier, return code=%d\n", ret); } #endif #ifdef _WIN32 __po_hi_main_initialization_event = CreateEvent (NULL, FALSE, FALSE, NULL); InitializeCriticalSection (&__po_hi_main_initialization_critical_section); #endif __po_hi_initialize_tasking (); /* Initialize protected objects */ #if __PO_HI_NB_PROTECTED > 0 __po_hi_protected_init(); #endif #if __PO_HI_MONITOR_ENABLED == 1 __po_hi_monitor_init (); #endif return (__PO_HI_SUCCESS); }