optimize ...

This commit is contained in:
zhengshuxin 2022-11-14 05:43:55 -05:00
parent eadd92e9e3
commit 14f0900b73
7 changed files with 59 additions and 4 deletions

View File

@ -34,6 +34,16 @@ struct MBOX {
pthread_mutex_t *lock;
};
socket_t mbox_in(MBOX *mbox)
{
return mbox->in;
}
socket_t mbox_out(MBOX *mbox)
{
return mbox->out;
}
MBOX *mbox_create(unsigned type)
{
MBOX *mbox;

View File

@ -52,4 +52,7 @@ size_t mbox_nsend(MBOX *mbox);
*/
size_t mbox_nread(MBOX *mbox);
socket_t mbox_in(MBOX *mbox);
socket_t mbox_out(MBOX *mbox);
#endif

View File

@ -126,6 +126,7 @@ struct FILE_EVENT {
#define TYPE_FILE (unsigned) (1 << 1)
#define TYPE_BADFD (unsigned) (1 << 2)
#define TYPE_EVENTABLE (unsigned) (1 << 3)
#define TYPE_INTERNAL (unsigned) (1 << 4)
unsigned oper;
#define EVENT_ADD_READ (unsigned) (1 << 0)

View File

@ -422,9 +422,15 @@ int fiber_wait_read(FILE_EVENT *fe)
fe->fiber_r->status = FIBER_STATUS_WAIT_READ;
SET_READWAIT(fe);
__thread_fiber->event->waiter++;
if (!(fe->type & TYPE_INTERNAL)) {
__thread_fiber->event->waiter++;
}
acl_fiber_switch();
__thread_fiber->event->waiter--;
if (!(fe->type & TYPE_INTERNAL)) {
__thread_fiber->event->waiter--;
}
return ret;
}

View File

@ -17,10 +17,27 @@ struct SYNC_TIMER {
static SYNC_TIMER *sync_timer_new(void)
{
SYNC_TIMER *timer = (SYNC_TIMER*) mem_calloc(1, sizeof(SYNC_TIMER));
socket_t in, out;
FILE_EVENT *fe;
pthread_mutex_init(&timer->lock, NULL);
timer->box = mbox_create(MBOX_T_MPSC);
timer->waiters = timer_cache_create();
out = mbox_out(timer->box);
assert(out != INVALID_SOCKET);
fe = fiber_file_open_write(out);
assert(fe);
fe->type |= TYPE_INTERNAL;
in = mbox_out(timer->box);
assert(in != INVALID_SOCKET);
if (in != out) {
fe = fiber_file_open_read(in);
assert(fe);
fe->type |= TYPE_INTERNAL;
}
return timer;
}

View File

@ -16,9 +16,26 @@ struct SYNC_WAITER {
static SYNC_WAITER *sync_waiter_new(void)
{
SYNC_WAITER *waiter = (SYNC_WAITER*) mem_calloc(1, sizeof(SYNC_WAITER));
socket_t in, out;
FILE_EVENT *fe;
pthread_mutex_init(&waiter->lock, NULL);
waiter->box = mbox_create(MBOX_T_MPSC);
out = mbox_out(waiter->box);
assert(out != INVALID_SOCKET);
fe = fiber_file_open_write(out);
assert(fe);
fe->type |= TYPE_INTERNAL;
in = mbox_out(waiter->box);
assert(in != INVALID_SOCKET);
if (in != out) {
fe = fiber_file_open_read(in);
assert(fe);
fe->type |= TYPE_INTERNAL;
}
return waiter;
}

View File

@ -20,6 +20,7 @@ static int __all_consumers_exit = 0;
static void fiber_producer(ACL_FIBER *fiber acl_unused, void *ctx acl_unused)
{
return;
int n = 0;
while (!__all_consumers_exit) {
acl_fiber_cond_signal(__cond);
@ -32,7 +33,7 @@ static void fiber_producer(ACL_FIBER *fiber acl_unused, void *ctx acl_unused)
if (--__nfibers == 0) {
printf("thread-%lu, all producers over!\r\n", pthread_self());
acl_fiber_schedule_stop();
//acl_fiber_schedule_stop();
}
}
@ -88,7 +89,7 @@ static void fiber_consumer(ACL_FIBER *fiber acl_unused, void *ctx acl_unused)
if (--__nfibers == 0) {
printf("---thread-%lu, all consumers over, count=%d!\r\n",
pthread_self(), __count);
acl_fiber_schedule_stop();
//acl_fiber_schedule_stop();
}
}