apitest.c 3.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <pthread.h>
#include "ierrors.h"
#include "iapi.h"
#include <gp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUM_THREADS 10

static int my_argc;
static char **my_argv;
static int my_argv_file;

typedef struct my_stdio_s
{
    FILE *stdout;
    FILE *stderr;
} my_stdio;

/* stdio functions */
static int GSDLLCALL
my_stdin(void *instance, char *buf, int len)
{
    return 0; /* We don't support stdin */
}

static int GSDLLCALL
my_stdout(void *instance, const char *str, int len)
{
    my_stdio *stdio = (my_stdio *)instance;

    fwrite(str, 1, len, stdio->stdout);
    fflush(stdio->stdout);
    return len;
}

static int GSDLLCALL
my_stderr(void *instance, const char *str, int len)
{
    my_stdio *stdio = (my_stdio *)instance;

    fwrite(str, 1, len, stdio->stderr);
    fflush(stdio->stderr);
    return len;
}

static void *gs_main(void *arg)
{
    int threadnum = (int)(void *)arg;
    int code, code1;
    char text[256];
53
    void *minst = NULL;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    char **gsargv;
    int gsargc;
    int i, pos, len;
    my_stdio stdio;

    snprintf(text, sizeof(text), "stdout.%d", threadnum);
    stdio.stdout = gp_fopen(text, "w");
    snprintf(text, sizeof(text), "stderr.%d", threadnum);
    stdio.stderr = gp_fopen(text, "w");

    gsargv = malloc(sizeof(*gsargv)*my_argc);
    if (!gsargv)
    {
        fprintf(stdio.stderr, "Failed to allocate arg space in thread %d\n", threadnum);
        return (void *)-1;
    }

    for (i=0; i < my_argc; i++)
        gsargv[i] = my_argv[i];
    gsargv[my_argv_file] = text;
    gsargc = my_argc;
    
    strncpy(text, my_argv[my_argv_file], sizeof(text));
    text[sizeof(text)-1]=0;
    pos = strlen(text);
    len = sizeof(text)+1-pos;
    snprintf(text+pos, len, "%d", threadnum);

    code = gsapi_new_instance(&minst, &stdio);
    if (code < 0)
    {
        fprintf(stdio.stderr, "gsapi_new_instance failure in thread %d\n", threadnum);
86
        free(gsargv);
87 88 89 90 91 92 93
        return (void *)-1;
    }

    gsapi_set_stdio(minst, my_stdin, my_stdout, my_stderr);

    code = gsapi_init_with_args(minst, gsargc, gsargv);
    code1 = gsapi_exit(minst);
94
    if ((code == 0) || (code == gs_error_Quit))
95 96 97 98
        code = code1;

    gsapi_delete_instance(minst);

99 100
    free(gsargv);

101
    if ((code == 0) || (code == gs_error_Quit))
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        return (void *)0;

    return (void *)1;
}

int main(int argc, char *argv[])
{
    int i;
    pthread_t thread[NUM_THREADS];

    my_argc = argc;
    my_argv = argv;
    
    for (i=0; i < argc; i++)
        if (!strcmp(argv[i], "-o"))
            break;
    
    if (i >= argc-1)
    {
        fprintf(stderr, "Expected a -o argument to rewrite!\n");
        exit(EXIT_FAILURE);
    }
    my_argv_file = i+1;

    for (i=0; i < NUM_THREADS; i++)
    {
        if (pthread_create(&thread[i], NULL, gs_main, (void *)i) != 0)
        {
            fprintf(stderr, "Thread %d creation failed\n", i);
            exit(EXIT_FAILURE);
        }
    }

    for (i=0; i < NUM_THREADS; i++)
    {
        if (pthread_join(thread[i], NULL) != 0)
        {
            fprintf(stderr, "Thread %d join failed\n", i);
            exit(EXIT_FAILURE);
        }
    }
    return EXIT_SUCCESS;
}