void uart_init(uart_receive_callback_t callback) { /* initialize receive fifo */ fifo_init(&uart_state.fifo, sizeof(uart_state.buf)); /* store callback */ uart_state.receive = callback; /* register threads */ THREAD_INIT(uart_send); thread_register(&uart_send); THREAD_INIT(uart_recv); thread_register(&uart_recv); }
/* * Starts a new thread with priority "prio" that will begin its execution in the * function "thread()". The "arg" argument will be passed as an argument to the * thread() function. The argument "ssize" is the requested stack size for the * new thread. The id of the new thread is returned. Both the id and the * priority are system dependent. */ sys_thread_t sys_arch_thread_new( void ( *thread ) ( void *arg ), void *arg, int prio, size_t ssize ) { sys_thread_t thread_hdl = SYS_THREAD_NULL; int i; sys_tcb_t *p; char thread_name[ configMAX_TASK_NAME_LEN ]; /* We disable the FreeRTOS scheduler because it might be the case that the new * tasks gets scheduled inside the xTaskCreate function. To prevent this we * disable the scheduling. Note that this can happen although we have interrupts * disabled because xTaskCreate contains a call to taskYIELD( ). */ vPortEnterCritical( ); p = tasks; i = 0; /* We are called the first time. Initialize it. */ if( p == NULL ) { p = pvPortMalloc( sizeof( sys_tcb_t ) ); if( p != NULL ) { tasks = p; } } else { /* First task already counter. */ i++; /* Cycle to the end of the list. */ while( p->next != NULL ) { i++; p = p->next; } p->next = pvPortMalloc( sizeof( sys_tcb_t ) ); p = p->next; } if( p != NULL ) { /* Memory allocated. Initialize the data structure. */ THREAD_INIT( p ); ( void )snprintf( thread_name, configMAX_TASK_NAME_LEN, "lwIP%d", i ); /* Now q points to a free element in the list. */ if( xTaskCreate( thread, thread_name, ssize, arg, prio, &p->pid ) == pdPASS ) { thread_hdl = p; } else { vPortFree( p ); } } vPortExitCritical( ); return thread_hdl; }
int main(int argc, char ** argv) { shared_queue_t q; shared_queue_init(&q, N, sizeof(int)); THREAD_T enqueue_thread; THREAD_T dequeue_thread; THREAD_INIT(enqueue_thread, enqueue_thread_main, &q); THREAD_INIT(dequeue_thread, dequeue_thread_main, &q); THREAD_JOIN(enqueue_thread); THREAD_JOIN(dequeue_thread); shared_queue_destroy(&q); return 0; }
void sys_arch_thread_remove( sys_thread_t hdl ) { sys_tcb_t *current = tasks, *prev; sys_tcb_t *toremove = hdl; xTaskHandle pid = ( xTaskHandle ) 0; LWIP_ASSERT( "sys_arch_thread_remove: assertion hdl != NULL failed!", hdl != NULL ); /* If we have to remove the first task we must update the global "tasks" * variable. */ vPortEnterCritical( ); if( hdl != NULL ) { prev = NULL; while( ( current != NULL ) && ( current != toremove ) ) { prev = current; current = current->next; } /* Found it. */ if( current == toremove ) { /* Not the first entry in the list. */ if( prev != NULL ) { prev->next = toremove->next; } else { tasks = toremove->next; } LWIP_ASSERT( "sys_arch_thread_remove: can't remove thread with timeouts!", toremove->timeouts.next == NULL ); pid = toremove->pid; THREAD_INIT( toremove ); vPortFree( toremove ); } } /* We are done with accessing the shared datastructure. Release the * resources. */ vPortExitCritical( ); if( pid != ( xTaskHandle ) 0 ) { vTaskDelete( pid ); /* not reached. */ } }
void clock_init(void) { THREAD_INIT(clock_thread); thread_register(&clock_thread); clock_state.stored = eeprom_load(); /* no state saved */ if (clock_state.stored & 0xc000) { return; } /* valid clock state stored */ if ((clock_state.stored & 0x07ff) <= CLOCK_MINUTES) { clock_state.time = clock_state.clock = clock_state.stored & 0x07ff; clock_state.state = CLOCK_PENDING; advance_set_polarity((~(clock_state.stored >> 11)) & 1); advance(); } else {
int main (int argc, char *argv[]) { long c; extern char *optarg; CLOCK(starttime); while ((c = getopt(argc, argv, "osh")) != -1) { switch(c) { case 'o': do_output = 1; break; case 's': do_stats = 1; break; case 'h': Help(); break; } } MAIN_INITENV(,40000000); GetArguments(); printf("Number of processors: %d\n", Number_Of_Processors); THREAD_INIT(Number_Of_Processors); InitGlobalMemory(); InitExpTables(); CreateDistribution(Cluster, Model); /* for (i = 1; i < Number_Of_Processors; i++) { CREATE(ParallelExecute); } ParallelExecute(); WAIT_FOR_END(Number_Of_Processors - 1);*/ CREATE(ParallelExecute, Number_Of_Processors); WAIT_FOR_END(Number_Of_Processors); printf("Finished FMM\n"); PrintTimes(); if (do_output) { PrintAllParticles(); } MAIN_END; }
int main(int argc, char ** argv) { if (argc != 2) { printf("usage: %s <number_of_philosopher>\n", argv[0]); return 0; } srand(time(NULL)); int n = (int) strtol(argv[1], (char **)NULL, 10); MUTEX_T* chopsticks = (MUTEX_T*) malloc(n * sizeof(MUTEX_T)); THREAD_T* philosophers = (THREAD_T*) malloc(n * sizeof(THREAD_T)); for (int i = 0; i < n; i++) { MUTEX_INIT(chopsticks[i]); } for (int i = 0; i < n; i++) { context_t* ctx = (context_t*) malloc(sizeof(context_t)); ctx->id = i; ctx->left_chopstick = &chopsticks[i]; ctx->right_chopstick = &chopsticks[(i+1) % n]; THREAD_INIT(philosophers[i], print_hello, ctx); } for (int i = 0; i < n; i++) { THREAD_JOIN(philosophers[i]); THREAD_DESTROY(philosophers[i]); } for (int i = 0; i < n; i++) { MUTEX_DESTROY(chopsticks[i]); } return 0; }
/* * Starts a new thread with priority "prio" that will begin its execution in the * function "thread()". The "arg" argument will be passed as an argument to the * thread() function. The argument "ssize" is the requested stack size for the * new thread. The id of the new thread is returned. Both the id and the * priority are system dependent. */ sys_thread_t sys_thread_new(char *name, void ( *thread ) ( void *arg ), void *arg, int stacksize, int prio ) { sys_thread_t thread_hdl = SYS_THREAD_NULL; int i,j; sys_tcb_t *p; /* We disable the FreeRTOS scheduler because it might be the case that the new * tasks gets scheduled inside the xTaskCreate function. To prevent this we * disable the scheduling. Note that this can happen although we have interrupts * disabled because xTaskCreate contains a call to taskYIELD( ). */ UserEnterCritical( ); p = tasks; i = 0; /* We are called the first time. Initialize it. */ if( p == NULL ) { p = (sys_tcb_t *)BRTOS_ALLOC( sizeof( sys_tcb_t ) ); if( p != NULL ) { tasks = p; } } else { /* First task already counter. */ i++; /* Cycle to the end of the list. */ while( p->next != NULL ) { i++; p = p->next; } p->next = (sys_tcb_t *)BRTOS_ALLOC( sizeof( sys_tcb_t ) ); p = p->next; } if( p != NULL ) { /* Memory allocated. Initialize the data structure. */ THREAD_INIT( p ); /* Now q points to a free element in the list. */ //if( xTaskCreate( thread, (const signed char *)name, stacksize, arg, prio, &p->pid ) == pdPASS ) if (InstallTask(thread, name, stacksize, prio, arg) == OK) { for(j = 1;j<NUMBER_OF_TASKS;j++) { if (ContextTask[j].Priority == prio) { p->pid = &ContextTask[j]; thread_hdl = p->pid; } } } else { BRTOS_DEALLOC( p ); } } UserExitCritical( ); return thread_hdl; }
Threading() { THREAD_INIT(); MUTEX_CREATE(); }
int sem_init(sem_t *sem, int pshared, unsigned int value) { int retval; if (!THREAD_SAFE()) THREAD_INIT(); /* * Range check the arguments. */ if (pshared != 0) { /* * The user wants a semaphore that can be shared among * processes, which this implementation can't do. Sounds like a * permissions problem to me (yeah right). */ errno = EPERM; retval = -1; goto RETURN; } if (value > SEM_VALUE_MAX) { errno = EINVAL; retval = -1; goto RETURN; } *sem = (sem_t)malloc(sizeof(struct sem)); if (*sem == NULL) { errno = ENOSPC; retval = -1; goto RETURN; } /* * Initialize the semaphore. */ if (pthread_mutex_init(&(*sem)->lock, NULL) != 0) { free(*sem); errno = ENOSPC; retval = -1; goto RETURN; } if (pthread_cond_init(&(*sem)->gtzero, NULL) != 0) { pthread_mutex_destroy(&(*sem)->lock); free(*sem); errno = ENOSPC; retval = -1; goto RETURN; } (*sem)->count = (u_int32_t)value; (*sem)->nwaiters = 0; (*sem)->magic = SEM_MAGIC; retval = 0; RETURN: return retval; }