<commit_before>/******************************************************************************

    @file    StateOS: os_mtx.c
    @author  <NAME>
    @date    13.06.2017
    @brief   This file provides set of functions for StateOS.

 ******************************************************************************

    StateOS - Copyright (C) 2013 <NAME>.

    This file is part of StateOS distribution.

    StateOS 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 3 of the License,
    or (at your option) any later version.

    StateOS 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 <os.h>

/* -------------------------------------------------------------------------- */
void mtx_init( mtx_t *mtx )
/* -------------------------------------------------------------------------- */
{
	assert(!port_isr_inside());
	assert(mtx);

	port_sys_lock();

	memset(mtx, 0, sizeof(mtx_t));

	port_sys_unlock();
}

/* -------------------------------------------------------------------------- */
mtx_t *mtx_create( void )
/* -------------------------------------------------------------------------- */
{
	mtx_t *mtx;

	assert(!port_isr_inside());

	port_sys_lock();

	mtx = core_sys_alloc(sizeof(mtx_t));

	if (mtx)
		mtx_init(mtx);

	port_sys_unlock();

	return mtx;
}

/* -------------------------------------------------------------------------- */
static
void priv_mtx_link( mtx_t *mtx, tsk_t *tsk )
/* -------------------------------------------------------------------------- */
{
	assert(mtx);

	mtx->owner = tsk;

	if (tsk)
	{
		mtx->list = tsk->mlist;
		tsk->mlist = mtx;
		tsk->mtree = 0;
	}
}

/* -------------------------------------------------------------------------- */
static
void priv_mtx_unlink( mtx_t *mtx )
/* -------------------------------------------------------------------------- */
{
	assert(mtx);

	tsk_t *tsk;
	mtx_t *lst;

	if (mtx->owner)
	{
		tsk = mtx->owner;

		if (tsk->mlist == mtx)
			tsk->mlist = mtx->list;

		for (lst = tsk->mlist; lst; lst = lst->list)
			if (lst->list == mtx)
				lst->list = mtx->list;

		mtx->list  = 0;
		mtx->owner = 0;

		core_tsk_prio(tsk, tsk->basic);
	}
}

/* -------------------------------------------------------------------------- */
void mtx_kill( mtx_t *mtx )
/* -------------------------------------------------------------------------- */
{
	assert(!port_isr_inside());
	assert(mtx);

	port_sys_lock();

	priv_mtx_unlink(mtx);

	mtx->count = 0;

	core_all_wakeup(mtx, E_STOPPED);

	port_sys_unlock();
}

/* -------------------------------------------------------------------------- */
static
unsigned priv_mtx_wait( mtx_t *mtx, unsigned time, unsigned(*wait)() )
/* -------------------------------------------------------------------------- */
{
	unsigned event = E_TIMEOUT;

	assert(mtx);

	port_sys_lock();

	if (mtx->owner == 0)
	{
		priv_mtx_link(mtx, Current);

		event = E_SUCCESS;
	}
	else
	if (mtx->owner == Current)
	{
		if (mtx->count < ~0U)
		{
			mtx->count++;
			event = E_SUCCESS;
		}
	}
	else
	{
		if (mtx->owner->prio < Current->prio)
			core_tsk_prio(mtx->owner, Current->prio);

		Current->mtree = mtx->owner;
		event = wait(mtx, time);
	}
	
	port_sys_unlock();

	return event;
}

/* -------------------------------------------------------------------------- */
unsigned mtx_waitUntil( mtx_t *mtx, unsigned time )
/* -------------------------------------------------------------------------- */
{
	assert(!port_isr_inside());

	return priv_mtx_wait(mtx, time, core_tsk_waitUntil);
}<commit_msg>fixed bug in mutex object<commit_after>/******************************************************************************

    @file    StateOS: os_mtx.c
    @author  <NAME>
    @date    13.06.2017
    @brief   This file provides set of functions for StateOS.

 ******************************************************************************

    StateOS - Copyright (C) 2013 <NAME>.

    This file is part of StateOS distribution.

    StateOS 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 3 of the License,
    or (at your option) any later version.

    StateOS 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 <os.h>

/* -------------------------------------------------------------------------- */
void mtx_init( mtx_t *mtx )
/* -------------------------------------------------------------------------- */
{
	assert(!port_isr_inside());
	assert(mtx);

	port_sys_lock();

	memset(mtx, 0, sizeof(mtx_t));

	port_sys_unlock();
}

/* -------------------------------------------------------------------------- */
mtx_t *mtx_create( void )
/* -------------------------------------------------------------------------- */
{
	mtx_t *mtx;

	assert(!port_isr_inside());

	port_sys_lock();

	mtx = core_sys_alloc(sizeof(mtx_t));

	if (mtx)
		mtx_init(mtx);

	port_sys_unlock();

	return mtx;
}

/* -------------------------------------------------------------------------- */
static
void priv_mtx_link( mtx_t *mtx, tsk_t *tsk )
/* -------------------------------------------------------------------------- */
{
	assert(mtx);

	mtx->owner = tsk;

	if (tsk)
	{
		mtx->list = tsk->mlist;
		tsk->mlist = mtx;
	}
}

/* -------------------------------------------------------------------------- */
static
void priv_mtx_unlink( mtx_t *mtx )
/* -------------------------------------------------------------------------- */
{
	assert(mtx);

	tsk_t *tsk;
	mtx_t *lst;

	if (mtx->owner)
	{
		tsk = mtx->owner;

		if (tsk->mlist == mtx)
			tsk->mlist = mtx->list;

		for (lst = tsk->mlist; lst; lst = lst->list)
			if (lst->list == mtx)
				lst->list = mtx->list;

		mtx->list  = 0;
		mtx->owner = 0;

		core_tsk_prio(tsk, tsk->basic);
	}
}

/* -------------------------------------------------------------------------- */
void mtx_kill( mtx_t *mtx )
/* -------------------------------------------------------------------------- */
{
	assert(!port_isr_inside());
	assert(mtx);

	port_sys_lock();

	priv_mtx_unlink(mtx);

	mtx->count = 0;

	core_all_wakeup(mtx, E_STOPPED);

	port_sys_unlock();
}

/* -------------------------------------------------------------------------- */
static
unsigned priv_mtx_wait( mtx_t *mtx, unsigned time, unsigned(*wait)() )
/* -------------------------------------------------------------------------- */
{
	unsigned event = E_TIMEOUT;

	assert(mtx);

	port_sys_lock();

	if (mtx->owner == 0)
	{
		priv_mtx_link(mtx, Current);

		event = E_SUCCESS;
	}
	else
	if (mtx->owner == Current)
	{
		if (mtx->count < ~0U)
		{
			mtx->count++;
			event = E_SUCCESS;
		}
	}
	else
	{
		if (mtx->owner->prio < Current->prio)
			core_tsk_prio(mtx->owner, Current->prio);

		Current->mtree = mtx->owner;
		event = wait(mtx, time);
		Current->mtree = 0;
	}
	
	port_sys_unlock();

	return event;
}

/* -------------------------------------------------------------------------- */
unsigned mtx_waitUntil( mtx_t *mtx, unsigned time )
/* -------------------------------------------------------------------------- */
{
	assert(!port_isr_inside());

	return priv_mtx_wait(mtx, time, core_tsk_waitUntil);
}
{'fraction_non_alphanumeric': 0.4149120634134258, 'fraction_numerical': 0.0049541738915035915, 'mean_word_length': 1.3467015402499274, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 71, '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': '4600741', 'n_tokens_mistral': 2461, 'n_tokens_neox': 2265, 'n_words': 556}