2019-07-28 10:31:56 +08:00
|
|
|
#include "lib_acl.h"
|
2018-11-30 14:38:22 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2022-06-27 10:26:49 +08:00
|
|
|
#include <sys/select.h>
|
2018-11-30 14:38:22 +08:00
|
|
|
#include "fiber/libfiber.h"
|
|
|
|
|
|
|
|
static int __fibers_count = 2;
|
2022-12-27 16:20:47 +08:00
|
|
|
static int __sleep_count = 10;
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2022-06-27 10:26:49 +08:00
|
|
|
enum {
|
|
|
|
SELECT_SLEEP = 1,
|
|
|
|
POLL_SLEEP = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __sleep_way = 0;
|
|
|
|
|
|
|
|
static void select_sleep(int seconds)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
tv.tv_sec = seconds;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
(void) select(0, NULL, NULL, NULL, &tv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void poll_sleep(int seconds)
|
|
|
|
{
|
|
|
|
int delay = seconds * 1000;
|
|
|
|
(void) poll(NULL, 0, delay);
|
|
|
|
}
|
|
|
|
|
2018-11-30 14:38:22 +08:00
|
|
|
static void sleep_main(ACL_FIBER *fiber, void *ctx)
|
|
|
|
{
|
2022-12-27 16:20:47 +08:00
|
|
|
int *n = (int *) ctx, i;
|
2018-11-30 14:38:22 +08:00
|
|
|
time_t last, now;
|
|
|
|
|
2022-12-27 16:20:47 +08:00
|
|
|
for (i = 0; i < __sleep_count; i++) {
|
2022-06-27 10:26:49 +08:00
|
|
|
switch (__sleep_way) {
|
|
|
|
case SELECT_SLEEP:
|
|
|
|
select_sleep(1);
|
|
|
|
break;
|
|
|
|
case POLL_SLEEP:
|
|
|
|
poll_sleep(1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
acl_fiber_sleep(1);
|
|
|
|
break;
|
|
|
|
}
|
2022-12-27 16:20:47 +08:00
|
|
|
printf("fiber-%d wakeup %d\r\n", acl_fiber_self(), i);
|
2021-11-23 17:40:37 +08:00
|
|
|
}
|
|
|
|
|
2018-11-30 14:38:22 +08:00
|
|
|
printf("fiber-%d: begin sleep %d\r\n", acl_fiber_id(fiber), *n);
|
|
|
|
time(&last);
|
|
|
|
*n = (int) acl_fiber_sleep(*n);
|
|
|
|
time(&now);
|
|
|
|
|
|
|
|
printf("fiber-%d: wakup, n: %d, sleep: %ld\r\n",
|
|
|
|
acl_fiber_id(fiber), *n, (long) (now - last));
|
|
|
|
|
|
|
|
acl_myfree(n);
|
|
|
|
|
|
|
|
if (--__fibers_count == 0) {
|
|
|
|
printf("All are over!\r\n");
|
|
|
|
//acl_fiber_schedule_stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(const char *procname)
|
|
|
|
{
|
2022-12-27 16:20:47 +08:00
|
|
|
printf("usage: %s -h [help]\r\n"
|
|
|
|
" -c nfibers\r\n"
|
|
|
|
" -n sleep_count\r\n"
|
|
|
|
" -s sleep_way[select|poll|normal]\r\n", procname);
|
2018-11-30 14:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ch, i;
|
|
|
|
|
2022-12-27 16:20:47 +08:00
|
|
|
while ((ch = getopt(argc, argv, "hc:s:n:")) > 0) {
|
2018-11-30 14:38:22 +08:00
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 'c':
|
|
|
|
__fibers_count = atoi(optarg);
|
|
|
|
break;
|
2022-06-27 10:26:49 +08:00
|
|
|
case 's':
|
|
|
|
if (strcasecmp(optarg, "select") == 0) {
|
|
|
|
__sleep_way = SELECT_SLEEP;
|
|
|
|
} else if (strcasecmp(optarg, "poll") == 0) {
|
|
|
|
__sleep_way = POLL_SLEEP;
|
|
|
|
}
|
|
|
|
break;
|
2022-12-27 16:20:47 +08:00
|
|
|
case 'n':
|
|
|
|
__sleep_count = atoi(optarg);
|
|
|
|
break;
|
2018-11-30 14:38:22 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acl_fiber_msg_stdout_enable(1);
|
|
|
|
|
|
|
|
printf("fibers: %d\r\n", __fibers_count);
|
|
|
|
|
|
|
|
for (i = 1; i <= __fibers_count; i++) {
|
|
|
|
int *n = (int *) acl_mymalloc(sizeof(int));
|
|
|
|
*n = i;
|
|
|
|
acl_fiber_create(sleep_main, n, 327680);
|
|
|
|
}
|
|
|
|
|
|
|
|
acl_fiber_schedule();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|