static void task_func1(long dummy) { rt_printk("Starting task1, waiting on the conditional variable to be 1.\n"); rt_mutex_lock(&mtx); while(cond_data < 1) { rt_cond_wait(&cond, &mtx); } rt_mutex_unlock(&mtx); if(cond_data == 1) { rt_printk("task1, conditional variable signalled, value: %d.\n", cond_data); } rt_printk("task1 signals after setting data to 2.\n"); rt_printk("task1 waits for a broadcast.\n"); rt_mutex_lock(&mtx); cond_data = 2; rt_cond_signal(&cond); while(cond_data < 3) { rt_cond_wait(&cond, &mtx); } rt_mutex_unlock(&mtx); if(cond_data == 3) { rt_printk("task1, conditional variable broadcasted, value: %d.\n", cond_data); } rt_printk("Ending task1.\n"); atomic_inc(&cleanup); }
/** * Timer Task */ void timerloop_task_proc(void *arg) { int ret = 0; getElapsedTime(); last_timeout_set = 0; last_occured_alarm = last_time_read; /* trigger first alarm */ SetAlarm(callback_od, 0, init_callback, 0, 0); RTIME current_time; RTIME real_alarm; do{ rt_mutex_acquire(&condition_mutex, TM_INFINITE); if(last_timeout_set == TIMEVAL_MAX) { ret = rt_cond_wait( &timer_set, &condition_mutex, TM_INFINITE ); /* Then sleep until next message*/ rt_mutex_release(&condition_mutex); }else{ current_time = rt_timer_read(); real_alarm = last_time_read + last_timeout_set; ret = rt_cond_wait( /* sleep until next deadline */ &timer_set, &condition_mutex, (real_alarm - current_time)); /* else alarm consider expired */ if(ret == -ETIMEDOUT){ last_occured_alarm = real_alarm; rt_mutex_release(&condition_mutex); EnterMutex(); TimeDispatch(); LeaveMutex(); }else{ rt_mutex_release(&condition_mutex); } } }while ((ret == 0 || ret == -EINTR || ret == -ETIMEDOUT) && !stop_timer); if(exitall){ EnterMutex(); exitall(callback_od, 0); LeaveMutex(); } }
int rtos_cond_wait(rt_cond_t *cond, rt_mutex_t *mutex) { CHK_LXRT_CALL(); int ret = rt_cond_wait(cond->cond, mutex->sem ); if (ret == 0) return 0; return -1; }
void ElevatorController::floorRun() { unsigned char topItem; while(true) { if(!this->eStat.getGDFailedEmptyHeap()) { rt_mutex_acquire(&(this->rtData.mutex), TM_INFINITE); rt_cond_wait(&(this->rtData.freeCond), &(this->rtData.mutex), TM_INFINITE); int upHeapSize = this->getUpHeap().getSize(); int downHeapSize = this->getDownHeap().getSize(); if(upHeapSize==0 && downHeapSize==0){this->eStat.setGDFailedEmptyHeap(true);} if(this->eStat.getServiceDirection() == DIRECTION_DOWN) { if(downHeapSize > 0) { topItem = this->getDownHeap().peek(); }else if(upHeapSize > 0) { topItem = this->getUpHeap().peek(); }this->downHeap.pushFloorRequestVector(this->missedFloorSelections); }else if(this->eStat.getServiceDirection() == DIRECTION_UP) { if(upHeapSize > 0) { topItem = this->getUpHeap().peek(); }else if(downHeapSize > 0) { topItem = this->getDownHeap().peek(); } } int tempDirection = this->eStat.getDirection(); if(topItem != this->eStat.getDestination()) { rt_printf("FR%d next Dest is %d\n.", this->getID(), topItem); this->getSimulator()->setFinalDestination(topItem); this->eStat.setDestination(topItem); rt_printf("FR%d tempDirection is %d afterDirection is %d\n.", this->getID(), tempDirection, this->getSimulator()->getDirection()); } if(tempDirection != this->getSimulator()->getDirection()) { this->updateMissedFloor(tempDirection); this->notifyDirectionChanged(this->getSimulator()->getDirection()); } rt_mutex_release(&(this->rtData.mutex)); } } }
static void task_func2(long dummy) { rt_printk("Starting task2, waiting on the conditional variable to be 2.\n"); rt_mutex_lock(&mtx); while(cond_data < 2) { rt_cond_wait(&cond, &mtx); } rt_mutex_unlock(&mtx); if(cond_data == 2) { rt_printk("task2, conditional variable signalled, value: %d.\n", cond_data); } rt_printk("task2 waits for a broadcast.\n"); rt_mutex_lock(&mtx); while(cond_data < 3) { rt_cond_wait(&cond, &mtx); } rt_mutex_unlock(&mtx); if(cond_data == 3) { rt_printk("task2, conditional variable broadcasted, value: %d.\n", cond_data); } rt_printk("Ending task2.\n"); atomic_inc(&cleanup); }
static void task_func4(long dummy) { rt_printk("Starting task4, signalling after setting data to 1, then waits for a broadcast.\n"); rt_mutex_lock(&mtx); cond_data = 1; rt_mutex_unlock(&mtx); rt_cond_signal(&cond); rt_mutex_lock(&mtx); while(cond_data < 3) { rt_cond_wait(&cond, &mtx); } rt_mutex_unlock(&mtx); if(cond_data == 3) { rt_printk("task4, conditional variable broadcasted, value: %d.\n", cond_data); } rt_printk("Ending task4.\n"); atomic_inc(&cleanup); }
int __po_hi_gqueue_get_value (__po_hi_task_id id, __po_hi_local_port_t port, __po_hi_request_t* request) { __po_hi_request_t* ptr; #ifdef RTEMS_PURE rtems_status_code ret; #endif #ifdef _WIN32 DWORD ret; #endif ptr = &__po_hi_gqueues_most_recent_values[id][port]; #if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX) pthread_mutex_lock (&__po_hi_gqueues_mutexes[id]); #elif defined (XENO_NATIVE) rt_mutex_acquire (&__po_hi_gqueues_mutexes[id], TM_INFINITE); #elif defined (RTEMS_PURE) ret = rtems_semaphore_obtain (__po_hi_gqueues_semaphores[id], RTEMS_WAIT, RTEMS_NO_TIMEOUT); if (ret != RTEMS_SUCCESSFUL) { __DEBUGMSG ("[GQUEUE] Cannot obtain semaphore in __po_hi_gqueue_store_in()\n"); } #elif defined (_WIN32) EnterCriticalSection(&__po_hi_gqueues_cs[id]); #endif /* * If the port is an event port, with no value queued, then we block * the thread. */ if (__po_hi_gqueues_sizes[id][port] != __PO_HI_GQUEUE_FIFO_INDATA) { while (__po_hi_gqueues_port_is_empty[id][port] == 1) { #if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX) pthread_cond_wait (&__po_hi_gqueues_conds[id], &__po_hi_gqueues_mutexes[id]); #elif defined (XENO_NATIVE) rt_cond_wait (&__po_hi_gqueues_conds[id], &__po_hi_gqueues_mutexes[id], TM_INFINITE); #elif defined (RTEMS_PURE) rtems_task_wake_after( RTEMS_YIELD_PROCESSOR ); #elif defined (_WIN32) LeaveCriticalSection(&__po_hi_gqueues_cs[id]); ret = WaitForSingleObject (__po_hi_gqueues_events[id], INFINITE); if (ret == WAIT_FAILED) { __PO_HI_DEBUG_CRITICAL ("[GQUEUE] Wait failed\n"); } EnterCriticalSection(&__po_hi_gqueues_cs[id]); #endif } } #if defined (MONITORING) update_sporadic_dispatch (id, port); #endif if (__po_hi_gqueues_used_size[id][port] == 0) { memcpy (request, ptr, sizeof (__po_hi_request_t)); //update_runtime (id, port, ptr); } else { ptr = ((__po_hi_request_t *) &__po_hi_gqueues[id][port]) + __po_hi_gqueues_first[id][port] + __po_hi_gqueues_offsets[id][port]; memcpy (request, ptr, sizeof (__po_hi_request_t)); } __PO_HI_DEBUG_INFO ("[GQUEUE] Task %d get a value on port %d\n", id, port); #if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX) pthread_mutex_unlock (&__po_hi_gqueues_mutexes[id]); #elif defined (XENO_NATIVE) rt_mutex_release (&__po_hi_gqueues_mutexes[id]); #elif defined (RTEMS_PURE) ret = rtems_semaphore_release (__po_hi_gqueues_semaphores[id]); if (ret != RTEMS_SUCCESSFUL) { __DEBUGMSG ("[GQUEUE] Cannot release semaphore in __po_hi_gqueue_store_in()\n"); } #elif defined (_WIN32) LeaveCriticalSection(&__po_hi_gqueues_cs[id]); #endif return 0; }
void __po_hi_gqueue_wait_for_incoming_event (__po_hi_task_id id, __po_hi_local_port_t* port) { #ifdef RTEMS_PURE rtems_status_code ret; #endif #ifdef _WIN32 DWORD ret; #endif #if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX) int error = pthread_mutex_lock (&__po_hi_gqueues_mutexes[id]); __DEBUGMSG("*** Locking (%d) %d\n", id, error); #elif defined (XENO_NATIVE) rt_mutex_acquire (&__po_hi_gqueues_mutexes[id], TM_INFINITE); #elif defined (RTEMS_PURE) ret = rtems_semaphore_obtain (__po_hi_gqueues_semaphores[id], RTEMS_WAIT, RTEMS_NO_TIMEOUT); if (ret != RTEMS_SUCCESSFUL) { __DEBUGMSG ("[GQUEUE] Cannot obtain semaphore in __po_hi_gqueue_store_in()\n"); } #elif defined (_WIN32) EnterCriticalSection(&__po_hi_gqueues_cs[id]); #endif while(__po_hi_gqueues_queue_is_empty[id] == 1) { __PO_HI_INSTRUMENTATION_VCD_WRITE("0t%d\n", id); #if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX) __DEBUGMSG("*** Waiting (%d)\n", id); int error = pthread_cond_wait (&__po_hi_gqueues_conds[id], &__po_hi_gqueues_mutexes[id]); __DEBUGMSG("*** Done Waiting (%d) %d\n", id, error); #elif defined (XENO_NATIVE) rt_cond_wait (&__po_hi_gqueues_conds[id], &__po_hi_gqueues_mutexes[id], TM_INFINITE); #elif defined (RTEMS_PURE) ret = rtems_semaphore_release (__po_hi_gqueues_semaphores[id]); if (ret != RTEMS_SUCCESSFUL) { __DEBUGMSG ("[GQUEUE] Cannot obtain semaphore in __po_hi_gqueue_store_in()\n"); } rtems_task_wake_after (1); ret = rtems_semaphore_obtain (__po_hi_gqueues_semaphores[id], RTEMS_WAIT, RTEMS_NO_TIMEOUT); if (ret != RTEMS_SUCCESSFUL) { __DEBUGMSG ("[GQUEUE] Cannot obtain semaphore in __po_hi_gqueue_store_in()\n"); } else { __PO_HI_DEBUG_CRITICAL ("[GQUEUE] semaphore %d obtained\n", id); } #elif defined (_WIN32) LeaveCriticalSection(&__po_hi_gqueues_cs[id]); ret = WaitForSingleObject (__po_hi_gqueues_events[id], INFINITE); if (ret == WAIT_FAILED) { __PO_HI_DEBUG_CRITICAL ("[GQUEUE] Wait failed\n"); } EnterCriticalSection(&__po_hi_gqueues_cs[id]); #endif __PO_HI_INSTRUMENTATION_VCD_WRITE("1t%d\n", id); } __DEBUGMSG ("[GQUEUE] Gogo kiki\n"); *port = __po_hi_gqueues_global_history[id][__po_hi_gqueues_global_history_offset[id]]; #if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX) pthread_mutex_unlock (&__po_hi_gqueues_mutexes[id]); #elif defined (XENO_NATIVE) rt_mutex_release (&__po_hi_gqueues_mutexes[id]); #elif defined (_WIN32) LeaveCriticalSection(&__po_hi_gqueues_cs[id]); #elif defined (RTEMS_PURE) ret = rtems_semaphore_release (__po_hi_gqueues_semaphores[id]); if (ret != RTEMS_SUCCESSFUL) { __DEBUGMSG ("[GQUEUE] Cannot release semaphore in __po_hi_gqueue_store_in()\n"); } __PO_HI_DEBUG_CRITICAL ("[GQUEUE] semaphore %d released\n", id); #endif }
int __po_hi_wait_initialization () { #if defined (POSIX) || defined (RTEMS_POSIX) || defined (XENO_POSIX) int cstate; if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, &cstate) != 0) { __DEBUGMSG ("[MAIN] Cannot modify the cancel state\n"); } if (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &cstate) != 0) { __DEBUGMSG ("[MAIN] Cannot modify the cancel type\n"); } if (pthread_mutex_lock (&mutex_init) != 0) { __DEBUGMSG ("[MAIN] Unable to lock the mutex\n"); return (__PO_HI_ERROR_PTHREAD_MUTEX); } __po_hi_initialized_tasks++; __DEBUGMSG ("[MAIN] %d task(s) initialized (total to init =%d)\n", __po_hi_initialized_tasks, __po_hi_nb_tasks_to_init); while (__po_hi_initialized_tasks < __po_hi_nb_tasks_to_init) { pthread_cond_wait (&cond_init, &mutex_init); } pthread_cond_broadcast (&cond_init); pthread_mutex_unlock (&mutex_init); __PO_HI_INSTRUMENTATION_VCD_INIT return (__PO_HI_SUCCESS); #elif defined (_WIN32) EnterCriticalSection (&__po_hi_main_initialization_critical_section); __po_hi_initialized_tasks++; __DEBUGMSG ("[MAIN] %d task(s) initialized (total to init =%d)\n", __po_hi_initialized_tasks, __po_hi_nb_tasks_to_init); while (__po_hi_initialized_tasks < __po_hi_nb_tasks_to_init) { LeaveCriticalSection (&__po_hi_main_initialization_critical_section); WaitForSingleObject (__po_hi_main_initialization_event, INFINITE); EnterCriticalSection (&__po_hi_main_initialization_critical_section); } SetEvent (__po_hi_main_initialization_event); LeaveCriticalSection (&__po_hi_main_initialization_critical_section); return (__PO_HI_SUCCESS); #elif defined (__PO_HI_RTEMS_CLASSIC_API) rtems_status_code ret; __DEBUGMSG ("[MAIN] Task wait for the barrier\n"); ret = rtems_barrier_wait (__po_hi_main_initialization_barrier, RTEMS_WAIT); if (ret != RTEMS_SUCCESSFUL) { __DEBUGMSG ("[MAIN] Error while waiting for the barrier, return code=%d\n", ret); return (__PO_HI_ERROR_UNKNOWN); } __DEBUGMSG ("[MAIN] Task release the barrier\n"); return (__PO_HI_SUCCESS); #elif defined (XENO_NATIVE) int ret; if (main_task_id == rt_task_self ()) { /* * Here, this function is called by the main thread (the one that executes * the main() function) so that we don't wait for the initialization of the * other tasks, we automatically pass through the function and immeditaly * return. */ return (__PO_HI_SUCCESS); } ret = rt_mutex_acquire (&mutex_init, TM_INFINITE); if (ret != 0) { __DEBUGMSG ("[MAIN] Cannot acquire mutex (return code = %d)\n", ret); return (__PO_HI_ERROR_PTHREAD_MUTEX); } __po_hi_initialized_tasks++; __DEBUGMSG ("[MAIN] %d task(s) initialized (total to init =%d)\n", __po_hi_initialized_tasks, __po_hi_nb_tasks_to_init); while (__po_hi_initialized_tasks < __po_hi_nb_tasks_to_init) { rt_cond_wait (&cond_init, &mutex_init, TM_INFINITE); } rt_cond_broadcast (&cond_init); rt_mutex_release (&mutex_init); return (__PO_HI_SUCCESS); #else return (__PO_HI_UNAVAILABLE); #endif }
static void *fun0(void *arg) { RT_TASK *task; int done1=0, cnt1=0; RTIME right_now; int ret; task = rt_task_init_schmod(nam2num("TASK0"), 0, 0, 0, SCHED_FIFO, 0xF); mlockall(MCL_CURRENT | MCL_FUTURE); rt_make_hard_real_time(); while (cnt1 < THRESHOLD) { rt_sem_wait(mutex); rt_printk("fun0: Hello World %d, instance_cnt %d!\n",cnt1,instance_cnt); while (instance_cnt<0) { ret = rt_cond_wait(cond, mutex); if (ret != 0) { rt_printk("error in rt_cond_wait, code %d\n",ret); switch (ret) { case RTE_PERM: rt_printk("error RTE_PERM %d\n",ret); break; case RTE_UNBLKD: rt_printk("error RTE_UNBLKD %d\n",ret); break; case RTE_OBJREM: rt_printk("error RTE_OBJREM %d\n",ret); break; default: rt_printk("unknown error code %d\n",ret); break; } break; } } rt_sem_signal(mutex); // do some work here cnt1++; rt_sem_wait(mutex); instance_cnt--; rt_sem_signal(mutex); } // makes task soft real time rt_make_soft_real_time(); printf("Back to soft RT\n"); // clean task rt_task_delete(task); printf("Task deleted. returning\n"); return 0; }