fluid let uses derfer now and can be returned from
This commit is contained in:
+44
-4
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
// #include <functional>
|
||||
|
||||
#define proc auto
|
||||
|
||||
@@ -55,15 +55,28 @@ struct {
|
||||
#define defer auto TOKENPASTE2(__deferred_lambda_call, __COUNTER__) = deferrer << [&]
|
||||
|
||||
|
||||
/**
|
||||
* fluid-let *
|
||||
/*
|
||||
defer {
|
||||
call();
|
||||
};
|
||||
|
||||
expands to:
|
||||
|
||||
auto __deferred_lambda_call0 = deferrer << [&] {
|
||||
call();
|
||||
};
|
||||
*/
|
||||
|
||||
/*****************
|
||||
* fluid-let *
|
||||
*****************/
|
||||
|
||||
#define fluid_let(var, val) \
|
||||
if (0) \
|
||||
TOKENPASTE2(finished,__LINE__): ; \
|
||||
else \
|
||||
for (auto TOKENPASTE2(fluid_let_, __LINE__) = var;;) \
|
||||
for (defer{var = TOKENPASTE2(fluid_let_, __LINE__);};;) \
|
||||
for(var = val;;) \
|
||||
if (1) { \
|
||||
goto TOKENPASTE2(body,__LINE__); \
|
||||
@@ -71,7 +84,34 @@ struct {
|
||||
else \
|
||||
while (1) \
|
||||
if (1) { \
|
||||
var = TOKENPASTE2(fluid_let_, __LINE__); \
|
||||
goto TOKENPASTE2(finished, __LINE__); \
|
||||
} \
|
||||
else TOKENPASTE2(body,__LINE__):
|
||||
|
||||
|
||||
/**
|
||||
fluid_let(var, val) {
|
||||
call1(var);
|
||||
call2(var);
|
||||
}
|
||||
|
||||
expands to
|
||||
|
||||
if (0)
|
||||
finished98:;
|
||||
else
|
||||
for (auto fluid_let_98 = var;;)
|
||||
for (auto __deferred_lambda_call0 = deferrer << [&] { var = fluid_let_98; };;)
|
||||
for (var = val;;)
|
||||
if (1) {
|
||||
goto body98;
|
||||
} else
|
||||
while (1)
|
||||
if (1) {
|
||||
goto finished98;
|
||||
} else
|
||||
body98 : {
|
||||
call1(var);
|
||||
call2(var);
|
||||
}
|
||||
*/
|
||||
|
||||
+18
-9
@@ -4,9 +4,11 @@
|
||||
# include <stdio.h>
|
||||
# include <string.h>
|
||||
# include <time.h>
|
||||
// for syscall:
|
||||
# include <unistd.h>
|
||||
# include <sys/syscall.h>
|
||||
|
||||
//# include <bits/stdc++.h>
|
||||
# include <iostream>
|
||||
# include <sys/stat.h>
|
||||
# include <sys/types.h>
|
||||
|
||||
struct Profiler {
|
||||
// same for all threads
|
||||
@@ -14,7 +16,7 @@ struct Profiler {
|
||||
|
||||
// thread local
|
||||
inline thread_local static bool is_initialized = false;
|
||||
inline thread_local static int thread_id = -1;
|
||||
inline thread_local static size_t thread_id = -1;
|
||||
inline thread_local static int call_depth = 0;
|
||||
inline thread_local static FILE* out_file = nullptr;
|
||||
Profiler(const char* file, const char* func, const int line) {
|
||||
@@ -22,21 +24,28 @@ struct Profiler {
|
||||
|
||||
// if we never used this thread before
|
||||
if (!is_initialized) {
|
||||
thread_id = syscall(__NR_gettid);
|
||||
printf("Hello I am %d\n", thread_id);
|
||||
thread_id = (size_t)&thread_id;
|
||||
time_t t = time(NULL);
|
||||
tm* tm_i = localtime(&t);
|
||||
|
||||
// create folder
|
||||
#ifdef _MSC_VER
|
||||
_mkdir("./profiler_reports/");
|
||||
#else
|
||||
mkdir("./profiler_reports/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||
#endif
|
||||
|
||||
// if we never even created the shared file name template
|
||||
if (!file_template[0]) {
|
||||
sprintf(file_template, "%02d.%02d.%04d-%02d.%02d.%02d-%%02d-profiler.report",
|
||||
// make sure folder exists
|
||||
sprintf(file_template, "./profiler_reports/%02d.%02d.%04d-%02d.%02d.%02d-%%zd-profiler.report",
|
||||
tm_i->tm_mday, tm_i->tm_mon+1, tm_i->tm_year+1900,
|
||||
tm_i->tm_hour, tm_i->tm_min, tm_i->tm_sec);
|
||||
}
|
||||
|
||||
|
||||
char file_name[38];
|
||||
char file_name[100];
|
||||
sprintf(file_name, file_template, thread_id);
|
||||
printf("Hello I am %zd\n", thread_id);
|
||||
out_file = fopen(file_name, "w");
|
||||
if (!out_file) {
|
||||
printf("could not open %s\n", file_name);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdio.h>
|
||||
|
||||
#include "profiler.hpp"
|
||||
#include "macros.h"
|
||||
|
||||
#include "macros.hpp"
|
||||
|
||||
int var = 100;
|
||||
|
||||
@@ -14,13 +14,13 @@ void test() {
|
||||
printf("var is %d\n", var);
|
||||
}
|
||||
|
||||
printf("var is %d\n", var);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
profile_this;
|
||||
|
||||
test();
|
||||
printf("var is %d\n", var);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user