bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fwd: Mutex class, mailing list


From: David Sugar
Subject: fwd: Mutex class, mailing list
Date: Mon, 12 Aug 2002 13:55:53 -0400
User-agent: KMail/1.4.1

from address@hidden:

--W/nzBZO5zC0uMSeA
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi all!

I attached a Mutex class implementation that is capable of
being recursive and having a timeout. It would be nice if it
could be use in libcommoncpp. I'd release it under LGPL.

Further I'd like to be on the libcommoncpp mailing list, but
couldn't find a registration interface on the libcommoncpp home
page(s). Is there a mailing list at all, and if yes, how can
I join it??

ciao

Matthias

--W/nzBZO5zC0uMSeA
Content-Type: text/x-c++src; charset=us-ascii
Content-Disposition: attachment; filename="mutex_with_timeout.cpp"

#ifndef _XOPEN_SOURCE 
#define _XOPEN_SOURCE 600
#endif

#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

#include <list>

class Mutex
{
    private:
        typedef struct _recursive_cond
        {
                unsigned short   count;
                pthread_cond_t * cond;

                _recursive_cond() : count( 0u ), cond( NULL ) 
                {};

                bool
                operator==( const _recursive_cond & _rec_cond )
                {
                        return( cond == _rec_cond.cond );
                }
        } RecursiveCond;
        
        pthread_mutex_t _mutex;
        std::list<RecursiveCond> _lock_list;

    public:
        Mutex( void );
        ~Mutex( void );
        int Enter( short unsigned _ms_timeout,
                   pthread_cond_t * _cond );
        int Leave( pthread_cond_t * _cond );
};

Mutex::Mutex( void )
{
        pthread_mutexattr_t _attr;
        int retval;
        
        pthread_mutexattr_init( &_attr );

        if ( ( retval = pthread_mutexattr_settype( &_attr,
                                                   PTHREAD_MUTEX_RECURSIVE_NP 
) ) != 0 )
 {
  printf( "Mutex::Mutex: Setting of mutex attribute failed:\n" );
  printf( "\"%s\"\n", strerror( retval ) );
                throw( this );
 }

        if ( pthread_mutex_init( &_mutex, &_attr) != 0 )
        {
                throw( this );
        }
}

Mutex::~Mutex( void )
{
        pthread_mutex_destroy( &_mutex );
}

int
Mutex::Enter( short unsigned _ms_timeout,
              pthread_cond_t * _cond )
{
        int retval;
        RecursiveCond _member;

        if ( _ms_timeout > 1000 )
        {
                printf( "Mutex::Enter: Timeout too long: %u (milli 
seconds)\n",
                        _ms_timeout );
                return( -1 );
        }
        

       
        if ( ( retval = pthread_mutex_lock( &_mutex ) ) != 0 )
        {
                printf( "Mutex::Enter: couldn't lock own mutex!\n" );
                printf( "Mutex::Enter:\"%s\"\n", strerror( retval ) );
                return( -1 );
        }

        if ( _lock_list.empty() )
        {
                // mutex is not used

                _member.count = 1;
                _member.cond = _cond;
                _lock_list.push_back( _member );
                if ( ( retval = pthread_mutex_unlock( &_mutex ) ) != 0 )
                {
                        printf( "Mutex::Enter: Error in unlocking mutex\n" );
                        printf( "Mutex::Enter:\"%s\"\n", strerror( retval ) );
                        return( -1 );
                }
        }
        else if ( _lock_list.front().cond == _cond )
        {
                 // mutex is used by the same thread
                _lock_list.front().count++;
                if ( ( retval = pthread_mutex_unlock( &_mutex ) ) != 0 )
                {
                        printf( "Mutex::Enter: Error in unlocking mutex\n" );
                        printf( "Mutex::Enter:\"%s\"\n", strerror( retval ) );
                        return( -1 );
                }
        }
        else
        {
                struct timespec time_out;
                struct timeval current_time;
                
                errno = 0;
                if ( gettimeofday( &current_time, NULL ) != 0 )
                {
                        printf( "Mutex::Enter: Error while getting tim




reply via email to

[Prev in Thread] Current Thread [Next in Thread]