void mt_sleep(int secs) { if ( inThread ) // When any of the thread calls sleep { myThreadList[ currentThread ].si.sleepMode = 1; myThreadList[ currentThread ].si.start_time = time(NULL); myThreadList[ currentThread ].si.sleepDuration = secs; _DEBUG_PRINT2( "Going to Sleep Thread %d.", myThreadList[currentThread].id ); mt_yield(); _DEBUG_PRINT2( "Returning from Sleep Thread %d.", myThreadList[currentThread].id ); myThreadList[ currentThread ].si.sleepMode = 0; myThreadList[ currentThread ].si.start_time = 0; myThreadList[ currentThread ].si.sleepDuration = 0; } else // When main calls sleep { sleepInfoMainThread.sleepMode = 1; sleepInfoMainThread.start_time = time(NULL); sleepInfoMainThread.sleepDuration = secs; _DEBUG_PRINT1( "Going to Sleep Thread Main."); while((sleepInfoMainThread.start_time + sleepInfoMainThread.sleepDuration) > time(NULL)) mt_yield(); // main thread yields _DEBUG_PRINT1( "Returning from Sleep Thread Main."); sleepInfoMainThread.sleepMode = 0; sleepInfoMainThread.start_time = 0; sleepInfoMainThread.sleepDuration = 0; } return; }
/*---------------------------------------------------------------------------*/ static void checkpoint_thread_loop(void *data) { uint8_t cmd; int fd; while(1) { /* Store command and file descriptor on stack */ cmd = preset_cmd; fd = preset_fd; /* Handle command */ if(cmd == COMMAND_ROLLBACK) { PRINTF_COMMAND("RB:START\n"); thread_rollback(fd); nr_rollbacks++; PRINTF_COMMAND("RB:DONE %u\n", nr_rollbacks); /* TODO Synch before leaving this thread. */ } else if(cmd == COMMAND_CHECKPOINT) { PRINTF_COMMAND("CP:START\n"); thread_checkpoint(fd); thread_metrics(); nr_checkpoints++; PRINTF_COMMAND("CP:DONE %u\n", nr_checkpoints); } else if(cmd == COMMAND_METRICS) { thread_metrics(); } else { printf("ERROR: Unknown thread command: %u\n", cmd); } /* Return to Contiki */ mt_yield(); } }
char* _std log_gettext(u32t flags) { getloginfo li = {flags, 0}; while (log_query(getlog,&li)==EBUSY) mt_yield(); // change block context to current process if ((flags&LOGTF_SHARED)==0) mem_localblock(li.rc); return li.rc; }
int mt_join(mythread_id identifier) { int threadsRemaining = 0; _DEBUG_PRINT2( "mt_join() Called - Waiting for identifier = %d",identifier); /* If we are in a thread, wait for all the *other* threads to quit */ if ( inThread ) threadsRemaining = 1; _DEBUG_PRINT2( "Waiting until there are only %d threads remaining...", threadsRemaining ); while ( numWaitingThreads > threadsRemaining ) { mt_yield(); _DEBUG_PRINT2( "mt_join() - After yield - myThreadList[currentThread].id = %d",myThreadList[currentThread].id); _DEBUG_PRINT2( "mt_join() - After yield - identifier = %d",identifier); _DEBUG_PRINT2( "mt_join() - Checking running status of myThreadList[identifier-1001].runningStatus = %d",myThreadList[identifier-1001].runningStatus); //if(myThreadList[currentThread].id == identifier && myThreadList[currentThread].runningStatus == 0) //If the thread for which we are waiting is finished running then exit if(myThreadList[identifier-INITIAL_MY_THREAD_IDENTIFIER+1].runningStatus == 0) { _DEBUG_PRINT2( "mt_join() - Completed execution of Thread id %d", identifier ); break; } } _DEBUG_PRINT2( "mt_join() - Exiting mt_join() - numWaitingThreads = %d",numWaitingThreads); _DEBUG_PRINT2( "mt_join() - Exiting mt_join()- threadsRemaining %d",threadsRemaining); return MT_NOERROR; }
/*---------------------------------------------------------------------------*/ static void thread_loop(void *data) { uint8_t cmd; int fd; while(1) { /* Store command and file descriptor on stack */ cmd = preset_cmd; fd = preset_fd; /* Handle command */ if(cmd == COMMAND_ROLLBACK) { PRINTF("Rolling back"); thread_rollback(fd); PRINTF(" done!\n"); } else if(cmd == COMMAND_CHECKPOINT) { PRINTF("Checkpointing"); thread_checkpoint(fd); PRINTF(" done!\n"); } else if(cmd == COMMAND_TBR) { PRINTF("Writing TBR"); // write_word(fd, TBR); PRINTF(" done!\n"); } else { printf("Error: unknown command: %u\n", cmd); } /* Return to main Contiki thread */ mt_yield(); } }
static void thread_handler2 (void* data) { while (1) { rs232_print_p (RS232_PORT_1, PSTR ("Thread 2. Data: ")); rs232_printf (RS232_PORT_1, "0x%x, %d\n", data, *(uint8_t*)data ); mt_yield (); } }
int mt_joinall() { int threadsRemaining = 0; _DEBUG_PRINT1( "mt_joinall() called."); /* If we are in a thread, wait for all the *other* threads to quit */ if ( inThread ) threadsRemaining = 1; _DEBUG_PRINT2( "Waiting until there are only %d threads remaining...", threadsRemaining ); while ( numWaitingThreads > threadsRemaining ) { mt_yield(); } return MT_NOERROR; }
int mt_sem_down(mt_sem ksem) { // Check if the same thread is trying to acquire the lock to enter the critical section // In that case don't allow it. Because it might create a deadlock int i; for(i=0;i<MAX_THREADS && ksem->threads[i] != -1;i++) { if(myThreadList[currentThread].id == ksem->threads[i]) { _DEBUG_PRINT2("Error Decrementing semaphore for thread %d",myThreadList[currentThread].id); return MT_ERROR; } } while(ksem->sVal < -(ksem->initVal)) { mt_yield(); } ksem->threads[-(ksem->sVal)] = myThreadList[currentThread].id; ksem->sVal--; _DEBUG_PRINT2("Decrementing semaphore to %d",ksem->sVal); return MT_NOERROR; }