2019-07-28 10:31:56 +08:00
|
|
|
#pragma once
|
2018-11-30 14:38:22 +08:00
|
|
|
#include "fiber_cpp_define.hpp"
|
2019-01-14 14:34:14 +08:00
|
|
|
#include <list>
|
|
|
|
#include <assert.h>
|
2018-11-30 14:38:22 +08:00
|
|
|
|
|
|
|
struct ACL_FIBER_SEM;
|
|
|
|
|
|
|
|
namespace acl {
|
|
|
|
|
2023-05-12 23:25:58 +08:00
|
|
|
typedef enum {
|
2023-06-18 17:36:58 +08:00
|
|
|
fiber_sem_t_sync = 0,
|
2023-05-12 23:25:58 +08:00
|
|
|
fiber_sem_t_async = (1 << 0),
|
|
|
|
} fiber_sem_attr_t;
|
|
|
|
|
2023-06-11 18:34:41 +08:00
|
|
|
class FIBER_CPP_API fiber_sem {
|
2018-11-30 14:38:22 +08:00
|
|
|
public:
|
2023-06-18 17:36:58 +08:00
|
|
|
fiber_sem(int max, fiber_sem_attr_t attr = fiber_sem_t_async);
|
2018-11-30 14:38:22 +08:00
|
|
|
~fiber_sem(void);
|
|
|
|
|
|
|
|
int wait(void);
|
|
|
|
int trywait(void);
|
|
|
|
int post(void);
|
|
|
|
|
2023-06-11 21:35:19 +08:00
|
|
|
size_t num(void) const;
|
|
|
|
|
2018-11-30 14:38:22 +08:00
|
|
|
private:
|
|
|
|
ACL_FIBER_SEM* sem_;
|
|
|
|
fiber_sem(const fiber_sem&);
|
|
|
|
const fiber_sem& operator=(const fiber_sem&);
|
|
|
|
};
|
|
|
|
|
2023-06-11 18:34:41 +08:00
|
|
|
class FIBER_CPP_API fiber_sem_guard {
|
2018-11-30 14:38:22 +08:00
|
|
|
public:
|
2023-06-11 18:34:41 +08:00
|
|
|
fiber_sem_guard(fiber_sem& sem) : sem_(sem) {
|
2018-11-30 14:38:22 +08:00
|
|
|
(void) sem_.wait();
|
|
|
|
}
|
|
|
|
|
2023-06-11 18:34:41 +08:00
|
|
|
~fiber_sem_guard(void) {
|
2018-11-30 14:38:22 +08:00
|
|
|
sem_.post();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
fiber_sem& sem_;
|
2020-06-24 16:46:05 +08:00
|
|
|
|
|
|
|
fiber_sem_guard(const fiber_sem_guard&);
|
|
|
|
void operator=(const fiber_sem_guard&);
|
2018-11-30 14:38:22 +08:00
|
|
|
};
|
|
|
|
|
2019-01-14 14:34:14 +08:00
|
|
|
template<typename T>
|
2023-06-11 18:34:41 +08:00
|
|
|
class fiber_sbox {
|
2019-01-14 14:34:14 +08:00
|
|
|
public:
|
2023-06-12 11:36:02 +08:00
|
|
|
fiber_sbox(bool free_obj = true, bool async = true)
|
2023-06-18 17:36:58 +08:00
|
|
|
: sem_(0, async ? fiber_sem_t_async : fiber_sem_t_sync)
|
2023-06-12 11:36:02 +08:00
|
|
|
, free_obj_(free_obj) {}
|
2019-01-14 14:34:14 +08:00
|
|
|
|
|
|
|
~fiber_sbox(void) { clear(free_obj_); }
|
|
|
|
|
2023-06-11 21:35:19 +08:00
|
|
|
void push(T* t) {
|
|
|
|
sbox_.push_back(t);
|
|
|
|
sem_.post();
|
|
|
|
}
|
|
|
|
|
2023-06-12 11:36:02 +08:00
|
|
|
T* pop(bool* found = NULL) {
|
|
|
|
if (sem_.wait() < 0) {
|
|
|
|
if (found) {
|
|
|
|
*found = false;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-06-11 21:35:19 +08:00
|
|
|
T* t = sbox_.front();
|
|
|
|
sbox_.pop_front();
|
2023-06-12 11:36:02 +08:00
|
|
|
if (found) {
|
|
|
|
*found = true;
|
|
|
|
}
|
2023-06-11 21:35:19 +08:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2023-06-12 18:27:07 +08:00
|
|
|
size_t size(void) const {
|
|
|
|
return sem_.num();
|
|
|
|
}
|
|
|
|
|
2023-06-11 21:35:19 +08:00
|
|
|
private:
|
|
|
|
fiber_sem sem_;
|
|
|
|
std::list<T*> sbox_;
|
|
|
|
bool free_obj_;
|
|
|
|
|
|
|
|
fiber_sbox(const fiber_sbox&);
|
|
|
|
void operator=(const fiber_sbox&);
|
|
|
|
|
2023-06-11 18:34:41 +08:00
|
|
|
void clear(bool free_obj = false) {
|
2019-01-14 14:34:14 +08:00
|
|
|
if (free_obj) {
|
|
|
|
for (typename std::list<T*>::iterator it =
|
|
|
|
sbox_.begin(); it != sbox_.end(); ++it) {
|
|
|
|
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sbox_.clear();
|
|
|
|
}
|
2023-06-11 21:35:19 +08:00
|
|
|
};
|
2019-01-14 14:34:14 +08:00
|
|
|
|
2023-06-11 21:35:19 +08:00
|
|
|
template<typename T>
|
|
|
|
class fiber_sbox2 {
|
|
|
|
public:
|
2023-06-12 11:36:02 +08:00
|
|
|
fiber_sbox2(bool async = true)
|
2023-06-18 17:36:58 +08:00
|
|
|
: sem_(0, async ? fiber_sem_t_async : fiber_sem_t_sync) {}
|
2023-06-12 11:36:02 +08:00
|
|
|
|
2023-06-11 21:35:19 +08:00
|
|
|
~fiber_sbox2(void) {}
|
|
|
|
|
|
|
|
void push(T t) {
|
2019-01-14 14:34:14 +08:00
|
|
|
sbox_.push_back(t);
|
|
|
|
sem_.post();
|
|
|
|
}
|
|
|
|
|
2023-06-12 11:36:02 +08:00
|
|
|
bool pop(T& t) {
|
|
|
|
if (sem_.wait() < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = sbox_.front();
|
2023-06-11 21:35:19 +08:00
|
|
|
sbox_.pop_front();
|
2023-06-12 11:36:02 +08:00
|
|
|
return true;
|
2019-01-14 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
2023-06-11 21:35:19 +08:00
|
|
|
size_t size(void) const {
|
|
|
|
return sem_.num();
|
|
|
|
}
|
2019-01-14 14:34:14 +08:00
|
|
|
|
2023-06-11 21:35:19 +08:00
|
|
|
private:
|
|
|
|
fiber_sem sem_;
|
|
|
|
std::list<T> sbox_;
|
2020-06-24 16:46:05 +08:00
|
|
|
|
2023-06-11 21:35:19 +08:00
|
|
|
fiber_sbox2(const fiber_sbox2&);
|
|
|
|
void operator=(const fiber_sbox2&);
|
2019-01-14 14:34:14 +08:00
|
|
|
};
|
|
|
|
|
2018-11-30 14:38:22 +08:00
|
|
|
} // namespace acl
|