From 6ed6961958a88d46b6088051c9911bac06e10ef5 Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Fri, 1 Jul 2011 14:11:16 +0100 Subject: [PATCH] Add checks to sample task systems to ensure that TasksInit has been called; if not, print an informative error message. --- examples/mandelbrot_tasks/tasks_concrt.cpp | 13 +++++++++++++ examples/mandelbrot_tasks/tasks_gcd.cpp | 13 +++++++++++++ examples/mandelbrot_tasks/tasks_pthreads.cpp | 10 ++++++++++ 3 files changed, 36 insertions(+) diff --git a/examples/mandelbrot_tasks/tasks_concrt.cpp b/examples/mandelbrot_tasks/tasks_concrt.cpp index a861ca87..753b4fb3 100644 --- a/examples/mandelbrot_tasks/tasks_concrt.cpp +++ b/examples/mandelbrot_tasks/tasks_concrt.cpp @@ -39,6 +39,7 @@ using namespace Concurrency; #include #include +#include // ispc expects these functions to have C linkage / not be mangled extern "C" { @@ -61,12 +62,14 @@ static int taskOffset; static TaskInfo taskInfo[MAX_TASKS]; static event *events[MAX_TASKS]; static CRITICAL_SECTION criticalSection; +static bool initialized = false; void TasksInit() { InitializeCriticalSection(&criticalSection); for (int i = 0; i < MAX_TASKS; ++i) events[i] = new event; + initialized = true; } @@ -91,6 +94,11 @@ lRunTask(LPVOID param) { void ISPCLaunch(void *func, void *data) { + if (!initialized) { + fprintf(stderr, "You must call TasksInit() before launching tasks.\n"); + exit(1); + } + // Get a TaskInfo struct for this task EnterCriticalSection(&criticalSection); TaskInfo *ti = &taskInfo[taskOffset++]; @@ -105,6 +113,11 @@ ISPCLaunch(void *func, void *data) { void ISPCSync() { + if (!initialized) { + fprintf(stderr, "You must call TasksInit() before launching tasks.\n"); + exit(1); + } + event::wait_for_multiple(&events[0], taskOffset, true, COOPERATIVE_TIMEOUT_INFINITE); diff --git a/examples/mandelbrot_tasks/tasks_gcd.cpp b/examples/mandelbrot_tasks/tasks_gcd.cpp index b8b8e80f..f759cc37 100644 --- a/examples/mandelbrot_tasks/tasks_gcd.cpp +++ b/examples/mandelbrot_tasks/tasks_gcd.cpp @@ -35,7 +35,10 @@ Dispatch. */ #include +#include +#include +static bool initialized = false; static dispatch_queue_t gcdQueue; static dispatch_group_t gcdGroup; @@ -55,6 +58,7 @@ void TasksInit() { gcdQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); gcdGroup = dispatch_group_create(); + initialized = true; } @@ -77,6 +81,10 @@ lRunTask(void *ti) { void ISPCLaunch(void *func, void *data) { + if (!initialized) { + fprintf(stderr, "You must call TasksInit() before launching tasks.\n"); + exit(1); + } TaskInfo *ti = new TaskInfo; ti->func = func; ti->data = data; @@ -85,6 +93,11 @@ void ISPCLaunch(void *func, void *data) { void ISPCSync() { + if (!initialized) { + fprintf(stderr, "You must call TasksInit() before launching tasks.\n"); + exit(1); + } + // Wait for all of the tasks in the group to complete before returning dispatch_group_wait(gcdGroup, DISPATCH_TIME_FOREVER); } diff --git a/examples/mandelbrot_tasks/tasks_pthreads.cpp b/examples/mandelbrot_tasks/tasks_pthreads.cpp index 4a23c5dc..7ec35e04 100644 --- a/examples/mandelbrot_tasks/tasks_pthreads.cpp +++ b/examples/mandelbrot_tasks/tasks_pthreads.cpp @@ -135,6 +135,11 @@ TasksInit() { void ISPCLaunch(void *f, void *d) { + if (threads == NULL) { + fprintf(stderr, "You must call TasksInit() before launching tasks.\n"); + exit(1); + } + // // Acquire mutex, add task // @@ -256,6 +261,11 @@ lTaskEntry(void *arg) { void ISPCSync() { + if (threads == NULL) { + fprintf(stderr, "You must call TasksInit() before launching tasks.\n"); + exit(1); + } + int err; if ((err = pthread_mutex_lock(&tasksRunningConditionMutex)) != 0) { fprintf(stderr, "Error from pthread_mutex_lock: %s\n", strerror(err));