Paste: pthreads test

Author: jedahu
Mode: factor
Date: Fri, 24 Jul 2009 05:45:53
Plain Text |
USING: alien.inline.syntax alien.marshall.syntax alien.syntax
tools.test kernel accessors alien fry math namespaces ;
IN: pthreads

DELETE-C-LIBRARY: test
C-LIBRARY: test

C-INCLUDE: <pthread.h>
C-INCLUDE: <stdio.h>
C-INCLUDE: <stdlib.h>

CM-STRUCTURE: thread_info
    { "int" "cside" }
    { "int" "fside" } ;

RAW-C:
thread_info info;

char* run_me(void* x, void* y) {
    void* (*a) (void*) = (void* (*) (void*)) x;
    void* (*b) (void*) = (void* (*) (void*)) y;
    info.cside = 0;
    info.fside = 0;
    pthread_t a_id, b_id;
    pthread_create(&a_id, NULL, a, &info);
    pthread_create(&b_id, NULL, b, &info);
    pthread_join(a_id, NULL);
    pthread_join(b_id, NULL);
    char* out = malloc(32 * sizeof(char));
    sprintf(out, "c %i f %i", info.cside, info.fside);
    return out;
}
;

;C-LIBRARY

FUNCTION: char* run_me ( void* x, void* y ) ;

SYMBOL: x

: fa ( -- alien )
       "void*" { "void*" } "cdecl" [
           \ thread_info new swap >>underlying dup
           '[ _ [ 1+ ] change-cside drop x inc ] 100 swap times
           x get >>fside drop f
       ] alien-callback ;

: fb ( -- alien )
    "void*" { "void*" } "cdecl" [
        \ thread_info new swap >>underlying dup
        '[ _ [ 1- ] change-cside drop x dec ] 100 swap times
        x get >>fside drop f
    ] alien-callback ;


! fa fb run_me  => The image refers to a library or symbol that was not found at load time

Annotation: this works, the above doesn't

Author: jedahu
Mode: factor
Date: Fri, 24 Jul 2009 05:48:32
Plain Text |
"run_me" "libpthreads_test.so" inline-library-file dlopen dlsym

=> ALIEN: 140692921116300

New Annotation

Summary:
Author:
Mode:
Body: