/src/os/rt-thread/ftk_select_rtthread.c

http://ftk.googlecode.com/ · C · 110 lines · 62 code · 19 blank · 29 comment · 8 complexity · 8fa1775e54de669510d3d4bc5adfe64d MD5 · raw file

  1. /*
  2. * File: ftk_select_rtthread.c
  3. * Author: Jiao JinXing <jiaojinxing1987@gmail.com>
  4. * Brief: RT-Thread select implemention.
  5. *
  6. * Copyright (c) 2009 - 2010 Jiao JinXing <jiaojinxing1987@gmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2010-10-2 Jiao JinXing <jiaojinxing1987@gmail.com> created
  28. *
  29. */
  30. #include "ftk_rtthread.h"
  31. #include "ftk_select_rtthread.h"
  32. #include "ftk_pipe.h"
  33. static struct rt_event ftk_event;
  34. static rt_uint32_t ftk_select_fd_pool;
  35. void ftk_rtthread_select_init(void)
  36. {
  37. rt_event_init(&ftk_event, "ftk_evt", RT_IPC_FLAG_FIFO);
  38. ftk_select_fd_pool = 0;
  39. }
  40. void ftk_rtthread_select_deinit(void)
  41. {
  42. rt_event_detach(&ftk_event);
  43. }
  44. int ftk_rtthread_select(int mfd, fd_set *read_fdset, fd_set *write_fdset, fd_set *err_fdset, struct timeval *tv)
  45. {
  46. rt_uint32_t tick = 0;
  47. rt_uint32_t flags = 0;
  48. int i = 0, n = 0;
  49. if (tv != NULL)
  50. {
  51. tick = (rt_uint32_t)(tv->tv_sec + tv->tv_usec/1000000)*RT_TICK_PER_SECOND;
  52. }
  53. else
  54. {
  55. tick = 3*RT_TICK_PER_SECOND;
  56. }
  57. flags = read_fdset->fds_bits[0];
  58. FD_ZERO(read_fdset);
  59. if (write_fdset) FD_ZERO(write_fdset);
  60. if (err_fdset) FD_ZERO(err_fdset);
  61. rt_event_recv(&ftk_event, flags, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, (rt_uint32_t)tick, &read_fdset->fds_bits[0]);
  62. for (i=0; i<32; i++)
  63. {
  64. if (read_fdset->fds_bits[0] & (0x01ul<<i))
  65. {
  66. n++;
  67. }
  68. }
  69. return n;
  70. }
  71. int ftk_rtthread_set_file_readble(int fd)
  72. {
  73. rt_event_send(&ftk_event, (0x01ul<<fd));
  74. }
  75. int ftk_rtthread_select_fd_alloc(void)
  76. {
  77. int i;
  78. for (i=0; i<32; i++)
  79. {
  80. if (!(ftk_select_fd_pool & (0x01ul<<i)))
  81. {
  82. ftk_select_fd_pool |= (0x01ul<<i);
  83. return i;
  84. }
  85. }
  86. return -1;
  87. }
  88. void ftk_rtthread_select_fd_free(int fd)
  89. {
  90. ftk_select_fd_pool &= ~(0x01ul<<fd);
  91. }