From 1091f108785a369c2db745f5980aed7ac27498b2 Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Sat, 9 Jan 2010 00:21:41 +0100 Subject: [PATCH] RESOURCE_ASSERT_STATE checks for asserting that a resource is in a given state --- src/nobug.h | 43 +++++++++++++++++++++++++++++++++-- src/nobug_resources.c | 25 ++++++++++++++++++++ tests/35resourcethreaded.tests | 22 +++++++++++++++++- tests/test_nobug_resources_threaded.c | 25 ++++++++++++++++++++ 4 files changed, 112 insertions(+), 3 deletions(-) diff --git a/src/nobug.h b/src/nobug.h index c5fedeb..fcb586e 100644 --- a/src/nobug.h +++ b/src/nobug.h @@ -1310,10 +1310,41 @@ nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, default) }))) +/* +//resourcemacros PARA RESOURCE_ASSERT_STATE; RESOURCE_ASSERT_STATE; assert the state of a resource +//resourcemacros RESOURCE_ASSERT_STATE(resource, state) +//resourcemacros RESOURCE_ASSERT_STATE_IF(when, resource, state) +//resourcemacros +//resourcemacros Assert that we have a resource in a given state. For multithreaded programms the topmost +//resourcemacros state of the calling thread is checked, for non threadeded programs the most recent state on +//resourcemacros resource is used. +//resourcemacros +//resourcemacros `when`:: +//resourcemacros Condition which must be true for testing the assertion +//resourcemacros `resource`:: +//resourcemacros Resource handle +//resourcemacros `state`:: +//resourcemacros The expected state +//resourcemacros +*/ +#define NOBUG_RESOURCE_ASSERT_STATE(resource, state) \ + do { \ + enum nobug_resource_state mystate = nobug_resource_mystate (resource); \ + NOBUG_RESOURCE_ASSERT(mystate == state, \ + "RESOURCE_ASSERT_STATE", NOBUG_LOCATION_INFO, \ + "resource %p has state %s but %s was expected", \ + resource, nobug_resource_states[mystate], nobug_resource_states[state]); \ + } while (0) + + +#define NOBUG_RESOURCE_ASSERT_STATE_IF(when, resource, state) \ + NOBUG_IF_ALPHA (NOBUG_WHEN(when, NOBUG_RESOURCE_ASSERT_STATE (resource, state)) + + /* assertion which dumps all resources */ -#define NOBUG_RESOURCE_ASSERT(resource, what, location, ...) \ +#define NOBUG_RESOURCE_ASSERT(expr, what, location, ...) \ NOBUG_IF_ALPHA( \ - NOBUG_WHEN (!(resource), \ + NOBUG_WHEN (!(expr), \ NOBUG_LOG_( &nobug_flag_NOBUG_ON, LOG_EMERG, \ location, what, \ ## __VA_ARGS__); \ @@ -1835,6 +1866,12 @@ NOBUG_IF(NOBUG_USE_VALGRIND, \ #ifndef RESOURCE_USER #define RESOURCE_USER NOBUG_RESOURCE_USER #endif +#ifndef RESOURCE_ASSERT_STATE +#define RESOURCE_ASSERT_STATE NOBUG_RESOURCE_ASSERT_STATE +#endif +#ifndef RESOURCE_ASSERT_STATE_IF +#define RESOURCE_ASSERT_STATE_IF NOBUG_RESOURCE_ASSERT_STATE_IF +#endif #ifndef RESOURCE_USER_INIT #define RESOURCE_USER_INIT NOBUG_RESOURCE_USER_INIT #endif @@ -2267,6 +2304,8 @@ struct nobug_resource_dump_context const char* func; }; +enum nobug_resource_state +nobug_resource_mystate (struct nobug_resource_record* res); void nobug_resource_dump (struct nobug_resource_record* resource, struct nobug_resource_dump_context* context); diff --git a/src/nobug_resources.c b/src/nobug_resources.c index fe0132e..90f7e53 100644 --- a/src/nobug_resources.c +++ b/src/nobug_resources.c @@ -746,6 +746,31 @@ nobug_resource_state (struct nobug_resource_user* user, } +enum nobug_resource_state +nobug_resource_mystate (struct nobug_resource_record* res) +{ + enum nobug_resource_state ret = NOBUG_RESOURCE_INVALID; + NOBUG_RESOURCE_LOCK; +#if NOBUG_USE_PTHREAD + struct nobug_tls_data* iam = nobug_thread_get (); +#endif + + LLIST_FOREACH_REV (&res->users, u) + { + struct nobug_resource_user* user = (struct nobug_resource_user*) u; +#if NOBUG_USE_PTHREAD + if (user->thread == iam) + ret = user->state; +#else + ret = user->state; +#endif + } + NOBUG_RESOURCE_UNLOCK; + + return ret; +} + + void nobug_resource_dump (struct nobug_resource_record* resource, struct nobug_resource_dump_context* context) { diff --git a/tests/35resourcethreaded.tests b/tests/35resourcethreaded.tests index bde75ec..59e55f9 100644 --- a/tests/35resourcethreaded.tests +++ b/tests/35resourcethreaded.tests @@ -2,6 +2,26 @@ TESTING "resource tracker, w/ threads" ./test_nobug_resources_threaded_mt_alpha +TEST "resource threaded, state assert, waiting ok" resourcethreaded_assert_state_waiting_ok <