Add checks to sample task systems to ensure that TasksInit has been
called; if not, print an informative error message.
This commit is contained in:
@@ -39,6 +39,7 @@
|
||||
using namespace Concurrency;
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// 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);
|
||||
|
||||
|
||||
@@ -35,7 +35,10 @@
|
||||
Dispatch. */
|
||||
|
||||
#include <dispatch/dispatch.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user