n2144 - CPLEX code examples

CPLEX code examples for continued discussions of the concerns raised in document n2131. Particularly these apply to the composability and synchronization section in that document.

#include <threads.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>

void init_application_state() {
    _Task _Block {
        _Task _Spawn {
            init_app_p1();
        }
        _Task _Spawn {
            init_app_p2();
        }
        _Task _Spawn {
            init_app_p3();
        }
    }
}

void do_physics() {
    hypre_init(...);
    init_application_state();
    hypre_run(...); // what thread am I?
}

// EX1: the thread continuing from a spawn, or block, is not guaranteed
void do_something_insidious() {
    _Task _Block {
        _Task _Spawn {
            do_something();
        }
    }
}

void use_lock() {
    mtx_lock(&mtx);
    do_something_insidious();
    something_that_requires_locks_for_safety();
    mtx_unlock(&mtx); // Oops
}

// EX1: the thread continuing from a spawn, or block, is not guaranteed
void do_something_insidious() {
    _Task _Block {
        _Task _Spawn {
            do_something();
        }
    }
}

void use_lock() {
    mtx_lock(&mtx);
    do_something_insidious();
    something_that_requires_locks_for_safety();
    mtx_unlock(&mtx); // Oops
}

// EX2: the thread continuing from a spawn, or block, is not guaranteed
void use_lock() {
    _Task _Block {
        mtx_lock(&mtx);
        _Task _Spawn {
            do_something();
        }
        something_that_requires_locks_for_safety();
        mtx_unlock(&mtx); // Oops
    }
}


// EX3: the thread continuing from a spawn, or block, is not guaranteed
void check_errno() {
    FILE * f;
    _Task _Block {
        _Task _Spawn {
            do_something();
        }
        f = fopen("non_existent_file.txt", "r");
    }
    if (!f)
        perror("Would errno be valid here?");
}

// EX4: the thread continuing from a spawn, or block, is not guaranteed
void check_errno() {
    FILE * f;
    _Task _Block {
        _Task _Spawn {
            f = fopen("non_existent_file.txt", "r");
        }
        _Task _Sync;
        if (!f)
            perror("Would errno be valid here?");
    }
}