summaryrefslogtreecommitdiff
path: root/tests/pthread.l
blob: 7d0dc7fd5bd27601c42860e5cd967950b7be89a8 (plain)
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
53
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * This file is part of flex.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the University nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

%{
/* A scanner file to build "scanner.c".
   Input language is any text made of spaces, newlines, and alphanumerics.

   We create N_THREADS number of threads. Each thread has it's own scanner.
   Each thread selects one of the files specified in ARGV, scans it, then
   closes it. This is repeated N_SCANS numebr of times for each thread.

   The idea is to press the scanner to break under threads.
   If we see  "Scanner Jammed", then we know

*/
#include <stdio.h>
#include <stdlib.h>
#include <config.h>

#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif

/* A naive test for segfaults when accessing yytext. */
static int process_text(char* s, yyscan_t  scanner);

%}

%option 8bit prefix="test"
%option nounput nomain nodefault noinput
%option yywrap
%option reentrant
%option warn

    /* Arbitrary states.*/
%x STATE_1
%x STATE_2

%%

    #define NUMBER 200
    #define WORD   201

<INITIAL>[[:digit:]]+ { BEGIN(STATE_1); process_text(yytext,yyscanner); return NUMBER; }
<INITIAL>[[:alpha:]]+ { BEGIN(STATE_2); process_text(yytext,yyscanner); return WORD; }

<STATE_1>[[:alpha:]]+ { BEGIN(0); process_text(yytext,yyscanner); return WORD; }
<STATE_1>[[:digit:]]+ { BEGIN(0); process_text(yytext,yyscanner); return NUMBER; }

<STATE_2>[[:alpha:]]+ { BEGIN(0); process_text(yytext,yyscanner); return WORD; }
<STATE_2>[[:digit:]]+ { BEGIN(0); process_text(yytext,yyscanner); return NUMBER; }

<INITIAL,STATE_1,STATE_2>" "|\t|\r|\n { process_text(yytext,yyscanner); }
<INITIAL,STATE_1,STATE_2>[^[:alnum:][:space:]\t\r\n] {
        /*fprintf(stderr,"*** Error: bad input char '%c'.\n", yytext[0]); */
        yyterminate();
    }
<INITIAL,STATE_1,STATE_2>[[:space:]\r\n]+  { }
%%

int yywrap( yyscan_t  scanner) {
    (void)scanner;
    return 1;
}
static int process_text(char* s, yyscan_t  scanner)
{
    (void)scanner;
    return (int)(*s) + (int) *(s + yyget_leng(scanner)-1);
}

int main(int ARGC, char *ARGV[]);

#ifndef HAVE_LIBPTHREAD
  int main (int ARGC, char *ARGV[]) {
    printf(
       "TEST ABORTED because pthread library not available \n"
       "-- This is expected on some systems. It is not a flex error.\n" );
    return 0;
  }
#else

#define N_THREADS 4
#define N_SCANS   20
#define INPUT_FILE "test.input"

/* Each thread selects the next file to scan in round-robin fashion.
   If there are less files than threads, some threads may block. */

static pthread_mutex_t next_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t go_ahead  = PTHREAD_MUTEX_INITIALIZER;
static int n_files, next_file;

static pthread_mutex_t *file_locks;
static char **filenames;


static void * thread_func ( void* arg )
{
    int i;

    (void)arg;

    /* Wait for go-ahead. */
    pthread_mutex_lock( &go_ahead);
    pthread_mutex_unlock(&go_ahead);

    for( i =0 ; i < N_SCANS ; i++ )
    {
        int next;
        yyscan_t  scanner;
        FILE * fp;

        pthread_mutex_lock ( &next_lock );
        next = (next_file++) % n_files;
        pthread_mutex_unlock ( &next_lock );

        pthread_mutex_lock ( &file_locks[ next ] );

        yylex_init( &scanner );
        /*printf("Scanning file %s  #%d\n",filenames[next],i); fflush(stdout); */
        if((fp = fopen(filenames[next],"r"))==NULL) {
            perror("fopen");
            return NULL;
        }
        yyset_in(fp,scanner);

        while( yylex( scanner) != 0)
        {
        }
        fclose(fp);
        yylex_destroy(scanner);
        pthread_mutex_unlock ( &file_locks[ next ] );
    }
    return NULL;
}

int main (int ARGC, char *ARGV[])
{
    int i;
    pthread_t threads[N_THREADS];

    if( ARGC < 2 ) {
        fprintf(stderr,"*** Error: No filenames specified.\n");
        exit(-1);
    }

    /* Allocate and initialize the locks. One for each filename in ARGV. */
    file_locks = malloc((size_t) (ARGC-1) * sizeof(pthread_mutex_t));
    for( i = 0; i < ARGC-1; i++)
        pthread_mutex_init( &file_locks[i], NULL );

    n_files = ARGC -1;
    filenames = ARGV + 1;
    next_file = 0;

    /* prevent threads from starting until all threads have been created. */
    pthread_mutex_lock(&go_ahead);

    /* Create N threads then wait for them. */
    for(i =0; i < N_THREADS ; i++ ) {
        if( pthread_create( &threads[i], NULL, thread_func, NULL) != 0)
        {
            fprintf(stderr, "*** Error: pthread_create failed.\n");
            exit(-1);
        }
        printf("Created thread %d.\n",i); fflush(stdout);
    }

    /* Tell threads to begin. */
    pthread_mutex_unlock(&go_ahead);

    for(i =0; i < N_THREADS ; i++ ) {
        pthread_join ( threads[i], NULL );
        printf("Thread %d done.\n", i ); fflush(stdout);
    }

    for( i = 0; i < ARGC-1; i++)
        pthread_mutex_destroy( &file_locks[i] );

    pthread_mutex_destroy( &next_lock );
    pthread_mutex_destroy( &go_ahead );
    free( file_locks );
    printf("TEST RETURNING OK.\n");
    return 0;
}

#endif /* HAVE_LIBPTHREAD */