Skip to content

Commit

Permalink
Add pre_block/post_block/wake_other
Browse files Browse the repository at this point in the history
  • Loading branch information
vlovich committed Mar 11, 2022
1 parent d5d0a30 commit 9ece196
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions include/coz.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,55 @@ typedef struct {
size_t backoff; // Used to batch updates to the shared counter. Currently unused.
} coz_counter_t;

// The type of the _coz_get_counter function
typedef coz_counter_t* (*coz_get_counter_t)(int, const char*);

// Locate and invoke _coz_get_counter
static coz_counter_t* _call_coz_get_counter(int type, const char* name) {
static unsigned char _initialized = 0;
static coz_get_counter_t fn; // The pointer to _coz_get_counter
// Use memcpy to avoid pedantic GCC complaint about storing function pointer in void*
static unsigned char initialized = dlsym == NULL;
static coz_counter_t* (*fn)(int, const char*);

if (!initialized) {
void* p = dlsym(RTLD_DEFAULT, "_coz_get_counter");
memcpy(&fn, &p, sizeof(p));
initialized = true;
}

if(!_initialized) {
if(dlsym) {
// Locate the _coz_get_counter method
void* p = dlsym(RTLD_DEFAULT, "_coz_get_counter");
if (fn) return fn(type, name);
else return 0;
}

// Use memcpy to avoid pedantic GCC complaint about storing function pointer in void*
memcpy(&fn, &p, sizeof(p));
}
static void _call_coz_pre_block() {
static unsigned char initialized = dlsym == 0;
static void (*fn)();

_initialized = 1;
if (!initialized) {
void* p = dlsym(RTLD_DEFAULT, "_coz_pre_block");
memcpy(&fn, &p, sizeof(p));
initialized = true;
}
if (fn) return fn();
}

// Call the function, or return null if profiler is not found
if(fn) return fn(type, name);
else return 0;
static void _call_coz_post_block() {
static unsigned char initialized = dlsym == 0;
static void (*fn)();

if (!initialized) {
void* p = dlsym(RTLD_DEFAULT, "_coz_post_block");
memcpy(&fn, &p, sizeof(p));
initialized = true;
}
if (fn) return fn();
}

static void _call_coz_wake_other() {
static unsigned char initialized = dlsym == 0;
static void (*fn)();

if (!initialized) {
void* p = dlsym(RTLD_DEFAULT, "_coz_wake_other");
memcpy(&fn, &p, sizeof(p));
initialized = true;
}
if (fn) return fn();
}

// Macro to initialize and increment a counter
Expand All @@ -85,6 +111,9 @@ static coz_counter_t* _call_coz_get_counter(int type, const char* name) {
#define COZ_PROGRESS COZ_INCREMENT_COUNTER(COZ_COUNTER_TYPE_THROUGHPUT, __FILE__ ":" STR(__LINE__))
#define COZ_BEGIN(name) COZ_INCREMENT_COUNTER(COZ_COUNTER_TYPE_BEGIN, name)
#define COZ_END(name) COZ_INCREMENT_COUNTER(COZ_COUNTER_TYPE_END, name)
#define COZ_PRE_BLOCK() _call_coz_pre_block()
#define COZ_POST_BLOCK() _call_coz_post_block()
#define COZ_WAKE_OTHER() _call_coz_wake_other()

#if defined(__cplusplus)
}
Expand Down

0 comments on commit 9ece196

Please sign in to comment.