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 = malloc( sizeof( valerie_notifier_t
) );
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
;
54 /** Get a stored status for the specified unit.
57 void valerie_notifier_get( valerie_notifier
this, valerie_status status
, int unit
)
59 pthread_mutex_lock( &this->mutex
);
60 if ( unit
>= 0 && unit
< MAX_UNITS
)
62 valerie_status_copy( status
, &this->store
[ unit
] );
66 memset( status
, 0, sizeof( valerie_status_t
) );
69 status
->dummy
= time( NULL
);
70 pthread_mutex_unlock( &this->mutex
);
73 /** Wait on a new status.
76 int valerie_notifier_wait( valerie_notifier
this, valerie_status status
)
79 struct timespec timeout
;
82 memset( status
, 0, sizeof( valerie_status_t
) );
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
)
90 pthread_mutex_lock( &this->mutex
);
91 valerie_status_copy( status
, &this->last
);
92 pthread_mutex_unlock( &this->mutex
);
98 pthread_mutex_unlock( &this->cond_mutex
);
103 /** Put a new status.
106 void valerie_notifier_put( valerie_notifier
this, valerie_status status
)
108 pthread_mutex_lock( &this->mutex
);
109 valerie_status_copy( &this->store
[ status
->unit
], status
);
110 valerie_status_copy( &this->last
, status
);
111 pthread_mutex_unlock( &this->mutex
);
112 pthread_cond_broadcast( &this->cond
);
115 /** Communicate a disconnected status for all units to all waiting.
118 void valerie_notifier_disconnected( valerie_notifier notifier
)
121 valerie_status_t status
;
122 for ( unit
= 0; unit
< MAX_UNITS
; unit
++ )
124 valerie_notifier_get( notifier
, &status
, unit
);
125 status
.status
= unit_disconnected
;
126 valerie_notifier_put( notifier
, &status
);
130 /** Close the notifier - note that all access must be stopped before we call this.
133 void valerie_notifier_close( valerie_notifier
this )
137 pthread_mutex_destroy( &this->mutex
);
138 pthread_mutex_destroy( &this->cond_mutex
);
139 pthread_cond_destroy( &this->cond
);