diff options
author | Yann Ylavic <ylavic@apache.org> | 2015-03-19 23:38:38 +0000 |
---|---|---|
committer | Yann Ylavic <ylavic@apache.org> | 2015-03-19 23:38:38 +0000 |
commit | 90c4770899c4c45288e1cd1cb0277324a7d3bdbf (patch) | |
tree | db6a0ab5bc8d6b81c56cd429c0b9bda4383c15ec /test/testlockperf.c | |
parent | c94867c4cd3211541e22b5a293ee96398c886e9c (diff) | |
download | apr-90c4770899c4c45288e1cd1cb0277324a7d3bdbf.tar.gz |
locks: introduce apr_{thread,proc,global}_mutex_timedlock().
For proc mutexes, the new mechanism APR_LOCK_DEFAULT_TIMED usable at creation time
allows for the best mechanism to be elected (unixes: 1 to 3, or specific: 4 to 7):
1. PROC_PTHREAD if pthread_mutex_timedlock() and pthread_mutex_set_robust_np()
are both available,
2. SYSV if semtimedop() is availale,
3. POSIXSEM if sem_timedwait() is available,
4. BeOS' acquire_sem_etc() if available,
5. NetWare falls back to apr_thread_mutex_timedlock() as for others functions,
6. OS2's DosRequestMutexSem(),
7. Windows' WaitForSingleObject().
Otherwise (like when fcntl and flock only are availble, if that's ever possible),
APR_ENOTIMPL is returned.
For thread mutexes, the new flag APR_THREAD_MUTEX_TIMED, usable at create()
time still, allows to switch to an implementation using a condition variable
and apr_thread_cond_timedwait() when if no native mechanism is available (eg.
NetWare, pthreads but without pthread_mutex_timedlock() available).
On windows, this initializes a WaitForSingleObject()able handle (Mutex) instead
of the fastest (but not timeout-able) CRITICAL_SECTION used by default.
All apr_{thread,proc,global}_mutex_timedlock() functions can take a relative or
absolute time, thanks to the last (boolean) argument.
Test suite updated accordingly.
git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1667900 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test/testlockperf.c')
-rw-r--r-- | test/testlockperf.c | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/test/testlockperf.c b/test/testlockperf.c index 6cca99d16..39fc256a1 100644 --- a/test/testlockperf.c +++ b/test/testlockperf.c @@ -60,7 +60,12 @@ void * APR_THREAD_FUNC thread_mutex_func(apr_thread_t *thd, void *data) int i; for (i = 0; i < max_counter; i++) { - apr_thread_mutex_lock(thread_lock); + if (data) { + apr_thread_mutex_timedlock(thread_lock, *(apr_time_t *)data, 0); + } + else { + apr_thread_mutex_lock(thread_lock); + } mutex_counter++; apr_thread_mutex_unlock(thread_lock); } @@ -175,6 +180,57 @@ int test_thread_mutex_nested(int num_threads) return APR_SUCCESS; } +int test_thread_mutex_timed(int num_threads) +{ + apr_thread_t *t[MAX_THREADS]; + apr_status_t s[MAX_THREADS]; + apr_time_t time_start, time_stop; + apr_time_t timeout; + int i; + + mutex_counter = 0; + + timeout = apr_time_from_sec(5); + + printf("apr_thread_mutex_t Tests\n"); + printf("%-60s", " Initializing the apr_thread_mutex_t (TIMED)"); + s[0] = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_TIMED, pool); + if (s[0] != APR_SUCCESS) { + printf("Failed!\n"); + return s[0]; + } + printf("OK\n"); + + apr_thread_mutex_lock(thread_lock); + /* set_concurrency(4)? -aaron */ + printf(" Starting %d threads ", num_threads); + for (i = 0; i < num_threads; ++i) { + s[i] = apr_thread_create(&t[i], NULL, thread_mutex_func, &timeout, pool); + if (s[i] != APR_SUCCESS) { + printf("Failed!\n"); + return s[i]; + } + } + printf("OK\n"); + + time_start = apr_time_now(); + apr_thread_mutex_unlock(thread_lock); + + /* printf("%-60s", " Waiting for threads to exit"); */ + for (i = 0; i < num_threads; ++i) { + apr_thread_join(&s[i], t[i]); + } + /* printf("OK\n"); */ + + time_stop = apr_time_now(); + printf("microseconds: %" APR_INT64_T_FMT " usec\n", + (time_stop - time_start)); + if (mutex_counter != max_counter * num_threads) + printf("error: counter = %ld\n", mutex_counter); + + return APR_SUCCESS; +} + int test_thread_rwlock(int num_threads) { apr_thread_t *t[MAX_THREADS]; @@ -273,6 +329,12 @@ int main(int argc, const char * const *argv) exit(-4); } + if ((rv = test_thread_mutex_timed(i)) != APR_SUCCESS) { + fprintf(stderr,"thread_mutex (TIMED) test failed : [%d] %s\n", + rv, apr_strerror(rv, (char*)errmsg, 200)); + exit(-5); + } + if ((rv = test_thread_rwlock(i)) != APR_SUCCESS) { fprintf(stderr,"thread_rwlock test failed : [%d] %s\n", rv, apr_strerror(rv, (char*)errmsg, 200)); |