Added a condvar implementation that works with rwlock_t

This commit is contained in:
Tobias Brunner 2012-09-20 17:56:20 +02:00
parent 4134108c77
commit 60dc44648f
3 changed files with 220 additions and 4 deletions

View File

@ -60,10 +60,10 @@ processing/jobs/job.h processing/jobs/callback_job.h processing/processor.h \
processing/scheduler.h selectors/traffic_selector.h \
threading/thread.h threading/thread_value.h \
threading/mutex.h threading/condvar.h threading/spinlock.h threading/semaphore.h \
threading/rwlock.h threading/lock_profiler.h utils.h utils/host.h \
utils/packet.h utils/identification.h utils/lexparser.h utils/linked_list.h \
utils/blocking_queue.h utils/hashtable.h utils/enumerator.h utils/optionsfrom.h \
utils/capabilities.h utils/backtrace.h utils/tun_device.h
threading/rwlock.h threading/rwlock_condvar.h threading/lock_profiler.h \
utils.h utils/host.h utils/packet.h utils/identification.h utils/lexparser.h \
utils/linked_list.h utils/blocking_queue.h utils/hashtable.h utils/enumerator.h \
utils/optionsfrom.h utils/capabilities.h utils/backtrace.h utils/tun_device.h
endif
library.lo : $(top_builddir)/config.status

View File

@ -21,11 +21,13 @@
#include <debug.h>
#include "rwlock.h"
#include "rwlock_condvar.h"
#include "condvar.h"
#include "mutex.h"
#include "lock_profiler.h"
typedef struct private_rwlock_t private_rwlock_t;
typedef struct private_rwlock_condvar_t private_rwlock_condvar_t;
/**
* private data of rwlock
@ -84,6 +86,27 @@ struct private_rwlock_t {
lock_profile_t profile;
};
/**
* private data of condvar
*/
struct private_rwlock_condvar_t {
/**
* public interface
*/
rwlock_condvar_t public;
/**
* mutex used to implement rwlock condvar
*/
mutex_t *mutex;
/**
* regular condvar to implement rwlock condvar
*/
condvar_t *condvar;
};
#ifdef HAVE_PTHREAD_RWLOCK_INIT
@ -357,3 +380,106 @@ rwlock_t *rwlock_create(rwlock_type_t type)
#endif /* HAVE_PTHREAD_RWLOCK_INIT */
METHOD(rwlock_condvar_t, wait_, void,
private_rwlock_condvar_t *this, private_rwlock_t *lock)
{
/* at this point we have the write lock locked, to make signals more
* predictable we try to prevent other threads from signaling by acquiring
* the mutex while we still hold the write lock (this assumes they will
* hold the write lock themselves when signaling, which is not mandatory) */
this->mutex->lock(this->mutex);
/* unlock the rwlock and wait for a signal */
lock->public.unlock(&lock->public);
this->condvar->wait(this->condvar, this->mutex);
/* we release the mutex to allow other threads into the condvar (might even
* be required so we can acquire the lock again below) */
this->mutex->unlock(this->mutex);
/* finally we reacquire the lock we held previously */
lock->public.write_lock(&lock->public);
}
METHOD(rwlock_condvar_t, timed_wait_abs, bool,
private_rwlock_condvar_t *this, private_rwlock_t *lock, timeval_t time)
{
bool timed_out;
/* see wait() above for details on what is going on here */
this->mutex->lock(this->mutex);
lock->public.unlock(&lock->public);
timed_out = this->condvar->timed_wait_abs(this->condvar, this->mutex, time);
this->mutex->unlock(this->mutex);
if (!timed_out)
{
lock->public.write_lock(&lock->public);
}
return timed_out;
}
METHOD(rwlock_condvar_t, timed_wait, bool,
private_rwlock_condvar_t *this, private_rwlock_t *lock, u_int timeout)
{
timeval_t tv;
u_int s, ms;
time_monotonic(&tv);
s = timeout / 1000;
ms = timeout % 1000;
tv.tv_sec += s;
tv.tv_usec += ms * 1000;
if (tv.tv_usec > 1000000 /* 1s */)
{
tv.tv_usec -= 1000000;
tv.tv_sec++;
}
return timed_wait_abs(this, lock, tv);
}
METHOD(rwlock_condvar_t, signal_, void,
private_rwlock_condvar_t *this)
{
this->mutex->lock(this->mutex);
this->condvar->signal(this->condvar);
this->mutex->unlock(this->mutex);
}
METHOD(rwlock_condvar_t, broadcast, void,
private_rwlock_condvar_t *this)
{
this->mutex->lock(this->mutex);
this->condvar->broadcast(this->condvar);
this->mutex->unlock(this->mutex);
}
METHOD(rwlock_condvar_t, condvar_destroy, void,
private_rwlock_condvar_t *this)
{
this->condvar->destroy(this->condvar);
this->mutex->destroy(this->mutex);
free(this);
}
/*
* see header file
*/
rwlock_condvar_t *rwlock_condvar_create()
{
private_rwlock_condvar_t *this;
INIT(this,
.public = {
.wait = (void*)_wait_,
.timed_wait = (void*)_timed_wait,
.timed_wait_abs = (void*)_timed_wait_abs,
.signal = _signal_,
.broadcast = _broadcast,
.destroy = _condvar_destroy,
},
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
.condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
);
return &this->public;
}

View File

@ -0,0 +1,90 @@
/*
* Copyright (C) 2012 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
/**
* @defgroup rwlock_condvar rwlock_condvar
* @{ @ingroup threading
*/
#ifndef RWLOCK_CONDVAR_H_
#define RWLOCK_CONDVAR_H_
typedef struct rwlock_condvar_t rwlock_condvar_t;
#include "rwlock.h"
/**
* A special condvar implementation that can be used in conjunction
* with rwlock_t (the write lock to be precise).
*
* @note The implementation does not verify that the current thread actually
* holds the write lock and not the read lock, so watch out.
*/
struct rwlock_condvar_t {
/**
* Wait on a condvar until it gets signalized.
*
* @param lock lock to release while waiting (write lock)
*/
void (*wait)(rwlock_condvar_t *this, rwlock_t *lock);
/**
* Wait on a condvar until it gets signalized, or times out.
*
* @param lock lock to release while waiting (write lock)
* @param timeout timeout im ms
* @return TRUE if timed out, FALSE otherwise
*/
bool (*timed_wait)(rwlock_condvar_t *this, rwlock_t *lock, u_int timeout);
/**
* Wait on a condvar until it gets signalized, or times out.
*
* The passed timeval should be calculated based on the time_monotonic()
* function.
*
* @param lock lock to release while waiting (write lock)
* @param tv absolute time until timeout
* @return TRUE if timed out, FALSE otherwise
*/
bool (*timed_wait_abs)(rwlock_condvar_t *this, rwlock_t *lock,
timeval_t tv);
/**
* Wake up a single thread in a condvar.
*/
void (*signal)(rwlock_condvar_t *this);
/**
* Wake up all threads in a condvar.
*/
void (*broadcast)(rwlock_condvar_t *this);
/**
* Destroy a condvar and free its resources.
*/
void (*destroy)(rwlock_condvar_t *this);
};
/**
* Create a condvar instance.
*
* @return condvar instance
*/
rwlock_condvar_t *rwlock_condvar_create();
#endif /** RWLOCK_CONDVAR_H_ @} */