2019-07-28 10:31:56 +08:00
|
|
|
|
#pragma once
|
2018-11-30 17:20:45 +08:00
|
|
|
|
#include "fiber_cpp_define.hpp"
|
|
|
|
|
#include <list>
|
2018-12-25 11:32:42 +08:00
|
|
|
|
#include <stdlib.h>
|
2018-11-30 17:20:45 +08:00
|
|
|
|
#include "fiber_event.hpp"
|
|
|
|
|
#include "fiber_cond.hpp"
|
|
|
|
|
|
2020-06-13 14:33:08 +08:00
|
|
|
|
namespace acl {
|
2018-11-30 17:20:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
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><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* <EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>
|
2018-11-30 17:20:45 +08:00
|
|
|
|
*
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-11-30 17:20:45 +08:00
|
|
|
|
*
|
|
|
|
|
* class myobj
|
|
|
|
|
* {
|
|
|
|
|
* public:
|
|
|
|
|
* myobj(void) {}
|
|
|
|
|
* ~myobj(void) {}
|
|
|
|
|
*
|
|
|
|
|
* void test(void) { printf("hello world\r\n"); }
|
|
|
|
|
* };
|
|
|
|
|
*
|
|
|
|
|
* acl::fiber_tbox<myobj> fiber_tbox;
|
|
|
|
|
*
|
|
|
|
|
* void thread_producer(void)
|
|
|
|
|
* {
|
|
|
|
|
* myobj* o = new myobj;
|
|
|
|
|
* fiber_tbox.push(o);
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* void thread_consumer(void)
|
|
|
|
|
* {
|
|
|
|
|
* myobj* o = fiber_tbox.pop();
|
|
|
|
|
* o->test();
|
|
|
|
|
* delete o;
|
|
|
|
|
* }
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2020-06-24 16:46:05 +08:00
|
|
|
|
class fiber_tbox
|
2018-11-30 17:20:45 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>췽<EFBFBD><EFBFBD>
|
|
|
|
|
* @param free_obj {bool} <EFBFBD><EFBFBD> fiber_tbox <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>
|
2018-11-30 17:20:45 +08:00
|
|
|
|
*/
|
2019-01-14 14:34:14 +08:00
|
|
|
|
fiber_tbox(bool free_obj = true) : size_(0), free_obj_(free_obj) {}
|
2018-11-30 17:20:45 +08:00
|
|
|
|
|
|
|
|
|
~fiber_tbox(void)
|
|
|
|
|
{
|
|
|
|
|
clear(free_obj_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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>
|
2018-11-30 17:20:45 +08:00
|
|
|
|
*/
|
|
|
|
|
void clear(bool free_obj = false)
|
|
|
|
|
{
|
|
|
|
|
if (free_obj) {
|
|
|
|
|
for (typename std::list<T*>::iterator it =
|
|
|
|
|
tbox_.begin(); it != tbox_.end(); ++it) {
|
|
|
|
|
|
|
|
|
|
delete *it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tbox_.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <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><EFBFBD><EFBFBD><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><EFBF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_tbox
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-12-25 11:32:42 +08:00
|
|
|
|
* @return {bool}
|
2018-11-30 17:20:45 +08:00
|
|
|
|
*/
|
2018-12-25 11:32:42 +08:00
|
|
|
|
bool push(T* t, bool notify_first = true)
|
2018-11-30 17:20:45 +08:00
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20>ȼ<EFBFBD><C8BC><EFBFBD>
|
2018-12-25 11:32:42 +08:00
|
|
|
|
if (event_.wait() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
2018-12-06 15:50:13 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
2018-11-30 17:20:45 +08:00
|
|
|
|
tbox_.push_back(t);
|
|
|
|
|
size_++;
|
2018-12-06 15:50:13 +08:00
|
|
|
|
|
|
|
|
|
if (notify_first) {
|
2018-12-25 11:32:42 +08:00
|
|
|
|
if (cond_.notify() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
if (event_.notify() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2018-12-06 15:50:13 +08:00
|
|
|
|
} else {
|
2018-12-25 11:32:42 +08:00
|
|
|
|
if (event_.notify() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
if (cond_.notify() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2018-12-06 15:50:13 +08:00
|
|
|
|
}
|
2018-11-30 17:20:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-28 10:31:56 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param wait_ms {int} >= 0 ʱ<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><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param found {bool*} <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><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿն<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|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ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><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>
|
2018-11-30 17:20:45 +08:00
|
|
|
|
*/
|
|
|
|
|
T* pop(int wait_ms = -1, bool* found = NULL)
|
|
|
|
|
{
|
|
|
|
|
bool found_flag;
|
2018-12-25 11:32:42 +08:00
|
|
|
|
if (event_.wait() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
2018-11-30 17:20:45 +08:00
|
|
|
|
while (true) {
|
|
|
|
|
T* t = peek(found_flag);
|
|
|
|
|
if (found_flag) {
|
2018-12-25 11:32:42 +08:00
|
|
|
|
if (event_.notify() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
2018-11-30 17:20:45 +08:00
|
|
|
|
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
|
2018-11-30 17:20:45 +08:00
|
|
|
|
if (!cond_.wait(event_, wait_ms) && wait_ms >= 0) {
|
2018-12-25 11:32:42 +08:00
|
|
|
|
if (event_.notify() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
2018-11-30 17:20:45 +08:00
|
|
|
|
if (found) {
|
|
|
|
|
*found = false;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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>
|
2018-11-30 17:20:45 +08:00
|
|
|
|
* @return {size_t}
|
|
|
|
|
*/
|
|
|
|
|
size_t size(void) const
|
|
|
|
|
{
|
|
|
|
|
return size_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
void lock(void)
|
|
|
|
|
{
|
2018-12-25 11:32:42 +08:00
|
|
|
|
if (event_.wait() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
2018-11-30 17:20:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void unlock(void)
|
|
|
|
|
{
|
2018-12-25 11:32:42 +08:00
|
|
|
|
if (event_.notify() == false) {
|
|
|
|
|
abort();
|
|
|
|
|
}
|
2018-11-30 17:20:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
fiber_tbox(const fiber_tbox&) {}
|
|
|
|
|
const fiber_tbox& operator=(const fiber_tbox&);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::list<T*> tbox_;
|
|
|
|
|
size_t size_;
|
|
|
|
|
bool free_obj_;
|
|
|
|
|
fiber_event event_;
|
|
|
|
|
fiber_cond cond_;
|
|
|
|
|
|
|
|
|
|
T* peek(bool& found_flag)
|
|
|
|
|
{
|
|
|
|
|
typename std::list<T*>::iterator it = tbox_.begin();
|
|
|
|
|
if (it == tbox_.end()) {
|
|
|
|
|
found_flag = false;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
found_flag = true;
|
|
|
|
|
size_--;
|
|
|
|
|
T* t = *it;
|
|
|
|
|
tbox_.erase(it);
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|
2018-12-12 19:12:42 +08:00
|
|
|
|
|