<commit_before>// RUN: %target-swiftc_driver %s -target %sanitizers-target-triple -g -sanitize=thread -o %t_tsan-binary
// RUN: not env %env-TSAN_OPTIONS="abort_on_error=0" %target-run %t_tsan-binary 2>&1 | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop
// REQUIRES: tsan_runtime

// Make sure we can handle swifterror and don't bail during the LLVM
// threadsanitizer pass.

enum MyError : Error {
    case A
}

public func foobar(_ x: Int) throws {
  if x == 0 {
    throw MyError.A
  }
}

public func call_foobar() {
  do {
    try foobar(1)
  } catch(_) { }
}

// Test ThreadSanitizer execution end-to-end.

import Darwin

var threads: [pthread_t?] = []
var racey_x: Int;

for _ in 1...5 {
  var t : pthread_t?
  pthread_create(&t, nil, { _ in
    print("pthread ran")
    racey_x = 5;

    return nil
  }, nil)
  threads.append(t)
}

for t in threads {
  if t == nil {
    print("nil thread")
    continue
  }
  pthread_join(t!, nil)
}

// CHECK: ThreadSanitizer: data race<commit_msg>Extend TSan test to work on Linux as well. (#11384)<commit_after>// RUN: %target-swiftc_driver %s -target %sanitizers-target-triple -g -sanitize=thread -o %t_tsan-binary
// RUN: not env %env-TSAN_OPTIONS="abort_on_error=0" %target-run %t_tsan-binary 2>&1 | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: tsan_runtime
#if os(OSX) || os(iOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin)
import Glibc
#endif

// Make sure we can handle swifterror and don't bail during the LLVM
// threadsanitizer pass.

enum MyError : Error {
    case A
}

public func foobar(_ x: Int) throws {
  if x == 0 {
    throw MyError.A
  }
}

public func call_foobar() {
  do {
    try foobar(1)
  } catch(_) { }
}

// Test ThreadSanitizer execution end-to-end.

var threads: [pthread_t] = []
var racey_x: Int;

for _ in 1...5 {
#if os(OSX) || os(iOS)
  var t : pthread_t?
#else
  var t : pthread_t = 0
#endif
  pthread_create(&t, nil, { _ in
    print("pthread ran")
    racey_x = 5;

    return nil
  }, nil)
#if os(OSX) || os(iOS)
  threads.append(t!)
#else
  threads.append(t)
#endif
}

for t in threads {
  if t == nil {
    print("nil thread")
    continue
  }
  pthread_join(t, nil)
}

// CHECK: ThreadSanitizer: data race
{'fraction_non_alphanumeric': 0.13945278022947927, 'fraction_numerical': 0.010150044130626656, 'mean_word_length': 3.198148148148148, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7028484', 'n_tokens_mistral': 932, 'n_tokens_neox': 896, 'n_words': 247}