r462 - in trunk/code/src/gsl: . common resources/timer resources/timer/test
Author: slang Date: 2009-04-29 21:09:41 -0500 (Wed, 29 Apr 2009) New Revision: 462 Added: trunk/code/src/gsl/resources/timer/test/ trunk/code/src/gsl/resources/timer/test/Makefile trunk/code/src/gsl/resources/timer/test/timer1.gs Modified: trunk/code/src/gsl/common/Makefile trunk/code/src/gsl/common/gs.c trunk/code/src/gsl/gs-parser.lhs trunk/code/src/gsl/resources/timer/gs-timer.c Log: working timer test. Modified: trunk/code/src/gsl/common/Makefile =================================================================== --- trunk/code/src/gsl/common/Makefile 2009-04-29 21:26:06 UTC (rev 461) +++ trunk/code/src/gsl/common/Makefile 2009-04-30 02:09:41 UTC (rev 462) @@ -1,6 +1,9 @@ all: gslib +clean: ; + rm *.o *.a + gslib: libgs.a libgs.a: gs.o gs-opcache.o Modified: trunk/code/src/gsl/common/gs.c =================================================================== --- trunk/code/src/gsl/common/gs.c 2009-04-29 21:26:06 UTC (rev 461) +++ trunk/code/src/gsl/common/gs.c 2009-04-30 02:09:41 UTC (rev 462) @@ -233,12 +233,15 @@ { if(!strcmp(gs_resource_entries[j].resource->resource_name, rname)) { - ret = gs_resource_entries[j].resource->register_context(c); - if(ret != 0) + if(gs_resource_entries[j].resource->register_context) { - /* what do we do if a context fails to register with a resource? */ - va_end(ap); - return ret; + ret = gs_resource_entries[j].resource->register_context(c); + if(ret != 0) + { + /* what do we do if a context fails to register with a resource? */ + va_end(ap); + return ret; + } } c->resource_ids[rindex] = gs_resource_entries[j].id; rindex++; Modified: trunk/code/src/gsl/gs-parser.lhs =================================================================== --- trunk/code/src/gsl/gs-parser.lhs 2009-04-29 21:26:06 UTC (rev 461) +++ trunk/code/src/gsl/gs-parser.lhs 2009-04-30 02:09:41 UTC (rev 462) @@ -39,7 +39,7 @@
([mkCDecl (CIntType ni) [] "__gs_cancel_ret" ni], [constructStmtFromC ni $ "child_ctl->parent->hit_pbreak = 1;", constructStmtFromC ni $ "gs_list_del(&child_ctl->link);", -> constructStmtFromC ni $ "__gs_cancel_ret = gs_cancel_children(child_ctl->parent);"]) +> constructStmtFromC ni $ "__gs_cancel_ret = gs_cancel_children(child_ctl->context, child_ctl->parent);"])
After a blocking operation within a pbranch, we need to check if the pbranch has been cancelled, either from a pbreak, or from an external cancel call of the entire blocking function. Modified: trunk/code/src/gsl/resources/timer/gs-timer.c =================================================================== --- trunk/code/src/gsl/resources/timer/gs-timer.c 2009-04-29 21:26:06 UTC (rev 461) +++ trunk/code/src/gsl/resources/timer/gs-timer.c 2009-04-30 02:09:41 UTC (rev 462) @@ -1,27 +1,28 @@ +#define _GNU_SOURCE + +#include <time.h> +#include <sys/time.h> +#include <errno.h> +#include "gs.h" +#include "gs-op.h" +#include "gs-opcache.h" + #define TIMER_DEFAULT_SIZE 1024 static gs_opcache_t timer_opcache; -static GS_LIST_HEAD(timer_list); +static GS_LIST_HEAD(timer_oplist); +static gs_mutex_t timer_mutex = GS_MUTEX_INITIALIZER; +static int gs_timer_resource_id; + struct timer_op { - struct timeval; - gs_op op; + struct timeval timer; + gs_op_id_t op_id; + struct gs_op op; }; -int gs_timer_init(void) -{ - ret = GS_OPCACHE_INIT(struct timer_op, op, TIMER_DEFAULT_SIZE, &timer_opcache); - return ret; -} - -int gs_timer_finalize(void) -{ - gs_opcache_destroy(timer_opcache); - return 0; -} - int gs_timer_post( int millisecs, void (*callback)(void *ptr, int ret), @@ -41,6 +42,7 @@ gs_op_fill(op, callback, user_ptr, hints, ctx); top = gs_op_entry(op, struct timer_op, op); + top->op_id = gs_id_gen(gs_timer_resource_id, (uint64_t)(op->cache_id)); adjust.tv_sec = (int)(millisecs / 1e3); adjust.tv_usec = (millisecs % 1000) * 1e3; @@ -48,8 +50,8 @@ timeradd(&adjust, &now, &(top->timer)); gs_mutex_lock(&timer_mutex); - holder = &timer_list; - gs_list_for_each(iter, &timer_list) + holder = &timer_oplist; + gs_list_for_each(iter, &timer_oplist) { gs_op_iter = gs_list_get_entry(iter, struct gs_op, link); timer_op_iter = gs_op_entry(gs_op_iter, struct timer_op, op); @@ -60,23 +62,23 @@ } holder = iter; } - gs_list_add(top, holder); + gs_list_add(&op->link, holder); gs_mutex_unlock(&timer_mutex); + *op_id = top->op_id; return 0; - } -static int gs_mpi_poll(gs_context_t context, int millisecs) +static int gs_timer_poll(gs_context_t context, int millisecs) { struct gs_list_head *entry; struct gs_op *gop; struct timer_op *top; - struct timeval adjust, now, diff; + struct timeval adjust, now, diff, timeout; struct timespec ts_diff, rem; - int ret, ns_errno; + int ret; gs_mutex_lock(&timer_mutex); - entry = gs_list_peek(&timer_list); + entry = gs_list_peek(&timer_oplist); if(entry) { gop = gs_list_get_entry(entry, struct gs_op, link); @@ -92,7 +94,7 @@ /* what sort of errors could we return? */ /* this timer has hit, so we pop and call callback */ - entry = gs_list_pop(&timer_list); + entry = gs_list_pop(&timer_oplist); gs_invoke_callback(gop, 0); /* give the op back to the opcache */ @@ -100,7 +102,7 @@ gs_opcache_put(timer_opcache, gop); /* setup for next iteration */ - entry = gs_list_peek(&timer_list); + entry = gs_list_peek(&timer_oplist); if(entry) { gop = gs_list_get_entry(entry, struct gs_op, link); @@ -112,7 +114,7 @@ adjust.tv_usec = (millisecs % 1000) * 1e3; timeradd(&now, &adjust, &timeout); - entry = gs_list_peek(&timer_list); + entry = gs_list_peek(&timer_oplist); if(entry) { gop = gs_list_get_entry(entry, struct gs_op, link); @@ -128,7 +130,7 @@ /* this timer will hit before the timeout is reached, so we pop, sleep, * and call callback */ - entry = gs_list_pop(&timer_list); + entry = gs_list_pop(&timer_oplist); ret = nanosleep(&ts_diff, &rem); if(ret != 0) { @@ -137,12 +139,14 @@ return -EINTR; } + gs_invoke_callback(gop, errno); + /* give the op back to the opcache */ gop = gs_list_get_entry(entry, struct gs_op, link); gs_opcache_put(timer_opcache, gop); /* setup for next iteration */ - entry = gs_list_peek(&timer_list); + entry = gs_list_peek(&timer_oplist); if(entry) { gop = gs_list_get_entry(entry, struct gs_op, link); @@ -161,18 +165,52 @@ return 0; } -static int gs_timer_cancel(gs_op_id_t op_id) +static int gs_timer_cancel(gs_context_t gs_ctx, gs_op_id_t op_id) { + int cache_id, resource_id; + gs_mutex_lock(&timer_mutex); + struct gs_op *op; + struct timer_op *top; + + cache_id = gs_id_lookup(op_id, &resource_id); + assert(resource_id == gs_timer_resource_id); + + op = gs_opcache_lookup(timer_opcache, (int)cache_id); + top = gs_op_entry(op, struct timer_op, op); + gs_list_del(&op->link); + gs_opcache_put(timer_opcache, op); + + gs_mutex_unlock(&timer_mutex); + + return 0; } -struct gs_api gs_mpi_api = +struct gs_resource gs_timer_resource = { - "timer", - NULL, - gs_timer_poll, - gs_timer_cancel + .resource_name = "timer", + .poll_context = gs_timer_poll, + .cancel = gs_timer_cancel }; +int gs_timer_init(void) +{ + int ret; + + ret = GS_OPCACHE_INIT(struct timer_op, op, TIMER_DEFAULT_SIZE, &timer_opcache); + + gs_timer_resource_id = gs_resource_register(&gs_timer_resource); + + return ret; +} + +int gs_timer_finalize(void) +{ + gs_resource_unregister(gs_timer_resource_id); + + gs_opcache_destroy(timer_opcache); + return 0; +} + /* * Local variables: * c-indent-level: 4 Added: trunk/code/src/gsl/resources/timer/test/Makefile =================================================================== --- trunk/code/src/gsl/resources/timer/test/Makefile (rev 0) +++ trunk/code/src/gsl/resources/timer/test/Makefile 2009-04-30 02:09:41 UTC (rev 462) @@ -0,0 +1,28 @@ + +GS=../../.. + +gsfiles=timer1.gs + +tests=$(patsubst %.gs,%,$(gsfiles)) +objs=$(patsubst %.gs,%.gs.o,$(gsfiles)) +cpps=$(patsubst %.gs,%.gs.gsi,$(gsfiles)) +trans=$(patsubst %.gs,%.gs.i,$(gsfiles)) + +all: $(tests) + +clean: ; + rm -f $(objs) $(tests) $(trans) $(cpps) + +%.gs: ; + +$(cpps): %.gs.gsi: %.gs + $(CC) -I.. -I${GS}/include -x c -E $< -o $@ + +$(trans): %.gs.i: %.gs.gsi + ${GS}/gs-parser -p $< > $@ + +$(objs): %.gs.o: %.gs.i + $(CC) -g -x c -c -o $@ $< + +$(tests): %: %.gs.o + $(CC) -o $@ $< ../gs-timer.o -L${GS}/common -lgs Added: trunk/code/src/gsl/resources/timer/test/timer1.gs =================================================================== --- trunk/code/src/gsl/resources/timer/test/timer1.gs (rev 0) +++ trunk/code/src/gsl/resources/timer/test/timer1.gs 2009-04-30 02:09:41 UTC (rev 462) @@ -0,0 +1,67 @@ + +#include <stdio.h> +#include "gs.h" +#include "gs-timer.gsh" + +__blocking int dotimer(void) +{ + pwait + { + pbranch + { + printf("sleeping for 100 ms\n"); + gs_timer(100); + printf("100 ms done.\n"); + } + + pbranch + { + printf("sleeping for 1000 ms\n"); + gs_timer(1000); + printf("1000 ms done.\n"); + } + + pbranch + { + printf("sleeping for 10 ms\n"); + gs_timer(10); + printf("10 ms done.\n"); + } + } + return 0; +} + +int done = 0; + +void done_callback(void *up, int ret) +{ + done = 1; +} + +int main(int argc, char *argv[]) +{ + gs_context_t ctx; + int pc = 0; + gs_op_id_t op_id; + + gs_timer_init(); + gs_context_create(&ctx, 1, "timer"); + + dotimer_post(done_callback, NULL, NULL, ctx, &op_id); + while(done == 0) + { + gs_poll(ctx, 10); + printf("polled %d times\n", ++pc); + } + + return 0; +} + +/* + * Local variables: + * c-indent-level: 4 + * c-basic-offset: 4 + * End: + * + * vim: ts=8 sts=4 sw=4 expandtab + */
participants (1)
-
slang@mcs.anl.gov