| 1 | /* |
| 2 | import.c -- Execute Lisp code from C-generated threads |
| 3 | */ |
| 4 | |
| 5 | #include <stdlib.h> |
| 6 | #include <stdio.h> |
| 7 | #include <unistd.h> |
| 8 | #include <pthread.h> |
| 9 | |
| 10 | /* |
| 11 | * GOAL: To execute lisp code from threads which have not |
| 12 | * been generated by our lisp environment. |
| 13 | * |
| 14 | * ASSUMES: ECL has been configured with threads (--enable-threads) |
| 15 | * and installed somewhere on the path. |
| 16 | * |
| 17 | * COMPILE: Run "make" from the command line. |
| 18 | * |
| 19 | * |
| 20 | * When this example is compiled and run, it generates a number of |
| 21 | * threads, each one executing some interpreted code -- in this case |
| 22 | * a bunch of PRINT statements. |
| 23 | * |
| 24 | * Importing other threads into lisp is possible if these threads have |
| 25 | * been intercepted by the garbage collector. The way to do it is to |
| 26 | * include the <ecl.h> on the source code that generates the threads, |
| 27 | * as we do here. This takes care of replacing calls to phtread_create |
| 28 | * or CreateThread (in unix and Windows respectively) with the |
| 29 | * GC_pthread_create and GC_CreateThread functions. |
| 30 | */ |
| 31 | |
| 32 | /* Unfortunately, the Bohem-Weiser garbage collector does not keep track |
| 33 | * of its configuration. We have to add the following flags by hand in |
| 34 | * order to force pthread_create being redefined. |
| 35 | */ |
| 36 | #define GC_THREADS |
| 37 | #define _REENTRANT |
| 38 | |
| 39 | #include <ecl/ecl.h> |
| 40 | |
| 41 | static void * |
| 42 | thread_entry_point(void *data) |
| 43 | { |
| 44 | cl_object form = (cl_object)data; |
| 45 | |
| 46 | /* |
| 47 | * This is the entry point of the threads we have created. |
| 48 | * These threads have no valid lisp environment. The following |
| 49 | * routine initializes the lisp and makes it ready for working |
| 50 | * in this thread. |
| 51 | */ |
| 52 | ecl_import_current_thread(ECL_NIL, ECL_NIL); |
| 53 | |
| 54 | /* |
| 55 | * Here we execute some lisp code code. |
| 56 | */ |
| 57 | cl_eval(form); |
| 58 | |
| 59 | /* |
| 60 | * Finally, when we exit the thread we have to release the |
| 61 | * resources allocated by the lisp environment. |
| 62 | */ |
| 63 | ecl_release_current_thread(); |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | int main(int narg, char **argv) |
| 69 | { |
| 70 | pthread_t child_thread; |
| 71 | int i, code; |
| 72 | |
| 73 | /* |
| 74 | * First of all, we have to initialize the ECL environment. |
| 75 | * This should be done from the main thread. |
| 76 | */ |
| 77 | cl_boot(narg, argv); |
| 78 | |
| 79 | /* |
| 80 | * Here we spawn 10 threads using the OS functions. The |
| 81 | * current version is for Unix and uses pthread_create. |
| 82 | * Since we have included <gc.h>, pthread_create will be |
| 83 | * replaced with the appropriate routine from the garbage |
| 84 | * collector. |
| 85 | */ |
| 86 | cl_object sym_print = c_string_to_object("PRINT"); |
| 87 | |
| 88 | /* |
| 89 | * This array will keep the forms we want to evaluate from |
| 90 | * being garbage collected. |
| 91 | */ |
| 92 | volatile cl_object forms[4]; |
| 93 | |
| 94 | for (i = 0; i < 4; i++) { |
| 95 | forms[i] = cl_list(2, sym_print, MAKE_FIXNUM(i)); |
| 96 | code = pthread_create(&child_thread, NULL, thread_entry_point, |
| 97 | (void*)forms[i]); |
| 98 | if (code) { |
| 99 | printf("Unable to create thread\n"); |
| 100 | exit(1); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Here we wait for the last thread to finish. |
| 106 | */ |
| 107 | pthread_join(child_thread, NULL); |
| 108 | |
| 109 | return 0; |
| 110 | } |