Equipped libdax_msgs with reference counter. Completed its mutex protection.

This commit is contained in:
Thomas Schmitt 2007-09-15 20:41:25 +00:00
parent 9face00707
commit c7bebfea3b
5 changed files with 90 additions and 49 deletions

View File

@ -1 +1 @@
#define Cdrskin_timestamP "2007.09.15.172141"
#define Cdrskin_timestamP "2007.09.15.204320"

View File

@ -25,8 +25,6 @@
#include "libdax_msgs.h"
struct libdax_msgs *libdax_messenger= NULL;
static int libdax_messenger_is_own = 1;
int burn_running = 0;
@ -309,10 +307,12 @@ void burn_allow_untested_profiles(int yes)
/* ts A70915 : API */
int burn_set_messenger(void *messenger)
{
if (libdax_messenger_is_own)
libdax_msgs_destroy(&libdax_messenger, 0);
libdax_messenger = (struct libdax_msgs *) messenger;
libdax_messenger_is_own = 0;
struct libdax_msgs *pt;
if (libdax_msgs_refer(&pt, messenger, 0) <= 0)
return 0;
libdax_msgs_destroy(&libdax_messenger, 0);
libdax_messenger = (struct libdax_msgs *) pt;
return 1;
}

View File

@ -1877,9 +1877,8 @@ int burn_msgs_obtain(char *minimum_severity,
/* ts A70915 */
/** Replace the messenger object handle of libburn by a compatible handle
obtained from a related library. The own message object gets destroyed,
so it is IMPORTANT to shut down libburn BEFORE the library which provides
the messenger object. See also: libisofs, API function iso_get_messenger().
obtained from a related library.
See also: libisofs, API function iso_get_messenger().
@param messenger The foreign but compatible message handle.
@return 1 : success, <=0 : failure
*/

View File

@ -138,6 +138,7 @@ int libdax_msgs_new(struct libdax_msgs **m, int flag)
(*m)= o= (struct libdax_msgs *) malloc(sizeof(struct libdax_msgs));
if(o==NULL)
return(-1);
o->refcount= 1;
o->oldest= NULL;
o->youngest= NULL;
o->count= 0;
@ -153,43 +154,6 @@ int libdax_msgs_new(struct libdax_msgs **m, int flag)
}
int libdax_msgs_destroy(struct libdax_msgs **m, int flag)
{
struct libdax_msgs *o;
struct libdax_msgs_item *item, *next_item;
o= *m;
if(o==NULL)
return(0);
#ifndef LIBDAX_MSGS_SINGLE_THREADED
if(pthread_mutex_destroy(&(o->lock_mutex))!=0) {
pthread_mutex_unlock(&(o->lock_mutex));
pthread_mutex_destroy(&(o->lock_mutex));
}
#endif
for(item= o->oldest; item!=NULL; item= next_item) {
next_item= item->next;
libdax_msgs_item_destroy(&item,0);
}
free((char *) o);
*m= NULL;
return(1);
}
int libdax_msgs_set_severities(struct libdax_msgs *m, int queue_severity,
int print_severity, char *print_id, int flag)
{
m->queue_severity= queue_severity;
m->print_severity= print_severity;
strncpy(m->print_id,print_id,80);
m->print_id[80]= 0;
return(1);
}
static int libdax_msgs_lock(struct libdax_msgs *m, int flag)
{
@ -220,6 +184,65 @@ static int libdax_msgs_unlock(struct libdax_msgs *m, int flag)
}
int libdax_msgs_destroy(struct libdax_msgs **m, int flag)
{
struct libdax_msgs *o;
struct libdax_msgs_item *item, *next_item;
o= *m;
if(o==NULL)
return(0);
if(o->refcount > 1) {
if(libdax_msgs_lock(*m,0)<=0)
return(-1);
o->refcount--;
libdax_msgs_unlock(*m,0);
*m= NULL;
return(1);
}
#ifndef LIBDAX_MSGS_SINGLE_THREADED
if(pthread_mutex_destroy(&(o->lock_mutex))!=0) {
pthread_mutex_unlock(&(o->lock_mutex));
pthread_mutex_destroy(&(o->lock_mutex));
}
#endif
for(item= o->oldest; item!=NULL; item= next_item) {
next_item= item->next;
libdax_msgs_item_destroy(&item,0);
}
free((char *) o);
*m= NULL;
return(1);
}
int libdax_msgs_refer(struct libdax_msgs **pt, struct libdax_msgs *m, int flag)
{
if(libdax_msgs_lock(m,0)<=0)
return(0);
m->refcount++;
*pt= m;
libdax_msgs_unlock(m,0);
return(1);
}
int libdax_msgs_set_severities(struct libdax_msgs *m, int queue_severity,
int print_severity, char *print_id, int flag)
{
if(libdax_msgs_lock(m,0)<=0)
return(0);
m->queue_severity= queue_severity;
m->print_severity= print_severity;
strncpy(m->print_id,print_id,80);
m->print_id[80]= 0;
libdax_msgs_unlock(m,0);
return(1);
}
int libdax_msgs__text_to_sev(char *severity_name, int *severity,
int flag)
{

View File

@ -43,6 +43,8 @@ struct libdax_msgs_item {
struct libdax_msgs {
int refcount;
struct libdax_msgs_item *oldest;
struct libdax_msgs_item *youngest;
int count;
@ -176,7 +178,8 @@ struct libdax_msgs_item;
/* Calls initiated from inside the direct owner (e.g. from libburn) */
/** Create new empty message handling facility with queue.
/** Create new empty message handling facility with queue and issue a first
official reference to it.
@param flag Bitfield for control purposes (unused yet, submit 0)
@return >0 success, <=0 failure
*/
@ -185,12 +188,28 @@ int libdax_msgs_new(struct libdax_msgs **m, int flag);
/** Destroy a message handling facility and all its eventual messages.
The submitted pointer gets set to NULL.
Actually only the last destroy call of all offical references to the object
will really dispose it. All others just decrement the reference counter.
Call this function only with official reference pointers obtained by
libdax_msgs_new() or libdax_msgs_refer(), and only once per such pointer.
@param flag Bitfield for control purposes (unused yet, submit 0)
@return 1 for success, 0 for pointer to NULL
@return 1 for success, 0 for pointer to NULL, -1 for fatal error
*/
int libdax_msgs_destroy(struct libdax_msgs **m, int flag);
/** Create an official reference to an existing libdax_msgs object. The
references keep the object alive at least until it is released by
a matching number of destroy calls. So each reference MUST be revoked
by exactly one call to libdax_msgs_destroy().
@param pt The pointer to be set and registered
@param m A pointer to the existing object
@param flag Bitfield for control purposes (unused yet, submit 0)
@return 1 for success, 0 for failure
*/
int libdax_msgs_refer(struct libdax_msgs **pt, struct libdax_msgs *o, int flag);
/** Submit a message to a message handling facility.
@param driveno program specific drive number. Use -1 if no number is known.
@param error_code Unique error code. Use only registered codes. See below.