Skip to content

Commit 5b772a8

Browse files
committed
test sleep in fiber mode
1 parent bced2d3 commit 5b772a8

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

lib_fiber/samples/sleep/main.c

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,50 @@
33
#include <stdlib.h>
44
#include <string.h>
55
#include <unistd.h>
6+
#include <sys/select.h>
67
#include "fiber/libfiber.h"
78

89
static int __fibers_count = 2;
910

11+
enum {
12+
SELECT_SLEEP = 1,
13+
POLL_SLEEP = 2,
14+
};
15+
16+
static int __sleep_way = 0;
17+
18+
static void select_sleep(int seconds)
19+
{
20+
struct timeval tv;
21+
22+
tv.tv_sec = seconds;
23+
tv.tv_usec = 0;
24+
(void) select(0, NULL, NULL, NULL, &tv);
25+
}
26+
27+
static void poll_sleep(int seconds)
28+
{
29+
int delay = seconds * 1000;
30+
(void) poll(NULL, 0, delay);
31+
}
32+
1033
static void sleep_main(ACL_FIBER *fiber, void *ctx)
1134
{
1235
int *n = (int *) ctx;
1336
time_t last, now;
1437

1538
while (1) {
16-
acl_fiber_sleep(1);
39+
switch (__sleep_way) {
40+
case SELECT_SLEEP:
41+
select_sleep(1);
42+
break;
43+
case POLL_SLEEP:
44+
poll_sleep(1);
45+
break;
46+
default:
47+
acl_fiber_sleep(1);
48+
break;
49+
}
1750
printf("fiber-%d wakeup\r\n", acl_fiber_self());
1851
}
1952

@@ -35,21 +68,28 @@ static void sleep_main(ACL_FIBER *fiber, void *ctx)
3568

3669
static void usage(const char *procname)
3770
{
38-
printf("usage: %s -h [help] -c nfibers\r\n", procname);
71+
printf("usage: %s -h [help] -c nfibers -s sleep_way[select|poll|normal]\r\n", procname);
3972
}
4073

4174
int main(int argc, char *argv[])
4275
{
4376
int ch, i;
4477

45-
while ((ch = getopt(argc, argv, "hc:")) > 0) {
78+
while ((ch = getopt(argc, argv, "hc:s:")) > 0) {
4679
switch (ch) {
4780
case 'h':
4881
usage(argv[0]);
4982
return 0;
5083
case 'c':
5184
__fibers_count = atoi(optarg);
5285
break;
86+
case 's':
87+
if (strcasecmp(optarg, "select") == 0) {
88+
__sleep_way = SELECT_SLEEP;
89+
} else if (strcasecmp(optarg, "poll") == 0) {
90+
__sleep_way = POLL_SLEEP;
91+
}
92+
break;
5393
default:
5494
break;
5595
}

0 commit comments

Comments
 (0)