here's what i'm trying to do:
i need a way for several threads to access several config files... i don't want to 
write my own parser
so i'm using Config::IniFiles. the code i wrote to attempt this follows...
the problem i'm having is i can't seem to share the object i'm creating w/ 
Config::IniFiles->new();
the _rlock _runlock is so any number of threads can read a value simultaneously, while 
protecting it during a write.
 
any ideas?
TIA,
jeremy
 
package SupportSite::Config;
use threads;
use threads::shared;
use Exporter;
use Error;
use Config::IniFiles;
 
our @ISA = qw(Exporter);
our @EXPORT = qw(ConfigOpenFile ConfigGetValue);
 
my %cfgFiles : shared;
 
sub _rlock
{
    my $ctxt = shift;
    lock $ctxt;
    cond_wait $ctxt while ($ctxt->{writers} > 0);
    ++$ctxt->{readers};
}
sub _runlock
{
    my $ctxt = shift;
    lock $ctxt;
    --$ctxt->{readers};
    cond_broadcast $ctxt;
}
sub _wlock
{
    my $ctxt = shift;
    lock $ctxt;
    cond_wait $ctxt while ($ctxt->{writers} > 0);
    ++$ctxt->{writers};
    cond_wait $ctxt while ($ctxt->{readers} > 0);
}
sub _wunlock
{
    my $ctxt = shift;
    lock $ctxt;
    --$ctxt->{writers};
    cond_broadcast $ctxt;
}
 
=head1 CONFIGURATION INTERFACE
 
=over 10
 
=item ConfigOpenFile ($file)
 
reads in config file pointed to by $file
returns $confCtxt
 
=cut
 
sub ConfigOpenFile
{
    lock %cfgFiles;
    my $file = shift;
    unless (defined $cfgFiles{$file}) {
print "BOB: opening file: '$file'\n";
use Data::Dumper;
        my $conf = Config::IniFiles->new (-file => $file);
        $conf = &share ($conf);
        my %conf : shared = (conf    => $conf,
                             readers => 0,
                             writers => 0);
print "BOB: ", Dumper $conf, "\n";
        $cfgFiles{$file} = \%conf;
        ## $cfgFiles{$file} = &share ({});
        ## $cfgFiles{$file}{conf} = Config::IniFiles->new (-file => $file);
        ## $cfgFiles{$file}{readers} = 0;
        ## $cfgFiles{$file}{writers} = 0;
        ## $cfgFiles{$file} = &share ({conf    => Config::IniFiles->new (-file => 
$file),
        ##                             readers => 0,
        ##                             writers => 0});
print "BOB: ", Dumper $cfgFiles{$file};
 
    }
    return $cfgFiles{$file};
}
 
=item ConfigGetValue ($confCtxt, $section, $key)
 
returns the value of the specified option
 
=cut
 
sub ConfigGetValue
{
    my $ctxt = shift;
    _rlock $ctxt;
    if (!defined $ctxt || !defined $ctxt->{conf}) {
        _runlock $ctxt;
        throw Error::Simple ("You must call ConfigOpenFile before calling 
ConfigGetValue");
    }
    my $section = shift;
    my $key = shift;
    my $value = $ctxt->{conf}->val ($section, $key);
    _runlock $ctxt;
    return $value;
}
 
1;
 
package SupportSite::Config::Error;
use base qw(Error::Simple);
1;
 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to