Browse Source

feat: all cases build ffi

legacy
Dnomd343 1 year ago
parent
commit
ccda3824dc
  1. 54
      src/core/ffi/all_cases.cc
  2. 11
      src/core/ffi/klotski.h
  3. 62
      src/main.c

54
src/core/ffi/all_cases.cc

@ -25,3 +25,57 @@ int is_all_cases_prebuild_available() {
return KLOTSKI_FALSE; return KLOTSKI_FALSE;
} }
} }
void all_cases_build() {
AllCases::Instance().Build();
}
void all_cases_build_async(executor_t executor, notifier_t callback) {
executor([](void *cb) {
all_cases_build();
((notifier_t)cb)();
}, (void*)callback);
}
void all_cases_parallel_build(executor_t executor) {
typedef std::function<void()> Runner;
AllCases::Instance().BuildParallel([executor](Runner &&runner) {
executor([](void *fn) {
(*(Runner*)fn)();
delete (Runner*)fn;
}, (void*)new Runner{std::move(runner)});
});
}
void all_cases_parallel_build_async(executor_t executor, notifier_t callback) {
typedef std::pair<executor_t, notifier_t> pp_t;
auto pp = new pp_t;
pp->first = executor;
pp->second = callback;
auto lambda = [](void *arg) {
std::cout << "enter lambda" << std::endl;
auto *pp = (pp_t*)arg;
all_cases_parallel_build(pp->first);
((notifier_t)pp->second)();
};
executor(lambda, (void*)pp);
}
int is_all_cases_available() {
if (AllCases::Instance().IsAvailable()) {
return KLOTSKI_TRUE;
} else {
return KLOTSKI_FALSE;
}
}

11
src/core/ffi/klotski.h

@ -32,8 +32,15 @@ EXTERN void all_cases_prebuild_async(executor_t executor, notifier_t callback);
/// not completed, non-0 otherwise. /// not completed, non-0 otherwise.
EXTERN int is_all_cases_prebuild_available(); EXTERN int is_all_cases_prebuild_available();
//EXTERN void all_cases_build(); EXTERN void all_cases_build();
//EXTERN int is_all_cases_available();
EXTERN void all_cases_build_async(executor_t executor, notifier_t callback);
EXTERN void all_cases_parallel_build(executor_t executor);
EXTERN void all_cases_parallel_build_async(executor_t executor, notifier_t callback);
EXTERN int is_all_cases_available();
//extern const uint32_t ALL_CASES_SIZE; //extern const uint32_t ALL_CASES_SIZE;
//EXTERN_FUNC void export_all_cases(uint64_t *buffer); //EXTERN_FUNC void export_all_cases(uint64_t *buffer);

62
src/main.c

@ -4,56 +4,76 @@
#include <unistd.h> #include <unistd.h>
#include "core/ffi/klotski.h" #include "core/ffi/klotski.h"
struct pthread_wrapper_t { typedef struct {
void (*fn)(void*); void (*fn)(void*);
void *arg; void *arg;
}; } pthread_wrapper_t;
void* pthread_wrapper(void *arg) { void* pthread_wrapper(void *arg) {
printf("wrapper at %ld\n", pthread_self());
printf("enter wrapper\n"); pthread_wrapper_t *kk = (pthread_wrapper_t*)arg;
struct pthread_wrapper_t *kk = (struct pthread_wrapper_t*)arg;
kk->fn(kk->arg); kk->fn(kk->arg);
return NULL; return NULL;
} }
void callback() { void callback() {
printf("enter callback\n"); printf("callback is triggered\n");
} }
void executor(void(*fn)(void*), void *arg) { void executor(void(*fn)(void*), void *arg) {
printf("executor receive task\n"); printf("executor receive task\n");
// fn(arg); // fn(arg);
struct pthread_wrapper_t *kk = (struct pthread_wrapper_t*)malloc(sizeof(struct pthread_wrapper_t)); pthread_wrapper_t *kk = (pthread_wrapper_t*)malloc(sizeof(pthread_wrapper_t));
kk->fn = fn; kk->fn = fn;
kk->arg = arg; kk->arg = arg;
pthread_t pp; pthread_t pp;
pthread_create(&pp, NULL, pthread_wrapper, (void*)kk); pthread_create(&pp, NULL, pthread_wrapper, (void*)kk);
} }
int main() { int main() {
printf("prebuild available -> %d\n", is_all_cases_prebuild_available()); // printf("prebuild available -> %d\n", is_all_cases_prebuild_available());
//
// printf("prebuild begin\n"); // printf("prebuild begin\n");
// all_cases_prebuild(); // all_cases_prebuild();
// printf("prebuild complete\n"); // printf("prebuild complete\n");
//
printf("prebuild begin\n"); // printf("prebuild begin\n");
all_cases_prebuild_async(executor, callback); // all_cases_prebuild_async(executor, callback);
printf("prebuild func exited\n"); // printf("prebuild func exited\n");
printf("prebuild available -> %d\n", is_all_cases_prebuild_available()); // printf("prebuild available -> %d\n", is_all_cases_prebuild_available());
// printf("start sleep 3s\n");
// sleep(3);
//
// printf("prebuild available -> %d\n", is_all_cases_prebuild_available());
printf("build available -> %d\n", is_all_cases_available());
// printf("build begin\n");
// all_cases_build();
// printf("build complete\n");
// printf("build begin\n");
// all_cases_build_async(executor, callback);
// printf("build func exited\n");
// printf("build available -> %d\n", is_all_cases_available());
// printf("start sleep 3s\n");
// sleep(3);
// printf("build begin\n");
// all_cases_parallel_build(executor);
// printf("build complete\n");
printf("build begin\n");
all_cases_parallel_build_async(executor, callback);
printf("build func exited\n");
printf("build available -> %d\n", is_all_cases_available());
printf("start sleep 3s\n"); printf("start sleep 3s\n");
sleep(3); sleep(3);
printf("prebuild available -> %d\n", is_all_cases_prebuild_available()); printf("build available -> %d\n", is_all_cases_available());
return 0; return 0;
} }

Loading…
Cancel
Save