[FEAT MERGE] add direct load function
Co-authored-by: Monk-Liu <1152761042@qq.com> Co-authored-by: saltonz <saltonzh@gmail.com> Co-authored-by: yongshige <598633031@qq.com>
This commit is contained in:
11
deps/oblib/src/lib/queue/ob_fixed_queue.h
vendored
11
deps/oblib/src/lib/queue/ob_fixed_queue.h
vendored
@ -33,6 +33,9 @@ public:
|
||||
int init(const int64_t max_num,
|
||||
ObIAllocator *allocator = global_default_allocator,
|
||||
const lib::ObLabel &label = ObModIds::OB_FIXED_QUEUE);
|
||||
int init(const int64_t max_num,
|
||||
ObIAllocator *allocator,
|
||||
const lib::ObMemAttr &attr);
|
||||
void destroy();
|
||||
public:
|
||||
int push(T *ptr);
|
||||
@ -81,9 +84,15 @@ ObFixedQueue<T>::~ObFixedQueue()
|
||||
template <typename T>
|
||||
int ObFixedQueue<T>::init(const int64_t max_num, ObIAllocator *allocator, const lib::ObLabel &label)
|
||||
{
|
||||
int ret = common::OB_SUCCESS;
|
||||
lib::ObMemAttr attr;
|
||||
attr.label_ = label;
|
||||
return init(max_num, allocator, attr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int ObFixedQueue<T>::init(const int64_t max_num, ObIAllocator *allocator, const lib::ObMemAttr &attr)
|
||||
{
|
||||
int ret = common::OB_SUCCESS;
|
||||
if (NULL == allocator || 0 >= max_num) {
|
||||
ret = common::OB_INVALID_ARGUMENT;
|
||||
} else if (is_inited_) {
|
||||
|
||||
95
deps/oblib/src/lib/queue/ob_lighty_queue.cpp
vendored
95
deps/oblib/src/lib/queue/ob_lighty_queue.cpp
vendored
@ -22,7 +22,7 @@ namespace oceanbase
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
void ObLightyQueue::ObLightyCond::signal()
|
||||
void ObLightyCond::signal()
|
||||
{
|
||||
(void)ATOMIC_FAA(&futex_.uval(), 1);
|
||||
if (ATOMIC_LOAD(&n_waiters_) > 0) {
|
||||
@ -30,7 +30,7 @@ void ObLightyQueue::ObLightyCond::signal()
|
||||
}
|
||||
}
|
||||
|
||||
void ObLightyQueue::ObLightyCond::wait(const uint32_t cmp, const int64_t timeout)
|
||||
void ObLightyCond::wait(const uint32_t cmp, const int64_t timeout)
|
||||
{
|
||||
if (timeout > 0) {
|
||||
(void)ATOMIC_FAA(&n_waiters_, 1);
|
||||
@ -188,5 +188,96 @@ void ObLightyQueue::store(uint64_t seq, void* p)
|
||||
}
|
||||
}
|
||||
|
||||
static int64_t get_us() { return ::oceanbase::common::ObTimeUtility::current_time(); }
|
||||
|
||||
int LightyQueue::init(const uint64_t capacity, const lib::ObLabel &label, const uint64_t tenant_id)
|
||||
{
|
||||
ObMemAttr attr(tenant_id, label);
|
||||
return queue_.init(capacity, global_default_allocator, attr);
|
||||
}
|
||||
|
||||
int LightyQueue::push(void *data, const int64_t timeout)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
int64_t abs_timeout = (timeout > 0 ? (get_us() + timeout) : 0);
|
||||
int64_t wait_timeout = 0;
|
||||
while (true) { // WHITESCAN: OB_CUSTOM_ERROR_COVERED
|
||||
uint32_t seq = cond_.get_seq();
|
||||
if (OB_SUCCESS == (ret = queue_.push(data))) {
|
||||
break;
|
||||
} else if (timeout <= 0 || (wait_timeout = abs_timeout - get_us()) <= 0) {
|
||||
ret = OB_TIMEOUT;
|
||||
break;
|
||||
} else {
|
||||
cond_.wait(seq, wait_timeout);
|
||||
}
|
||||
}
|
||||
if (OB_SUCCESS == ret) {
|
||||
cond_.signal();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int LightyQueue::pop(void *&data, const int64_t timeout)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
int64_t abs_timeout = (timeout > 0 ? (get_us() + timeout) : 0);
|
||||
int64_t wait_timeout = 0;
|
||||
while (true) { // WHITESCAN: OB_CUSTOM_ERROR_COVERED
|
||||
uint32_t seq = cond_.get_seq();
|
||||
if (OB_SUCCESS == (ret = queue_.pop(data))) {
|
||||
break;
|
||||
} else if (timeout <= 0 || (wait_timeout = abs_timeout - get_us()) <= 0) {
|
||||
break;
|
||||
} else {
|
||||
cond_.wait(seq, wait_timeout);
|
||||
}
|
||||
}
|
||||
if (OB_SUCCESS == ret) {
|
||||
cond_.signal();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int LightyQueue::multi_pop(void **data, const int64_t data_count, int64_t &avail_count,
|
||||
const int64_t timeout)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
avail_count = 0;
|
||||
if (data_count > 0) {
|
||||
void *curr_data = NULL;
|
||||
int64_t abs_timeout = (timeout > 0 ? (get_us() + timeout) : 0);
|
||||
int64_t wait_timeout = 0;
|
||||
while (true) { // WHITESCAN: OB_CUSTOM_ERROR_COVERED
|
||||
uint32_t seq = cond_.get_seq();
|
||||
if (OB_SUCCESS == (ret = queue_.pop(curr_data))) {
|
||||
data[avail_count++] = curr_data;
|
||||
curr_data = NULL;
|
||||
cond_.signal();
|
||||
if (avail_count >= data_count) {
|
||||
//finish then break
|
||||
break;
|
||||
}
|
||||
} else if (avail_count > 0) {
|
||||
//not finish, but has already got one, break
|
||||
ret = OB_SUCCESS;
|
||||
break;
|
||||
} else if (timeout <= 0 || (wait_timeout = abs_timeout - get_us()) <= 0) {
|
||||
break;
|
||||
} else {
|
||||
cond_.wait(seq, wait_timeout);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void LightyQueue::reset()
|
||||
{
|
||||
void *p = NULL;
|
||||
while (0 == pop(p, 0))
|
||||
;
|
||||
}
|
||||
|
||||
}; // end namespace common
|
||||
}; // end namespace oceanbase
|
||||
|
||||
58
deps/oblib/src/lib/queue/ob_lighty_queue.h
vendored
58
deps/oblib/src/lib/queue/ob_lighty_queue.h
vendored
@ -19,28 +19,30 @@
|
||||
#include "lib/alloc/alloc_struct.h"
|
||||
#include "lib/allocator/ob_mod_define.h"
|
||||
#include "lib/lock/ob_futex.h"
|
||||
#include "lib/queue/ob_fixed_queue.h"
|
||||
|
||||
namespace oceanbase
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
struct ObLightyCond
|
||||
{
|
||||
public:
|
||||
ObLightyCond(): n_waiters_(0) {}
|
||||
~ObLightyCond() {}
|
||||
|
||||
void signal();
|
||||
uint32_t get_seq() { return ATOMIC_LOAD(&futex_.uval()); }
|
||||
void wait(const uint32_t cmp, const int64_t timeout);
|
||||
private:
|
||||
lib::ObFutex futex_;
|
||||
uint32_t n_waiters_;
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(ObLightyCond);
|
||||
};
|
||||
|
||||
class ObLightyQueue
|
||||
{
|
||||
struct ObLightyCond
|
||||
{
|
||||
public:
|
||||
ObLightyCond(): n_waiters_(0) {}
|
||||
~ObLightyCond() {}
|
||||
|
||||
void signal();
|
||||
uint32_t get_seq() { return ATOMIC_LOAD(&futex_.uval()); }
|
||||
void wait(const uint32_t cmp, const int64_t timeout);
|
||||
private:
|
||||
lib::ObFutex futex_;
|
||||
uint32_t n_waiters_;
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(ObLightyCond);
|
||||
};
|
||||
public:
|
||||
typedef ObLightyCond Cond;
|
||||
ObLightyQueue(): capacity_(0), n_cond_(0), data_(NULL), cond_(NULL), push_(0), pop_(0) {}
|
||||
@ -80,6 +82,32 @@ private:
|
||||
uint64_t pop_ CACHE_ALIGNED;
|
||||
};
|
||||
|
||||
class LightyQueue
|
||||
{
|
||||
public:
|
||||
typedef ObLightyCond Cond;
|
||||
LightyQueue() {}
|
||||
~LightyQueue() { destroy(); }
|
||||
public:
|
||||
int init(const uint64_t capacity,
|
||||
const lib::ObLabel &label = ObModIds::OB_LIGHTY_QUEUE,
|
||||
const uint64_t tenant_id = common::OB_SERVER_TENANT_ID);
|
||||
void destroy() { queue_.destroy(); }
|
||||
void reset();
|
||||
int64_t size() const { return queue_.get_total(); }
|
||||
int64_t curr_size() const { return queue_.get_total(); }
|
||||
int64_t max_size() const { return queue_.capacity(); }
|
||||
bool is_inited() const { return queue_.is_inited(); }
|
||||
int push(void *data, const int64_t timeout = 0);
|
||||
int pop(void *&data, const int64_t timeout = 0);
|
||||
int multi_pop(void **data, const int64_t data_count, int64_t &avail_count, const int64_t timeout = 0);
|
||||
private:
|
||||
typedef ObFixedQueue<void> Queue;
|
||||
Queue queue_;
|
||||
Cond cond_;
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(LightyQueue);
|
||||
};
|
||||
}; // end namespace common
|
||||
}; // end namespace oceanbase
|
||||
|
||||
|
||||
Reference in New Issue
Block a user