PageRenderTime 173ms CodeModel.GetById 70ms app.highlight 6ms RepoModel.GetById 94ms app.codeStats 1ms

/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/*
 26 * History:
 27 * ================================================================
 28 * 2010-10-2 Jiao JinXing <jiaojinxing1987@gmail.com> created
 29 *
 30 */
 31
 32#include "ftk_rtthread.h"
 33#include "ftk_select_rtthread.h"
 34#include "ftk_pipe.h"
 35
 36static struct rt_event 	ftk_event;
 37static rt_uint32_t 		ftk_select_fd_pool;
 38
 39void ftk_rtthread_select_init(void)
 40{
 41	rt_event_init(&ftk_event, "ftk_evt", RT_IPC_FLAG_FIFO);
 42
 43	ftk_select_fd_pool = 0;
 44}
 45
 46void ftk_rtthread_select_deinit(void)
 47{
 48	rt_event_detach(&ftk_event);
 49}
 50
 51int ftk_rtthread_select(int mfd, fd_set *read_fdset, fd_set *write_fdset, fd_set *err_fdset, struct timeval *tv)
 52{
 53	rt_uint32_t tick = 0;
 54	rt_uint32_t flags = 0;
 55	int i = 0, n = 0;
 56
 57	if (tv != NULL)
 58	{
 59		tick = (rt_uint32_t)(tv->tv_sec + tv->tv_usec/1000000)*RT_TICK_PER_SECOND;
 60	}
 61	else
 62	{
 63	    tick = 3*RT_TICK_PER_SECOND;
 64	}
 65
 66	flags = read_fdset->fds_bits[0];
 67	FD_ZERO(read_fdset);
 68
 69	if (write_fdset) FD_ZERO(write_fdset);
 70
 71	if (err_fdset) FD_ZERO(err_fdset);
 72
 73	rt_event_recv(&ftk_event, flags, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, (rt_uint32_t)tick, &read_fdset->fds_bits[0]);
 74
 75	for (i=0; i<32; i++)
 76	{
 77		if (read_fdset->fds_bits[0] & (0x01ul<<i))
 78		{
 79			n++;
 80		}
 81	}
 82
 83	return n;
 84}
 85
 86int ftk_rtthread_set_file_readble(int fd)
 87{
 88	rt_event_send(&ftk_event, (0x01ul<<fd));
 89}
 90
 91int ftk_rtthread_select_fd_alloc(void)
 92{
 93	int i;
 94
 95	for (i=0; i<32; i++)
 96	{
 97		if (!(ftk_select_fd_pool & (0x01ul<<i)))
 98		{
 99			ftk_select_fd_pool |= (0x01ul<<i);
100			return i;	
101		}
102	}
103	return -1;
104}
105
106void ftk_rtthread_select_fd_free(int fd)
107{
108	ftk_select_fd_pool &= ~(0x01ul<<fd);
109}
110