mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-15 17:30:53 +08:00
117 lines
2.1 KiB
C
117 lines
2.1 KiB
C
#include "lib_acl.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include "fiber/lib_fiber.h"
|
|
|
|
static int __fibers_count = 2;
|
|
|
|
static void sleep_main(FIBER *fiber, void *ctx)
|
|
{
|
|
int n = *((int*) ctx);
|
|
time_t last, now;
|
|
|
|
printf("fiber-%d: begin sleep %d\r\n", fiber_id(fiber), n);
|
|
time(&last);
|
|
n = (int) fiber_sleep(n);
|
|
time(&now);
|
|
|
|
printf("fiber-%d: wakup, n: %d, sleep: %ld\r\n",
|
|
fiber_id(fiber), n, (long) (now - last));
|
|
|
|
if (--__fibers_count == 0)
|
|
fiber_io_stop();
|
|
}
|
|
|
|
static void usage(const char *procname)
|
|
{
|
|
printf("usage: %s -h [help] -n seconds -c nfibers\r\n", procname);
|
|
}
|
|
|
|
typedef struct {
|
|
ACL_RING me;
|
|
int id;
|
|
} TT;
|
|
|
|
static void test(void)
|
|
{
|
|
int i;
|
|
ACL_RING head;
|
|
ACL_RING_ITER iter;
|
|
|
|
acl_ring_init(&head);
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
TT *t;
|
|
|
|
acl_ring_foreach_reverse(iter, &head) {
|
|
t = acl_ring_to_appl(iter.ptr, TT, me);
|
|
printf(">>curr tt: %d, i: %d\r\n", t->id, i);
|
|
if (i >= t->id) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
t = (TT *) acl_mycalloc(1, sizeof(TT));
|
|
t->id = i;
|
|
acl_ring_append(iter.ptr, &t->me);
|
|
printf("------\r\n");
|
|
/*
|
|
if (next == NULL)
|
|
acl_ring_prepend(&head, &t->me);
|
|
else
|
|
acl_ring_prepend(&next->me, &t->me);
|
|
*/
|
|
}
|
|
|
|
printf("\r\n--------------------------\r\n");
|
|
|
|
acl_ring_foreach(iter, &head) {
|
|
TT *t = acl_ring_to_appl(iter.ptr, TT, me);
|
|
printf(">>tt: %d\r\n", t->id);
|
|
}
|
|
|
|
printf("\r\n--------------------------\r\n");
|
|
|
|
acl_ring_foreach_reverse(iter, &head) {
|
|
TT *t = acl_ring_to_appl(iter.ptr, TT, me);
|
|
printf(">>tt: %d\r\n", t->id);
|
|
acl_myfree(t);
|
|
}
|
|
|
|
exit(0);
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int ch, n = 1, i;
|
|
|
|
test();
|
|
|
|
while ((ch = getopt(argc, argv, "hn:c:")) > 0) {
|
|
switch (ch) {
|
|
case 'h':
|
|
usage(argv[0]);
|
|
return 0;
|
|
case 'n':
|
|
n = atoi(optarg);
|
|
break;
|
|
case 'c':
|
|
__fibers_count = atoi(optarg);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
printf("sleep n: %d, fibers: %d\r\n", n, __fibers_count);
|
|
|
|
for (i = 0; i < __fibers_count; i++)
|
|
fiber_create(sleep_main, &n, 32768);
|
|
|
|
fiber_schedule();
|
|
|
|
return 0;
|
|
}
|