Miracle mods
[melted] / src / valerie / valerie_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 = malloc( sizeof( valerie_notifier_t ) );
41 if ( this != NULL )
42 {
43 int index = 0;
44 memset( this, 0, sizeof( valerie_notifier_t ) );
45 pthread_mutex_init( &this->mutex, NULL );
46 pthread_cond_init( &this->cond, NULL );
47 pthread_mutex_init( &this->cond_mutex, NULL );
48 for ( index = 0; index < MAX_UNITS; index ++ )
49 this->store[ index ].unit = index;
50 }
51 return this;
52 }
53
54 /** Get a stored status for the specified unit.
55 */
56
57 void valerie_notifier_get( valerie_notifier this, valerie_status status, int unit )
58 {
59 pthread_mutex_lock( &this->mutex );
60 if ( unit >= 0 && unit < MAX_UNITS )
61 {
62 valerie_status_copy( status, &this->store[ unit ] );
63 }
64 else
65 {
66 memset( status, 0, sizeof( valerie_status_t ) );
67 status->unit = unit;
68 }
69 status->dummy = time( NULL );
70 pthread_mutex_unlock( &this->mutex );
71 }
72
73 /** Wait on a new status.
74 */
75
76 int valerie_notifier_wait( valerie_notifier this, valerie_status status )
77 {
78 struct timeval now;
79 struct timespec timeout;
80 int error = 0;
81
82 memset( status, 0, sizeof( valerie_status_t ) );
83
84 pthread_mutex_lock( &this->cond_mutex );
85 gettimeofday( &now, NULL );
86 timeout.tv_sec = now.tv_sec + 1;
87 timeout.tv_nsec = now.tv_usec * 1000;
88 if ( pthread_cond_timedwait( &this->cond, &this->cond_mutex, &timeout ) != ETIMEDOUT )
89 {
90 pthread_mutex_lock( &this->mutex );
91 valerie_status_copy( status, &this->last );
92 pthread_mutex_unlock( &this->mutex );
93 }
94 else
95 {
96 pthread_mutex_lock( &this->mutex );
97 valerie_status_copy( status, &this->last );
98 pthread_mutex_unlock( &this->mutex );
99 }
100 pthread_mutex_unlock( &this->cond_mutex );
101
102 return error;
103 }
104
105 /** Put a new status.
106 */
107
108 void valerie_notifier_put( valerie_notifier this, valerie_status status )
109 {
110 pthread_mutex_lock( &this->mutex );
111 valerie_status_copy( &this->store[ status->unit ], status );
112 valerie_status_copy( &this->last, status );
113 pthread_mutex_unlock( &this->mutex );
114 pthread_cond_broadcast( &this->cond );
115 }
116
117 /** Communicate a disconnected status for all units to all waiting.
118 */
119
120 void valerie_notifier_disconnected( valerie_notifier notifier )
121 {
122 int unit = 0;
123 valerie_status_t status;
124 for ( unit = 0; unit < MAX_UNITS; unit ++ )
125 {
126 valerie_notifier_get( notifier, &status, unit );
127 status.status = unit_disconnected;
128 valerie_notifier_put( notifier, &status );
129 }
130 }
131
132 /** Close the notifier - note that all access must be stopped before we call this.
133 */
134
135 void valerie_notifier_close( valerie_notifier this )
136 {
137 if ( this != NULL )
138 {
139 pthread_mutex_destroy( &this->mutex );
140 pthread_mutex_destroy( &this->cond_mutex );
141 pthread_cond_destroy( &this->cond );
142 free( this );
143 }
144 }