/*
 * Compiler reordering example (without compiler barrier)
 */

#include <assert.h>
#include <stdbool.h>

/* Stand-in for PostgreSQL's Assert() macro */
#define Assert(condition) assert(condition)

typedef struct SharedState
{
    int  tuples;
    bool done;
} SharedState;

/* Executed by CPU 1, 'shared' points to shared memory */
void writer(SharedState *shared, int pages, int tuples_per_page)
{
    shared->tuples = pages * tuples_per_page;
    shared->done = true;
}

/* Executed by CPU 2, 'shared' points to shared memory */
void reader(SharedState *shared)
{
    while (!shared->done)
        /* Wait for the writer to finish */;

    Assert(shared->tuples != 0);
}
