void ActiveObjectBase::start_thread() { // prevent the started thread from running until the thread id has been assigned // so that calls to isCurrentThread() work correctly set_thread(std::thread(run_active_object, this)); while (!started) { os_thread_yield(); } }
void thread::_M_start_thread(thread::__shared_base_type base) { thread_startup startup; startup.call = base.get(); startup.started = false; if (os_thread_create(&_M_id._M_thread, "std::thread", OS_THREAD_PRIORITY_DEFAULT, invoke_thread, &startup, 1024*3)) { PANIC(AssertionFailure, "%s %s", __FILE__, __LINE__); } else { // C++ ensure the thread has started execution, as required by the standard while (!startup.started) os_thread_yield(); } }
void radioTask(const void *arg) { //while(true){ unsigned long tv; char getChar; char count = 0; char receiverBuffer2[RECEIVER_BUFFER_SIZE]; while (true){ if (Udp.read(receiverBuffer, sizeof(receiverBuffer)) > 0) { for(int i=0;i<sizeof(receiverBuffer);i++){ getChar=receiverBuffer[i]; if (getChar == '@') { memset(receiverBuffer2, '\0', sizeof(receiverBuffer2)); receiverBuffer2[0] = getChar; count = 1; } else if ((getChar == '#') && (receiverBuffer2[0] == '@') && (count < sizeof(receiverBuffer2))) { receiverBuffer2[count] = getChar; processRadioMessages( receiverBuffer2, strlen(receiverBuffer2)); memset(receiverBuffer2, '\0', sizeof(receiverBuffer2)); count = 0; } else { if (receiverBuffer2[0] == '@' && (count < sizeof(receiverBuffer2))) { receiverBuffer2[count] = getChar; count++; } else { memset(receiverBuffer2, '\0', sizeof(receiverBuffer2)); count = 0; } } } memset(receiverBuffer,'\0',sizeof(receiverBuffer)); memset(receiverBuffer2,'\0',sizeof(receiverBuffer2)); } os_thread_yield(); delay(1); } }
/***********************************************************************//** Chooses the next undo log to purge and updates the info in purge_sys. This function is used to initialize purge_sys when the next record to purge is not known, and also to update the purge system info on the next record when purge has handled the whole undo log for a transaction. */ static void trx_purge_choose_next_log(void) /*===========================*/ { ulint zip_size; ut_ad(purge_sys->next_stored == FALSE); zip_size = trx_purge_get_rseg_with_min_trx_id(purge_sys); if (purge_sys->rseg != NULL) { trx_purge_read_undo_rec(purge_sys, zip_size); } else { /* There is nothing to do yet. */ os_thread_yield(); } }
/** * Fuehrt die Verarbeitung in der Hauptschleife nach dem * Verhaltenscode durch. Dazu gehoert beispielsweise, die * Bildschirmanzeige zu steuern und den Simulator ueber den aktuellen * Zustand zu informieren. */ void post_behaviour(void) { static uint16_t comm_ticks = 0; static uint8_t uart_gui = 0; #if defined MCU && defined DEBUG_TIMES static uint32_t log_ticks = 0; #endif #ifdef BOT_2_RPI_AVAILABLE bot_2_linux_listen(); #endif // BOT_2_RPI_AVAILABLE #ifdef CREATE_TRACEFILE_AVAILABLE trace_add_sensors(); #endif // CREATE_TRACEFILE_AVAILABLE /* jeweils alle 100 ms kommunizieren Bot, User und Sim */ if (timer_ms_passed_16(&comm_ticks, 50) #ifdef RC5_AVAILABLE || RC5_Code != 0 #endif ) { if (uart_gui == 0 #ifdef RC5_AVAILABLE || RC5_Code != 0 #endif ) { #ifdef DISPLAY_AVAILABLE /* GUI-Behandlung starten */ gui_display(display_screen); #endif // DISPLAY_AVAILABLE uart_gui = 1; // bot-2-sim ist erst beim naechsten Mal dran } else { #ifdef BOT_2_SIM_AVAILABLE /* Den Sim ueber Sensoren und Aktuatoren informieren */ bot_2_sim_inform(); // NOP auf PC #endif // BOT_2_SIM_AVAILABLE uart_gui = 0; // naechstes Mal wieder mit GUI anfangen } } #if defined MCU && defined DEBUG_TIMES if (timer_ms_passed_32(&log_ticks, 1000UL)) { uint8_t cpu, uart_in, uart_out; os_get_utilizations(&cpu, &uart_in, &uart_out); LOG_INFO("CPU=%3u%% UART_IN=%3u%% UART_OUT=%3u%%", cpu, uart_in, uart_out); } #endif // MCU && DEBUG_TIMES #ifdef PC #ifdef CREATE_TRACEFILE_AVAILABLE trace_add_actuators(); #endif // CREATE_TRACEFILE_AVAILABLE /* Sim oder ATmega ueber naechsten Schleifendurchlauf / Bot-Zyklus informieren */ #ifdef ARM_LINUX_BOARD set_bot_2_atmega(); #endif // ARM_LINUX_BOARD command_write(CMD_DONE, SUB_CMD_NORM, simultime, 0, 0); // flusht auch den Sendepuffer #ifdef DEBUG_TIMES /* Zum Debuggen der Zeiten */ GETTIMEOFDAY(&init_stop, NULL); int t2 = (init_stop.tv_sec - init_start.tv_sec) * 1000000 + init_stop.tv_usec - init_start.tv_usec; LOG_DEBUG("Done-Token (%d) out after %d usec", simultime, t2); #endif // DEBUG_TIMES #endif // PC #if defined OS_AVAILABLE /* Rest der Zeitscheibe (OS_TIME_SLICE ms) schlafen legen */ os_thread_yield(); #endif // OS_AVAILABLE }
void rw_lock_x_lock_func( /*================*/ rw_lock_t* lock, /* in: pointer to rw-lock */ ulint pass, /* in: pass value; != 0, if the lock will be passed to another thread to unlock */ const char* file_name,/* in: file name where lock requested */ ulint line) /* in: line where requested */ { ulint index; /* index of the reserved wait cell */ ulint state; /* lock state acquired */ ulint i; /* spin round count */ ut_ad(rw_lock_validate(lock)); lock_loop: /* Acquire the mutex protecting the rw-lock fields */ mutex_enter_fast(&(lock->mutex)); state = rw_lock_x_lock_low(lock, pass, file_name, line); mutex_exit(&(lock->mutex)); if (state == RW_LOCK_EX) { return; /* Locking succeeded */ } else if (state == RW_LOCK_NOT_LOCKED) { /* Spin waiting for the writer field to become free */ i = 0; while (rw_lock_get_writer(lock) != RW_LOCK_NOT_LOCKED && i < SYNC_SPIN_ROUNDS) { if (srv_spin_wait_delay) { ut_delay(ut_rnd_interval(0, srv_spin_wait_delay)); } i++; } if (i == SYNC_SPIN_ROUNDS) { os_thread_yield(); } } else if (state == RW_LOCK_WAIT_EX) { /* Spin waiting for the reader count field to become zero */ i = 0; while (rw_lock_get_reader_count(lock) != 0 && i < SYNC_SPIN_ROUNDS) { if (srv_spin_wait_delay) { ut_delay(ut_rnd_interval(0, srv_spin_wait_delay)); } i++; } if (i == SYNC_SPIN_ROUNDS) { os_thread_yield(); } } else { i = 0; /* Eliminate a compiler warning */ ut_error; } if (srv_print_latch_waits) { fprintf(stderr, "Thread %lu spin wait rw-x-lock at %p" " cfile %s cline %lu rnds %lu\n", os_thread_pf(os_thread_get_curr_id()), (void*) lock, lock->cfile_name, (ulong) lock->cline, (ulong) i); } rw_x_spin_wait_count++; /* We try once again to obtain the lock. Acquire the mutex protecting the rw-lock fields */ mutex_enter(rw_lock_get_mutex(lock)); state = rw_lock_x_lock_low(lock, pass, file_name, line); if (state == RW_LOCK_EX) { mutex_exit(rw_lock_get_mutex(lock)); return; /* Locking succeeded */ } rw_x_system_call_count++; sync_array_reserve_cell(sync_primary_wait_array, lock, #ifdef __WIN__ /* On windows RW_LOCK_WAIT_EX signifies that this thread should wait on the special wait_ex_event. */ (state == RW_LOCK_WAIT_EX) ? RW_LOCK_WAIT_EX : #endif RW_LOCK_EX, file_name, line, &index); rw_lock_set_waiters(lock, 1); mutex_exit(rw_lock_get_mutex(lock)); if (srv_print_latch_waits) { fprintf(stderr, "Thread %lu OS wait for rw-x-lock at %p" " cfile %s cline %lu\n", os_thread_pf(os_thread_get_curr_id()), (void*) lock, lock->cfile_name, (ulong) lock->cline); } rw_x_system_call_count++; rw_x_os_wait_count++; sync_array_wait_event(sync_primary_wait_array, index); goto lock_loop; }
void rw_lock_s_lock_spin( /*================*/ rw_lock_t* lock, /* in: pointer to rw-lock */ ulint pass, /* in: pass value; != 0, if the lock will be passed to another thread to unlock */ const char* file_name, /* in: file name where lock requested */ ulint line) /* in: line where requested */ { ulint index; /* index of the reserved wait cell */ ulint i; /* spin round count */ ut_ad(rw_lock_validate(lock)); lock_loop: rw_s_spin_wait_count++; /* Spin waiting for the writer field to become free */ i = 0; while (rw_lock_get_writer(lock) != RW_LOCK_NOT_LOCKED && i < SYNC_SPIN_ROUNDS) { if (srv_spin_wait_delay) { ut_delay(ut_rnd_interval(0, srv_spin_wait_delay)); } i++; } if (i == SYNC_SPIN_ROUNDS) { os_thread_yield(); } if (srv_print_latch_waits) { fprintf(stderr, "Thread %lu spin wait rw-s-lock at %p" " cfile %s cline %lu rnds %lu\n", (ulong) os_thread_pf(os_thread_get_curr_id()), (void*) lock, lock->cfile_name, (ulong) lock->cline, (ulong) i); } mutex_enter(rw_lock_get_mutex(lock)); /* We try once again to obtain the lock */ if (TRUE == rw_lock_s_lock_low(lock, pass, file_name, line)) { mutex_exit(rw_lock_get_mutex(lock)); return; /* Success */ } else { /* If we get here, locking did not succeed, we may suspend the thread to wait in the wait array */ rw_s_system_call_count++; sync_array_reserve_cell(sync_primary_wait_array, lock, RW_LOCK_SHARED, file_name, line, &index); rw_lock_set_waiters(lock, 1); mutex_exit(rw_lock_get_mutex(lock)); if (srv_print_latch_waits) { fprintf(stderr, "Thread %lu OS wait rw-s-lock at %p" " cfile %s cline %lu\n", os_thread_pf(os_thread_get_curr_id()), (void*) lock, lock->cfile_name, (ulong) lock->cline); } rw_s_system_call_count++; rw_s_os_wait_count++; sync_array_wait_event(sync_primary_wait_array, index); goto lock_loop; } }
/******************************************************************//** NOTE! Use the corresponding macro, not directly this function! Lock an rw-lock in exclusive mode for the current thread. If the rw-lock is locked in shared or exclusive mode, or there is an exclusive lock request waiting, the function spins a preset time (controlled by SYNC_SPIN_ROUNDS), waiting for the lock before suspending the thread. If the same thread has an x-lock on the rw-lock, locking succeed, with the following exception: if pass != 0, only a single x-lock may be taken on the lock. NOTE: If the same thread has an s-lock, locking does not succeed! */ UNIV_INTERN void rw_lock_x_lock_func( /*================*/ rw_lock_t* lock, /*!< in: pointer to rw-lock */ ulint pass, /*!< in: pass value; != 0, if the lock will be passed to another thread to unlock */ const char* file_name,/*!< in: file name where lock requested */ ulint line) /*!< in: line where requested */ { ulint index; /*!< index of the reserved wait cell */ ulint i; /*!< spin round count */ ibool spinning = FALSE; ut_ad(rw_lock_validate(lock)); #ifdef UNIV_SYNC_DEBUG ut_ad(!rw_lock_own(lock, RW_LOCK_SHARED)); #endif /* UNIV_SYNC_DEBUG */ i = 0; lock_loop: if (rw_lock_x_lock_low(lock, pass, file_name, line)) { rw_x_spin_round_count += i; return; /* Locking succeeded */ } else { if (!spinning) { spinning = TRUE; rw_x_spin_wait_count++; } /* Spin waiting for the lock_word to become free */ while (i < SYNC_SPIN_ROUNDS && lock->lock_word <= 0) { if (srv_spin_wait_delay) { ut_delay(ut_rnd_interval(0, srv_spin_wait_delay)); } i++; } if (i == SYNC_SPIN_ROUNDS) { os_thread_yield(); } else { goto lock_loop; } } rw_x_spin_round_count += i; if (srv_print_latch_waits) { fprintf(stderr, "Thread %lu spin wait rw-x-lock at %p" " cfile %s cline %lu rnds %lu\n", os_thread_pf(os_thread_get_curr_id()), (void*) lock, innobase_basename(lock->cfile_name), (ulong) lock->cline, (ulong) i); } sync_array_reserve_cell(sync_primary_wait_array, lock, RW_LOCK_EX, file_name, line, &index); /* Waiters must be set before checking lock_word, to ensure signal is sent. This could lead to a few unnecessary wake-up signals. */ rw_lock_set_waiter_flag(lock); if (rw_lock_x_lock_low(lock, pass, file_name, line)) { sync_array_free_cell(sync_primary_wait_array, index); return; /* Locking succeeded */ } if (srv_print_latch_waits) { fprintf(stderr, "Thread %lu OS wait for rw-x-lock at %p" " cfile %s cline %lu\n", os_thread_pf(os_thread_get_curr_id()), (void*) lock, innobase_basename(lock->cfile_name), (ulong) lock->cline); } /* these stats may not be accurate */ lock->count_os_wait++; rw_x_os_wait_count++; sync_array_wait_event(sync_primary_wait_array, index); i = 0; goto lock_loop; }
/******************************************************************//** Lock an rw-lock in shared mode for the current thread. If the rw-lock is locked in exclusive mode, or there is an exclusive lock request waiting, the function spins a preset time (controlled by SYNC_SPIN_ROUNDS), waiting for the lock, before suspending the thread. */ UNIV_INTERN void rw_lock_s_lock_spin( /*================*/ rw_lock_t* lock, /*!< in: pointer to rw-lock */ ulint pass, /*!< in: pass value; != 0, if the lock will be passed to another thread to unlock */ const char* file_name, /*!< in: file name where lock requested */ ulint line) /*!< in: line where requested */ { ulint index; /* index of the reserved wait cell */ ulint i = 0; /* spin round count */ ut_ad(rw_lock_validate(lock)); rw_s_spin_wait_count++; /*!< Count calls to this function */ lock_loop: /* Spin waiting for the writer field to become free */ while (i < SYNC_SPIN_ROUNDS && lock->lock_word <= 0) { if (srv_spin_wait_delay) { ut_delay(ut_rnd_interval(0, srv_spin_wait_delay)); } i++; } if (i == SYNC_SPIN_ROUNDS) { os_thread_yield(); } if (srv_print_latch_waits) { fprintf(stderr, "Thread %lu spin wait rw-s-lock at %p" " cfile %s cline %lu rnds %lu\n", (ulong) os_thread_pf(os_thread_get_curr_id()), (void*) lock, innobase_basename(lock->cfile_name), (ulong) lock->cline, (ulong) i); } /* We try once again to obtain the lock */ if (TRUE == rw_lock_s_lock_low(lock, pass, file_name, line)) { rw_s_spin_round_count += i; return; /* Success */ } else { if (i < SYNC_SPIN_ROUNDS) { goto lock_loop; } rw_s_spin_round_count += i; sync_array_reserve_cell(sync_primary_wait_array, lock, RW_LOCK_SHARED, file_name, line, &index); /* Set waiters before checking lock_word to ensure wake-up signal is sent. This may lead to some unnecessary signals. */ rw_lock_set_waiter_flag(lock); if (TRUE == rw_lock_s_lock_low(lock, pass, file_name, line)) { sync_array_free_cell(sync_primary_wait_array, index); return; /* Success */ } if (srv_print_latch_waits) { fprintf(stderr, "Thread %lu OS wait rw-s-lock at %p" " cfile %s cline %lu\n", os_thread_pf(os_thread_get_curr_id()), (void*) lock, innobase_basename(lock->cfile_name), (ulong) lock->cline); } /* these stats may not be accurate */ lock->count_os_wait++; rw_s_os_wait_count++; sync_array_wait_event(sync_primary_wait_array, index); i = 0; goto lock_loop; } }