/**
|
/**
|
* @file
|
* @file
|
* @copyright Copyright 2016 GNSS Sensor Ltd. All right reserved.
|
* @copyright Copyright 2016 GNSS Sensor Ltd. All right reserved.
|
* @author Sergey Khabarov - sergeykhbr@gmail.com
|
* @author Sergey Khabarov - sergeykhbr@gmail.com
|
* @brief Asynchronous queue with time markers.
|
* @brief Asynchronous queue with time markers.
|
*/
|
*/
|
|
|
#include "api_utils.h"
|
#include "api_utils.h"
|
#include "async_tqueue.h"
|
#include "async_tqueue.h"
|
|
|
namespace debugger {
|
namespace debugger {
|
|
|
AsyncTQueueType::AsyncTQueueType() {
|
AsyncTQueueType::AsyncTQueueType() {
|
preLen_ = 0;
|
preLen_ = 0;
|
curLen_ = 0;
|
curLen_ = 0;
|
stepPreQueued_.make_list(0);
|
stepPreQueued_.make_list(0);
|
stepQueue_.make_list(16); /** it will be auto reallocated if needed */
|
stepQueue_.make_list(16); /** it will be auto reallocated if needed */
|
item_.make_list(Queue_Total);
|
item_.make_list(Queue_Total);
|
RISCV_mutex_init(&mutex_);
|
RISCV_mutex_init(&mutex_);
|
}
|
}
|
|
|
AsyncTQueueType::~AsyncTQueueType() {
|
AsyncTQueueType::~AsyncTQueueType() {
|
RISCV_mutex_destroy(&mutex_);
|
RISCV_mutex_destroy(&mutex_);
|
}
|
}
|
|
|
void AsyncTQueueType::put(uint64_t time, IFace *cb) {
|
void AsyncTQueueType::put(uint64_t time, IFace *cb) {
|
RISCV_mutex_lock(&mutex_);
|
RISCV_mutex_lock(&mutex_);
|
item_[Queue_Time].make_uint64(time);
|
item_[Queue_Time].make_uint64(time);
|
item_[Queue_IFace].make_iface(cb);
|
item_[Queue_IFace].make_iface(cb);
|
if (preLen_ == stepPreQueued_.size()) {
|
if (preLen_ == stepPreQueued_.size()) {
|
unsigned new_sz = 2 * stepPreQueued_.size();
|
unsigned new_sz = 2 * stepPreQueued_.size();
|
if (new_sz == 0) {
|
if (new_sz == 0) {
|
new_sz = 1;
|
new_sz = 1;
|
}
|
}
|
stepPreQueued_.realloc_list(new_sz);
|
stepPreQueued_.realloc_list(new_sz);
|
}
|
}
|
stepPreQueued_[preLen_].attr_free();
|
stepPreQueued_[preLen_].attr_free();
|
stepPreQueued_[preLen_] = item_;
|
stepPreQueued_[preLen_] = item_;
|
preLen_++;
|
preLen_++;
|
RISCV_mutex_unlock(&mutex_);
|
RISCV_mutex_unlock(&mutex_);
|
}
|
}
|
void AsyncTQueueType::pushPreQueued() {
|
void AsyncTQueueType::pushPreQueued() {
|
if (preLen_ == 0) {
|
if (preLen_ == 0) {
|
return;
|
return;
|
}
|
}
|
RISCV_mutex_lock(&mutex_);
|
RISCV_mutex_lock(&mutex_);
|
for (unsigned i = 0; i < preLen_; i++) {
|
for (unsigned i = 0; i < preLen_; i++) {
|
if (curLen_ < stepQueue_.size()) {
|
if (curLen_ < stepQueue_.size()) {
|
stepQueue_[curLen_].attr_free();
|
stepQueue_[curLen_].attr_free();
|
stepQueue_[curLen_] = stepPreQueued_[i];
|
stepQueue_[curLen_] = stepPreQueued_[i];
|
} else {
|
} else {
|
stepQueue_.add_to_list(&stepPreQueued_[i]);
|
stepQueue_.add_to_list(&stepPreQueued_[i]);
|
}
|
}
|
curLen_++;
|
curLen_++;
|
}
|
}
|
preLen_= 0;
|
preLen_= 0;
|
RISCV_mutex_unlock(&mutex_);
|
RISCV_mutex_unlock(&mutex_);
|
}
|
}
|
|
|
void AsyncTQueueType::initProc() {
|
void AsyncTQueueType::initProc() {
|
curIdx_ = 0;
|
curIdx_ = 0;
|
}
|
}
|
|
|
IFace *AsyncTQueueType::getNext(uint64_t step_cnt) {
|
IFace *AsyncTQueueType::getNext(uint64_t step_cnt) {
|
IFace *ret = 0;
|
IFace *ret = 0;
|
if (curIdx_ >= curLen_) {
|
if (curIdx_ >= curLen_) {
|
return ret;
|
return ret;
|
}
|
}
|
for (unsigned i = curIdx_; i < curLen_; i++) {
|
for (unsigned i = curIdx_; i < curLen_; i++) {
|
uint64_t ev_time = stepQueue_[i][Queue_Time].to_uint64();
|
uint64_t ev_time = stepQueue_[i][Queue_Time].to_uint64();
|
|
|
if (step_cnt < ev_time) {
|
if (step_cnt < ev_time) {
|
continue;
|
continue;
|
}
|
}
|
ret = stepQueue_[i][Queue_IFace].to_iface();
|
ret = stepQueue_[i][Queue_IFace].to_iface();
|
|
|
// remove item from list using swap function to avoid usage
|
// remove item from list using swap function to avoid usage
|
// of allocation/deallocation calls.
|
// of allocation/deallocation calls.
|
stepQueue_.swap_list_item(i, curLen_ - 1);
|
stepQueue_.swap_list_item(i, curLen_ - 1);
|
curLen_--;
|
curLen_--;
|
curIdx_--;
|
curIdx_--;
|
break;
|
break;
|
}
|
}
|
return ret;
|
return ret;
|
}
|
}
|
|
|
} // namespace debugger
|
} // namespace debugger
|
|
|
|
|