| 1 |
/* |
| 2 |
* This file is part of flex. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* Neither the name of the University nor the names of its contributors |
| 15 |
* may be used to endorse or promote products derived from this software |
| 16 |
* without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 19 |
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 20 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 21 |
* PURPOSE. |
| 22 |
*/ |
| 23 |
|
| 24 |
%{ |
| 25 |
/* A scanner file to build "scanner.c". |
| 26 |
Input language is any text made of spaces, newlines, and alphanumerics. |
| 27 |
|
| 28 |
We create N_THREADS number of threads. Each thread has it's own scanner. |
| 29 |
Each thread selects one of the files specified in ARGV, scans it, then |
| 30 |
closes it. This is repeated N_SCANS number of times for each thread. |
| 31 |
|
| 32 |
The idea is to press the scanner to break under threads. |
| 33 |
If we see "Scanner Jammed", then we know |
| 34 |
|
| 35 |
*/ |
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <config.h> |
| 39 |
|
| 40 |
#ifdef HAVE_PTHREAD_H |
| 41 |
#include <pthread.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
/* A naive test for segfaults when accessing yytext. */ |
| 45 |
static int process_text(char* s, yyscan_t scanner); |
| 46 |
|
| 47 |
%} |
| 48 |
|
| 49 |
%option 8bit prefix="test" |
| 50 |
%option nounput nomain nodefault noinput |
| 51 |
%option yywrap |
| 52 |
%option reentrant |
| 53 |
%option warn |
| 54 |
|
| 55 |
/* Arbitrary states.*/ |
| 56 |
%x STATE_1 |
| 57 |
%x STATE_2 |
| 58 |
|
| 59 |
%% |
| 60 |
|
| 61 |
#define NUMBER 200 |
| 62 |
#define WORD 201 |
| 63 |
|
| 64 |
<INITIAL>[[:digit:]]+ { BEGIN(STATE_1); process_text(yytext,yyscanner); return NUMBER; } |
| 65 |
<INITIAL>[[:alpha:]]+ { BEGIN(STATE_2); process_text(yytext,yyscanner); return WORD; } |
| 66 |
|
| 67 |
<STATE_1>[[:alpha:]]+ { BEGIN(0); process_text(yytext,yyscanner); return WORD; } |
| 68 |
<STATE_1>[[:digit:]]+ { BEGIN(0); process_text(yytext,yyscanner); return NUMBER; } |
| 69 |
|
| 70 |
<STATE_2>[[:alpha:]]+ { BEGIN(0); process_text(yytext,yyscanner); return WORD; } |
| 71 |
<STATE_2>[[:digit:]]+ { BEGIN(0); process_text(yytext,yyscanner); return NUMBER; } |
| 72 |
|
| 73 |
<INITIAL,STATE_1,STATE_2>" "|\t|\r|\n { process_text(yytext,yyscanner); } |
| 74 |
<INITIAL,STATE_1,STATE_2>[^[:alnum:][:space:]\t\r\n] { |
| 75 |
/*fprintf(stderr,"*** Error: bad input char '%c'.\n", yytext[0]); */ |
| 76 |
yyterminate(); |
| 77 |
} |
| 78 |
<INITIAL,STATE_1,STATE_2>[[:space:]\r\n]+ { } |
| 79 |
%% |
| 80 |
|
| 81 |
int testwrap( yyscan_t scanner) { |
| 82 |
(void)scanner; |
| 83 |
return 1; |
| 84 |
} |
| 85 |
static int process_text(char* s, yyscan_t scanner) |
| 86 |
{ |
| 87 |
(void)scanner; |
| 88 |
return (int)(*s) + (int) *(s + testget_leng(scanner)-1); |
| 89 |
} |
| 90 |
|
| 91 |
int main(int ARGC, char *ARGV[]); |
| 92 |
|
| 93 |
#ifndef HAVE_PTHREAD_H |
| 94 |
int main (int ARGC, char *ARGV[]) { |
| 95 |
puts( |
| 96 |
"TEST ABORTED because pthread library not available \n" |
| 97 |
"-- This is expected on some systems. It is not a flex error."); |
| 98 |
/* Exit status for a skipped test */ |
| 99 |
return 77; |
| 100 |
} |
| 101 |
#else |
| 102 |
|
| 103 |
#define N_THREADS 4 |
| 104 |
#define N_SCANS 20 |
| 105 |
|
| 106 |
/* Each thread selects the next file to scan in round-robin fashion. |
| 107 |
If there are less files than threads, some threads may block. */ |
| 108 |
|
| 109 |
static pthread_mutex_t next_lock = PTHREAD_MUTEX_INITIALIZER; |
| 110 |
static pthread_mutex_t go_ahead = PTHREAD_MUTEX_INITIALIZER; |
| 111 |
static int n_files, next_file; |
| 112 |
|
| 113 |
static pthread_mutex_t *file_locks; |
| 114 |
static char **filenames; |
| 115 |
|
| 116 |
|
| 117 |
static void * thread_func ( void* arg ) |
| 118 |
{ |
| 119 |
int i; |
| 120 |
|
| 121 |
(void)arg; |
| 122 |
|
| 123 |
/* Wait for go-ahead. */ |
| 124 |
pthread_mutex_lock( &go_ahead); |
| 125 |
pthread_mutex_unlock(&go_ahead); |
| 126 |
|
| 127 |
for( i =0 ; i < N_SCANS ; i++ ) |
| 128 |
{ |
| 129 |
int next; |
| 130 |
yyscan_t scanner; |
| 131 |
FILE * fp; |
| 132 |
|
| 133 |
pthread_mutex_lock ( &next_lock ); |
| 134 |
next = (next_file++) % n_files; |
| 135 |
pthread_mutex_unlock ( &next_lock ); |
| 136 |
|
| 137 |
pthread_mutex_lock ( &file_locks[ next ] ); |
| 138 |
|
| 139 |
testlex_init( &scanner ); |
| 140 |
/*printf("Scanning file %s #%d\n",filenames[next],i); fflush(stdout); */ |
| 141 |
if((fp = fopen(filenames[next],"r"))==NULL) { |
| 142 |
perror("fopen"); |
| 143 |
return NULL; |
| 144 |
} |
| 145 |
testset_in(fp,scanner); |
| 146 |
|
| 147 |
while( testlex( scanner) != 0) |
| 148 |
{ |
| 149 |
} |
| 150 |
fclose(fp); |
| 151 |
testlex_destroy(scanner); |
| 152 |
pthread_mutex_unlock ( &file_locks[ next ] ); |
| 153 |
} |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
|
| 157 |
int main (int ARGC, char *ARGV[]) |
| 158 |
{ |
| 159 |
int i; |
| 160 |
pthread_t threads[N_THREADS]; |
| 161 |
|
| 162 |
if( ARGC < 2 ) { |
| 163 |
fprintf(stderr,"*** Error: No filenames specified.\n"); |
| 164 |
exit(-1); |
| 165 |
} |
| 166 |
|
| 167 |
/* Allocate and initialize the locks. One for each filename in ARGV. */ |
| 168 |
file_locks = malloc((size_t) (ARGC-1) * sizeof(pthread_mutex_t)); |
| 169 |
for( i = 0; i < ARGC-1; i++) |
| 170 |
pthread_mutex_init( &file_locks[i], NULL ); |
| 171 |
|
| 172 |
n_files = ARGC -1; |
| 173 |
filenames = ARGV + 1; |
| 174 |
next_file = 0; |
| 175 |
|
| 176 |
/* prevent threads from starting until all threads have been created. */ |
| 177 |
pthread_mutex_lock(&go_ahead); |
| 178 |
|
| 179 |
/* Create N threads then wait for them. */ |
| 180 |
for(i =0; i < N_THREADS ; i++ ) { |
| 181 |
if( pthread_create( &threads[i], NULL, thread_func, NULL) != 0) |
| 182 |
{ |
| 183 |
fprintf(stderr, "*** Error: pthread_create failed.\n"); |
| 184 |
exit(-1); |
| 185 |
} |
| 186 |
printf("Created thread %d.\n",i); fflush(stdout); |
| 187 |
} |
| 188 |
|
| 189 |
/* Tell threads to begin. */ |
| 190 |
pthread_mutex_unlock(&go_ahead); |
| 191 |
|
| 192 |
for(i =0; i < N_THREADS ; i++ ) { |
| 193 |
pthread_join ( threads[i], NULL ); |
| 194 |
printf("Thread %d done.\n", i ); fflush(stdout); |
| 195 |
} |
| 196 |
|
| 197 |
for( i = 0; i < ARGC-1; i++) |
| 198 |
pthread_mutex_destroy( &file_locks[i] ); |
| 199 |
|
| 200 |
pthread_mutex_destroy( &next_lock ); |
| 201 |
pthread_mutex_destroy( &go_ahead ); |
| 202 |
free( file_locks ); |
| 203 |
printf("TEST RETURNING OK.\n"); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
#endif /* HAVE_PTHREAD_H */ |
| 208 |
|