mirror of https://github.com/dnomd343/klotski.git
				
				
			
				 6 changed files with 143 additions and 9 deletions
			
			
		@ -0,0 +1,27 @@ | 
				
			|||
#include "klotski.h" | 
				
			|||
#include "all_cases.h" | 
				
			|||
 | 
				
			|||
#include <iostream> | 
				
			|||
#include <future> | 
				
			|||
 | 
				
			|||
using klotski::cases::AllCases; | 
				
			|||
using klotski::cases::BasicRanges; | 
				
			|||
 | 
				
			|||
void all_cases_prebuild() { | 
				
			|||
    BasicRanges::Instance().Build(); | 
				
			|||
} | 
				
			|||
 | 
				
			|||
void all_cases_prebuild_async(executor_t executor, notifier_t callback) { | 
				
			|||
    executor([](void *cb) { | 
				
			|||
        all_cases_prebuild(); | 
				
			|||
        ((notifier_t)cb)(); | 
				
			|||
    }, (void*)callback); | 
				
			|||
} | 
				
			|||
 | 
				
			|||
int is_all_cases_prebuild_available() { | 
				
			|||
    if (BasicRanges::Instance().IsAvailable()) { | 
				
			|||
        return KLOTSKI_TRUE; | 
				
			|||
    } else { | 
				
			|||
        return KLOTSKI_FALSE; | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
@ -0,0 +1,47 @@ | 
				
			|||
#ifndef KLOTSKI_H_ | 
				
			|||
#define KLOTSKI_H_ | 
				
			|||
 | 
				
			|||
#ifdef __cplusplus | 
				
			|||
#include <cstdint> | 
				
			|||
#else | 
				
			|||
#include <stdint.h> | 
				
			|||
#endif | 
				
			|||
 | 
				
			|||
#define KLOTSKI_TRUE  1 | 
				
			|||
#define KLOTSKI_FALSE 0 | 
				
			|||
 | 
				
			|||
#define EXTERN extern | 
				
			|||
 | 
				
			|||
typedef void (*notifier_t)(); | 
				
			|||
typedef void (*executor_t)(void (*fn)(void*), void *arg); | 
				
			|||
 | 
				
			|||
#ifdef __cplusplus | 
				
			|||
extern "C" { | 
				
			|||
#endif | 
				
			|||
 | 
				
			|||
/// Execute all_cases pre-build work, blocking until the end of the build. If
 | 
				
			|||
/// the build has been completed, the function will return directly.
 | 
				
			|||
EXTERN void all_cases_prebuild(); | 
				
			|||
 | 
				
			|||
/// Execute all_cases pre-build work, the function will return directly without
 | 
				
			|||
/// blocking, and will be notified with callback when completed. If the build
 | 
				
			|||
/// has been completed, the parameter function will still be called.
 | 
				
			|||
EXTERN void all_cases_prebuild_async(executor_t executor, notifier_t callback); | 
				
			|||
 | 
				
			|||
/// Returns the pre-build status of all_cases without any block, value is 0 if
 | 
				
			|||
/// not completed, non-0 otherwise.
 | 
				
			|||
EXTERN int is_all_cases_prebuild_available(); | 
				
			|||
 | 
				
			|||
//EXTERN void all_cases_build();
 | 
				
			|||
//EXTERN int is_all_cases_available();
 | 
				
			|||
 | 
				
			|||
//extern const uint32_t ALL_CASES_SIZE;
 | 
				
			|||
//EXTERN_FUNC void export_all_cases(uint64_t *buffer);
 | 
				
			|||
 | 
				
			|||
//extern const uint32_t BASIC_RANGES_SIZE;
 | 
				
			|||
//EXTERN_FUNC void export_basic_ranges(uint32_t *buffer);
 | 
				
			|||
#ifdef __cplusplus | 
				
			|||
} | 
				
			|||
#endif | 
				
			|||
 | 
				
			|||
#endif /* KLOTSKI_H_ */ | 
				
			|||
@ -1,6 +1,59 @@ | 
				
			|||
#include "klotski.h" | 
				
			|||
#include <printf.h> | 
				
			|||
#include <pthread.h> | 
				
			|||
#include <stdlib.h> | 
				
			|||
#include <unistd.h> | 
				
			|||
#include "core/ffi/klotski.h" | 
				
			|||
 | 
				
			|||
struct pthread_wrapper_t { | 
				
			|||
    void (*fn)(void*); | 
				
			|||
    void *arg; | 
				
			|||
}; | 
				
			|||
 | 
				
			|||
void* pthread_wrapper(void *arg) { | 
				
			|||
 | 
				
			|||
    printf("enter wrapper\n"); | 
				
			|||
 | 
				
			|||
    struct pthread_wrapper_t *kk = (struct pthread_wrapper_t*)arg; | 
				
			|||
 | 
				
			|||
    kk->fn(kk->arg); | 
				
			|||
 | 
				
			|||
    return NULL; | 
				
			|||
} | 
				
			|||
 | 
				
			|||
void callback() { | 
				
			|||
    printf("enter callback\n"); | 
				
			|||
} | 
				
			|||
 | 
				
			|||
void executor(void(*fn)(void*), void *arg) { | 
				
			|||
    printf("executor receive task\n"); | 
				
			|||
//    fn(arg);
 | 
				
			|||
 | 
				
			|||
    struct pthread_wrapper_t *kk = (struct pthread_wrapper_t*)malloc(sizeof(struct pthread_wrapper_t)); | 
				
			|||
 | 
				
			|||
    kk->fn = fn; | 
				
			|||
    kk->arg = arg; | 
				
			|||
 | 
				
			|||
    pthread_t pp; | 
				
			|||
    pthread_create(&pp, NULL, pthread_wrapper, (void*)kk); | 
				
			|||
 | 
				
			|||
} | 
				
			|||
 | 
				
			|||
int main() { | 
				
			|||
    tmain(); | 
				
			|||
    printf("prebuild available -> %d\n", is_all_cases_prebuild_available()); | 
				
			|||
 | 
				
			|||
//    printf("prebuild begin\n");
 | 
				
			|||
//    all_cases_prebuild();
 | 
				
			|||
//    printf("prebuild complete\n");
 | 
				
			|||
 | 
				
			|||
    printf("prebuild begin\n"); | 
				
			|||
    all_cases_prebuild_async(executor, callback); | 
				
			|||
    printf("prebuild func exited\n"); | 
				
			|||
    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()); | 
				
			|||
 | 
				
			|||
    return 0; | 
				
			|||
} | 
				
			|||
 | 
				
			|||
					Loading…
					
					
				
		Reference in new issue