apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mathihalli, Madhusudan" <mad...@hp.com>
Subject global_rwlock functions.
Date Thu, 29 Apr 2004 17:41:58 GMT
Hi,
	Would it be useful to extend the rwlocks to the global level also ? I was thinking of having
something like below. Any comments ?

-Madhu

struct apr_global_rwlock_t {
    apr_pool_t *pool;
    char *fname;
    pthread_rwlock_t *rwlock;
    apr_shm_t *pthread_shmid;
};


static apr_status_t global_rwlock_cleanup(void *data)
{
    apr_global_rwlock_t *rwlock = (apr_global_rwlock_t *)data;
    apr_status_t stat;

    pthread_rwlock_unlock(rwlock->rwlock);
    stat = pthread_rwlock_destroy(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (stat) {
        stat = errno;
    }
#endif
    return stat;
}
APR_DECLARE(apr_status_t) apr_global_rwlock_create(apr_global_rwlock_t **rwlock,
                                                   const char *fname,
                                                   apr_pool_t *pool)
{
    apr_status_t rv;
    pthread_rwlockattr_t rwattr;
    apr_global_rwlock_t *new_rwlock;

    new_rwlock = (apr_global_rwlock_t *)apr_pcalloc(pool,
                                                  sizeof(apr_global_rwlock_t));
    if (new_rwlock == NULL) {
        return APR_ENOMEM;
    }

    new_rwlock->pool = pool;
    new_rwlock->fname = apr_pstrdup(pool, fname);

#if APR_HAS_SHARED_MEMORY
    if ((rv = apr_shm_create(&new_rwlock->pthread_shmid,
                             sizeof(pthread_rwlock_t),
                             fname, new_rwlock->pool)) != APR_SUCCESS) {
        return rv;
    }

    new_rwlock->rwlock = apr_shm_baseaddr_get(new_rwlock->pthread_shmid);
    if (new_rwlock->rwlock == (pthread_rwlock_t *) (caddr_t)NULL) {
        return rv;
    }

#elif APR_HAS_MMAP
    char some_buffer[100];

    fd = open(fname, O_RDWR|O_CREAT, 0600);
    if (fd < 0) {
        return errno;
    }
    write(fd, some_buffer, 100);

    new_rwlock->rwlock = (pthread_rwlock_t *)mmap(
                                       (caddr_t) 0, sizeof(pthread_rwlock_t),
                                       PROT_READ | PROT_WRITE, MAP_SHARED,
                                       fd, 0);
    if (new_rwlock->rwlock == (pthread_rwlock_t *) (caddr_t) -1) {
        return errno;
    }
    close(fd);

#else
    new_rwlock->pool = pool;
    new_rwlock->rwlock = (pthread_rwlock_t *)apr_palloc(pool,
                                                     sizeof(pthread_rwlock_t));

    if (new_rwlock->rwlock == NULL) {
        return APR_ENOMEM;
    }
#endif

    if ((rv = pthread_rwlockattr_init(&mattr))) {
#ifdef PTHREAD_SETS_ERRNO
        rv = errno;
#endif
        global_rwlock_cleanup(new_rwlock);
        return rv;
    }
    if ((rv = pthread_rwlockattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED))) {
#ifdef PTHREAD_SETS_ERRNO
        rv = errno;
#endif
        global_rwlock_cleanup(new_rwlock);
        return rv;
    }

    if ((rv = pthread_rwlock_init(new_rwlock->rwlock, NULL))) {
#ifdef PTHREAD_SETS_ERRNO
        rv = errno;
#endif
        global_rwlock_cleanup(new_rwlock);
        return rv;
    }

    if ((rv = pthread_rwlockattr_destroy(&rwattr))) {
#ifdef PTHREAD_SETS_ERRNO
        rv = errno;
#endif
        global_rwlock_cleanup(new_rwlock);
        return rv;
    }

    apr_pool_cleanup_register(new_rwlock->pool,
                              (void *)new_rwlock, global_rwlock_cleanup,
                              apr_pool_cleanup_null);

    *rwlock = new_rwlock;
    return APR_SUCCESS;
}

APR_DECLARE(apr_status_t) apr_global_rwlock_rdlock(apr_global_rwlock_t *rwlock)
{
    apr_status_t rv;

    rv = pthread_rwlock_rdlock(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (rv) {
        rv = errno;
    }
#endif
    return rv;
}

APR_DECLARE(apr_status_t) apr_global_rwlock_tryrdlock(apr_global_rwlock_t *rwlock)
{
    apr_status_t rv;

    rv = pthread_rwlock_tryrdlock(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (rv) {
        rv = errno;
    }
#endif
    /* Normalize the return code. */
    if (rv == EBUSY)
        rv = APR_EBUSY;
    return rv;
}

APR_DECLARE(apr_status_t) apr_global_rwlock_wrlock(apr_global_rwlock_t *rwlock)
{
    apr_status_t rv;

    rv = pthread_rwlock_wrlock(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (rv) {
        rv = errno;
    }
#endif
    return rv;
}

APR_DECLARE(apr_status_t) apr_global_rwlock_trywrlock(apr_global_rwlock_t *rwlock)
{
    apr_status_t rv;

    rv = pthread_rwlock_trywrlock(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (rv) {
        rv = errno;
    }
#endif
    /* Normalize the return code. */
    if (rv == EBUSY)
        rv = APR_EBUSY;
    return rv;
}

APR_DECLARE(apr_status_t) apr_global_rwlock_unlock(apr_global_rwlock_t *rwlock)
{
    apr_status_t rv;

    rv = pthread_rwlock_unlock(rwlock->rwlock);
#ifdef PTHREAD_SETS_ERRNO
    if (rv) {
        rv = errno;
    }
#endif
    return rv;
}

APR_DECLARE(apr_status_t) apr_global_rwlock_destroy(apr_global_rwlock_t *rwlock)
{
    apr_status_t rv;
    if ((rv = global_rwlock_cleanup(rwlock)) == APR_SUCCESS) {
        apr_pool_cleanup_kill(rwlock->pool, rwlock, global_rwlock_cleanup);
        return APR_SUCCESS;
    }
    return rv;
}

Mime
View raw message