Skip to content

Instantly share code, notes, and snippets.

@toddlipcon
Created February 26, 2016 06:11
Show Gist options
  • Save toddlipcon/761fa7f8bd9e91f8a8dd to your computer and use it in GitHub Desktop.
Save toddlipcon/761fa7f8bd9e91f8a8dd to your computer and use it in GitHub Desktop.
diff --git a/include/coz.h b/include/coz.h
index 3821992..0263c3c 100644
--- a/include/coz.h
+++ b/include/coz.h
@@ -27,27 +27,59 @@ 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*);
+typedef struct {
+ coz_counter_t* (*get_counter)(int, const char*);
+ void (*pre_block)();
+ void (*post_block)(int);
+ void (*wake_other)();
+} coz_funcs_t;
-// 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
-
- if(!_initialized) {
- // Locate the _coz_get_counter method
- void* p = dlsym(RTLD_DEFAULT, "_coz_get_counter");
-
+static coz_funcs_t _coz_funcs;
+
+static void _coz_init() {
+ static volatile unsigned char _initialized = 0;
+ if (!_initialized) {
// Use memcpy to avoid pedantic GCC complaint about storing function pointer in void*
- memcpy(&fn, &p, sizeof(p));
-
+ void* p = dlsym(RTLD_DEFAULT, "_coz_get_counter");
+ memcpy(&_coz_funcs.get_counter, &p, sizeof(p));
+ p = dlsym(RTLD_DEFAULT, "_coz_pre_block");
+ memcpy(&_coz_funcs.pre_block, &p, sizeof(p));
+ p = dlsym(RTLD_DEFAULT, "_coz_post_block");
+ memcpy(&_coz_funcs.post_block, &p, sizeof(p));
+ p = dlsym(RTLD_DEFAULT, "_coz_wake_other");
+ memcpy(&_coz_funcs.wake_other, &p, sizeof(p));
_initialized = 1;
}
-
- // Call the function, or return null if profiler is not found
- if(fn) return fn(type, name);
- else return 0;
+}
+
+// Locate and invoke _coz_get_counter
+static coz_counter_t* _call_coz_get_counter(int type, const char* name) {
+ _coz_init();
+ if (_coz_funcs.get_counter) {
+ return _coz_funcs.get_counter(type, name);
+ }
+ return 0;
+}
+
+static void _call_coz_pre_block() {
+ _coz_init();
+ if (_coz_funcs.pre_block) {
+ _coz_funcs.pre_block();
+ }
+}
+
+static void _call_coz_post_block(int woken) {
+ _coz_init();
+ if (_coz_funcs.post_block) {
+ _coz_funcs.post_block(woken);
+ }
+}
+
+static void _call_coz_wake_other() {
+ _coz_init();
+ if (_coz_funcs.wake_other) {
+ _coz_funcs.wake_other();
+ }
}
// Macro to initialize and increment a counter
@@ -74,6 +106,10 @@ static coz_counter_t* _call_coz_get_counter(int type, const char* name) {
#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_WAKE_OTHER _call_coz_wake_other
+#define COZ_PRE_BLOCK _call_coz_pre_block
+#define COZ_POST_BLOCK _call_coz_post_block
+
#if defined(__cplusplus)
}
#endif
diff --git a/libcoz/libcoz.cpp b/libcoz/libcoz.cpp
index 659e152..f954dd3 100644
--- a/libcoz/libcoz.cpp
+++ b/libcoz/libcoz.cpp
@@ -54,6 +54,18 @@ extern "C" coz_counter_t* _coz_get_counter(progress_point_type t, const char* na
}
}
+extern "C" void _coz_pre_block() {
+ if(initialized) profiler::get_instance().pre_block();
+}
+
+extern "C" void _coz_post_block(bool woken) {
+ if(initialized) profiler::get_instance().post_block(woken);
+}
+
+extern "C" void _coz_wake_other() {
+ if(initialized) profiler::get_instance().catch_up();
+}
+
/**
* Read a link's contents and return it as a string
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment