#include <stdio.h>
#include <stdlib.h>
#include <syscall.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <ev.h>
extern int Createfd();
extern int UdpRecver(int fd);
struct ev_loop *loop2;
static int ioCnt=0;
int CreateTask();
static void async_callback(struct ev_loop *loop, ev_async *w, int revents)
{
ioCnt--;
if (revents & EV_ASYNC) {
printf("Call async_callback %d,data %d, pri=%d, ioCnt=%d\n",
syscall(SYS_gettid), *(int *)w->data, w->priority, ioCnt);
}
sleep(2);
}
static void idle_callback(struct ev_loop *loop, ev_idle *w, int revents)
{
ioCnt--;
printf("Call async_callback %d,data %d, pri=%d, ioCnt=%d\n",
syscall(SYS_gettid), *(int *)w->data, w->priority, ioCnt);
sleep(2);
}
static void stdin_cb (EV_P_ ev_io *w, int revents)
{
printf("stdin_cb\n");
sleep(2);
ev_io_stop (EV_A_ w);
ev_break (EV_A_ EVBREAK_ALL);
}
void Trans(EV_P_ int a)
{
ioCnt++;
int *i;
i = (int *)malloc(sizeof(int));
*i = a;
#if 0
ev_async async_watcher;
ev_async_init(&async_watcher, async_callback);
async_watcher.data = i;
ev_set_priority(&async_watcher, a);
//以下两个一起用
//ev_async_start(EV_A, &async_watcher);
//ev_async_send(EV_A, &async_watcher);
ev_feed_event(EV_A, &async_watcher,EV_ASYNC);
#endif
ev_idle idle_watcher;
ev_idle_init(&idle_watcher, idle_callback);
idle_watcher.data = i;
ev_set_priority(&idle_watcher, a);
ev_feed_event(EV_A, &idle_watcher,EV_IDLE);
//ev_invoke(EV_A, &idle_watcher,EV_IDLE);
//ev_idle_start (EV_A_ &idle_watcher);
}
static void io_cb (EV_P_ ev_io *w, int revents)
{
int i;
printf("Call io_cb %d,fd %d, ioCnt=%d\n",
syscall(SYS_gettid), w->fd, ioCnt);
i = UdpRecver(w->fd);
Trans(EV_A_ i-3);
//ev_io_stop(loop, w);
}
static void timeout_cb3 (EV_P_ ev_timer *w, int revents)
{
printf("timeout_cb main3\n");
}
static void timeout_cb (EV_P_ ev_timer *w, int revents)
{
printf("timeout_cb\n");
//ev_break (EV_A_ EVBREAK_ONE);
//ev_timer_stop(EV_A_ &timeout_watcher2);
#if 0
ev_timer timeout_watcher3;
ev_timer_init (&timeout_watcher3, timeout_cb3, 2.0, 2.0);
ev_set_priority(&timeout_watcher3, -2);
ev_invoke(EV_A_ &timeout_watcher3,EV_TIMER);
#endif
#if 0
int *i;
i = (int *)malloc(sizeof(int));
*i = 110;
ev_async async_watcher;
ev_async_init(&async_watcher, async_callback);
async_watcher.data = i;
ev_set_priority(&async_watcher, 0);
//以下两个一起用
//ev_async_start(loop2, &async_watcher);
//ev_async_send(loop2, &async_watcher);
//ev_invoke(loop2,&async_watcher,EV_ASYNC);
ev_feed_event(loop,&async_watcher,EV_ASYNC);
#endif
}
static void timeout_cb2 (EV_P_ ev_timer *w, int revents)
{
printf("timeout_cb main2\n");
}
int main(void)
{
printf("main start %d\n", syscall(SYS_gettid));
unsigned int flags = 0;
flags = ev_supported_backends();
printf("ev_supported_backends(%d)\n", flags);
flags = ev_recommended_backends();
printf("ev_recommended_backends(%d)\n", flags);
//struct ev_loop *loop = EV_DEFAULT;
struct ev_loop *loop = ev_loop_new(EVFLAG_AUTO);
unsigned int backend = ev_backend(loop);
printf("loop backend %d\n", backend);
ev_io stdin_watcher;
ev_io_init (&stdin_watcher, stdin_cb, 0, EV_READ);
ev_io_start (loop, &stdin_watcher);
ev_timer timeout_watcher;
ev_timer timeout_watcher2;
ev_timer_init (&timeout_watcher, timeout_cb, 1.0, 0.0);
ev_timer_init (&timeout_watcher2, timeout_cb2, 1.0, 0.0);
ev_timer_start (loop, &timeout_watcher);
ev_timer_start (loop, &timeout_watcher2);
ev_set_priority(&timeout_watcher, 1);
ev_set_priority(&timeout_watcher2,-1);
#if 0
ev_timer timeout_watcher3;
ev_timer_init (&timeout_watcher3, timeout_cb3, 2.0, 2.0);
ev_set_priority(&timeout_watcher3, -2);
ev_timer_start (loop, &timeout_watcher3);
#endif
int ifd = Createfd();
ev_io io_watcher;
ev_io_init (&io_watcher, io_cb, ifd, EV_READ);
ev_set_priority(&io_watcher, 0);
ev_io_start (loop, &io_watcher);
printf("wait on fd(%d)\n",ifd);
//loop2 = ev_loop_new(EVFLAG_AUTO);
//CreateTask();
ev_run (loop, 0);
printf("main stop\n");
return 0;
}
/*****************************************************************************************/
static void
task2_timeout_cb (EV_P_ ev_timer *w, int revents)
{
printf("timeout_cb 222\n");
}
static void *Task2(void *arg)
{
printf("Task2 start %d\n", syscall(SYS_gettid));
ev_timer task2_timeout_watcher;
ev_timer_init (&task2_timeout_watcher, task2_timeout_cb, 1, 2);
ev_timer_start (loop2, &task2_timeout_watcher);
ev_run (loop2, 0);
printf("Task2 stop\n");
return NULL;
}
int CreateTask()
{
pthread_t TaskID;
errno = 0;
if (0 != pthread_create(&TaskID, NULL, (void *)Task2, NULL)) {
printf("pthread_create err(%d:%s)\n", errno, strerror(errno));
return -1;
}
return 0;
}
#include <stdlib.h>
#include <syscall.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <ev.h>
extern int Createfd();
extern int UdpRecver(int fd);
struct ev_loop *loop2;
static int ioCnt=0;
int CreateTask();
static void async_callback(struct ev_loop *loop, ev_async *w, int revents)
{
ioCnt--;
if (revents & EV_ASYNC) {
printf("Call async_callback %d,data %d, pri=%d, ioCnt=%d\n",
syscall(SYS_gettid), *(int *)w->data, w->priority, ioCnt);
}
sleep(2);
}
static void idle_callback(struct ev_loop *loop, ev_idle *w, int revents)
{
ioCnt--;
printf("Call async_callback %d,data %d, pri=%d, ioCnt=%d\n",
syscall(SYS_gettid), *(int *)w->data, w->priority, ioCnt);
sleep(2);
}
static void stdin_cb (EV_P_ ev_io *w, int revents)
{
printf("stdin_cb\n");
sleep(2);
ev_io_stop (EV_A_ w);
ev_break (EV_A_ EVBREAK_ALL);
}
void Trans(EV_P_ int a)
{
ioCnt++;
int *i;
i = (int *)malloc(sizeof(int));
*i = a;
#if 0
ev_async async_watcher;
ev_async_init(&async_watcher, async_callback);
async_watcher.data = i;
ev_set_priority(&async_watcher, a);
//以下两个一起用
//ev_async_start(EV_A, &async_watcher);
//ev_async_send(EV_A, &async_watcher);
ev_feed_event(EV_A, &async_watcher,EV_ASYNC);
#endif
ev_idle idle_watcher;
ev_idle_init(&idle_watcher, idle_callback);
idle_watcher.data = i;
ev_set_priority(&idle_watcher, a);
ev_feed_event(EV_A, &idle_watcher,EV_IDLE);
//ev_invoke(EV_A, &idle_watcher,EV_IDLE);
//ev_idle_start (EV_A_ &idle_watcher);
}
static void io_cb (EV_P_ ev_io *w, int revents)
{
int i;
printf("Call io_cb %d,fd %d, ioCnt=%d\n",
syscall(SYS_gettid), w->fd, ioCnt);
i = UdpRecver(w->fd);
Trans(EV_A_ i-3);
//ev_io_stop(loop, w);
}
static void timeout_cb3 (EV_P_ ev_timer *w, int revents)
{
printf("timeout_cb main3\n");
}
static void timeout_cb (EV_P_ ev_timer *w, int revents)
{
printf("timeout_cb\n");
//ev_break (EV_A_ EVBREAK_ONE);
//ev_timer_stop(EV_A_ &timeout_watcher2);
#if 0
ev_timer timeout_watcher3;
ev_timer_init (&timeout_watcher3, timeout_cb3, 2.0, 2.0);
ev_set_priority(&timeout_watcher3, -2);
ev_invoke(EV_A_ &timeout_watcher3,EV_TIMER);
#endif
#if 0
int *i;
i = (int *)malloc(sizeof(int));
*i = 110;
ev_async async_watcher;
ev_async_init(&async_watcher, async_callback);
async_watcher.data = i;
ev_set_priority(&async_watcher, 0);
//以下两个一起用
//ev_async_start(loop2, &async_watcher);
//ev_async_send(loop2, &async_watcher);
//ev_invoke(loop2,&async_watcher,EV_ASYNC);
ev_feed_event(loop,&async_watcher,EV_ASYNC);
#endif
}
static void timeout_cb2 (EV_P_ ev_timer *w, int revents)
{
printf("timeout_cb main2\n");
}
int main(void)
{
printf("main start %d\n", syscall(SYS_gettid));
unsigned int flags = 0;
flags = ev_supported_backends();
printf("ev_supported_backends(%d)\n", flags);
flags = ev_recommended_backends();
printf("ev_recommended_backends(%d)\n", flags);
//struct ev_loop *loop = EV_DEFAULT;
struct ev_loop *loop = ev_loop_new(EVFLAG_AUTO);
unsigned int backend = ev_backend(loop);
printf("loop backend %d\n", backend);
ev_io stdin_watcher;
ev_io_init (&stdin_watcher, stdin_cb, 0, EV_READ);
ev_io_start (loop, &stdin_watcher);
ev_timer timeout_watcher;
ev_timer timeout_watcher2;
ev_timer_init (&timeout_watcher, timeout_cb, 1.0, 0.0);
ev_timer_init (&timeout_watcher2, timeout_cb2, 1.0, 0.0);
ev_timer_start (loop, &timeout_watcher);
ev_timer_start (loop, &timeout_watcher2);
ev_set_priority(&timeout_watcher, 1);
ev_set_priority(&timeout_watcher2,-1);
#if 0
ev_timer timeout_watcher3;
ev_timer_init (&timeout_watcher3, timeout_cb3, 2.0, 2.0);
ev_set_priority(&timeout_watcher3, -2);
ev_timer_start (loop, &timeout_watcher3);
#endif
int ifd = Createfd();
ev_io io_watcher;
ev_io_init (&io_watcher, io_cb, ifd, EV_READ);
ev_set_priority(&io_watcher, 0);
ev_io_start (loop, &io_watcher);
printf("wait on fd(%d)\n",ifd);
//loop2 = ev_loop_new(EVFLAG_AUTO);
//CreateTask();
ev_run (loop, 0);
printf("main stop\n");
return 0;
}
/*****************************************************************************************/
static void
task2_timeout_cb (EV_P_ ev_timer *w, int revents)
{
printf("timeout_cb 222\n");
}
static void *Task2(void *arg)
{
printf("Task2 start %d\n", syscall(SYS_gettid));
ev_timer task2_timeout_watcher;
ev_timer_init (&task2_timeout_watcher, task2_timeout_cb, 1, 2);
ev_timer_start (loop2, &task2_timeout_watcher);
ev_run (loop2, 0);
printf("Task2 stop\n");
return NULL;
}
int CreateTask()
{
pthread_t TaskID;
errno = 0;
if (0 != pthread_create(&TaskID, NULL, (void *)Task2, NULL)) {
printf("pthread_create err(%d:%s)\n", errno, strerror(errno));
return -1;
}
return 0;
}