please dont rip this site

A simple but efficient mutex implementation for FreeRTOS

by Isaac Marino Bavaresco

This is file "mutex.c".

NEW: Fixed the declaration of "pxOwner".

//==============================================================================
// Copyright (c) 2007-2009, Isaac Marino Bavaresco
// All rights reserved
// isaacbavaresco@yahoo.com.br
//==============================================================================
#include "FreeRTOS.h"
#include "list.h"
#include "task.h"
//==============================================================================
typedef struct
{
	xList			xTasksWaitingToTake;
	xTaskHandle		pxOwner;
	size_t			uxCount;
} xMUTEX;
//==============================================================================
typedef xMUTEX *xMutexHandle;
//==============================================================================
xMutexHandle xMutexCreate( void )
{
xMUTEX *pxNewMutex;
	
	pxNewMutex = pvPortMalloc( sizeof( xMUTEX ));
	if( pxNewMutex != NULL )
	{
		pxNewMutex->pxOwner	= NULL;
		pxNewMutex->uxCount	= 0;
		vListInitialise( &( pxNewMutex->xTasksWaitingToTake ) );
	}

	return pxNewMutex;
}
//==============================================================================
signed portBASE_TYPE xMutexTake( xMutexHandle pxMutex, portTickType xTicksToWait )
	{
	portENTER_CRITICAL();
	if( pxMutex->pxOwner == xTaskGetCurrentTaskHandle() )
		{
		pxMutex->uxCount++;
		portEXIT_CRITICAL();
		return pdTRUE;
		}

	if(( xTicksToWait > ( portTickType ) 0 ) && ( pxMutex->pxOwner != NULL ))
		{
		vTaskPlaceOnEventList( &( pxMutex->xTasksWaitingToTake ), xTicksToWait );
		taskYIELD();

		if( pxMutex->pxOwner == xTaskGetCurrentTaskHandle() )
			{
			pxMutex->uxCount = 1;
			portEXIT_CRITICAL();
			return pdTRUE;
			}
		else
			{
			portEXIT_CRITICAL();
			return pdFALSE;
			}
		}

	if( pxMutex->pxOwner == NULL )
		{
		pxMutex->pxOwner = xTaskGetCurrentTaskHandle();
		pxMutex->uxCount = 1;
		portEXIT_CRITICAL();
		return pdTRUE;
		}

	portEXIT_CRITICAL();
	return pdFALSE;
	}
//==============================================================================
signed portBASE_TYPE xMutexGive( xMutexHandle pxMutex, portBASE_TYPE Release )
	{
	portENTER_CRITICAL();
	if( pxMutex->pxOwner != xTaskGetCurrentTaskHandle() )
		{
		portEXIT_CRITICAL();
		return pdFALSE;
		}

	if( Release )
		pxMutex->uxCount = 0;
	else
		{
		if( --pxMutex->uxCount != 0 )
			{
			portEXIT_CRITICAL();
			return pdFALSE;
			}
		}
	

	if( !listLIST_IS_EMPTY( &pxMutex->xTasksWaitingToTake ))
		{
		pxMutex->pxOwner = (xTaskHandle)listGET_OWNER_OF_HEAD_ENTRY( (&pxMutex->xTasksWaitingToTake) );
		pxMutex->uxCount = 1;

		if( xTaskRemoveFromEventList( &pxMutex->xTasksWaitingToTake ) == pdTRUE )
			taskYIELD();
		}
	else
		pxMutex->pxOwner = NULL;

	portEXIT_CRITICAL();
	return pdTRUE;
	}
//==============================================================================
signed portBASE_TYPE xDoIOwnTheMutex( xMutexHandle pxMutex )
	{
	portBASE_TYPE c;

	portENTER_CRITICAL();
	c = pxMutex->pxOwner == xTaskGetCurrentTaskHandle();
	portEXIT_CRITICAL();

	return c;
	}
//==============================================================================

file: /Techref/member/IMB-yahoo-J86/mutex.c.htm, 3KB, , updated: 2009/9/1 14:37, local time: 2024/12/22 17:53, owner: IMB-yahoo-J86,
TOP NEW HELP FIND: 
3.15.229.217:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.org/techref/member/IMB-yahoo-J86/mutex.c.htm"> A simple mutex implementation for FreeRTOS - mutex.c</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?