2019-01-21 15:00:04 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// GNU GPL OS/K //
|
|
|
|
// //
|
2019-02-16 23:36:33 +01:00
|
|
|
// Desc: Locks and synchronization //
|
2019-01-21 15:00:04 +01:00
|
|
|
// //
|
2019-02-16 23:36:33 +01:00
|
|
|
// //
|
|
|
|
// Copyright © 2018-2019 The OS/K Team //
|
|
|
|
// //
|
|
|
|
// This file is part of OS/K. //
|
|
|
|
// //
|
|
|
|
// OS/K 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 //
|
|
|
|
// any later version. //
|
|
|
|
// //
|
|
|
|
// OS/K 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 OS/K. If not, see <https://www.gnu.org/licenses/>. //
|
2019-01-21 15:00:04 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
#ifndef _KALBASE_H
|
|
|
|
#include <kalbase.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _KALEID_KERNEL
|
2019-02-16 23:36:33 +01:00
|
|
|
|
|
|
|
#ifndef _KALKERN_PANIC_H
|
|
|
|
#include <kernel/panic.h>
|
2019-01-21 15:00:04 +01:00
|
|
|
#endif
|
2019-02-16 23:36:33 +01:00
|
|
|
|
2019-01-21 15:00:04 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _KALEXTRAS_LOCKS_H
|
|
|
|
#define _KALEXTRAS_LOCKS_H
|
|
|
|
|
2019-02-16 23:36:33 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2019-01-21 15:00:04 +01:00
|
|
|
|
2019-02-16 23:36:33 +01:00
|
|
|
typedef enum LockType_t LockType_t;
|
|
|
|
typedef struct Lock_t Lock_t;
|
2019-02-06 14:07:38 +01:00
|
|
|
|
2019-02-16 23:36:33 +01:00
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
enum LockType_t
|
|
|
|
{
|
2019-01-21 15:00:04 +01:00
|
|
|
// Mutex-type lock
|
|
|
|
//
|
|
|
|
// WARNING
|
2019-03-19 13:37:23 +01:00
|
|
|
// AcquireLock() panics when used on a mutex while not running a process
|
2019-01-21 15:00:04 +01:00
|
|
|
KLOCK_MUTEX,
|
|
|
|
|
|
|
|
// Spinlock-type lock
|
|
|
|
KLOCK_SPINLOCK,
|
|
|
|
|
2019-02-16 23:36:33 +01:00
|
|
|
};
|
2019-01-21 15:00:04 +01:00
|
|
|
|
2019-02-16 23:36:33 +01:00
|
|
|
struct Lock_t
|
|
|
|
{
|
|
|
|
unsigned int initDone;
|
|
|
|
LockType_t type;
|
|
|
|
volatile int locked;
|
|
|
|
|
|
|
|
/* #ifdef _KALEID_KERNEL
|
2019-01-21 15:00:04 +01:00
|
|
|
Thread_t *ownerThread; // unused
|
|
|
|
Thread_t *waitingThread; // unused
|
2019-02-16 23:36:33 +01:00
|
|
|
#endif */
|
|
|
|
};
|
2019-01-21 15:00:04 +01:00
|
|
|
|
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Linux syscall vs unimplemented syscall...
|
|
|
|
//
|
|
|
|
#ifndef _KALEID_KERNEL
|
|
|
|
#ifdef _OSK_SOURCE
|
|
|
|
int KalYieldCPU(void),
|
|
|
|
#else
|
|
|
|
int sched_yield(void);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// Initialize a lock
|
|
|
|
//
|
|
|
|
static inline
|
|
|
|
void InitLock(Lock_t *lock, LockType_t type)
|
|
|
|
{
|
|
|
|
lock->type = type;
|
|
|
|
lock->locked = FALSE;
|
|
|
|
lock->initDone = INITOK;
|
2019-02-16 23:36:33 +01:00
|
|
|
/* #ifdef _KALEID_KERNEL
|
2019-01-21 15:00:04 +01:00
|
|
|
lock->ownerThread = NULL;
|
|
|
|
lock->waitingThread = NULL;
|
2019-02-16 23:36:33 +01:00
|
|
|
#endif */
|
2019-01-21 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Alternative way to initalize a lock
|
|
|
|
//
|
|
|
|
#ifdef _KALEID_KERNEL
|
2019-02-16 23:36:33 +01:00
|
|
|
# define INITLOCK(type) { INITOK, FALSE, (type), /* NULL, NULL */ }
|
2019-01-21 15:00:04 +01:00
|
|
|
#else
|
|
|
|
# define INITLOCK(type) { INITOK, FALSE, (type) }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// Destroy a lock
|
|
|
|
//
|
|
|
|
static inline
|
|
|
|
void DestroyLock(Lock_t *lock)
|
|
|
|
{
|
|
|
|
KalAssert(lock->initDone);
|
|
|
|
|
|
|
|
__sync_synchronize();
|
|
|
|
lock->initDone = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-03-19 13:37:23 +01:00
|
|
|
// Acquire the lock
|
|
|
|
// Panic on double acquisition since that should never happen
|
2019-01-21 15:00:04 +01:00
|
|
|
// until we have at least a basic scheduler
|
|
|
|
//
|
|
|
|
static inline
|
2019-03-19 13:37:23 +01:00
|
|
|
void AcquireLock(Lock_t *lock)
|
2019-01-21 15:00:04 +01:00
|
|
|
{
|
|
|
|
KalAssert(lock->initDone == INITOK);
|
|
|
|
|
|
|
|
while (!__sync_bool_compare_and_swap(&lock->locked, 0, 1)) {
|
|
|
|
#ifdef _KALEID_KERNEL
|
2019-03-19 13:37:23 +01:00
|
|
|
StartPanic("AcquireLock on an already locked object");
|
2019-01-21 15:00:04 +01:00
|
|
|
#else
|
|
|
|
if likely (lock->type == KLOCK_SPINLOCK) continue;
|
|
|
|
#ifdef _OSK_SOURCE
|
2019-02-06 14:07:38 +01:00
|
|
|
else (void)KalYieldCPU();
|
2019-01-21 15:00:04 +01:00
|
|
|
#else
|
2019-02-06 14:07:38 +01:00
|
|
|
else (void)sched_yield();
|
2019-01-21 15:00:04 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
__sync_synchronize();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-03-19 13:37:23 +01:00
|
|
|
// Release an already acquired lock
|
|
|
|
// Panic if the lock was never acquired
|
2019-01-21 15:00:04 +01:00
|
|
|
//
|
|
|
|
static inline
|
|
|
|
void ReleaseLock(Lock_t *lock)
|
|
|
|
{
|
2019-02-16 23:36:33 +01:00
|
|
|
/*#ifdef _KALEID_KERNEL
|
2019-01-21 15:00:04 +01:00
|
|
|
KalAssert(lock->ownerThread == GetCurThread());
|
2019-02-16 23:36:33 +01:00
|
|
|
#endif*/
|
2019-01-21 15:00:04 +01:00
|
|
|
|
|
|
|
__sync_synchronize();
|
|
|
|
lock->locked = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-03-19 13:37:23 +01:00
|
|
|
// Tries to acquire lock
|
2019-01-21 15:00:04 +01:00
|
|
|
//
|
|
|
|
static inline
|
|
|
|
bool AttemptLock(Lock_t *lock)
|
|
|
|
{
|
|
|
|
KalAssert(lock->initDone == INITOK);
|
|
|
|
|
|
|
|
bool retval = __sync_bool_compare_and_swap(&lock->locked, 0, 1);
|
|
|
|
|
|
|
|
__sync_synchronize();
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------//
|
|
|
|
|
2019-02-16 23:36:33 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-21 15:00:04 +01:00
|
|
|
#endif
|