Ok, used your code, and created some paired functions in OWLIB:
// ow_api.c
int  OWLIB_can_init_start( void ) ;
void OWLIB_can_init_end(   void ) ;
int  OWLIB_can_access_start( void ) ;
void OWLIB_can_access_end(   void ) ;
int  OWLIB_can_finish_start( void ) ;
void OWLIB_can_finish_end(   void ) ;

The gist of this is that all the mutexes and conditions variables are hidden 
from the C api, and also they can be used by other programs.

Paul

The implementation is in modules/owlib/src/c/ow_api.c and looks like this:
/* code for SWIG and owcapi safe access */
/* Uses two mutexes, one for indevices, showing of there are valid 1-wire 
adapters */
/* Second is paired with a condition variable to prevent "finish" when a "get" 
or "put" is in progress */
/* Thanks to Geo Carncross for the implementation */

#include "owfs_config.h"
#include "ow.h"

/* ------- Globals ----------- */
#ifdef OW_MT
    pthread_mutex_t init_mutex    = PTHREAD_MUTEX_INITIALIZER ;
    pthread_mutex_t access_mutex  = PTHREAD_MUTEX_INITIALIZER ;
    pthread_cond_t  access_cond   = PTHREAD_COND_INITIALIZER  ;
    #define INITLOCK       pthread_mutex_lock(    &init_mutex   )
    #define INITUNLOCK     pthread_mutex_unlock(  &init_mutex   )
    #define ACCESSLOCK     pthread_mutex_lock(  &access_mutex   )
    #define ACCESSUNLOCK   pthread_mutex_unlock(&access_mutex   )
    #define ACCESSWAIT     pthread_cond_wait(    &access_cond, &access_mutex )
    #define ACCESSSIGNAL   pthread_cond_signal(  &access_cond )
#else /* OW_MT */
    #define INITLOCK
    #define INITUNLOCK
    #define ACCESSLOCK
    #define ACCESSUNLOCK
    #define ACCESSWAIT
    #define ACCESSSIGNAL
#endif /* OW_MT */

int  access_num = 0 ;

/* Returns 0 if ok, else 1 */
/* MUST BE PAIRED with OWLIB_can_init_end() */
int OWLIB_can_init_start( void ) {
#ifdef OW_MT
  #ifdef __UCLIBC__
    if ( INITLOCK == EINVAL ) { /* Not initialized */
        pthread_mutex_init(&init_mutex, pmattr);
        pthread_mutex_init(&access_mutex, pmattr);
        INITLOCK ;
    }
  #else /* UCLIBC */
    INITLOCK ;
  #endif /* UCLIBC */
#endif /* OW_MT */
  return indevices > 0 ;
}
    
void OWLIB_can_init_end( void ) {
    INITUNLOCK ;
}

/* Returns 0 if ok, else 1 */
/* MUST BE PAIRED with OWLIB_can_access_end() */
int OWLIB_can_access_start( void ) {
    int ret ;
    ACCESSLOCK ;
        access_num++ ;    
    ACCESSUNLOCK ;
    INITLOCK ;
        ret = (indevices == 0) ;
    INITUNLOCK ;
    return ret ;
}
        
void OWLIB_can_access_end( void ) {
    ACCESSLOCK ;
        access_num-- ;
        ACCESSSIGNAL ;
    ACCESSUNLOCK ;
}

/* Returns 0 always */
/* MUST BE PAIRED with OWLIB_can_finish_end() */
int OWLIB_can_finish_start( void ) {
    ACCESSLOCK ;
    while ( access_num > 0 ) ACCESSWAIT;
    INITLOCK ;
    return 0 ; /* just for symetry */
}
void OWLIB_can_finish_end( void ) {
    INITUNLOCK ;
    ACCESSUNLOCK ;
}


On Monday 17 October 2005 10:24 am, Geo Carncross wrote:
> A mutual exclusion device is necessary.
>
> OW_get, OW_put, and OW_finish all lock the common mutex.
>
> If OW_get and OW_put /really can/ be used simultaneously, then a
> condition is required:
>
>
> in OW_get and OW_put:
>
> LOCK(mutex);
> level++;
> UNLOCK(mutex);
> { real code }
> LOCK(mutex);
> level--;
> SIGNAL(cond);
> UNLOCK(mutex);
>
>
> and in OW_finish:
>
> LOCK(mutex);
> while (level > 0) COND(cond,mutex);
> { destroy interface safely now }
> UNLOCK(mutex);
>
>
> in pthreads this is:
>
> pthread_cond_t cond;
> pthread_mutex_t mutex;
>
> #define COND(q,p) pthread_cond_wait(&q,&p)
> #define SIGNAL(q) pthread_cond_signal(&q)
> #define LOCK(q) pthread_mutex_lock(&q)
> #define UNLOCK(q) pthread_mutex_unlock(&q)
>
>
> NOTE: this is NOT the same mutex as you're using for acessing indev
> (capi_mutex) - but a different one JUST for this cond variable.
>
>
> BY THE WAY, shouldn't these things be static? Is there really a reason
> user programs should have access to it?
>
> On Sun, 2005-10-16 at 22:57 -0400, Paul Alfille wrote:
> > Added the same protection for the SWIG prototype (owperl, owpython,
> > owphp).
> >
> > It occurs to me that it is possible that a "OW_finish" could be called
> > while an OW_get or OW_put is in progress -- could be messy since the
> > adapters would be disappearing while being used. I'll have to get out the
> > old textbook to figure the proper flag for this one -- a semaphore?
> >
> > Paul
> >
> > On Sunday 16 October 2005 01:33 am, Paul Alfille wrote:
> > > Ok uploaded some OWCAPI changes.
> > >
> > > 1. I've put in your char** mode. I'm really starting to warm up to it.
> > >
> > > 2. Protection against multiple "init"s, no "init"s, simultaneous
> > > "init"s...
> > >
> > > Paul
> >
> > -------------------------------------------------------
> > This SF.Net email is sponsored by:
> > Power Architecture Resource Center: Free content, downloads, discussions,
> > and more. http://solutions.newsforge.com/ibmarch.tmpl
> > _______________________________________________
> > Owfs-developers mailing list
> > Owfs-developers@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/owfs-developers


-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
Owfs-developers mailing list
Owfs-developers@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/owfs-developers

Reply via email to