From 7c5e0f1834595a83053ffa7cad1a1947a2490b60 Mon Sep 17 00:00:00 2001 From: "Bjoern B. Brandenburg" <bbb@cs.unc.edu> Date: Mon, 28 Jan 2008 11:55:16 -0500 Subject: bin: add error checking to example tasks --- bin/base_mt_task.c | 29 ++++++++++++++++++++++++----- bin/base_task.c | 20 ++++++++++++++++---- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/bin/base_mt_task.c b/bin/base_mt_task.c index 096b124..24f2070 100644 --- a/bin/base_mt_task.c +++ b/bin/base_mt_task.c @@ -12,6 +12,9 @@ #include <stdio.h> #include <stdlib.h> +/* Include gettid() */ +#include <sys/types.h> + /* Include threading support. */ #include <pthread.h> @@ -42,6 +45,19 @@ void* rt_thread(struct thread_context* ctx); */ int job(void); + +/* Catch errors. + */ +#define CALL( exp ) do { \ + int ret; \ + ret = exp; \ + if (ret != 0) \ + fprintf(stderr, "%s failed: %m\n", #exp);\ + else \ + fprintf(stderr, "%s ok.\n", #exp); \ + } while (0) + + /* Basic setup is the same as in the single-threaded example. However, * we do some thread initiliazation first before invoking the job. */ @@ -105,6 +121,7 @@ int main(int argc, char** argv) void* rt_thread(struct thread_context* ctx) { int do_exit; + rt_param_t param; /* Make presence visible. */ printf("RT Thread %d active.\n", ctx->id); @@ -112,15 +129,17 @@ void* rt_thread(struct thread_context* ctx) /***** * 1) Initialize real-time settings. */ - init_rt_thread(); - sporadic_global(EXEC_COST, PERIOD); - + CALL( init_rt_thread() ); + CALL( sporadic_global(EXEC_COST, PERIOD) ); + /* Just for fun display the real-time parameters of this thread. */ + CALL( get_rt_task_param(gettid(), ¶m) ); + show_rt_param(¶m); /***** * 2) Transition to real-time mode. */ - task_mode(LITMUS_RT_TASK); + CALL( task_mode(LITMUS_RT_TASK) ); /* The task is now executing as a real-time task if the call didn't fail. */ @@ -142,7 +161,7 @@ void* rt_thread(struct thread_context* ctx) /***** * 4) Transition to background mode. */ - task_mode(BACKGROUND_TASK); + CALL( task_mode(BACKGROUND_TASK) ); return NULL; diff --git a/bin/base_task.c b/bin/base_task.c index 067c088..c50990b 100644 --- a/bin/base_task.c +++ b/bin/base_task.c @@ -26,6 +26,18 @@ #define PERIOD 100 #define EXEC_COST 10 +/* Catch errors. + */ +#define CALL( exp ) do { \ + int ret; \ + ret = exp; \ + if (ret != 0) \ + fprintf(stderr, "%s failed: %m\n", #exp);\ + else \ + fprintf(stderr, "%s ok.\n", #exp); \ + } while (0) + + /* Declare the periodically invoked job. * Returns 1 -> task should exit. * 0 -> task should continue. @@ -72,8 +84,8 @@ int main(int argc, char** argv) * If this were to execute under a partitioned scheduler, it would be assigned * to the first partition (since partitioning is performed offline). */ - init_litmus(); - sporadic_global(EXEC_COST, PERIOD); + CALL( init_litmus() ); + CALL( sporadic_global(EXEC_COST, PERIOD) ); /* To specify a partition, use sporadic_partitioned(). * Example: @@ -88,7 +100,7 @@ int main(int argc, char** argv) /***** * 4) Transition to real-time mode. */ - task_mode(LITMUS_RT_TASK); + CALL( task_mode(LITMUS_RT_TASK) ); /* The task is now executing as a real-time task if the call didn't fail. */ @@ -110,7 +122,7 @@ int main(int argc, char** argv) /***** * 6) Transition to background mode. */ - task_mode(BACKGROUND_TASK); + CALL( task_mode(BACKGROUND_TASK) ); -- cgit v1.2.2