| 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 | #include "PeriodicSignal.hxx"
|
|---|
| 22 | //for signals
|
|---|
| 23 | #include <sigc++/signal_system.h>
|
|---|
| 24 | using namespace SigC;
|
|---|
| 25 | //for threads
|
|---|
| 26 | #include <pthread.h>
|
|---|
| 27 | //for sleep,select
|
|---|
| 28 | #include <unistd.h>
|
|---|
| 29 | #include <sys/time.h>
|
|---|
| 30 | #include <sys/types.h>
|
|---|
| 31 |
|
|---|
| 32 | PeriodicSignal::PeriodicSignal(unsigned int period_)
|
|---|
| 33 | :period(period_)
|
|---|
| 34 | {}
|
|---|
| 35 | void PeriodicSignal::Start()
|
|---|
| 36 | {
|
|---|
| 37 | stopped=false;
|
|---|
| 38 | pthread_mutex_init(&mutex4stop,NULL);
|
|---|
| 39 |
|
|---|
| 40 | pthread_create(&thread,NULL,&PeriodicSignal::SleepPeriodAndSignal,this);
|
|---|
| 41 |
|
|---|
| 42 | };
|
|---|
| 43 | void PeriodicSignal::Stop()
|
|---|
| 44 | {
|
|---|
| 45 | pthread_mutex_lock(&mutex4stop);
|
|---|
| 46 | stopped=true;
|
|---|
| 47 | pthread_mutex_unlock(&mutex4stop);
|
|---|
| 48 | pthread_join(thread,NULL);
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | void * PeriodicSignal::SleepPeriodAndSignal( void* arg)
|
|---|
| 52 | {
|
|---|
| 53 | PeriodicSignal* self=(PeriodicSignal *) arg;
|
|---|
| 54 | while(1){
|
|---|
| 55 | pthread_mutex_lock(&(self->mutex4stop));
|
|---|
| 56 | if(self->stopped) break;
|
|---|
| 57 | pthread_mutex_unlock(&(self->mutex4stop));
|
|---|
| 58 |
|
|---|
| 59 | usleep((unsigned long)self->period);
|
|---|
| 60 |
|
|---|
| 61 | self->signal.emit();
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|