5e34043724cc2242422f8e1da0adde42ac0aea1d
[melted] / src / mvcp / mvcp_notifier.c
1 /*
2 * valerie_notifier.c -- Unit Status Notifier Handling
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 /* System header files */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/time.h>
31
32 /* Application header files */
33 #include "valerie_notifier.h"
34
35 /** Notifier initialisation.
36 */
37
38 valerie_notifier valerie_notifier_init( )
39 {
40 valerie_notifier this = calloc( 1, sizeof( valerie_notifier_t ) );
41 if ( this != NULL )
42 {
43 int index = 0;
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;
48 }
49 return this;
50 }
51
52 /** Get a stored status for the specified unit.
53 */
54
55 void valerie_notifier_get( valerie_notifier this, valerie_status status, int unit )
56 {
57 pthread_mutex_lock( &this->mutex );
58 if ( unit >= 0 && unit < MAX_UNITS )
59 valerie_status_copy( status, &this->store[ unit ] );
60 else
61 memset( status, 0, sizeof( valerie_status_t ) );
62 status->unit = unit;
63 status->dummy = time( NULL );
64 pthread_mutex_unlock( &this->mutex );
65 }
66
67 /** Wait on a new status.
68 */
69
70 int valerie_notifier_wait( valerie_notifier this, valerie_status status )
71 {
72 struct timeval now;
73 struct timespec timeout;
74 int error = 0;
75
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 );
84
85 return error;
86 }
87
88 /** Put a new status.
89 */
90
91 void valerie_notifier_put( valerie_notifier this, valerie_status status )
92 {
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 );
98 }
99
100 /** Communicate a disconnected status for all units to all waiting.
101 */
102
103 void valerie_notifier_disconnected( valerie_notifier notifier )
104 {
105 int unit = 0;
106 valerie_status_t status;
107 for ( unit = 0; unit < MAX_UNITS; unit ++ )
108 {
109 valerie_notifier_get( notifier, &status, unit );
110 status.status = unit_disconnected;
111 valerie_notifier_put( notifier, &status );
112 }
113 }
114
115 /** Close the notifier - note that all access must be stopped before we call this.
116 */
117
118 void valerie_notifier_close( valerie_notifier this )
119 {
120 if ( this != NULL )
121 {
122 pthread_mutex_destroy( &this->mutex );
123 pthread_cond_destroy( &this->cond );
124 free( this );
125 }
126 }