Re: Help with apr mutex

2011-02-28 Thread Jim Jagielski
Why not check the return status of the lock and unlock calls??

On Feb 28, 2011, at 12:26 PM, Simone Caruso wrote:

 Hi all,
 
 I wrote a simple cache inside my module with apr_shm and apr_rmm, but I have 
 problems with apr mutex...
 
 I have this peace of code:
 
 static apr_global_mutex_t *mtx = NULL;
 static void module_translate_name(request_rec *r)
 {
   if(apr_global_mutex_trylock(mtx) == APR_EBUSY)
   ap_log_rerror(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r, 
 Memory Locked!! );
   apr_global_mutex_lock(mtx);
   element = get_from_cache();
   if(element == NULL){
   element = insert_into_cache();
   strcpy(element.name,name\0);
   }
   apr_global_mutex_unlock(mtx);
 }
 
 static void module_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t 
 *ptemp, server_rec *s)
 {
   result = apr_global_mutex_create(mtx, cache_entries_lck, 
 APR_LOCK_DEFAULT, p);
 }
 
 
 static void mod_vhost_ldap_child_init(apr_pool_t * p, server_rec * s)
 {
   apr_status_t ret;
   ret = apr_global_mutex_child_init(mtx, cache_entries_lck, p);
 }
 
 
 
 During my tests with 'ab -c 15 -n 3000' i NEVER get the Memory Locked error 
 in logs, plus i get duplicated entries in cache but I don't know why!
 
 Does someone have an idea? (please consider i use apr_global_mutex_trylock() 
 only for test)
 Regards.
 
 -- 
 Simone Caruso
 



Re: Help with apr mutex

2011-02-28 Thread thomas bonfort
a few things to try:

* you should use APR_STATUS_IS_EBUSY(s) instead of ... == APR_EBUSY
* after creating the mutex, you might have to set permissions on it:
#ifdef AP_NEED_SET_MUTEX_PERMS
   rc = unixd_set_global_mutex_perms(mutex);
   if(rc != APR_SUCCESS) {
  ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s, Could not set
permissions on global parent mutex %s, mutex_name);
  return rc;
   }
#endif

* I *think* that calling the lock functions twice from a same thread
(without unlocking first) can have undefined behavior, i.e. don't do
the lock() just after the trylock() call.

regards,
thomas



On Mon, Feb 28, 2011 at 18:26, Simone Caruso i...@simonecaruso.com wrote:
 Hi all,

 I wrote a simple cache inside my module with apr_shm and apr_rmm, but I have
 problems with apr mutex...

 I have this peace of code:

 static apr_global_mutex_t *mtx = NULL;
 static void module_translate_name(request_rec *r)
 {
        if(apr_global_mutex_trylock(mtx) == APR_EBUSY)
                ap_log_rerror(APLOG_MARK, APLOG_WARNING|APLOG_NOERRNO, 0, r,
 Memory Locked!! );
        apr_global_mutex_lock(mtx);
        element = get_from_cache();
        if(element == NULL){
                element = insert_into_cache();
                strcpy(element.name,name\0);
        }
        apr_global_mutex_unlock(mtx);
 }

 static void module_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t
 *ptemp, server_rec *s)
 {
        result = apr_global_mutex_create(mtx, cache_entries_lck,
 APR_LOCK_DEFAULT, p);
 }


 static void mod_vhost_ldap_child_init(apr_pool_t * p, server_rec * s)
 {
        apr_status_t ret;
        ret = apr_global_mutex_child_init(mtx, cache_entries_lck, p);
 }



 During my tests with 'ab -c 15 -n 3000' i NEVER get the Memory Locked
 error in logs, plus i get duplicated entries in cache but I don't know why!

 Does someone have an idea? (please consider i use apr_global_mutex_trylock()
 only for test)
 Regards.

 --
 Simone Caruso



Re: Help with apr mutex

2011-02-28 Thread Simone Caruso

On 28/02/2011 18:35, Jim Jagielski wrote:

Why not check the return status of the lock and unlock calls??


Ehm... i'm an idiot...

i create the lock in this way:
module_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, 
server_rec *s)
{
lock = ap_server_root_relative(p, mymod.lck);
result = apr_global_mutex_create(mtx, lock, APR_LOCK_DEFAULT, p);
if(result != APR_SUCCESS)
...
}

apr_global_mutex_create returns APR_SUCCESS, but apr_global_mutex_lock returns 
Permission denied,
maybe the lock is created wrong permissions ??

--
Simone Caruso
IT Consultant


Re: Help with apr mutex

2011-02-28 Thread Simone Caruso

On 28/02/2011 18:53, thomas bonfort wrote:

a few things to try:

* you should use APR_STATUS_IS_EBUSY(s) instead of ... == APR_EBUSY
* after creating the mutex, you might have to set permissions on it:
#ifdef AP_NEED_SET_MUTEX_PERMS
rc = unixd_set_global_mutex_perms(mutex);
if(rc != APR_SUCCESS) {
   ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s, Could not set
permissions on global parent mutex %s, mutex_name);
   return rc;
}
#endif

* I *think* that calling the lock functions twice from a same thread
(without unlocking first) can have undefined behavior, i.e. don't do
the lock() just after the trylock() call.

regards,
thomas


Thanks Thoms,
i'm going to try your code!

--
Simone Caruso
IT Consultant
+39 349 65 90 805
p.iva: 03045250838


Re: Help with apr mutex

2011-02-28 Thread Ben Noordhuis
On Mon, Feb 28, 2011 at 18:26, Simone Caruso i...@simonecaruso.com wrote:
 I wrote a simple cache inside my module with apr_shm and apr_rmm

Simone, have a look at ap_socache.h and ap_slotmem.h, they're two
simple cache facilities that were added in 2.3.0. Might save you some
work. :)