blob: d869c62a59e1703e2d41af4d44140677a995c3e9 [file] [log] [blame]
/*************************************************************************/ /*!
@File
@Title Environment related functions
@Copyright Copyright (c) Imagination Technologies Ltd. All Rights Reserved
@License MIT
The contents of this file are subject to the MIT license as set out below.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
This License is also included in this distribution in the file called
"MIT-COPYING".
EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ /**************************************************************************/
#include <atomic>
#include <mutex>
#include "magma_util/macros.h"
extern "C" {
#include "osfunc.h"
#include "cache_km.h"
#include "img_types.h"
#include "allocmem.h"
#include "pvr_debug.h"
}
struct _OS_LOCK_
{
std::mutex mutex;
std::atomic<bool> locked;
};
/**************************************************************************/ /*!
@Function OSLockCreate
@Description Creates an operating system lock object.
@Output phLock The created lock.
@Input eLockType The type of lock required. This may be:
LOCK_TYPE_PASSIVE - the lock will not be used
in interrupt context or
LOCK_TYPE_DISPATCH - the lock may be used
in interrupt context.
@Return PVRSRV_OK on success. PVRSRV_ERROR_OUT_OF_MEMORY if the driver
cannot allocate CPU memory needed for the lock.
PVRSRV_ERROR_INIT_FAILURE if the Operating System fails to
allocate the lock.
*/ /**************************************************************************/
IMG_INTERNAL
PVRSRV_ERROR
OSLockCreate(POS_LOCK *phLock, LOCK_TYPE eLockType)
{
if (!phLock)
{
return PVRSRV_ERROR_INVALID_PARAMS;
}
*phLock = new _OS_LOCK_;
return PVRSRV_OK;
}
/**************************************************************************/ /*!
@Function OSLockDestroy
@Description Destroys an operating system lock object.
@Input hLock The lock to be destroyed.
@Return None.
*/ /**************************************************************************/
IMG_INTERNAL
PVRSRV_ERROR
OSLockDestroy(POS_LOCK hLock)
{
delete hLock;
return PVRSRV_OK;
}
/**************************************************************************/ /*!
@Function OSLockAcquire
@Description Acquires an operating system lock.
NB. This function must not return until the lock is acquired
(meaning the implementation should not timeout or return with
an error, as the caller will assume they have the lock).
@Input hLock The lock to be acquired.
@Return None.
*/ /**************************************************************************/
IMG_INTERNAL
void
OSLockAcquire(POS_LOCK hLock) MAGMA_NO_THREAD_SAFETY_ANALYSIS
{
hLock->mutex.lock();
hLock->locked.store(true);
}
/**************************************************************************/ /*!
@Function OSTryLockAcquire
@Description Try to acquire an operating system lock.
NB. If lock is acquired successfully in the first attempt,
then the function returns true and else it will return false.
@Input hLock The lock to be acquired.
@Return IMG_TRUE if lock acquired successfully,
IMG_FALSE otherwise.
*/ /**************************************************************************/
IMG_INTERNAL
IMG_BOOL
OSTryLockAcquire(POS_LOCK hLock) MAGMA_NO_THREAD_SAFETY_ANALYSIS
{
if (hLock->mutex.try_lock())
{
hLock->locked.store(true);
return IMG_TRUE;
}
return IMG_FALSE;
}
/**************************************************************************/ /*!
@Function OSLockRelease
@Description Releases an operating system lock.
@Input hLock The lock to be released.
@Return None.
*/ /**************************************************************************/
IMG_INTERNAL
void
OSLockRelease(POS_LOCK hLock) MAGMA_NO_THREAD_SAFETY_ANALYSIS
{
hLock->locked.store(false);
hLock->mutex.unlock();
}
/**************************************************************************/ /*!
@Function OSLockIsLocked
@Description Tests whether or not an operating system lock is currently
locked.
@Input hLock The lock to be tested.
@Return IMG_TRUE if locked, IMG_FALSE if not locked.
*/ /**************************************************************************/
IMG_INTERNAL
IMG_BOOL
OSLockIsLocked(POS_LOCK hLock) { return hLock->locked.load() ? IMG_TRUE : IMG_FALSE; }