Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3523467
  • 博文数量: 1805
  • 博客积分: 135
  • 博客等级: 入伍新兵
  • 技术积分: 3345
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-19 20:01
文章分类

全部博文(1805)

文章存档

2017年(19)

2016年(80)

2015年(341)

2014年(438)

2013年(349)

2012年(332)

2011年(248)

分类: 云计算

2015-02-04 22:30:32


redworker笔记

1)redworker由dispatcher(Red_dispatcher.c)以线程的形式启动
2)red_worker_main流程

  1. SPICE_GNUC_NORETURN void *red_worker_main(void *arg)
  2. {
  3.     RedWorker *worker = spice_malloc(sizeof(RedWorker));

  4.     spice_info("begin");
  5.     spice_assert(MAX_PIPE_SIZE > WIDE_CLIENT_ACK_WINDOW &&
  6.            MAX_PIPE_SIZE > NARROW_CLIENT_ACK_WINDOW); //ensure wakeup by ack message

  7. #if defined(RED_WORKER_STAT) || defined(COMPRESS_STAT)
  8.     if (pthread_getcpuclockid(pthread_self(), &clock_id)) {
  9.         spice_error("pthread_getcpuclockid failed");
  10.     }
  11. #endif

  12. //初始化worker,设置回调,内存池初始化,cache初始化等
  13.     red_init(worker, (WorkerInitData *)arg);
  14. //初始化解压器
  15.     red_init_quic(worker);
  16.     red_init_lz(worker);
  17.     red_init_jpeg(worker);
  18.     red_init_zlib(worker);
  19.     worker->event_timeout = INF_EVENT_WAIT;
  20. //进入循环,POLL处理事件
  21.     for (;;) {
  22.         int i, num_events;

  23.         worker->event_timeout = MIN(red_get_streams_timout(worker), worker->event_timeout);
  24.         num_events = poll(worker->poll_fds, MAX_EVENT_SOURCES, worker->event_timeout);
  25.         red_handle_streams_timout(worker);

  26.         if (worker->display_channel) {
  27.             /* during migration, in the dest, the display channel can be initialized
  28.                while the global lz data not since migrate data msg hasn't been
  29.                received yet */
  30.             red_channel_apply_clients(&worker->display_channel->common.base,
  31.                                       red_display_cc_free_glz_drawables);
  32.         }

  33.         worker->event_timeout = INF_EVENT_WAIT;
  34.         if (num_events == -1) {
  35.             if (errno != EINTR) {
  36.                 spice_error("poll failed, %s", strerror(errno));
  37.             }
  38.         }

  39.         for (i = 0; i < MAX_EVENT_SOURCES; i++) {
  40.             /* The watch may have been removed by the watch-func from
  41.                another fd (ie a disconnect through the dispatcher),
  42.                in this case watch_func is NULL. */
  43.             if (worker->poll_fds[i].revents && worker->watches[i].watch_func) {
  44.                 int events = 0;
  45.                 if (worker->poll_fds[i].revents & POLLIN) {
  46.                     events |= SPICE_WATCH_EVENT_READ;
  47.                 }
  48.                 if (worker->poll_fds[i].revents & POLLOUT) {
  49.                     events |= SPICE_WATCH_EVENT_WRITE;
  50.                 }
  51. //dispatcher 交互
  52.                 worker->watches[i].watch_func(worker->poll_fds[i].fd, events,
  53.                                         worker->watches[i].watch_func_opaque);
  54.             }
  55.         }

  56.         /* Clear the poll_fd for any removed watches, see the comment in
  57.            watch_remove for why we don't do this there. */
  58.         for (i = 0; i < MAX_EVENT_SOURCES; i++) {
  59.             if (!worker->watches[i].watch_func) {
  60.                 worker->poll_fds[i].fd = -1;
  61.             }
  62.         }

  63. //QXL Device 交互
  64.         if (worker->running) {
  65.             int ring_is_empty;
  66.             red_process_cursor(worker, MAX_PIPE_SIZE, &ring_is_empty);
  67.             red_process_commands(worker, MAX_PIPE_SIZE, &ring_is_empty);
  68.         }
  69.         red_push(worker);
  70.     }
  71.     abort();
  72. }
参考:spice_for_newbies.pdf


阅读(531) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~