USBH: MSD: replace mutex with semaphores (more lightweight)

This commit is contained in:
Diego Ismirlian 2017-07-14 12:14:59 -03:00
parent 0f269d99fd
commit 3d6f481ba8
2 changed files with 33 additions and 49 deletions

View File

@ -59,7 +59,7 @@ struct USBHMassStorageLUNDriver {
_base_block_device_data _base_block_device_data
/* for serializing access to the LUN driver */ /* for serializing access to the LUN driver */
mutex_t mtx; semaphore_t sem;
BlockDeviceInfo info; BlockDeviceInfo info;
USBHMassStorageDriver *msdp; USBHMassStorageDriver *msdp;

View File

@ -695,12 +695,12 @@ static const struct USBHMassStorageDriverVMT blk_vmt = {
static void _lun_object_deinit(USBHMassStorageLUNDriver *lunp) { static void _lun_object_deinit(USBHMassStorageLUNDriver *lunp) {
osalDbgCheck(lunp != NULL); osalDbgCheck(lunp != NULL);
osalMutexLock(&lunp->mtx); chSemWait(&lunp->sem);
lunp->msdp = NULL; lunp->msdp = NULL;
lunp->next = NULL; lunp->next = NULL;
memset(&lunp->info, 0, sizeof(lunp->info)); memset(&lunp->info, 0, sizeof(lunp->info));
lunp->state = BLK_STOP; lunp->state = BLK_STOP;
osalMutexUnlock(&lunp->mtx); chSemSignal(&lunp->sem);
} }
static void _lun_object_init(USBHMassStorageLUNDriver *lunp) { static void _lun_object_init(USBHMassStorageLUNDriver *lunp) {
@ -708,8 +708,7 @@ static void _lun_object_init(USBHMassStorageLUNDriver *lunp) {
memset(lunp, 0, sizeof(*lunp)); memset(lunp, 0, sizeof(*lunp));
lunp->vmt = &blk_vmt; lunp->vmt = &blk_vmt;
lunp->state = BLK_STOP; lunp->state = BLK_STOP;
osalMutexObjectInit(&lunp->mtx); chSemObjectInit(&lunp->sem, 1);
/* Unnecessary because of the memset: /* Unnecessary because of the memset:
lunp->msdp = NULL; lunp->msdp = NULL;
lunp->next = NULL; lunp->next = NULL;
@ -717,42 +716,20 @@ static void _lun_object_init(USBHMassStorageLUNDriver *lunp) {
*/ */
} }
/*
void usbhmsdLUNStart(USBHMassStorageLUNDriver *lunp) {
osalDbgCheck(lunp != NULL);
osalSysLock();
osalDbgAssert((lunp->state == BLK_STOP) || (lunp->state == BLK_ACTIVE),
"invalid state");
//TODO: complete
//lunp->state = BLK_ACTIVE;
osalSysUnlock();
}
void usbhmsdLUNStop(USBHMassStorageLUNDriver *lunp) {
osalDbgCheck(lunp != NULL);
osalSysLock();
osalDbgAssert((lunp->state == BLK_STOP) || (lunp->state == BLK_ACTIVE),
"invalid state");
//TODO: complete
//lunp->state = BLK_STOP;
osalSysUnlock();
}
*/
bool usbhmsdLUNConnect(USBHMassStorageLUNDriver *lunp) { bool usbhmsdLUNConnect(USBHMassStorageLUNDriver *lunp) {
osalDbgCheck(lunp != NULL); osalDbgCheck(lunp != NULL);
osalDbgCheck(lunp->msdp != NULL); osalDbgCheck(lunp->msdp != NULL);
msd_result_t res; msd_result_t res;
osalMutexLock(&lunp->mtx); osalSysLock();
if (lunp->state == BLK_READY) { if (lunp->state == BLK_READY) {
osalMutexUnlock(&lunp->mtx); osalSysUnlock();
return HAL_SUCCESS; return HAL_SUCCESS;
} else if (lunp->state != BLK_ACTIVE) {
osalMutexUnlock(&lunp->mtx);
return HAL_FAILED;
} }
chSemWaitS(&lunp->sem);
osalDbgAssert((lunp->state == BLK_ACTIVE), "invalid state");
lunp->state = BLK_CONNECTING; lunp->state = BLK_CONNECTING;
osalSysUnlock();
{ {
USBH_DEFINE_BUFFER(scsi_inquiry_response_t inq); USBH_DEFINE_BUFFER(scsi_inquiry_response_t inq);
@ -820,13 +797,13 @@ bool usbhmsdLUNConnect(USBHMassStorageLUNDriver *lunp) {
uinfo("MSD Connected."); uinfo("MSD Connected.");
lunp->state = BLK_READY; lunp->state = BLK_READY;
osalMutexUnlock(&lunp->mtx); chSemSignal(&lunp->sem);
return HAL_SUCCESS; return HAL_SUCCESS;
/* Connection failed, state reset to BLK_ACTIVE.*/ /* Connection failed, state reset to BLK_ACTIVE.*/
failed: failed:
lunp->state = BLK_ACTIVE; lunp->state = BLK_ACTIVE;
osalMutexUnlock(&lunp->mtx); chSemSignal(&lunp->sem);
return HAL_FAILED; return HAL_FAILED;
} }
@ -834,20 +811,20 @@ failed:
bool usbhmsdLUNDisconnect(USBHMassStorageLUNDriver *lunp) { bool usbhmsdLUNDisconnect(USBHMassStorageLUNDriver *lunp) {
osalDbgCheck(lunp != NULL); osalDbgCheck(lunp != NULL);
osalMutexLock(&lunp->mtx); osalSysLock();
osalDbgAssert((lunp->state == BLK_ACTIVE) || (lunp->state == BLK_READY),
"invalid state");
if (lunp->state == BLK_ACTIVE) { if (lunp->state == BLK_ACTIVE) {
osalMutexUnlock(&lunp->mtx); osalSysUnlock();
return HAL_SUCCESS; return HAL_SUCCESS;
} }
chSemWaitS(&lunp->sem);
osalDbgAssert((lunp->state == BLK_READY), "invalid state");
lunp->state = BLK_DISCONNECTING; lunp->state = BLK_DISCONNECTING;
osalSysUnlock();
//TODO: complete //TODO: complete: sync, etc.
lunp->state = BLK_ACTIVE; lunp->state = BLK_ACTIVE;
osalMutexUnlock(&lunp->mtx); chSemSignal(&lunp->sem);
return HAL_SUCCESS; return HAL_SUCCESS;
} }
@ -861,9 +838,9 @@ bool usbhmsdLUNRead(USBHMassStorageLUNDriver *lunp, uint32_t startblk,
msd_result_t res; msd_result_t res;
uint32_t actual_len; uint32_t actual_len;
osalMutexLock(&lunp->mtx); chSemWait(&lunp->sem);
if (lunp->state != BLK_READY) { if (lunp->state != BLK_READY) {
osalMutexUnlock(&lunp->mtx); chSemSignal(&lunp->sem);
return ret; return ret;
} }
lunp->state = BLK_READING; lunp->state = BLK_READING;
@ -893,7 +870,7 @@ bool usbhmsdLUNRead(USBHMassStorageLUNDriver *lunp, uint32_t startblk,
exit: exit:
lunp->state = BLK_READY; lunp->state = BLK_READY;
osalMutexUnlock(&lunp->mtx); chSemSignal(&lunp->sem);
return ret; return ret;
} }
@ -906,9 +883,9 @@ bool usbhmsdLUNWrite(USBHMassStorageLUNDriver *lunp, uint32_t startblk,
msd_result_t res; msd_result_t res;
uint32_t actual_len; uint32_t actual_len;
osalMutexLock(&lunp->mtx); chSemWait(&lunp->sem);
if (lunp->state != BLK_READY) { if (lunp->state != BLK_READY) {
osalMutexUnlock(&lunp->mtx); chSemSignal(&lunp->sem);
return ret; return ret;
} }
lunp->state = BLK_WRITING; lunp->state = BLK_WRITING;
@ -938,7 +915,7 @@ bool usbhmsdLUNWrite(USBHMassStorageLUNDriver *lunp, uint32_t startblk,
exit: exit:
lunp->state = BLK_READY; lunp->state = BLK_READY;
osalMutexUnlock(&lunp->mtx); chSemSignal(&lunp->sem);
return ret; return ret;
} }
@ -952,8 +929,15 @@ bool usbhmsdLUNSync(USBHMassStorageLUNDriver *lunp) {
bool usbhmsdLUNGetInfo(USBHMassStorageLUNDriver *lunp, BlockDeviceInfo *bdip) { bool usbhmsdLUNGetInfo(USBHMassStorageLUNDriver *lunp, BlockDeviceInfo *bdip) {
osalDbgCheck(lunp != NULL); osalDbgCheck(lunp != NULL);
osalDbgCheck(bdip != NULL); osalDbgCheck(bdip != NULL);
*bdip = lunp->info;
return HAL_SUCCESS; osalSysLock();
if (lunp->state >= BLK_READY) {
*bdip = lunp->info;
osalSysUnlock();
return HAL_SUCCESS;
}
osalSysUnlock();
return HAL_FAILED;
} }
bool usbhmsdLUNIsInserted(USBHMassStorageLUNDriver *lunp) { bool usbhmsdLUNIsInserted(USBHMassStorageLUNDriver *lunp) {