Merge branch 'gitee-master' into gitlab-upstream

This commit is contained in:
zhengshuxin 2023-06-12 11:36:45 +08:00
commit 9fe4083187
19 changed files with 278 additions and 162 deletions

View File

@ -77,10 +77,17 @@ FIBER_API int acl_fiber_sem_post(ACL_FIBER_SEM* sem);
/**
* Get the specificed semaphore's value
* @param sem {ACL_FIBER_SEM*} created by acl_fiber_sem_create
* @retur {int} current semaphore's value returned
* @return {int} current semaphore's value returned
*/
FIBER_API int acl_fiber_sem_num(ACL_FIBER_SEM* sem);
/**
* Get the number of the waiters for the semaphore.
* @param sem {ACL_FIBER_SEM*} created by acl_fiber_sem_create
* @return {int} the waiters' number.
*/
FIBER_API int acl_fiber_sem_waiters_num(ACL_FIBER_SEM *sem);
#ifdef __cplusplus
}
#endif

View File

@ -53,6 +53,11 @@ int acl_fiber_sem_num(ACL_FIBER_SEM *sem)
return sem->num;
}
int acl_fiber_sem_waiters_num(ACL_FIBER_SEM *sem)
{
return ring_size(&sem->waiting);
}
int acl_fiber_sem_wait(ACL_FIBER_SEM *sem)
{
ACL_FIBER *curr;

View File

@ -12,32 +12,26 @@ int channel_send(ACL_CHANNEL *c, void *v);
int channel_recv(ACL_CHANNEL *c, void *v);
template <typename T>
class channel
{
class channel {
public:
channel(void)
{
channel(void) {
chan_ = channel_create(sizeof(T), 100);
}
~channel(void)
{
~channel(void) {
channel_free(chan_);
}
channel& operator << (T& t)
{
channel& operator << (T& t) {
return put(t);
}
channel& put(T& t)
{
channel& put(T& t) {
channel_send(chan_, &t);
return *this;
}
void pop(T& t)
{
void pop(T& t) {
channel_recv(chan_, &t);
}

View File

@ -8,8 +8,7 @@ struct ACL_FIBER;
namespace acl {
typedef enum
{
typedef enum {
FIBER_EVENT_T_KERNEL, // Linux: epoll, FreeBSD: kquque, Windows: iocp
FIBER_EVENT_T_POLL, // Linux, FreeBSD, MacOS, Windows
FIBER_EVENT_T_SELECT, // Linux, FreeBSD, MacOS, Windows
@ -17,8 +16,7 @@ typedef enum
FIBER_EVENT_T_IO_URING, // Linux
} fiber_event_t;
struct FIBER_CPP_API fiber_frame
{
struct FIBER_CPP_API fiber_frame {
fiber_frame(void) : pc(0), off(0) {}
~fiber_frame(void) {}
@ -30,8 +28,7 @@ struct FIBER_CPP_API fiber_frame
/**
*
*/
class FIBER_CPP_API fiber
{
class FIBER_CPP_API fiber {
public:
fiber(void);
fiber(ACL_FIBER *fb);
@ -268,8 +265,9 @@ public:
* @param ctx {void*}
* @param size {size_t}
* @param share_stack {bool}
* @return {ACL_FIBER*}
*/
static void fiber_create(void (*fn)(ACL_FIBER*, void*),
static ACL_FIBER* fiber_create(void (*fn)(ACL_FIBER*, void*),
void* ctx, size_t size, bool share_stack = false);
/**
@ -308,8 +306,7 @@ private:
/**
*
*/
class FIBER_CPP_API fiber_timer
{
class FIBER_CPP_API fiber_timer {
public:
fiber_timer(void);
virtual ~fiber_timer(void) {}
@ -342,8 +339,7 @@ private:
*
*/
template <typename T>
class fiber_trigger : public fiber
{
class fiber_trigger : public fiber {
public:
fiber_trigger(timer_trigger<T>& timer)
: delay_(100)
@ -354,37 +350,35 @@ public:
virtual ~fiber_trigger(void) {}
void add(T* o)
{
void add(T* o) {
mbox_.push(o);
}
void del(T* o)
{
void del(T* o) {
timer_.del(o);
}
timer_trigger<T>& get_trigger(void)
{
timer_trigger<T>& get_trigger(void) {
return timer_;
}
// @override
void run(void)
{
void run(void) {
while (!stop_) {
T* o = mbox_.pop(delay_);
if (o)
if (o) {
timer_.add(o);
}
long long next = timer_.trigger();
long long curr = get_curr_stamp();
if (next == -1)
if (next == -1) {
delay_ = 100;
else {
} else {
delay_ = next - curr;
if (delay_ <= 0)
if (delay_ <= 0) {
delay_ = 1;
}
}
}
}

View File

@ -10,8 +10,7 @@ class fiber_mutex;
/**
* 线线
*/
class FIBER_CPP_API fiber_cond
{
class FIBER_CPP_API fiber_cond {
public:
fiber_cond(void);
~fiber_cond(void);
@ -36,8 +35,7 @@ public:
* C
* @return {ACL_FIBER_COND*}
*/
ACL_FIBER_COND* get_cond(void) const
{
ACL_FIBER_COND* get_cond(void) const {
return cond_;
}
@ -49,4 +47,3 @@ private:
};
}

View File

@ -9,8 +9,7 @@ namespace acl {
* 线线/
*
*/
class FIBER_CPP_API fiber_event
{
class FIBER_CPP_API fiber_event {
public:
/**
*
@ -49,8 +48,7 @@ public:
* C
* @return {ACL_FIBER_EVENT*}
*/
ACL_FIBER_EVENT* get_event(void) const
{
ACL_FIBER_EVENT* get_event(void) const {
return event_;
}

View File

@ -9,8 +9,7 @@ namespace acl {
/**
* 线
*/
class FIBER_CPP_API fiber_lock
{
class FIBER_CPP_API fiber_lock {
public:
fiber_lock(void);
~fiber_lock(void);
@ -43,8 +42,7 @@ private:
/**
* 线
*/
class FIBER_CPP_API fiber_rwlock
{
class FIBER_CPP_API fiber_rwlock {
public:
fiber_rwlock(void);
~fiber_rwlock(void);

View File

@ -11,8 +11,7 @@ namespace acl {
* 线线,
* 线线.
*/
class FIBER_CPP_API fiber_mutex
{
class FIBER_CPP_API fiber_mutex {
public:
/**
*
@ -46,8 +45,7 @@ public:
* C
* @return {ACL_FIBER_MUTEX*}
*/
ACL_FIBER_MUTEX* get_mutex(void) const
{
ACL_FIBER_MUTEX* get_mutex(void) const {
return mutex_;
}
@ -71,8 +69,7 @@ private:
void operator=(const fiber_mutex&);
};
class FIBER_CPP_API fiber_mutex_guard
{
class FIBER_CPP_API fiber_mutex_guard {
public:
fiber_mutex_guard(fiber_mutex& mutex) : mutex_(mutex) {
mutex_.lock();

View File

@ -6,8 +6,7 @@ namespace acl {
class fiber;
struct FIBER_CPP_API fiber_mutex_stat
{
struct FIBER_CPP_API fiber_mutex_stat {
fiber_mutex_stat(void) : fb(NULL), waiting(NULL) {}
~fiber_mutex_stat(void) {}
@ -16,8 +15,7 @@ struct FIBER_CPP_API fiber_mutex_stat
std::vector<ACL_FIBER_MUTEX*> holding;
};
struct FIBER_CPP_API fiber_mutex_stats
{
struct FIBER_CPP_API fiber_mutex_stats {
fiber_mutex_stats(void) {}
~fiber_mutex_stats(void);

View File

@ -12,8 +12,7 @@ typedef enum {
fiber_sem_t_async = (1 << 0),
} fiber_sem_attr_t;
class FIBER_CPP_API fiber_sem
{
class FIBER_CPP_API fiber_sem {
public:
fiber_sem(int max, fiber_sem_attr_t attr = fiber_sem_t_def);
~fiber_sem(void);
@ -22,22 +21,21 @@ public:
int trywait(void);
int post(void);
size_t num(void) const;
private:
ACL_FIBER_SEM* sem_;
fiber_sem(const fiber_sem&);
const fiber_sem& operator=(const fiber_sem&);
};
class FIBER_CPP_API fiber_sem_guard
{
class FIBER_CPP_API fiber_sem_guard {
public:
fiber_sem_guard(fiber_sem& sem) : sem_(sem)
{
fiber_sem_guard(fiber_sem& sem) : sem_(sem) {
(void) sem_.wait();
}
~fiber_sem_guard(void)
{
~fiber_sem_guard(void) {
sem_.post();
}
@ -49,38 +47,29 @@ private:
};
template<typename T>
class fiber_sbox
{
class fiber_sbox {
public:
fiber_sbox(bool free_obj = true)
: sem_(0), size_(0), free_obj_(free_obj) {}
fiber_sbox(bool free_obj = true, bool async = true)
: sem_(0, async ? fiber_sem_t_async : fiber_sem_t_def)
, free_obj_(free_obj) {}
~fiber_sbox(void) { clear(free_obj_); }
void clear(bool free_obj = false)
{
if (free_obj) {
for (typename std::list<T*>::iterator it =
sbox_.begin(); it != sbox_.end(); ++it) {
delete *it;
}
}
sbox_.clear();
}
void push(T* t)
{
void push(T* t) {
sbox_.push_back(t);
sem_.post();
}
T* pop(bool* found = NULL)
{
sem_.wait();
bool found_flag;
T* t = peek(found_flag);
assert(found_flag);
T* pop(bool* found = NULL) {
if (sem_.wait() < 0) {
if (found) {
*found = false;
}
return NULL;
}
T* t = sbox_.front();
sbox_.pop_front();
if (found) {
*found = true;
}
@ -90,25 +79,56 @@ public:
private:
fiber_sem sem_;
std::list<T*> sbox_;
size_t size_;
bool free_obj_;
fiber_sbox(const fiber_sbox&);
void operator=(const fiber_sbox&);
T* peek(bool& found_flag)
{
typename std::list<T*>::iterator it = sbox_.begin();
if (it == sbox_.end()) {
found_flag = false;
return NULL;
void clear(bool free_obj = false) {
if (free_obj) {
for (typename std::list<T*>::iterator it =
sbox_.begin(); it != sbox_.end(); ++it) {
delete *it;
}
}
found_flag = true;
size_--;
T* t = *it;
sbox_.erase(it);
return t;
sbox_.clear();
}
};
template<typename T>
class fiber_sbox2 {
public:
fiber_sbox2(bool async = true)
: sem_(0, async ? fiber_sem_t_async : fiber_sem_t_def) {}
~fiber_sbox2(void) {}
void push(T t) {
sbox_.push_back(t);
sem_.post();
}
bool pop(T& t) {
if (sem_.wait() < 0) {
return false;
}
t = sbox_.front();
sbox_.pop_front();
return true;
}
size_t size(void) const {
return sem_.num();
}
private:
fiber_sem sem_;
std::list<T> sbox_;
fiber_sbox2(const fiber_sbox2&);
void operator=(const fiber_sbox2&);
};
} // namespace acl

View File

@ -5,12 +5,13 @@
#include "fiber.hpp"
#include "fiber_tbox.hpp"
#if __cplusplus >= 201103L // Support c++11 ?
struct ACL_FIBER;
namespace acl {
class fiber_ctx
{
class fiber_ctx {
public:
fiber_ctx(std::function<void()> fn) {
fn_ = std::move(fn);
@ -29,20 +30,17 @@ public:
#define go_wait_thread acl::go_fiber()<<
#define go_wait go_wait_thread
class go_fiber
{
class go_fiber {
public:
go_fiber(void) {}
go_fiber(size_t stack_size, bool on) : stack_size_(stack_size), stack_share_(on) {}
void operator > (std::function<void()> fn)
{
ACL_FIBER* operator > (std::function<void()> fn) {
fiber_ctx* ctx = new fiber_ctx(fn);
fiber::fiber_create(fiber_main, (void*) ctx, stack_size_, stack_share_);
return fiber::fiber_create(fiber_main, (void*) ctx, stack_size_, stack_share_);
}
void operator < (std::function<void()> fn)
{
void operator < (std::function<void()> fn) {
fiber_tbox<int> box;
go[&] {
@ -52,8 +50,7 @@ public:
(void) box.pop();
}
void operator << (std::function<void()> fn)
{
void operator << (std::function<void()> fn) {
fiber_tbox<int> box;
std::thread thread([&]() {
@ -69,8 +66,7 @@ private:
size_t stack_size_ = 320000;
bool stack_share_ = false;
static void fiber_main(ACL_FIBER*, void* ctx)
{
static void fiber_main(ACL_FIBER*, void* ctx) {
fiber_ctx* fc = (fiber_ctx *) ctx;
std::function<void()> fn = fc->fn_;
delete fc;
@ -81,30 +77,27 @@ private:
} // namespace acl
#endif // __cplusplus >= 201103L
/**
* static void fiber1(void)
* {
* static void fiber1(void) {
* printf("fiber: %d\r\n", acl::fiber::self());
* }
*
* static void fiber2(acl::string& buf)
* {
* static void fiber2(acl::string& buf) {
* printf("in fiber: %d, buf: %s\r\n", acl::fiber::self(), buf.c_str());
* buf = "world";
* }
*
* static void fiber3(const acl::string& buf)
* {
* static void fiber3(const acl::string& buf) {
* printf("in fiber: %d, buf: %s\r\n", acl::fiber::self(), buf.c_str());
* }
*
* static void incr(int& n)
* {
* static void incr(int& n) {
* n++;
* }
*
* static void waiter(void)
* {
* static void waiter(void) {
* int n = 100;
*
* // run in thread and wait for result
@ -118,8 +111,7 @@ private:
* // here: n should be 201
* }
*
* static test(void)
* {
* static test(void) {
* go fiber1;
*
* acl::string buf("hello");

View File

@ -11,8 +11,7 @@ class socket_stream;
/**
*
*/
class FIBER_CPP_API master_fiber : public master_base
{
class FIBER_CPP_API master_fiber : public master_base {
public:
/**
* acl_master
@ -36,9 +35,9 @@ public:
const char* get_conf_path(void) const;
protected:
master_fiber();
master_fiber(void);
virtual ~master_fiber();
virtual ~master_fiber(void);
/**
*

View File

@ -13,8 +13,7 @@ class thread_mutex;
* ping rtt 10ms rtt
*
*/
class tcp_keeper : public thread
{
class tcp_keeper : public thread {
public:
tcp_keeper(void);
~tcp_keeper(void);

View File

@ -308,7 +308,7 @@ void fiber::stdout_open(bool on)
acl_fiber_msg_stdout_enable(on ? 1 : 0);
}
void fiber::fiber_create(void (*fn)(ACL_FIBER*, void*), void* ctx,
ACL_FIBER* fiber::fiber_create(void (*fn)(ACL_FIBER*, void*), void* ctx,
size_t stack_size, bool share_stack /* false */)
{
ACL_FIBER_ATTR attr;
@ -316,7 +316,7 @@ void fiber::fiber_create(void (*fn)(ACL_FIBER*, void*), void* ctx,
acl_fiber_attr_setstacksize(&attr, stack_size);
acl_fiber_attr_setsharestack(&attr, share_stack ? 1 : 0);
acl_fiber_create2(&attr, fn, ctx);
return acl_fiber_create2(&attr, fn, ctx);
}
void fiber::stacktrace(const fiber& fb, std::vector<fiber_frame>& out, size_t max)

View File

@ -34,4 +34,9 @@ int fiber_sem::post(void)
return acl_fiber_sem_post(sem_);
}
size_t fiber_sem::num(void) const
{
return (size_t) acl_fiber_sem_num(sem_);
}
}

View File

@ -48,28 +48,6 @@ private:
using shared_message = std::shared_ptr<message>;
class mybox {
public:
mybox(void) : sem_(0) {}
~mybox(void) {}
void push(shared_message msg) {
msgs_.emplace_back(msg);
sem_.post();
}
shared_message pop(void) {
(void) sem_.wait();
shared_message msg = msgs_.front();
msgs_.pop_front();
return msg;
}
private:
acl::fiber_sem sem_;
std::list<shared_message> msgs_;
};
static void usage(const char* procname) {
printf("usage: %s -h[help]\r\n"
" -s ip:port, default: 127.0.0.1:9001\r\n"
@ -109,7 +87,7 @@ int main(int argc, char* argv[]) {
printf("listen on %s, fiber pool: %d\r\n", addr.c_str(), nfibers);
mybox box;
acl::fiber_sbox2<shared_message> box;
for (int i = 0; i < nfibers; i++) {
go[&box] {
@ -125,7 +103,7 @@ int main(int argc, char* argv[]) {
};
}
std::atomic<long> nusers, nmsgs;
std::atomic<long> nusers(0), nmsgs(0);
go[&nusers, &nmsgs] {
while (true) {
@ -136,7 +114,7 @@ int main(int argc, char* argv[]) {
go[&ss, &box, &nusers, &nmsgs] {
while (true) {
acl::socket_stream* conn = ss.accept();
auto conn = ss.accept();
if (conn == NULL) {
printf("accept error %s\r\n", acl::last_serror());
break;
@ -154,12 +132,13 @@ int main(int argc, char* argv[]) {
break;
}
#if 0
if (client->get_conn().write(buf, ret) != ret) {
break;
} else {
continue;
}
#endif
++nmsgs;
auto msg = std::make_shared<message>(client, nmsgs, buf, ret);
box.push(msg);

View File

@ -0,0 +1,3 @@
include ../Makefile_cpp.in
CFLAGS += -std=c++11
PROG = fiber_sbox

View File

@ -0,0 +1,112 @@
#include "stdafx.h"
#include <memory>
#include <atomic>
class message {
public:
message(std::atomic<long>& nmsgs, int id)
: nmsgs_(nmsgs), id_(id) {}
~message(void) { --nmsgs_; }
int get_id(void) const {
return id_;
}
private:
std::atomic<long>& nmsgs_;
int id_;
};
using shared_message = std::shared_ptr<message>;
static void usage(const char* procname) {
printf("usage: %s -h[help]\r\n"
" -c fiber_pool_count [default: 5] \r\n"
" -n message_count [default: 100]\r\n"
" -S [if post in sync mode, default: async mode]\r\n"
, procname);
}
int main(int argc, char* argv[]) {
bool sync = false;
int ch, nfibers = 5, count = 100;
while ((ch = getopt(argc, argv, "hc:n:S")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 'c':
nfibers = atoi(optarg);
break;
case 'n':
count = atoi(optarg);
break;
case 'S':
sync = true;
break;
default:
break;
}
}
acl::fiber::stdout_open(true);
acl::log::stdout_open(true);
acl::fiber_sbox2<shared_message> box(!sync);
std::vector<ACL_FIBER*> fibers;
std::atomic<int> nfibers_left(nfibers);
for (int i = 0; i < nfibers; i++) {
auto fb = go[&box, &fibers, &nfibers_left] {
while (true) {
shared_message msg;
if (!box.pop(msg)) {
std::cout << "POP end!" << std::endl;
break;
}
int id = msg->get_id();
std::cout << "fiber-" << acl::fiber::self()
<< ", id=" << id << std::endl;
}
std::cout << "fiber-" << acl::fiber::self()
<< " exited!" << std::endl;
--nfibers_left;
};
fibers.push_back(fb);
}
std::atomic<long> nmsgs(0);
go[&nmsgs, &nfibers_left] {
while (nfibers_left > 0) {
std::cout << "message count: " << nmsgs << std::endl;
acl::fiber::delay(500);
}
std::cout << "All consumers exited!" << std::endl;
};
go[&box, &fibers, &nmsgs, nfibers, count] {
for (int i = 0; i < count; i++) {
auto msg = std::make_shared<message>(nmsgs, i);
++nmsgs;
box.push(msg);
if (i > 0 && i % 10 == 0) {
::sleep(1);
}
}
for (auto fb : fibers) {
std::cout << "Begin kill fiber-"
<< acl_fiber_id(fb) << std::endl;
acl_fiber_kill(fb);
}
};
acl::fiber::schedule();
return 0;
}

View File

@ -0,0 +1,19 @@
// stdafx.h : 标准系统包含文件的包含文件,
// 或是常用但不常更改的项目特定的包含文件
//
#pragma once
//#include <iostream>
//#include <tchar.h>
// TODO: 在此处引用程序要求的附加头文件
#include <assert.h>
#include <iostream>
#include "lib_acl.h" // just for getopt on Windows
#include "acl_cpp/lib_acl.hpp"
#include "fiber/libfiber.h"
#include "fiber/libfiber.hpp"
#include "fiber/go_fiber.hpp"