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;
|
using namespace Concurrency;
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
// ispc expects these functions to have C linkage / not be mangled
|
// ispc expects these functions to have C linkage / not be mangled
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -61,12 +62,14 @@ static int taskOffset;
|
|||||||
static TaskInfo taskInfo[MAX_TASKS];
|
static TaskInfo taskInfo[MAX_TASKS];
|
||||||
static event *events[MAX_TASKS];
|
static event *events[MAX_TASKS];
|
||||||
static CRITICAL_SECTION criticalSection;
|
static CRITICAL_SECTION criticalSection;
|
||||||
|
static bool initialized = false;
|
||||||
|
|
||||||
void
|
void
|
||||||
TasksInit() {
|
TasksInit() {
|
||||||
InitializeCriticalSection(&criticalSection);
|
InitializeCriticalSection(&criticalSection);
|
||||||
for (int i = 0; i < MAX_TASKS; ++i)
|
for (int i = 0; i < MAX_TASKS; ++i)
|
||||||
events[i] = new event;
|
events[i] = new event;
|
||||||
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -91,6 +94,11 @@ lRunTask(LPVOID param) {
|
|||||||
|
|
||||||
void
|
void
|
||||||
ISPCLaunch(void *func, void *data) {
|
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
|
// Get a TaskInfo struct for this task
|
||||||
EnterCriticalSection(&criticalSection);
|
EnterCriticalSection(&criticalSection);
|
||||||
TaskInfo *ti = &taskInfo[taskOffset++];
|
TaskInfo *ti = &taskInfo[taskOffset++];
|
||||||
@@ -105,6 +113,11 @@ ISPCLaunch(void *func, void *data) {
|
|||||||
|
|
||||||
|
|
||||||
void ISPCSync() {
|
void ISPCSync() {
|
||||||
|
if (!initialized) {
|
||||||
|
fprintf(stderr, "You must call TasksInit() before launching tasks.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
event::wait_for_multiple(&events[0], taskOffset, true,
|
event::wait_for_multiple(&events[0], taskOffset, true,
|
||||||
COOPERATIVE_TIMEOUT_INFINITE);
|
COOPERATIVE_TIMEOUT_INFINITE);
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,10 @@
|
|||||||
Dispatch. */
|
Dispatch. */
|
||||||
|
|
||||||
#include <dispatch/dispatch.h>
|
#include <dispatch/dispatch.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static bool initialized = false;
|
||||||
static dispatch_queue_t gcdQueue;
|
static dispatch_queue_t gcdQueue;
|
||||||
static dispatch_group_t gcdGroup;
|
static dispatch_group_t gcdGroup;
|
||||||
|
|
||||||
@@ -55,6 +58,7 @@ void
|
|||||||
TasksInit() {
|
TasksInit() {
|
||||||
gcdQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
gcdQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
||||||
gcdGroup = dispatch_group_create();
|
gcdGroup = dispatch_group_create();
|
||||||
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -77,6 +81,10 @@ lRunTask(void *ti) {
|
|||||||
|
|
||||||
|
|
||||||
void ISPCLaunch(void *func, void *data) {
|
void ISPCLaunch(void *func, void *data) {
|
||||||
|
if (!initialized) {
|
||||||
|
fprintf(stderr, "You must call TasksInit() before launching tasks.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
TaskInfo *ti = new TaskInfo;
|
TaskInfo *ti = new TaskInfo;
|
||||||
ti->func = func;
|
ti->func = func;
|
||||||
ti->data = data;
|
ti->data = data;
|
||||||
@@ -85,6 +93,11 @@ void ISPCLaunch(void *func, void *data) {
|
|||||||
|
|
||||||
|
|
||||||
void ISPCSync() {
|
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
|
// Wait for all of the tasks in the group to complete before returning
|
||||||
dispatch_group_wait(gcdGroup, DISPATCH_TIME_FOREVER);
|
dispatch_group_wait(gcdGroup, DISPATCH_TIME_FOREVER);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,6 +135,11 @@ TasksInit() {
|
|||||||
|
|
||||||
void
|
void
|
||||||
ISPCLaunch(void *f, void *d) {
|
ISPCLaunch(void *f, void *d) {
|
||||||
|
if (threads == NULL) {
|
||||||
|
fprintf(stderr, "You must call TasksInit() before launching tasks.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Acquire mutex, add task
|
// Acquire mutex, add task
|
||||||
//
|
//
|
||||||
@@ -256,6 +261,11 @@ lTaskEntry(void *arg) {
|
|||||||
|
|
||||||
|
|
||||||
void ISPCSync() {
|
void ISPCSync() {
|
||||||
|
if (threads == NULL) {
|
||||||
|
fprintf(stderr, "You must call TasksInit() before launching tasks.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
int err;
|
int err;
|
||||||
if ((err = pthread_mutex_lock(&tasksRunningConditionMutex)) != 0) {
|
if ((err = pthread_mutex_lock(&tasksRunningConditionMutex)) != 0) {
|
||||||
fprintf(stderr, "Error from pthread_mutex_lock: %s\n", strerror(err));
|
fprintf(stderr, "Error from pthread_mutex_lock: %s\n", strerror(err));
|
||||||
|
|||||||
Reference in New Issue
Block a user