<commit_before>//===-- main.cpp ------------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// C includes
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>

pthread_t g_thread_1 = NULL;
pthread_t g_thread_2 = NULL;
pthread_t g_thread_3 = NULL;

pthread_barrier_t g_barrier;

char *g_char_ptr = NULL;

void
do_bad_thing_with_location(char *char_ptr, char new_val)
{
    unsigned what = new_val;
    printf("new value written to location(%p) = %u\n", char_ptr, what);
    *char_ptr = new_val;
}

uint32_t access_pool (uint32_t flag = 0);

uint32_t
access_pool (uint32_t flag)
{
    static pthread_mutex_t g_access_mutex = PTHREAD_MUTEX_INITIALIZER;
    if (flag == 0)
        ::pthread_mutex_lock (&g_access_mutex);

    char old_val = *g_char_ptr;
    if (flag != 0)
        do_bad_thing_with_location(g_char_ptr, old_val + 1);

    if (flag == 0)
        ::pthread_mutex_unlock (&g_access_mutex);
    return *g_char_ptr;
}

void *
thread_func (void *arg)
{
    uint32_t thread_index = *((uint32_t *)arg);
    printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index);

    pthread_barrier_wait(&g_barrier);

    uint32_t count = 0;
    uint32_t val;
    while (count++ < 15)
    {
        // random micro second sleep from zero to 3 seconds
        int usec = ::rand() % 3000000;
        printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec);
        ::usleep (usec);
        
        if (count < 7)
            val = access_pool ();
        else
            val = access_pool (1);
                
        printf ("%s (thread = %u) after usleep access_pool returns %d (count=%d)...\n", __FUNCTION__, thread_index, val, count);
    }
    printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index);
    return NULL;
}


int main (int argc, char const *argv[])
{
    int err;
    void *thread_result = NULL;
    uint32_t thread_index_1 = 1;
    uint32_t thread_index_2 = 2;
    uint32_t thread_index_3 = 3;

    g_char_ptr = (char *)malloc (1);
    *g_char_ptr = 0;

    pthread_barrier_init(&g_barrier, NULL, 4);

    // Create 3 threads
    err = ::pthread_create (&g_thread_1, NULL, thread_func, &thread_index_1);
    err = ::pthread_create (&g_thread_2, NULL, thread_func, &thread_index_2);
    err = ::pthread_create (&g_thread_3, NULL, thread_func, &thread_index_3);

    printf ("Before turning all three threads loose...\n"); // Set break point at this line.
    pthread_barrier_wait(&g_barrier);

    // Join all of our threads
    err = ::pthread_join (g_thread_1, &thread_result);
    err = ::pthread_join (g_thread_2, &thread_result);
    err = ::pthread_join (g_thread_3, &thread_result);

    pthread_barrier_destroy(&g_barrier);
    free(g_char_ptr);

    return 0;
}
<commit_msg>Fixes http://reviews.llvm.org/rL230691<commit_after>//===-- main.cpp ------------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// C includes
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <atomic>

pthread_t g_thread_1 = NULL;
pthread_t g_thread_2 = NULL;
pthread_t g_thread_3 = NULL;

std::atomic_bool g_ready(false);

char *g_char_ptr = NULL;

void
do_bad_thing_with_location(char *char_ptr, char new_val)
{
    unsigned what = new_val;
    printf("new value written to location(%p) = %u\n", char_ptr, what);
    *char_ptr = new_val;
}

uint32_t access_pool (uint32_t flag = 0);

uint32_t
access_pool (uint32_t flag)
{
    static pthread_mutex_t g_access_mutex = PTHREAD_MUTEX_INITIALIZER;
    if (flag == 0)
        ::pthread_mutex_lock (&g_access_mutex);

    char old_val = *g_char_ptr;
    if (flag != 0)
        do_bad_thing_with_location(g_char_ptr, old_val + 1);

    if (flag == 0)
        ::pthread_mutex_unlock (&g_access_mutex);
    return *g_char_ptr;
}

void *
thread_func (void *arg)
{
    uint32_t thread_index = *((uint32_t *)arg);
    printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index);

    while (!g_ready);

    uint32_t count = 0;
    uint32_t val;
    while (count++ < 15)
    {
        // random micro second sleep from zero to 3 seconds
        int usec = ::rand() % 3000000;
        printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec);
        ::usleep (usec);
        
        if (count < 7)
            val = access_pool ();
        else
            val = access_pool (1);
                
        printf ("%s (thread = %u) after usleep access_pool returns %d (count=%d)...\n", __FUNCTION__, thread_index, val, count);
    }
    printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index);
    return NULL;
}


int main (int argc, char const *argv[])
{
    int err;
    void *thread_result = NULL;
    uint32_t thread_index_1 = 1;
    uint32_t thread_index_2 = 2;
    uint32_t thread_index_3 = 3;

    g_char_ptr = (char *)malloc (1);
    *g_char_ptr = 0;

    // Create 3 threads
    err = ::pthread_create (&g_thread_1, NULL, thread_func, &thread_index_1);
    err = ::pthread_create (&g_thread_2, NULL, thread_func, &thread_index_2);
    err = ::pthread_create (&g_thread_3, NULL, thread_func, &thread_index_3);

    printf ("Before turning all three threads loose...\n"); // Set break point at this line.
    g_ready = true;

    // Join all of our threads
    err = ::pthread_join (g_thread_1, &thread_result);
    err = ::pthread_join (g_thread_2, &thread_result);
    err = ::pthread_join (g_thread_3, &thread_result);
    free(g_char_ptr);

    return 0;
}

{'fraction_non_alphanumeric': 0.16949431724592323, 'fraction_numerical': 0.02157799374073464, 'mean_word_length': 2.5759717314487633, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 14, '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': '4262620', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2073, 'n_words': 543}