From 7fa631b4745f89957b8add63d502be3343181d1e Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Thu, 7 Jan 2010 09:17:43 +0100 Subject: [PATCH] back to old semantics for resource acquisition. only LEAVE has a block --- src/nobug.h | 342 ++++++++++++++++------------------ tests/test_nobug_resources_threaded.c | 4 +- 2 files changed, 167 insertions(+), 179 deletions(-) diff --git a/src/nobug.h b/src/nobug.h index 662733c..6baa709 100644 --- a/src/nobug.h +++ b/src/nobug.h @@ -1133,7 +1133,7 @@ nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, default) /* //resourcemacros PARA RESOURCE_ENTER; RESOURCE_ENTER; claim a resource -//resourcemacros RESOURCE_ENTER(flag, announced, user, state, handle){...} +//resourcemacros RESOURCE_ENTER(flag, announced, user, state, handle) //resourcemacros //resourcemacros Acquire a resource. //resourcemacros @@ -1150,61 +1150,36 @@ nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, default) //resourcemacros a `NOBUG_RESOURCE_HANDLE` which will be initialized to the //resourcemacros entering node //resourcemacros -//resourcemacros 'RESOURCE_ENTER()' acts like the head of a C loop statement, it ties to the following -//resourcemacros (block-) statement. Entering and the user defined following statement are atomic. -//resourcemacros This statement must not be left by break, return or any other kind of jump. NoBug does -//resourcemacros not assert this (for for Performance reasons). -//resourcemacros -//resourcemacros .How to use it -//resourcemacros [source,c] -//resourcemacros ---- -//resourcemacros // in the simplest form just terminate it with a semicolon, -//resourcemacros // no user statement is executed -//resourcemacros NOBUG_RESOURCE_ENTER(flag, resource, user, state, handle); -//resourcemacros -//resourcemacros // followed by a single statement -//resourcemacros NOBUG_RESOURCE_ENTER(flag, resource, user, state, handle) -//resourcemacros lock_my_resource(); -//resourcemacros -//resourcemacros // or a block statement -//resourcemacros NOBUG_RESOURCE_ENTER(flag, resource, user, state, handle) -//resourcemacros { -//resourcemacros lock_my_resource(); -//resourcemacros } -//resourcemacros ---- //resourcemacros */ #define NOBUG_RESOURCE_ENTER(flag, resource, user, state, handle) \ - for ( \ - int nobug_locked_ = ({ \ - NOBUG_REQUIRE(resource, "Announced resource handle not initialized"); \ - NOBUG_REQUIRE(!handle, "Resource handle already entered"); \ - NOBUG_RESOURCE_LOCK; \ - NOBUG_IF(NOBUG_RESOURCE_LOGGING, \ - NOBUG_LOG_(&NOBUG_FLAG(flag), NOBUG_RESOURCE_LOG_LEVEL, \ - NOBUG_LOCATION_INFO, "RESOURCE_ENTER", \ - "%s: %s@%p: %s: %s", \ - resource?resource->type:"", \ - resource?resource->hdr.name:"", \ - resource?resource->object_id:NULL, \ - user, \ - nobug_resource_states[state]);) \ - NOBUG_RESOURCE_ASSERT(handle = \ - nobug_resource_enter (resource, \ - user, state, \ - NOBUG_BASENAME(__FILE__ ":" NOBUG_STRINGIZE(__LINE__))), \ - "RESOURCE_ASSERT_ENTER", NOBUG_LOCATION_INFO, \ - "%s: %s@%p: %s: %s: %s", \ - resource?resource->type:"", \ - resource?resource->hdr.name:"", \ - resource?resource->object_id:NULL, \ - user, nobug_resource_states[state], \ - nobug_resource_error); \ - NOBUG_RESOURCE_UNLOCK; \ - 1; \ - }); \ - nobug_locked_; \ - nobug_locked_ = 0) + NOBUG_IF_ALPHA( \ + do { \ + NOBUG_REQUIRE(resource, "Announced resource handle not initialized"); \ + NOBUG_REQUIRE(!handle, "Resource handle already entered"); \ + NOBUG_RESOURCE_LOCK; \ + NOBUG_IF(NOBUG_RESOURCE_LOGGING, \ + NOBUG_LOG_(&NOBUG_FLAG(flag), NOBUG_RESOURCE_LOG_LEVEL, \ + NOBUG_LOCATION_INFO, "RESOURCE_ENTER", \ + "%s: %s@%p: %s: %s", \ + resource?resource->type:"", \ + resource?resource->hdr.name:"", \ + resource?resource->object_id:NULL, \ + user, \ + nobug_resource_states[state]);) \ + NOBUG_RESOURCE_ASSERT(handle = \ + nobug_resource_enter (resource, \ + user, state, \ + NOBUG_BASENAME(__FILE__ ":" NOBUG_STRINGIZE(__LINE__))), \ + "RESOURCE_ASSERT_ENTER", NOBUG_LOCATION_INFO, \ + "%s: %s@%p: %s: %s: %s", \ + resource?resource->type:"", \ + resource?resource->hdr.name:"", \ + resource?resource->object_id:NULL, \ + user, nobug_resource_states[state], \ + nobug_resource_error); \ + NOBUG_RESOURCE_UNLOCK; \ + } while(0)) //resourcemacros PARA RESOURCE_WAIT; RESOURCE_WAIT; wait for a resource to become available @@ -1215,14 +1190,13 @@ nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, default) //resourcemacros .How to use it //resourcemacros [source,c] //resourcemacros ---- -//resourcemacros RESOURCE_WAIT(flag, resource, user, handle) -//resourcemacros { -//resourcemacros if (lock_my_resource() == ERROR) -//resourcemacros NOBUG_RESOURCE_LEAVE(flag, handle); -//resourcemacros else -//resourcemacros RESOURCE_STATE(flag, NOBUG_RESOURCE_EXCLUSIVE, handle); -//resourcemacros } +//resourcemacros RESOURCE_WAIT(flag, resource, user, handle); +//resourcemacros if (lock_my_resource() == ERROR) +//resourcemacros NOBUG_RESOURCE_LEAVE(flag, handle); +//resourcemacros else +//resourcemacros RESOURCE_STATE(flag, NOBUG_RESOURCE_EXCLUSIVE, handle); //resourcemacros ---- +//resourcemacros #define NOBUG_RESOURCE_WAIT(flag, resource, user, handle) \ NOBUG_RESOURCE_ENTER(flag, resource, user, NOBUG_RESOURCE_WAITING, handle) @@ -1241,48 +1215,38 @@ nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, default) //resourcemacros `entered`:: //resourcemacros the handle set by `RESOURCE_ENTER` //resourcemacros -//resourcemacros 'RESOURCE_STATE()' acts like the head of a C loop statement, it ties to the following -//resourcemacros (block-) statement. Entering and the user defined following statement are atomic. -//resourcemacros This statement must not be left by break, return or any other kind of jump. NoBug does -//resourcemacros not assert this (for for Performance reasons). -//resourcemacros */ #define NOBUG_RESOURCE_STATE(flag, state, entered) \ NOBUG_RESOURCE_STATE_RAW(&NOBUG_FLAG(flag), state, entered) - -#define NOBUG_RESOURCE_STATE_RAW(flag, nstate, entered) \ - NOBUG_IF_ALPHA( \ - for ( \ - int nobug_locked_ = ({ \ - NOBUG_RESOURCE_LOCK; \ - NOBUG_IF(NOBUG_RESOURCE_LOGGING, \ - NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \ - "RESOURCE_STATE", "%s: %s@%p: %s: %s->%s", \ - entered?entered->current->resource->type:"", \ - entered?entered->current->resource->hdr.name:"", \ - entered?entered->current->resource->object_id:"", \ - entered?entered->hdr.name:"", \ - nobug_resource_states[entered?entered->state \ - :NOBUG_RESOURCE_INVALID], \ - nobug_resource_states[nstate]); \ - ) \ - NOBUG_RESOURCE_ASSERT(nobug_resource_state (entered, nstate), \ - "RESOURCE_ASSERT_STATE", NOBUG_LOCATION_INFO, \ - "%s: %s@%p: %s: %s->%s: %s", \ - entered?entered->current->resource->type:"", \ - entered?entered->current->resource->hdr.name:"", \ - entered?entered->current->resource->object_id:"", \ - entered?entered->hdr.name:"", \ - nobug_resource_states[entered?entered->state \ - :NOBUG_RESOURCE_INVALID], \ - nobug_resource_states[nstate], \ - nobug_resource_error); \ - NOBUG_RESOURCE_UNLOCK; \ - 1; \ - }); \ - nobug_locked_; \ - nobug_locked_ = 0)) +#define NOBUG_RESOURCE_STATE_RAW(flag, nstate, entered) \ + NOBUG_IF_ALPHA( \ + do { \ + NOBUG_RESOURCE_LOCK; \ + NOBUG_IF(NOBUG_RESOURCE_LOGGING, \ + NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \ + "RESOURCE_STATE", "%s: %s@%p: %s: %s->%s", \ + entered?entered->current->resource->type:"", \ + entered?entered->current->resource->hdr.name:"", \ + entered?entered->current->resource->object_id:"", \ + entered?entered->hdr.name:"", \ + nobug_resource_states[entered?entered->state \ + :NOBUG_RESOURCE_INVALID], \ + nobug_resource_states[nstate]); \ + ) \ + NOBUG_RESOURCE_ASSERT(nobug_resource_state (entered, nstate), \ + "RESOURCE_ASSERT_STATE", NOBUG_LOCATION_INFO, \ + "%s: %s@%p: %s: %s->%s: %s", \ + entered?entered->current->resource->type:"", \ + entered?entered->current->resource->hdr.name:"", \ + entered?entered->current->resource->object_id:"", \ + entered?entered->hdr.name:"", \ + nobug_resource_states[entered?entered->state \ + :NOBUG_RESOURCE_INVALID], \ + nobug_resource_states[nstate], \ + nobug_resource_error); \ + NOBUG_RESOURCE_UNLOCK; \ + } while (0)) /* @@ -1297,58 +1261,68 @@ nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, default) //resourcemacros the handle you got while entering the resource //resourcemacros //resourcemacros 'RESOURCE_LEAVE()' acts like the head of a C loop statement, it ties to the following -//resourcemacros (block-) statement. Entering and the user defined following statement are atomic. +//resourcemacros (block-) statement. Leaving and the user defined following statement are atomic. //resourcemacros This statement must not be left by break, return or any other kind of jump. NoBug does //resourcemacros not assert this (for for Performance reasons). //resourcemacros +//resourcemacros .How to use it +//resourcemacros [source,c] +//resourcemacros ---- +//resourcemacros NOBUG_RESOURCE_LEAVE(flag, handle) +//resourcemacros { +//resourcemacros unlock_my_resource(); +//resourcemacros } +//resourcemacros ---- +//resourcemacros */ #define NOBUG_RESOURCE_LEAVE(flag, handle) \ NOBUG_RESOURCE_LEAVE_RAW(&NOBUG_FLAG(flag), handle) -#define NOBUG_RESOURCE_LEAVE_RAW(flag, handle) \ - NOBUG_IF_ALPHA( \ - for ( \ - int nobug_locked_ = (NOBUG_RESOURCE_LOCK, 1); \ - nobug_locked_; \ - ({ \ - NOBUG_IF(NOBUG_RESOURCE_LOGGING, \ - NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \ - "RESOURCE_LEAVE", "%s: %s@%p: %s: %s", \ - handle?handle->current->resource->type:"", \ - handle?handle->current->resource->hdr.name:"", \ - handle?handle->current->resource->object_id:"", \ - handle?handle->hdr.name:"", \ - nobug_resource_states[handle?handle->state \ - :NOBUG_RESOURCE_INVALID]); \ - ) \ - NOBUG_RESOURCE_ASSERT(nobug_resource_leave (handle), \ - "RESOURCE_ASSERT_LEAVE", NOBUG_LOCATION_INFO, "%s: %s@%p: %s: %s: %s", \ - handle?handle->current->resource->type:"", \ - handle?handle->current->resource->hdr.name:"", \ - handle?handle->current->resource->object_id:"", \ - handle?handle->hdr.name:"", \ - nobug_resource_states[handle?handle->state \ - :NOBUG_RESOURCE_INVALID], \ - nobug_resource_error); \ - handle = NULL; \ - NOBUG_RESOURCE_UNLOCK; \ - nobug_locked_ = 0; \ - }))) +#define NOBUG_RESOURCE_LEAVE_RAW(flag, handle) \ + NOBUG_IF_ALPHA( \ + for ( \ + int nobug_locked_ = (NOBUG_RESOURCE_LOCK, 1); \ + nobug_locked_; \ + ({ \ + NOBUG_IF(NOBUG_RESOURCE_LOGGING, \ + NOBUG_LOG_(flag, NOBUG_RESOURCE_LOG_LEVEL, NOBUG_LOCATION_INFO, \ + "RESOURCE_LEAVE", "%s: %s@%p: %s: %s", \ + handle?handle->current->resource->type:"", \ + handle?handle->current->resource->hdr.name:"", \ + handle?handle->current->resource->object_id:"", \ + handle?handle->hdr.name:"", \ + nobug_resource_states[handle?handle->state \ + :NOBUG_RESOURCE_INVALID]); \ + ) \ + NOBUG_RESOURCE_ASSERT(nobug_resource_leave (handle), \ + "RESOURCE_ASSERT_LEAVE", NOBUG_LOCATION_INFO, \ + "%s: %s@%p: %s: %s: %s", \ + handle?handle->current->resource->type:"", \ + handle?handle->current->resource->hdr.name:"", \ + handle?handle->current->resource->object_id:"", \ + handle?handle->hdr.name:"", \ + nobug_resource_states[handle?handle->state \ + :NOBUG_RESOURCE_INVALID], \ + nobug_resource_error); \ + handle = NULL; \ + NOBUG_RESOURCE_UNLOCK; \ + nobug_locked_ = 0; \ + }))) /* assertion which dumps all resources */ #define NOBUG_RESOURCE_ASSERT(resource, what, location, ...) \ - NOBUG_WHEN (!(resource), \ - NOBUG_LOG_( &nobug_flag_NOBUG_ON, LOG_EMERG, \ - location, what, \ - ## __VA_ARGS__); \ - nobug_resource_dump_all (&(struct nobug_resource_dump_context) \ - {&nobug_flag_NOBUG_ON, \ - LOG_EMERG, \ - NOBUG_LOCATION_ARGS}); \ - NOBUG_BACKTRACE; \ - NOBUG_ABORT \ - ) + NOBUG_IF_ALPHA( \ + NOBUG_WHEN (!(resource), \ + NOBUG_LOG_( &nobug_flag_NOBUG_ON, LOG_EMERG, \ + location, what, \ + ## __VA_ARGS__); \ + nobug_resource_dump_all (&(struct nobug_resource_dump_context) \ + {&nobug_flag_NOBUG_ON, \ + LOG_EMERG, \ + NOBUG_LOCATION_ARGS}); \ + NOBUG_BACKTRACE; \ + NOBUG_ABORT)) /* @@ -1366,21 +1340,25 @@ nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, default) //resourcemacros handle of the resource to be dumped //resourcemacros */ -#define NOBUG_RESOURCE_DUMP(flag, handle) \ - do { NOBUG_RESOURCE_LOCK; \ - nobug_resource_dump (handle, &(struct nobug_resource_dump_context) \ - {&NOBUG_FLAG(flag), \ - NOBUG_RESOURCE_LOG_LEVEL, \ - NOBUG_LOCATION_ARGS}); \ - NOBUG_RESOURCE_UNLOCK; } while (0) +#define NOBUG_RESOURCE_DUMP(flag, handle) \ + NOBUG_IF_ALPHA( \ + do { \ + NOBUG_RESOURCE_LOCK; \ + nobug_resource_dump (handle, &(struct nobug_resource_dump_context) \ + {&NOBUG_FLAG(flag), \ + NOBUG_RESOURCE_LOG_LEVEL, \ + NOBUG_LOCATION_ARGS}); \ + NOBUG_RESOURCE_UNLOCK; \ + } while (0)) -#define NOBUG_RESOURCE_DUMP_IF(when, flag, handle) \ - NOBUG_WHEN(when, NOBUG_RESOURCE_LOCK; \ - nobug_resource_dump (handle, &(struct nobug_resource_dump_context) \ - {&NOBUG_FLAG(flag), \ - NOBUG_RESOURCE_LOG_LEVEL, \ - NOBUG_LOCATION_ARGS}); \ - NOBUG_RESOURCE_UNLOCK) +#define NOBUG_RESOURCE_DUMP_IF(when, flag, handle) \ + NOBUG_IF_ALPHA( \ + NOBUG_WHEN(when, NOBUG_RESOURCE_LOCK; \ + nobug_resource_dump (handle, &(struct nobug_resource_dump_context) \ + {&NOBUG_FLAG(flag), \ + NOBUG_RESOURCE_LOG_LEVEL, \ + NOBUG_LOCATION_ARGS}); \ + NOBUG_RESOURCE_UNLOCK)) /* @@ -1397,22 +1375,26 @@ nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, default) //resourcemacros */ #define NOBUG_RESOURCE_DUMPALL(flag) \ - do { NOBUG_RESOURCE_LOCK; \ - nobug_resource_dump_all (&(struct nobug_resource_dump_context) \ - {&NOBUG_FLAG(flag), \ - NOBUG_RESOURCE_LOG_LEVEL, \ - NOBUG_LOCATION_ARGS}); \ - NOBUG_RESOURCE_UNLOCK; } while (0) + NOBUG_IF_ALPHA( \ + do { \ + NOBUG_RESOURCE_LOCK; \ + nobug_resource_dump_all (&(struct nobug_resource_dump_context) \ + {&NOBUG_FLAG(flag), \ + NOBUG_RESOURCE_LOG_LEVEL, \ + NOBUG_LOCATION_ARGS}); \ + NOBUG_RESOURCE_UNLOCK; \ + } while (0)) #define NOBUG_RESOURCE_DUMPALL_IF(when, flag) \ - NOBUG_WHEN(when, NOBUG_RESOURCE_LOCK; \ - nobug_resource_dump_all (&(struct nobug_resource_dump_context) \ - {&NOBUG_FLAG(flag), \ - NOBUG_RESOURCE_LOG_LEVEL, \ - NOBUG_LOCATION_ARGS}); \ - NOBUG_RESOURCE_UNLOCK) - + NOBUG_IF_ALPHA( \ + NOBUG_WHEN(when, \ + NOBUG_RESOURCE_LOCK; \ + nobug_resource_dump_all (&(struct nobug_resource_dump_context) \ + {&NOBUG_FLAG(flag), \ + NOBUG_RESOURCE_LOG_LEVEL, \ + NOBUG_LOCATION_ARGS}); \ + NOBUG_RESOURCE_UNLOCK)) /* //resourcemacros PARA RESOURCE_LIST; RESOURCE_LIST; enumerate all registered resources @@ -1428,20 +1410,26 @@ nobug_env_init_flag (&NOBUG_FLAG(name), NOBUG_LOG_TARGET, default) //resourcemacros */ #define NOBUG_RESOURCE_LIST(flag) \ - do { NOBUG_RESOURCE_LOCK; \ - nobug_resource_list (&(struct nobug_resource_dump_context) \ + NOBUG_IF_ALPHA( \ + do { \ + NOBUG_RESOURCE_LOCK; \ + nobug_resource_list (&(struct nobug_resource_dump_context) \ {&NOBUG_FLAG(flag), \ - NOBUG_RESOURCE_LOG_LEVEL, \ - NOBUG_LOCATION_ARGS}); \ - NOBUG_RESOURCE_UNLOCK; } while (0) + NOBUG_RESOURCE_LOG_LEVEL, \ + NOBUG_LOCATION_ARGS}); \ + NOBUG_RESOURCE_UNLOCK; \ + } while (0)) + #define NOBUG_RESOURCE_LIST_IF(when, flag) \ - NOBUG_WHEN(when, NOBUG_RESOURCE_LOCK; \ - nobug_resource_list (&(struct nobug_resource_dump_context) \ - {&NOBUG_FLAG(flag), \ - NOBUG_RESOURCE_LOG_LEVEL, \ - NOBUG_LOCATION_ARGS}); \ - NOBUG_RESOURCE_UNLOCK) + NOBUG_IF_ALPHA( \ + NOBUG_WHEN(when, \ + NOBUG_RESOURCE_LOCK; \ + nobug_resource_list (&(struct nobug_resource_dump_context) \ + {&NOBUG_FLAG(flag), \ + NOBUG_RESOURCE_LOG_LEVEL, \ + NOBUG_LOCATION_ARGS}); \ + NOBUG_RESOURCE_UNLOCK)) /* diff --git a/tests/test_nobug_resources_threaded.c b/tests/test_nobug_resources_threaded.c index a3cd8a1..3ed0c7e 100644 --- a/tests/test_nobug_resources_threaded.c +++ b/tests/test_nobug_resources_threaded.c @@ -15,8 +15,8 @@ void* threadfn(void* nop) if (threadenter != NOBUG_RESOURCE_WAITING) { ECHO ("enter via wait"); - RESOURCE_WAIT(NOBUG_ON, t, "thread", e) - NOBUG_RESOURCE_STATE(NOBUG_ON, threadenter, e); + RESOURCE_WAIT(NOBUG_ON, t, "thread", e); + NOBUG_RESOURCE_STATE(NOBUG_ON, threadenter, e); } else { -- 2.11.4.GIT