#include <sys/types.h>
#include <unistd.h>
#include <fstream>
#include <sstream>
#include <string>

#include <mutex>
#include "tsan_support.h"

using namespace std;

long count_memory_mappings() {
  pid_t my_pid = getpid();
  ifstream proc_file{"/proc/" + to_string(my_pid) + "/maps"};

  string line;
  long line_count{0};
  while (getline(proc_file, line)) {
    line_count++;
  }

  return line_count;
}

int main() {
  mutex mut;

  int count = 0;
  while (true) {
    printf("iteration: %d, mappings: %ld\n", count++, count_memory_mappings());
    void *main_fiber = __tsan_get_current_fiber();
    void *other_fiber = __tsan_create_fiber(0);
    __tsan_switch_to_fiber(other_fiber, 0);
    mut.lock();
    mut.unlock();
    __tsan_switch_to_fiber(main_fiber, 0);
    __tsan_destroy_fiber(other_fiber);

  }

  return 0;
}
