source: trunk/MagicSoft/Control/SubsystemIO/IONotifier.cxx@ 9472

Last change on this file since 9472 was 1054, checked in by casaldaliga, 23 years ago
changed .H to .hxx in includes to work with new naming
File size: 3.5 KB
Line 
1/* Copyright (C) 2001 Marc Casaldaliga Albisu <casaldaliga@ifae.es>
2================================================================
3
4 This code is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This code is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with Emacs (which is required to make this stuff work); if
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA.
18==================================================================
19*/
20
21
22//g++ -c IONotifier.C `sigc-config --cflags`
23#include "IONotifier.hxx"
24//for printf
25#include <stdio.h>
26//for select
27#include <sys/time.h>
28#include <sys/types.h>
29#include <unistd.h>
30//for signals
31#include <sigc++/signal_system.h>
32using namespace SigC;
33//for threads
34#include <pthread.h>
35
36IONotifier::IONotifier(int fd_)
37 :fd(fd_),againWritableEmited(false),fdIsValid(true)
38 {
39 pthread_mutex_init(&mutex,NULL);
40 pthread_create(&thread,NULL,&IONotifier::watchFor,this);
41 }
42IONotifier::~IONotifier()
43 {
44 pthread_mutex_lock(&mutex);
45 fdIsValid=false;
46 pthread_mutex_unlock(&mutex);
47 pthread_join(thread,NULL);
48 }
49
50 void * IONotifier::watchFor( void* arg)
51 {
52 IONotifier* self=(IONotifier *) arg;
53 fd_set rfds, wfds;
54 int retval;
55 //first time:
56 self->againWritableEmited=false;
57 //include the actual fd in the sets checked for reading and writing
58
59 while(1){
60 pthread_mutex_lock(&(self->mutex));
61 if(!self->fdIsValid) break;
62 pthread_mutex_unlock(&(self->mutex));
63 if(!self->againWritableEmited){
64//againWritable has not been emited, so we check both if its readable and writable
65 FD_ZERO(&rfds);FD_ZERO(&wfds);
66 FD_SET(self->fd, &rfds);FD_SET(self->fd, &wfds);
67 retval = select(FD_SETSIZE, &rfds, &wfds, NULL, NULL);
68 }else{
69//if againWritable has been emited we don't want to emit if it's writable until something changes. Until then, only check if it's readable
70 FD_ZERO(&rfds);FD_ZERO(&wfds);
71 FD_SET(self->fd, &rfds);
72 retval = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
73 }
74
75 if (retval){
76
77// printf("Data is available now.\n");
78 if(FD_ISSET(self->fd, &rfds)){
79 //fd is readable
80 self->readable.emit();
81//since this new situation:
82 self->againWritableEmited=false;
83 }
84 if(FD_ISSET(self->fd, &wfds)){
85 //fd is writable
86 self->againWritable.emit();
87 self->againWritableEmited=true;
88 }
89 }else{
90 perror("Shouldn't arrive here");
91
92 }
93
94 }
95// pthread_exit
96 }
97
98
99
100
Note: See TracBrowser for help on using the repository browser.