void thread_6_and_7_entry(ULONG thread_input) { UINT status; /* This function is executed from thread 6 and thread 7. As the loop below shows, these function compete for ownership of mutex_0. */ while(1) { /* Increment the thread counter. */ if (thread_input == 6) thread_6_counter++; else thread_7_counter++; /* Get the mutex with suspension. */ status = tx_mutex_get(&mutex_0, TX_WAIT_FOREVER); /* Check status. */ if (status != TX_SUCCESS) break; /* Get the mutex again with suspension. This shows that an owning thread may retrieve the mutex it owns multiple times. */ status = tx_mutex_get(&mutex_0, TX_WAIT_FOREVER); /* Check status. */ if (status != TX_SUCCESS) break; /* Sleep for 2 ticks to hold the mutex. */ tx_thread_sleep(2); /* Release the mutex. */ status = tx_mutex_put(&mutex_0); /* Check status. */ if (status != TX_SUCCESS) break; /* Release the mutex again. This will actually release ownership since it was obtained twice. */ status = tx_mutex_put(&mutex_0); /* Check status. */ if (status != TX_SUCCESS) break; } }
int UnLockMutex(wolfSSL_Mutex* m) { if (tx_mutex_put(m) == 0) return 0; else return BAD_MUTEX_E; }
FS_STATUS fs_filesize(const char* filename, unsigned* length) { SectorDescriptor sec; FS_STATUS status; uint8_t secEuIdx; if (filename == NULL || length == NULL) { return COMMAND_PARAMETERS_ERROR; } if (!gFsIsReady) { return FS_NOT_READY; } if (tx_mutex_get(&gFsGlobalLock,TX_NO_WAIT) != TX_SUCCESS) { return FS_IS_BUSY; } //search for this filename status = getSectorDescriptor(filename,&sec,&secEuIdx,NULL); *length = sec.size; tx_mutex_put(&gFsGlobalLock); return status; }
wiced_result_t wiced_rtos_unlock_mutex( wiced_mutex_t* mutex ) { if ( tx_mutex_put( mutex ) != TX_SUCCESS ) { return WICED_ERROR; } return WICED_SUCCESS; }
FS_STATUS fs_read(const char* filename, unsigned* length, char* data) { SectorDescriptor sec; FS_STATUS status = FS_SUCCESS; uint8_t secEuIndex; if (filename == NULL || length == NULL || data == NULL) { return COMMAND_PARAMETERS_ERROR; } if (!gFsIsReady) { return FS_NOT_READY; } if (tx_mutex_get(&gFsGlobalLock,TX_NO_WAIT) != TX_SUCCESS) { return FS_IS_BUSY; } do { //get the file status = getSectorDescriptor(filename,&sec,&secEuIndex,NULL); if (status != FS_SUCCESS) { break; } //given buffer is to small if (sec.size > *length) { status = FAILURE; break; } *length = sec.size; //read the file's content if (flash_read(FLASH_BLOCK_SIZE*secEuIndex + sec.offset,sec.size,(uint8_t*)data) != OPERATION_SUCCESS) { status = FAILURE_ACCESSING_FLASH; break; } }while(false); tx_mutex_put(&gFsGlobalLock); return status; }
void Slow_Thread_entry(ULONG thread_input) { ULONG current_time; while(1) { /* Activity 5 - 12 timer-ticks *** critical section *** */ /* Get the mutex with suspension */ tx_mutex_get(&my_mutex, TX_WAIT_FOREVER); tx_thread_sleep(12); /* Release the mutex */ tx_mutex_put(&my_mutex); /* Activity 6 - 8 timer-ticks */ tx_thread_sleep(8); /* Activity 7 - 11 timer-ticks *** critical section *** */ /* Get the mutex with suspension */ tx_mutex_get(&my_mutex, TX_WAIT_FOREVER); tx_thread_sleep(11); /* Release the mutex */ tx_mutex_put(&my_mutex); /* Activity 8 - 9 timer-ticks */ tx_thread_sleep(9); current_time = tx_time_get(); printf("Current Time: %5lu Slow_Thread finished a cycle¡\n", current_time); } }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : DriveWriteSector * Returned Value : SUCCESS or ERROR * - SUCCESS * - ERROR_DDI_LDL_LDRIVE_INVALID_DRIVE_NUMBER * - ERROR_DDI_LDL_LDRIVE_NOT_INITIALIZED * - Others possible from drive type's WriteSector API * Comments : * Writes a sector of a logical drive. * *END*--------------------------------------------------------------------*/ RtStatus_t DriveWriteSector ( /* [IN] Unique tag for the drive to operate on. */ DriveTag_t tag, /* [IN] Sector to write; 0-based at the start of the drive. */ uint32_t u32SectorNumber, /* [IN] Pointer to buffer of sector data to write. */ const SECTOR_BUFFER * pSectorData ) { /* Body */ /* Get drive depending on its tag */ LogicalDrive * drive = DriveGetDriveFromTag(tag); if (!drive) { return ERROR_DDI_LDL_LDRIVE_INVALID_DRIVE_NUMBER; } else if (!drive->isInitialized()) { return ERROR_DDI_LDL_LDRIVE_NOT_INITIALIZED; } /* Endif */ #if defined(USE_NAND_STACK) && defined(NO_SDRAM) if (drive->getMedia()->getPhysicalType() != kMediaTypeMMC) { static RtStatus_t s_RetValue; TX_THREAD *pCurrentThread; tx_mutex_get(&g_NANDThreadSafeMutex, TX_WAIT_FOREVER); pCurrentThread = tx_thread_identify(); if (pCurrentThread != NULL) { os_thi_SaveStackContext(&g_NewNandStackContext, pCurrentThread, &g_OldNandStackContext, 40); } s_RetValue = drive->writeSector(u32SectorNumber, pSectorData); if (pCurrentThread != NULL) { os_thi_RestoreStackContext(&g_OldNandStackContext, pCurrentThread); } tx_mutex_put(&g_NANDThreadSafeMutex); return s_RetValue; } /* Endif */ #endif /* Defined(USE_NAND_STACK) && defined(NO_SDRAM) */ return drive->writeSector(u32SectorNumber, pSectorData); } /* Endbody */
void Speedy_Thread_entry(ULONG thread_input) { ULONG current_time; while (1) { /* Activity 1: 2 timer-ticks */ tx_thread_sleep(2); /* Get the mutex with suspension */ tx_mutex_get(&my_mutex, TX_WAIT_FOREVER); /* Activity 2: 5 timer-ticks *** critical section *** */ tx_thread_sleep(5); /* Release the mutex */ tx_mutex_put(&my_mutex); /* Activity 3: 4 timer-ticks */ tx_thread_sleep(4); /* Get the mutex with suspension */ tx_mutex_get(&my_mutex, TX_WAIT_FOREVER); /* Activity 4: 3 timer-ticks *** critical section *** */ tx_thread_sleep(3); /* Release the mutex */ tx_mutex_put(&my_mutex); current_time = tx_time_get(); printf(" Current Time: %5lu Speedy_Thread finished a cycle¡\n", current_time); } }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : DriveFlush * Returned Value : SUCCESS of ERROR * - SUCCESS * - ERROR_DDI_LDL_LDRIVE_INVALID_DRIVE_NUMBER * - ERROR_DDI_LDL_LDRIVE_NOT_INITIALIZED * - ERROR_DDI_LDL_LMEDIA_MEDIA_NOT_INITIALIZED * - MMC_DATA_DRIVE_ERROR_WRITE_SECTOR_FAIL * - ERROR_DDI_LDL_LDRIVE_HARDWARE_FAILURE * Comments : * Flush the logical drive number's contents from RAM to physical media. * *END*--------------------------------------------------------------------*/ RtStatus_t DriveFlush ( /* [IN] Unique tag for the drive to operate on. */ DriveTag_t tag ) { /* Body */ /* Get drive object */ LogicalDrive * drive = DriveGetDriveFromTag(tag); if (!drive) { return ERROR_DDI_LDL_LDRIVE_INVALID_DRIVE_NUMBER; } else if (!drive->isInitialized() ) { return ERROR_DDI_LDL_LDRIVE_NOT_INITIALIZED; } /* Endif */ #if (defined(USE_NAND_STACK) && defined(NO_SDRAM)) if (drive->getMedia()->getPhysicalType() != kMediaTypeMMC) { static RtStatus_t s_RetValue; TX_THREAD *pCurrentThread; tx_mutex_get(&g_NANDThreadSafeMutex, TX_WAIT_FOREVER); pCurrentThread=tx_thread_identify(); if (pCurrentThread != NULL) { os_thi_SaveStackContext(&g_NewNandStackContext, pCurrentThread, &g_OldNandStackContext, 40); } /* Endif */ s_RetValue = drive->flush(); if (pCurrentThread != NULL) { os_thi_RestoreStackContext(&g_OldNandStackContext, pCurrentThread); } /* Endif */ tx_mutex_put(&g_NANDThreadSafeMutex); return s_RetValue; } /* Endif */ #endif /* Flush */ return drive->flush(); } /* Endbody */
FS_STATUS fs_count(unsigned* file_count) { //take the lock if (!gFsIsReady) { return FS_NOT_READY; } //take lock if (tx_mutex_get(&gFsGlobalLock,TX_NO_WAIT) != TX_SUCCESS) { return FS_IS_BUSY; } *file_count = gFilesCount; tx_mutex_put(&gFsGlobalLock); return FS_SUCCESS; }
FS_STATUS fs_erase(const char* filename) { FS_STATUS status; if (!gFsIsReady) { return FS_NOT_READY; } //take lock if (tx_mutex_get(&gFsGlobalLock,TX_NO_WAIT) != TX_SUCCESS) { return FS_IS_BUSY; } status = eraseFileFromFS(filename); tx_mutex_put(&gFsGlobalLock); return status; }
void __malloc_unlock(struct _reent *ptr) { UNUSED_PARAMETER( ptr ); tx_mutex_put( &malloc_mutex ); }
FS_STATUS fs_write(const char* filename, unsigned length, const char* data) { LogEntry entry; FS_STATUS status = MAXIMUM_FLASH_SIZE_EXCEEDED; uint8_t euIndex,oldLogIndex; if (filename == NULL || data == NULL) { return COMMAND_PARAMETERS_ERROR; } if (length > MAX_FILE_SIZE) { return MAXIMUM_FILE_SIZE_EXCEEDED; } if (gFilesCount > MAX_FILES_CAPACITY) { return MAXIMUM_FILES_CAPACITY_EXCEEDED; } if (!gFsIsReady) { return FS_NOT_READY; } if (tx_mutex_get(&gFsGlobalLock,TX_NO_WAIT) != TX_SUCCESS) { return FS_IS_BUSY; } do { //try to find free space if (findFreeEraseUnit(length,&euIndex)) { status = eraseFileFromFS(filename); if (status != FS_SUCCESS && status != FILE_NOT_FOUND) { status = FAILURE_ACCESSING_FLASH; break; } status = writeFile(filename,length,data,euIndex); } //try to find EraseUnit that after compaction will have enough space else if (findEraseUnitToCompact(&euIndex,length+sizeof(SectorDescriptor))) { entry.data = FLASH_UNINIT_4_BYTES; oldLogIndex = gLogIndex; if (CompactBlock(euIndex,entry) == FS_SUCCESS) { //erase previouse file with same name if such exist status = eraseFileFromFS(filename); if (status != FS_SUCCESS && status != FILE_NOT_FOUND) { status = FAILURE_ACCESSING_FLASH; break; } //write the actual file status = writeFile(filename,length,data,oldLogIndex); } } }while(false); tx_mutex_put(&gFsGlobalLock); return status; }
FS_STATUS fs_get_filename_by_index(unsigned index,unsigned* length, char* name) { SectorDescriptor desc; uint16_t actualSecDesc; uint8_t euIdx = 0,i,fileNameLen = 0; FS_STATUS status = FS_SUCCESS; if (!gFsIsReady) { return FS_NOT_READY; } //take lock if (tx_mutex_get(&gFsGlobalLock,TX_NO_WAIT) != TX_SUCCESS) { return FS_IS_BUSY; } do { if (index >= gFilesCount) { status = FILE_NOT_FOUND; break; } while (index >= gEUList[euIdx].validDescriptors) { index-=gEUList[euIdx].validDescriptors; ++euIdx; } actualSecDesc = 0; for(i = 0 ; i <= index ; ++i) { if ((status = getNextValidSectorDescriptor(euIdx,&actualSecDesc,&desc)) != FS_SUCCESS) { break; } } while(fileNameLen < FILE_NAME_MAX_LEN && desc.fileName[fileNameLen] != '\0') { ++fileNameLen; } if (fileNameLen > *length) { status = FAILURE; break; } memcpy(name,desc.fileName,fileNameLen); *length = fileNameLen; }while(false); tx_mutex_put(&gFsGlobalLock); return status; }
FS_STATUS fs_list(unsigned* length, char* files) { SectorDescriptor secDesc; uint16_t secIdx,lastActualSecIdx,ansIndex = 0; bool found = false; uint8_t euIdx; uint8_t filenameLen; char fsFilename[FILE_NAME_MAX_LEN+1] = {'\0'}; FS_STATUS status = FS_SUCCESS; if (files == NULL || length == NULL) { return COMMAND_PARAMETERS_ERROR; } if (!gFsIsReady) { return FS_NOT_READY; } //take lock if (tx_mutex_get(&gFsGlobalLock,TX_NO_WAIT) != TX_SUCCESS) { return FS_IS_BUSY; } for(euIdx = 0 ; euIdx < ERASE_UNITS_NUMBER ; ++euIdx) { //skip the log EU if (gLogIndex == euIdx || gEUList[euIdx].validDescriptors == 0) { continue; } lastActualSecIdx = 0; //read current EraseUnit valid sectors for(secIdx = 0 ; secIdx < gEUList[euIdx].validDescriptors ; ++secIdx) { getNextValidSectorDescriptor(euIdx,&lastActualSecIdx,&secDesc); strncpy(fsFilename,secDesc.fileName,FILE_NAME_MAX_LEN); filenameLen = strlen(fsFilename); //check if the given buffer long enough to store current file name + '\0' if ((uint16_t)(ansIndex + filenameLen + 1) > *length) { status = FAILURE; break; } strcpy(&files[ansIndex],fsFilename); ansIndex+=(filenameLen+1);//+1 for the terminator '\0' } } *length = ansIndex; //return lock tx_mutex_put(&gFsGlobalLock); return status; }