2019-07-28 10:31:56 +08:00
|
|
|
|
#pragma once
|
2019-03-13 10:28:51 +08:00
|
|
|
|
#include "../acl_cpp_define.hpp"
|
|
|
|
|
#include <list>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include "thread_mutex.hpp"
|
|
|
|
|
#include "thread_cond.hpp"
|
|
|
|
|
#include "noncopyable.hpp"
|
|
|
|
|
|
|
|
|
|
namespace acl
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣͨ<EFBFBD>ţ<EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>
|
2019-03-13 10:28:51 +08:00
|
|
|
|
*
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-03-13 10:28:51 +08:00
|
|
|
|
*
|
|
|
|
|
* class myobj
|
|
|
|
|
* {
|
|
|
|
|
* public:
|
|
|
|
|
* myobj(void) {}
|
|
|
|
|
* ~myobj(void) {}
|
|
|
|
|
*
|
|
|
|
|
* void test(void) { printf("hello world\r\n"); }
|
|
|
|
|
* };
|
|
|
|
|
*
|
|
|
|
|
* acl::tbox_array<myobj> tbox;
|
|
|
|
|
*
|
|
|
|
|
* void thread_producer(void)
|
|
|
|
|
* {
|
|
|
|
|
* myobj* o = new myobj;
|
|
|
|
|
* tbox.push(o);
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* void thread_consumer(void)
|
|
|
|
|
* {
|
|
|
|
|
* myobj* o = tbox.pop();
|
|
|
|
|
* o->test();
|
|
|
|
|
* delete o;
|
|
|
|
|
* }
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
class tbox_array : public noncopyable
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>췽<EFBFBD><EFBFBD>
|
|
|
|
|
* @param free_obj {bool} <EFBFBD><EFBFBD> tbox_array <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>鲢<EFBFBD>ͷ<EFBFBD>
|
|
|
|
|
* δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѵĶ<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-03-13 10:28:51 +08:00
|
|
|
|
*/
|
|
|
|
|
tbox_array(bool free_obj = true)
|
|
|
|
|
: capacity_(10000)
|
|
|
|
|
, off_curr_(0)
|
|
|
|
|
, off_next_(0)
|
|
|
|
|
, waiters_(0)
|
|
|
|
|
, free_obj_(free_obj)
|
|
|
|
|
, cond_(&lock_)
|
|
|
|
|
{
|
|
|
|
|
array_ = (T**) malloc(sizeof(T*) * capacity_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~tbox_array(void)
|
|
|
|
|
{
|
|
|
|
|
clear(free_obj_);
|
|
|
|
|
free(array_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѵ<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param free_obj {bool} <EFBFBD>ͷŵ<EFBFBD><EFBFBD><EFBFBD> delete <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-03-13 10:28:51 +08:00
|
|
|
|
*/
|
|
|
|
|
void clear(bool free_obj = false)
|
|
|
|
|
{
|
|
|
|
|
if (free_obj) {
|
|
|
|
|
for (size_t i = off_curr_; i < off_next_; i++) {
|
|
|
|
|
delete array_[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֪ͨ
|
|
|
|
|
* @param t {T*} <EFBFBD>ǿ<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param notify_first {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ true<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD>֪ͨ<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-03-13 10:28:51 +08:00
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
bool push(T* t, bool notify_first = false)
|
|
|
|
|
{
|
|
|
|
|
if (lock_.lock() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (off_next_ == capacity_) {
|
|
|
|
|
if (off_curr_ >= 10000) {
|
|
|
|
|
#if 1
|
|
|
|
|
size_t n = 0;
|
|
|
|
|
for (size_t i = off_curr_; i < off_next_; i++) {
|
|
|
|
|
array_[n++] = array_[i];
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
memmove(array_, array_ + off_curr_,
|
|
|
|
|
(off_next_ - off_curr_) * sizeof(T*));
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
off_next_ -= off_curr_;
|
|
|
|
|
off_curr_ = 0;
|
|
|
|
|
} else {
|
|
|
|
|
capacity_ += 10000;
|
|
|
|
|
array_ = (T**) realloc(array_, sizeof(T*) * capacity_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
array_[off_next_++] = t;
|
|
|
|
|
|
|
|
|
|
if (notify_first) {
|
|
|
|
|
if (cond_.notify() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
if (lock_.unlock() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (lock_.unlock() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
if (cond_.notify() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
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FBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> wait_ms <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ü<EFBFBD><EFBFBD><EFBFBD> found <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ true <EFBFBD><EFBFBD><EFBFBD><EFBFBD> false <EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD>ж<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-03-13 10:28:51 +08:00
|
|
|
|
*/
|
|
|
|
|
T* pop(int wait_ms = -1, bool* found = NULL)
|
|
|
|
|
{
|
|
|
|
|
long long n = ((long long) wait_ms) * 1000;
|
|
|
|
|
bool found_flag;
|
|
|
|
|
|
|
|
|
|
if (lock_.lock() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
while (true) {
|
|
|
|
|
T* t = peek(found_flag);
|
|
|
|
|
if (found_flag) {
|
|
|
|
|
if (lock_.unlock() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
if (found) {
|
|
|
|
|
*found = found_flag;
|
|
|
|
|
}
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD><F2A3ACB1><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD> wait <20><><EFBFBD>ж<EFBFBD> wait_ms
|
2019-03-13 10:28:51 +08:00
|
|
|
|
waiters_++;
|
|
|
|
|
if (!cond_.wait(n, true) && wait_ms >= 0) {
|
|
|
|
|
waiters_--;
|
|
|
|
|
if (lock_.unlock() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
if (found) {
|
|
|
|
|
*found = false;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
waiters_--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-03-13 10:28:51 +08:00
|
|
|
|
* @return {size_t}
|
|
|
|
|
*/
|
|
|
|
|
size_t size(void) const
|
|
|
|
|
{
|
|
|
|
|
return off_next_ - off_curr_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
void lock(void)
|
|
|
|
|
{
|
|
|
|
|
if (lock_.lock() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void unlock(void)
|
|
|
|
|
{
|
|
|
|
|
if (lock_.unlock() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
T** array_;
|
|
|
|
|
size_t capacity_;
|
|
|
|
|
size_t off_curr_;
|
|
|
|
|
size_t off_next_;
|
|
|
|
|
size_t waiters_;
|
|
|
|
|
bool free_obj_;
|
|
|
|
|
thread_mutex lock_;
|
|
|
|
|
thread_cond cond_;
|
|
|
|
|
|
|
|
|
|
T* peek(bool& found_flag)
|
|
|
|
|
{
|
|
|
|
|
if (off_curr_ == off_next_) {
|
|
|
|
|
found_flag = false;
|
|
|
|
|
if (off_curr_ > 0) {
|
|
|
|
|
off_curr_ = off_next_ = 0;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
found_flag = true;
|
|
|
|
|
T* t = array_[off_curr_++];
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|