WireContext *TwoWire::getWireContext(void) { WireContext *wc; wc = (WireContext *)Task_getEnv(Task_self()); if (wc == NULL) { wc = (WireContext *)Memory_alloc(NULL, sizeof(WireContext), 4, NULL); wc->idle = true; wc->rxReadIndex = 0; wc->rxWriteIndex = 0; wc->txReadIndex = 0; wc->txWriteIndex = 0; /* I2C Transfer initial params */ wc->i2cTransaction.slaveAddress = 0; wc->i2cTransaction.writeBuf = wc->txBuffer; wc->i2cTransaction.readBuf = wc->rxBuffer; wc->i2cTransaction.readCount = 0; wc->i2cTransaction.writeCount = 0; Task_setEnv(Task_self(), (void *)wc); } return (wc); }
/* * ======== _pthread_runStub ======== */ static void _pthread_runStub(UArg arg0, UArg arg1) { UInt key; Ptr arg; pthread_Obj *thread = (pthread_Obj *)(xdc_uargToPtr(arg1)); arg = Task_getEnv(thread->task); thread->ret = thread->fxn(arg); /* Pop and execute the cleanup handlers */ while (thread->cleanupList != NULL) { _pthread_cleanup_pop(thread->cleanupList, 1); } /* Cleanup any pthread specific data */ _pthread_removeThreadKeys((pthread_t)thread); key = Task_disable(); if (!thread->detached) { Semaphore_post(Semaphore_handle(&(thread->joinSem))); /* * Set this task's priority to -1 to prevent it from being put * on the terminated queue (and deleted if Task.deleteTerminatedTasks * is true). pthread_join() will delete the Task object. */ Task_setPri(thread->task, -1); Task_restore(key); } else { Task_restore(key); /* Free memory */ #if ti_sysbios_posix_Settings_supportsMutexPriority__D Queue_destruct(&(thread->mutexList)); #endif Semaphore_destruct(&(thread->joinSem)); Memory_free(Task_Object_heap(), thread, sizeof(pthread_Obj)); /* The system will have to clean up the Task object */ } /* Task_exit() is called when returning from this function */ }
thread_t thread_create(thread_f func, void * arg) { #ifdef POSIX thread_t handle = 0; pthread_t p; if (f_initialized == false_e) { mutex_init(&f_lock); f_initialized = true_e; } int err = pthread_create(&p, NULL, func, arg); if (err == EINVAL) { SOSAL_PRINT(SOSAL_ZONE_ERROR, "ERROR! Invalid settings in pthread_attr\n"); } else if (err == EAGAIN) { SOSAL_PRINT(SOSAL_ZONE_ERROR, "ERROR! Insufficient resources to start thread!\n"); } handle = p; return handle; #elif defined(SYSBIOS) thread_t sosal_thread_hdl; #ifdef JOIN_SEMAPHORE semaphore_t *semaphore_hdl; mutex_t *mutex_hdl; #else event_t *event_hdl; #endif /* void *stack_ptr; */ bool_e ret_val; Task_Params taskParams; long pid = 0; sosal_thread_hdl = malloc(sizeof(*sosal_thread_hdl)); if(!sosal_thread_hdl) { SOSAL_PRINT(SOSAL_ZONE_ERROR, "ERROR! Can't allocate memory for thread SOSAL hdl!\n"); return NULL; } #ifdef JOIN_SEMAPHORE semaphore_hdl = malloc(sizeof(semaphore_t)); if(!semaphore_hdl) { free(sosal_thread_hdl); SOSAL_PRINT(SOSAL_ZONE_ERROR, "ERROR! Can't create join semaphore!\n"); return NULL; } ret_val = semaphore_create(semaphore_hdl, 0, (bool_e)0); if(!ret_val) { free(semaphore_hdl); free(sosal_thread_hdl); SOSAL_PRINT(SOSAL_ZONE_ERROR, "ERROR! Can't create exit event!\n"); return NULL; } sosal_thread_hdl->join_semaphore = semaphore_hdl; mutex_hdl = malloc(sizeof(mutex_t)); if(!mutex_hdl) { free(semaphore_hdl); free(sosal_thread_hdl); SOSAL_PRINT(SOSAL_ZONE_ERROR, "ERROR! Can't create join semaphore!\n"); return NULL; } mutex_init(mutex_hdl); sosal_thread_hdl->join_mutex = mutex_hdl; sosal_thread_hdl->join_number = 0; #else event_hdl = malloc(sizeof(event_t)); if(!event_hdl) { free(sosal_thread_hdl); SOSAL_PRINT(SOSAL_ZONE_ERROR, "ERROR! Can't allocate memory for exit event!\n"); return NULL; } /* TODO: It isn't necessary to allocate stack memory stack_ptr = malloc(SYSBIOS_DEFAULT_STASK_SIZE); if(!stack_ptr) { SOSAL_PRINT(SOSAL_ZONE_ERROR, "ERROR! Can't allocate memory for thread stack!\n"); free(sosal_thread_hdl->exit_event); free(sosal_thread_hdl); return NULL; } sosal_thread_hdl->stack_ptr = stack_ptr; */ ret_val = event_init(event_hdl, false_e); if(!ret_val) { /* free(sosal_thread_hdl->stack_ptr); */ free(event_hdl); free(sosal_thread_hdl); SOSAL_PRINT(SOSAL_ZONE_ERROR, "ERROR! Can't create exit event!\n"); return NULL; } sosal_thread_hdl->exit_event = event_hdl; sosal_thread_hdl->pAckList = NULL; #endif sosal_thread_hdl->client_arg = arg; sosal_thread_hdl->client_func = func; Task_Params_init(&taskParams); taskParams.arg0 = 1; taskParams.arg1 = (UArg)(sosal_thread_hdl); taskParams.env = NULL; taskParams.priority = SYSBIOS_DEFAULT_PRIORITY; /* taskParams.stack = (void *)stack_ptr;*/ taskParams.stackSize = SYSBIOS_DEFAULT_STASK_SIZE; taskParams.instance->name = NULL; pid = (long) Task_getEnv(Task_self()); taskParams.env = (void *) pid; sosal_thread_hdl->thread_hdl = Task_create((Task_FuncPtr)thread_main_func_translate, &taskParams, NULL); sosal_thread_hdl->bTaskReady = false_e; return sosal_thread_hdl; #else thread_t handle = 0; HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, CREATE_SUSPENDED, NULL); if (hThread) { ResumeThread(hThread); } handle = hThread; return handle; #endif }
/* * ======== ThreadSupport_self ======== */ IThreadSupport_Handle ThreadSupport_self(Error_Block* eb) { /* thread handls is stored in Task's env during create */ return (IThreadSupport_Handle)Task_getEnv(Task_self()); }