<commit_before>/*
 * Copyright (C) 2010-2016 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "patomic.h"
#include "pspinlock.h"
#include "pmem.h"
#include "puthread.h"
#include "puthread-private.h"
#include "perror-private.h"

#include <stdlib.h>
#include <string.h>
#include <time.h>

extern void p_uthread_init_internal (void);
extern void p_uthread_shutdown_internal (void);
extern void p_uthread_exit_internal (void);
extern void p_uthread_wait_internal (PUThread *thread);
extern void p_uthread_free_internal (PUThread *thread);
extern PUThread * p_uthread_create_internal (PUThreadFunc	func,
					     pboolean		joinable,
					     PUThreadPriority	prio,
					     psize		stack_size);

static void pp_uthread_cleanup (ppointer data);
static ppointer pp_uthread_proxy (ppointer data);

#if !defined (P_OS_WIN) && !defined (PLIBSYS_HAS_NANOSLEEP)
static pint pp_uthread_nanosleep (puint32 msec);
#endif

static PUThreadKey * pp_uthread_specific_data = NULL;
static PSpinLock * pp_uthread_new_spin = NULL;

static void
pp_uthread_cleanup (ppointer data)
{
	p_uthread_unref (data);
}

static ppointer
pp_uthread_proxy (ppointer data)
{
	PUThreadBase *base_thread = data;

	p_uthread_set_local (pp_uthread_specific_data, data);

	p_spinlock_lock (pp_uthread_new_spin);
	p_spinlock_unlock (pp_uthread_new_spin);

	base_thread->func (base_thread->data);

	return NULL;
}

void
p_uthread_init (void)
{
	if (P_LIKELY (pp_uthread_specific_data == NULL))
		pp_uthread_specific_data = p_uthread_local_new ((PDestroyFunc) pp_uthread_cleanup);

	if (P_LIKELY (pp_uthread_new_spin == NULL))
		pp_uthread_new_spin = p_spinlock_new ();

	p_uthread_init_internal ();
}

void
p_uthread_shutdown (void)
{
	if (P_LIKELY (pp_uthread_specific_data != NULL)) {
		p_uthread_local_free (pp_uthread_specific_data);
		pp_uthread_specific_data = NULL;<commit_msg>Cleanup thread data structure in the main thread in p_libsys_shutdown()<commit_after>/*
 * Copyright (C) 2010-2016 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "patomic.h"
#include "pspinlock.h"
#include "pmem.h"
#include "puthread.h"
#include "puthread-private.h"
#include "perror-private.h"

#include <stdlib.h>
#include <string.h>
#include <time.h>

extern void p_uthread_init_internal (void);
extern void p_uthread_shutdown_internal (void);
extern void p_uthread_exit_internal (void);
extern void p_uthread_wait_internal (PUThread *thread);
extern void p_uthread_free_internal (PUThread *thread);
extern PUThread * p_uthread_create_internal (PUThreadFunc	func,
					     pboolean		joinable,
					     PUThreadPriority	prio,
					     psize		stack_size);

static void pp_uthread_cleanup (ppointer data);
static ppointer pp_uthread_proxy (ppointer data);

#if !defined (P_OS_WIN) && !defined (PLIBSYS_HAS_NANOSLEEP)
static pint pp_uthread_nanosleep (puint32 msec);
#endif

static PUThreadKey * pp_uthread_specific_data = NULL;
static PSpinLock * pp_uthread_new_spin = NULL;

static void
pp_uthread_cleanup (ppointer data)
{
	p_uthread_unref (data);
}

static ppointer
pp_uthread_proxy (ppointer data)
{
	PUThreadBase *base_thread = data;

	p_uthread_set_local (pp_uthread_specific_data, data);

	p_spinlock_lock (pp_uthread_new_spin);
	p_spinlock_unlock (pp_uthread_new_spin);

	base_thread->func (base_thread->data);

	return NULL;
}

void
p_uthread_init (void)
{
	if (P_LIKELY (pp_uthread_specific_data == NULL))
		pp_uthread_specific_data = p_uthread_local_new ((PDestroyFunc) pp_uthread_cleanup);

	if (P_LIKELY (pp_uthread_new_spin == NULL))
		pp_uthread_new_spin = p_spinlock_new ();

	p_uthread_init_internal ();
}

void
p_uthread_shutdown (void)
{
	PUThread *cur_thread;

	if (P_LIKELY (pp_uthread_specific_data != NULL)) {
		cur_thread = p_uthread_get_local (pp_uthread_specific_data);

		if (P_UNLIKELY (cur_thread != NULL)) {
			p_uthread_unref (cur_thread);
			p_uthread_set_local (pp_uthread_specific_data, NULL);
		}

		p_uthread_local_free (pp_uthread_specific_data);
		pp_uthread_specific_data = NULL;
{'fraction_non_alphanumeric': 0.08709800918836141, 'fraction_numerical': 0.004211332312404288, 'mean_word_length': 5.387530562347188, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7411211', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1782, 'n_words': 502}