Add naive parallelism

parent ed165b9e
......@@ -7,6 +7,7 @@ WINBASH='/c/Windows/System32/bash'
# Who needs a build system? KISS
gcc -o $NAME \
-Os -pedantic -std=c99 -Wall -fstrict-aliasing -fomit-frame-pointer \
-fopenmp \
-I./lib/duktape \
lib/duktape/duktape.c \
src/main.c \
......
......@@ -4,12 +4,13 @@ extern char **environ;
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include "duktape.h"
#ifdef _WIN32
// @see [1]
#include <io.h>
#include <fcntl.h>
#endif
#include <omp.h>
#include "duktape.h"
// Include helper functions
#include "app_push_argv.c"
......@@ -34,46 +35,54 @@ int main(int argc, char *argv[] /* char *environ[] */) {
exit(0);
}
// Create a JS environment
duk_int_t ret = 0;
duk_context *ctx = duk_create_heap_default();
duk_int_t result;
#pragma omp parallel
{
// Create a JS environment
duk_context *ctx = duk_create_heap_default();
// Load top-level function module
ret = native_import(ctx, argv[1]);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
goto finally;
}
// Load top-level function module
duk_int_t ret = native_import(ctx, argv[1]);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
goto finally;
}
// Construct the all-powerful god argument
duk_idx_t god_arg_idx = duk_push_object(ctx);
ret = app_push_argv(ctx, argc, argv);
duk_put_prop_string(ctx, god_arg_idx, "argv");
// Create environment variables object
ret = app_push_environ(ctx);
duk_put_prop_string(ctx, god_arg_idx, "environ");
// Provide access to native functions
ret = duk_push_object(ctx);
duk_push_c_function(ctx, app_push_dir, 1);
duk_put_prop_string(ctx, ret, "dir");
duk_push_c_function(ctx, native_print, DUK_VARARGS);
duk_put_prop_string(ctx, ret, "print");
duk_put_prop_string(ctx, god_arg_idx, "modules");
// Construct the all-powerful god argument
duk_push_object(ctx);
// Create argv object
app_push_argv(ctx, argc, argv);
duk_put_prop_string(ctx, -2, "argv");
// Create environment variables object
app_push_environ(ctx);
duk_put_prop_string(ctx, -2, "environ");
// Provide access to native functions
duk_push_object(ctx);
duk_push_c_function(ctx, app_push_dir, 1);
duk_put_prop_string(ctx, -2, "dir");
duk_push_c_function(ctx, native_print, DUK_VARARGS);
duk_put_prop_string(ctx, -2, "print");
duk_put_prop_string(ctx, -2, "modules");
// Run top-level module, passing the god arg as the sole argument.
ret = duk_pcall(ctx, 1);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
goto finally;
}
// else {
// duk_push_context_dump(ctx);
// printf("%s\n", duk_to_string(ctx, -1));
// }
// Run top-level module, passing the god arg as the sole argument.
ret = duk_pcall(ctx, 1);
if (ret != 0) {
fprintf(stderr, "%s %s\n", argv[1], duk_safe_to_string(ctx, -1));
goto finally;
}
// else {
// duk_push_context_dump(ctx);
// printf("%s\n", duk_to_string(ctx, -1));
// }
finally:
duk_destroy_heap(ctx);
return ret;
finally:
duk_destroy_heap(ctx);
#pragma omp critical
// For now we will OR the exit codes for each thread.
// That lets you easily check the result against multiple flags.
result |= ret;
}
return result;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment