@@ -1,5 +1,6 @@ |
| 1 | 1 | module ferp_output |
| 2 | 2 | !> Output formatting for FERP |
| 3 | + !> All output functions are thread-safe via OMP critical sections |
| 3 | 4 | use ferp_kinds |
| 4 | 5 | use ferp_options |
| 5 | 6 | use, intrinsic :: iso_fortran_env, only: output_unit, error_unit |
@@ -41,6 +42,7 @@ contains |
| 41 | 42 | |
| 42 | 43 | subroutine print_match(line, filename, line_num, byte_off, opts) |
| 43 | 44 | !> Print a matching line with appropriate prefixes |
| 45 | + !> Thread-safe via OMP critical section |
| 44 | 46 | character(len=*), intent(in) :: line |
| 45 | 47 | character(len=*), intent(in) :: filename |
| 46 | 48 | integer, intent(in) :: line_num |
@@ -50,6 +52,7 @@ contains |
| 50 | 52 | ! Quiet mode - no output |
| 51 | 53 | if (opts%quiet) return |
| 52 | 54 | |
| 55 | + !$omp critical(output_lock) |
| 53 | 56 | ! Print filename prefix |
| 54 | 57 | if (opts%show_filename .and. .not. opts%hide_filename) then |
| 55 | 58 | if (opts%null_after_filename) then |
@@ -83,11 +86,13 @@ contains |
| 83 | 86 | |
| 84 | 87 | ! Line-buffered mode |
| 85 | 88 | if (opts%line_buffered) flush(output_unit) |
| 89 | + !$omp end critical(output_lock) |
| 86 | 90 | |
| 87 | 91 | end subroutine print_match |
| 88 | 92 | |
| 89 | 93 | subroutine print_context_line(line, filename, line_num, byte_off, opts) |
| 90 | 94 | !> Print a context line (uses - instead of : as separator) |
| 95 | + !> Thread-safe via OMP critical section |
| 91 | 96 | character(len=*), intent(in) :: line |
| 92 | 97 | character(len=*), intent(in) :: filename |
| 93 | 98 | integer, intent(in) :: line_num |
@@ -96,6 +101,7 @@ contains |
| 96 | 101 | |
| 97 | 102 | if (opts%quiet) return |
| 98 | 103 | |
| 104 | + !$omp critical(output_lock) |
| 99 | 105 | ! Print filename prefix with - separator |
| 100 | 106 | if (opts%show_filename .and. .not. opts%hide_filename) then |
| 101 | 107 | if (opts%null_after_filename) then |
@@ -129,31 +135,37 @@ contains |
| 129 | 135 | |
| 130 | 136 | ! Line-buffered mode |
| 131 | 137 | if (opts%line_buffered) flush(output_unit) |
| 138 | + !$omp end critical(output_lock) |
| 132 | 139 | |
| 133 | 140 | end subroutine print_context_line |
| 134 | 141 | |
| 135 | 142 | subroutine print_separator(opts) |
| 136 | 143 | !> Print group separator between context groups |
| 144 | + !> Thread-safe via OMP critical section |
| 137 | 145 | type(grep_options), intent(in) :: opts |
| 138 | 146 | |
| 139 | 147 | if (opts%quiet) return |
| 140 | 148 | if (opts%no_group_separator) return |
| 141 | 149 | |
| 150 | + !$omp critical(output_lock) |
| 142 | 151 | write(output_unit, '(A)') trim(opts%group_separator) |
| 143 | 152 | |
| 144 | 153 | ! Line-buffered mode |
| 145 | 154 | if (opts%line_buffered) flush(output_unit) |
| 155 | + !$omp end critical(output_lock) |
| 146 | 156 | |
| 147 | 157 | end subroutine print_separator |
| 148 | 158 | |
| 149 | 159 | subroutine print_count(count, filename, opts) |
| 150 | 160 | !> Print match count (for -c option) |
| 161 | + !> Thread-safe via OMP critical section |
| 151 | 162 | integer, intent(in) :: count |
| 152 | 163 | character(len=*), intent(in) :: filename |
| 153 | 164 | type(grep_options), intent(in) :: opts |
| 154 | 165 | |
| 155 | 166 | if (opts%quiet) return |
| 156 | 167 | |
| 168 | + !$omp critical(output_lock) |
| 157 | 169 | if (opts%show_filename .and. .not. opts%hide_filename) then |
| 158 | 170 | if (opts%null_after_filename) then |
| 159 | 171 | write(output_unit, '(A,A,I0)') trim(filename), char(0), count |
@@ -166,16 +178,19 @@ contains |
| 166 | 178 | |
| 167 | 179 | ! Line-buffered mode |
| 168 | 180 | if (opts%line_buffered) flush(output_unit) |
| 181 | + !$omp end critical(output_lock) |
| 169 | 182 | |
| 170 | 183 | end subroutine print_count |
| 171 | 184 | |
| 172 | 185 | subroutine print_filename(filename, opts) |
| 173 | 186 | !> Print just filename (for -l, -L options) |
| 187 | + !> Thread-safe via OMP critical section |
| 174 | 188 | character(len=*), intent(in) :: filename |
| 175 | 189 | type(grep_options), intent(in) :: opts |
| 176 | 190 | |
| 177 | 191 | if (opts%quiet) return |
| 178 | 192 | |
| 193 | + !$omp critical(output_lock) |
| 179 | 194 | if (opts%null_after_filename) then |
| 180 | 195 | write(output_unit, '(A,A)', advance='no') trim(filename), char(0) |
| 181 | 196 | else |
@@ -184,25 +199,30 @@ contains |
| 184 | 199 | |
| 185 | 200 | ! Line-buffered mode |
| 186 | 201 | if (opts%line_buffered) flush(output_unit) |
| 202 | + !$omp end critical(output_lock) |
| 187 | 203 | |
| 188 | 204 | end subroutine print_filename |
| 189 | 205 | |
| 190 | 206 | subroutine print_binary_match(filename, opts) |
| 191 | 207 | !> Print binary file match message |
| 208 | + !> Thread-safe via OMP critical section |
| 192 | 209 | character(len=*), intent(in) :: filename |
| 193 | 210 | type(grep_options), intent(in) :: opts |
| 194 | 211 | |
| 195 | 212 | if (opts%quiet) return |
| 196 | 213 | |
| 214 | + !$omp critical(output_lock) |
| 197 | 215 | write(output_unit, '(A)') 'Binary file ' // trim(filename) // ' matches' |
| 198 | 216 | |
| 199 | 217 | ! Line-buffered mode |
| 200 | 218 | if (opts%line_buffered) flush(output_unit) |
| 219 | + !$omp end critical(output_lock) |
| 201 | 220 | |
| 202 | 221 | end subroutine print_binary_match |
| 203 | 222 | |
| 204 | 223 | subroutine print_only_match(line, match_start, match_end, filename, line_num, byte_off, opts) |
| 205 | 224 | !> Print only the matched portion of a line (for -o option) |
| 225 | + !> Thread-safe via OMP critical section |
| 206 | 226 | character(len=*), intent(in) :: line |
| 207 | 227 | integer, intent(in) :: match_start, match_end |
| 208 | 228 | character(len=*), intent(in) :: filename |
@@ -212,6 +232,7 @@ contains |
| 212 | 232 | |
| 213 | 233 | if (opts%quiet) return |
| 214 | 234 | |
| 235 | + !$omp critical(output_lock) |
| 215 | 236 | ! Print filename prefix |
| 216 | 237 | if (opts%show_filename .and. .not. opts%hide_filename) then |
| 217 | 238 | if (opts%null_after_filename) then |
@@ -247,12 +268,14 @@ contains |
| 247 | 268 | |
| 248 | 269 | ! Line-buffered mode |
| 249 | 270 | if (opts%line_buffered) flush(output_unit) |
| 271 | + !$omp end critical(output_lock) |
| 250 | 272 | |
| 251 | 273 | end subroutine print_only_match |
| 252 | 274 | |
| 253 | 275 | subroutine print_match_colored(line, filename, line_num, byte_off, opts, & |
| 254 | 276 | match_starts, match_ends, num_matches) |
| 255 | 277 | !> Print a matching line with colored highlighting of matches |
| 278 | + !> Thread-safe via OMP critical section |
| 256 | 279 | character(len=*), intent(in) :: line |
| 257 | 280 | character(len=*), intent(in) :: filename |
| 258 | 281 | integer, intent(in) :: line_num |
@@ -274,6 +297,7 @@ contains |
| 274 | 297 | use_color = stdout_is_tty() |
| 275 | 298 | end if |
| 276 | 299 | |
| 300 | + !$omp critical(output_lock) |
| 277 | 301 | ! Print filename prefix |
| 278 | 302 | if (opts%show_filename .and. .not. opts%hide_filename) then |
| 279 | 303 | if (use_color) then |
@@ -363,6 +387,7 @@ contains |
| 363 | 387 | |
| 364 | 388 | ! Line-buffered mode |
| 365 | 389 | if (opts%line_buffered) flush(output_unit) |
| 390 | + !$omp end critical(output_lock) |
| 366 | 391 | |
| 367 | 392 | end subroutine print_match_colored |
| 368 | 393 | |