
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
operation/OperationStore.cpp
- Committer:
- vwochnik
- Date:
- 2014-10-29
- Revision:
- 62:86a04c5bda18
- Child:
- 63:010bbbb4732a
File content as of revision 62:86a04c5bda18:
#include "OperationStore.h" #include <stdlib.h> #include <string.h> /*#define OPERATION_PENDING 0 #define OPERATION_EXECUTING 1 #define OPERATION_SUCCESS 2 #define OPERATION_FAILURE 3 #define OPERATION_STORE_SIZE 128*/ OperationStore::OperationStore() : _count(0), _offset(0) { } bool OperationStore::enqueue(Operation& operation) { _mutex.lock(); if ((full()) || (getByIdentifier(operation.identifier) != NULL) || (operation.state != OPERATION_PENDING)) { _mutex.unlock(); return false; } memcpy(get(_count++), &operation, sizeof(OperationStore::Operation)); _mutex.unlock(); return true; } bool OperationStore::takePending(Operation& operation) { size_t n; Operation *tmp; _mutex.lock(); for (n = 0; n < _count; n++) { tmp = get(n); if (tmp->state != OPERATION_PENDING) continue; tmp->state = OPERATION_EXECUTING; memcpy(&operation, tmp, sizeof(OperationStore::Operation)); _mutex.unlock(); return true; } _mutex.unlock(); return false; } bool OperationStore::markAsDone(Operation& operation, bool successful) { Operation *tmp; _mutex.lock(); tmp = getByIdentifier(operation.identifier); if ((tmp == NULL) || (tmp->state != OPERATION_EXECUTING)) { _mutex.unlock(); return false; } tmp->state = (successful) ? OPERATION_SUCCESSFUL : OPERATION_FAILED; _mutex.unlock(); return true; } bool OperationStore::takeDone(Operation& operation) { Operation *tmp; _mutex.lock(); tmp = get(0); if ((tmp == NULL) || ((tmp->state != OPERATION_SUCCESSFUL) && (tmp->state != OPERATION_FAILED))) { _mutex.unlock(); return false; } if (++_offset == OPERATION_STORE_SIZE) _offset = 0; _count--; memcpy(&operation, tmp, sizeof(OperationStore::Operation)); _mutex.unlock(); return true; } bool OperationStore::full() { bool ret; _mutex.lock(); ret = (_count == OPERATION_STORE_SIZE); _mutex.unlock(); return ret; } bool OperationStore::empty() { bool ret; _mutex.lock(); ret = (_count == 0); _mutex.unlock(); return ret; } OperationStore::Operation * OperationStore::get(size_t index) { size_t i; if (_offset + index < OPERATION_STORE_SIZE) i = _offset + index; else i = index - (OPERATION_STORE_SIZE - _offset); return &_store[i]; } OperationStore::Operation * OperationStore::getByIdentifier(long identifier) { size_t n; Operation *tmp; for (n = 0; n < _count; n++) { tmp = get(n); if (tmp->identifier == identifier) return tmp; } return NULL; }