2 * valerie_notifier.c -- Unit Status Notifier Handling
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /* System header files */
32 /* Application header files */
33 #include "valerie_notifier.h"
35 /** Notifier initialisation.
38 valerie_notifier valerie_notifier_init( )
40 valerie_notifier this = calloc( 1, sizeof( valerie_notifier_t ) );
44 pthread_mutex_init( &this->mutex, NULL );
45 pthread_cond_init( &this->cond, NULL );
46 for ( index = 0; index < MAX_UNITS; index ++ )
47 this->store[ index ].unit = index;
52 /** Get a stored status for the specified unit.
55 void valerie_notifier_get( valerie_notifier this, valerie_status status, int unit )
57 pthread_mutex_lock( &this->mutex );
58 if ( unit >= 0 && unit < MAX_UNITS )
59 valerie_status_copy( status, &this->store[ unit ] );
61 memset( status, 0, sizeof( valerie_status_t ) );
63 status->dummy = time( NULL );
64 pthread_mutex_unlock( &this->mutex );
67 /** Wait on a new status.
70 int valerie_notifier_wait( valerie_notifier this, valerie_status status )
73 struct timespec timeout;
76 memset( status, 0, sizeof( valerie_status_t ) );
77 gettimeofday( &now, NULL );
78 timeout.tv_sec = now.tv_sec + 1;
79 timeout.tv_nsec = now.tv_usec * 1000;
80 pthread_mutex_lock( &this->mutex );
81 pthread_cond_timedwait( &this->cond, &this->mutex, &timeout );
82 valerie_status_copy( status, &this->last );
83 pthread_mutex_unlock( &this->mutex );
91 void valerie_notifier_put( valerie_notifier this, valerie_status status )
93 pthread_mutex_lock( &this->mutex );
94 valerie_status_copy( &this->store[ status->unit ], status );
95 valerie_status_copy( &this->last, status );
96 pthread_mutex_unlock( &this->mutex );
97 pthread_cond_broadcast( &this->cond );
100 /** Communicate a disconnected status for all units to all waiting.
103 void valerie_notifier_disconnected( valerie_notifier notifier )
106 valerie_status_t status;
107 for ( unit = 0; unit < MAX_UNITS; unit ++ )
109 valerie_notifier_get( notifier, &status, unit );
110 status.status = unit_disconnected;
111 valerie_notifier_put( notifier, &status );
115 /** Close the notifier - note that all access must be stopped before we call this.
118 void valerie_notifier_close( valerie_notifier this )
122 pthread_mutex_destroy( &this->mutex );
123 pthread_cond_destroy( &this->cond );