From 7d881c2e5fef91a6570e46ef69a5d4a5cf0e8b4d Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 22 Aug 2010 10:25:26 +0800 Subject: [PATCH] Changed spinlock mechananisim - LOCK, TIGHTLOCK and VTIGHTLOCK have been removed - Replaced by SHORTLOCK and Mutex_Acquire > SHORTLOCK disables interrupts and busy polls the lock waiting for other CPUs > Mutex_Acquire puts the thread to sleep if the lock is in use, and is given the lock when woken. --- Kernel/Makefile | 5 +- Kernel/arch/x86/include/arch.h | 58 ++++++++------- Kernel/arch/x86/mm_phys.c | 23 +++--- Kernel/arch/x86/mm_virt.c | 27 ++++--- Kernel/arch/x86/proc.c | 2 +- Kernel/arch/x86/vm8086.c | 10 +-- Kernel/arch/x86_64/link.ld | 2 +- Kernel/binary.c | 12 ++-- Kernel/drv/iocache.c | 48 ++++++------- Kernel/heap.c | 20 +++--- Kernel/include/acess.h | 12 ++++ Kernel/include/threads.h | 2 +- Kernel/logging.c | 12 ++-- Kernel/messages.c | 26 ++++--- Kernel/modules.c | 6 +- Kernel/threads.c | 120 +++++++++++++++++++++++++------ Kernel/vfs/fs/devfs.c | 10 +-- Kernel/vfs/main.c | 6 +- Kernel/vfs/mount.c | 6 +- Kernel/vfs/nodecache.c | 6 +- Makefile.cfg | 2 +- Modules/Display/VESA/main.c | 6 +- Modules/Filesystems/FAT/fat.c | 4 +- Modules/Filesystems/FAT/fs_fat.h | 2 +- Modules/IPStack/arp.c | 24 +++---- Modules/IPStack/main.c | 20 +++--- Modules/IPStack/tcp.c | 52 +++++++------- Modules/IPStack/tcp.h | 8 +-- Modules/IPStack/udp.c | 76 ++++++++++---------- Modules/IPStack/udp.h | 4 +- Modules/Storage/ATA/io.c | 14 ++-- Modules/Storage/FDD/fdd.c | 18 ++--- 32 files changed, 370 insertions(+), 273 deletions(-) diff --git a/Kernel/Makefile b/Kernel/Makefile index efd5e057..8649d501 100644 --- a/Kernel/Makefile +++ b/Kernel/Makefile @@ -16,9 +16,9 @@ MAKEDEP = $(CC) -M CPPFLAGS += -I./include -I./arch/$(ARCHDIR)/include -D_MODULE_NAME_=\"Kernel\" CPPFLAGS += -DARCH=$(ARCH) -DARCHDIR=$(ARCHDIR) -DKERNEL_VERSION=$(KERNEL_VERSION) -DBUILD_NUM=$(BUILD_NUM) -CFLAGS += -Wall -Werror -O3 -fno-stack-protector -fno-builtin -Wstrict-prototypes +CFLAGS += -Wall -Werror -O3 -fno-stack-protector -fno-builtin -Wstrict-prototypes -g ASFLAGS += -D ARCH=\"$(ARCH)\" -D ARCHDIR=\"$(ARCHDIR)\" -LDFLAGS += -T arch/$(ARCHDIR)/link.ld +LDFLAGS += -T arch/$(ARCHDIR)/link.ld -g ifeq ($(DEBUG_BUILD),yes) LDFLAGS += -g @@ -65,6 +65,7 @@ $(BIN): $(OBJ) $(MODS) arch/$(ARCHDIR)/link.ld Makefile $(DISASM) $(BIN) > $(BIN).dsm @wc -l $(SRCFILES) include/*.h > LineCounts.$(ARCH).txt @echo BUILD_NUM = $$(( $(BUILD_NUM) + 1 )) > Makefile.BuildNum.$(ARCH) + @$(STRIP) $(BIN) %.ao.$(ARCH): %.asm Makefile @echo --- NASM -o $@ diff --git a/Kernel/arch/x86/include/arch.h b/Kernel/arch/x86/include/arch.h index 77355684..67a62901 100644 --- a/Kernel/arch/x86/include/arch.h +++ b/Kernel/arch/x86/include/arch.h @@ -29,44 +29,48 @@ #define __ASM__ __asm__ __volatile__ +#define LONGLOCK_NUM_THREADS 8 + // === MACROS === -typedef volatile int tSpinlock; -#define IS_LOCKED(lockptr) (!!(*(tSpinlock*)lockptr)) -/** - * \brief Inter-Process interrupt (does a Yield) - */ -#define LOCK(lockptr) do {\ - int v=1;\ - while(v) {\ - __ASM__("xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(lockptr));\ - if(v) Threads_Yield();\ - }\ -}while(0) +struct sShortSpinlock { + volatile int Lock; + int IF; +}; /** - * \brief Tight spinlock (does a HLT) + * \brief Determine if a short spinlock is locked */ -#define TIGHTLOCK(lockptr) do{\ - int v=1;\ - while(v) {\ - __ASM__("xchgl %%eax,(%%edi)":"=a"(v):"a"(1),"D"(lockptr));\ - if(v) __ASM__("hlt");\ - }\ -}while(0) +static inline int IS_LOCKED(struct sShortSpinlock *Lock) { + return !!Lock->Lock; +} /** - * \brief Very Tight spinlock (short inter-cpu lock) + * \brief Acquire a Short Spinlock + * \note Stops interrupts, so be careful */ -#define VTIGHTLOCK(lockptr) do{\ - int v=1;\ - while(v)__ASM__("xchgl %%eax,(%%edi)":"=a"(v):"a"(1),"D"(lockptr));\ -}while(0) +static inline void SHORTLOCK(struct sShortSpinlock *Lock) { + int v = 1; + __ASM__ ("pushf;\n\tpop %%eax" : "=a"(Lock->IF)); + Lock->IF &= 0x200; + __ASM__ ("cli"); // Stop task switches + // Wait for another CPU to release + while(v) + __ASM__("xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(&Lock->Lock)); +} /** - * \brief Release a held spinlock + * \brief Release a short lock */ -#define RELEASE(lockptr) __ASM__("lock andl $0, (%%edi)"::"D"(lockptr)); +static inline void SHORTREL(struct sShortSpinlock *Lock) { + Lock->Lock = 0; + #if 0 + __ASM__ ("pushf;\n\tor %0, (%%esp);\n\tpopf" : : "a"(Lock->IF)); + #else + if(Lock->IF) __ASM__ ("sti"); + #endif +} /** * \brief Halt the CPU */ #define HALT() __asm__ __volatile__ ("hlt") +#define MAGIC_BREAK() __asm__ __volatile__ ("xchg %bx, %bx") // === TYPES === typedef unsigned int Uint; // Unsigned machine native integer diff --git a/Kernel/arch/x86/mm_phys.c b/Kernel/arch/x86/mm_phys.c index 3c7dcddc..cb4f9e0b 100644 --- a/Kernel/arch/x86/mm_phys.c +++ b/Kernel/arch/x86/mm_phys.c @@ -21,6 +21,7 @@ void MM_RefPhys(tPAddr PAddr); void MM_DerefPhys(tPAddr PAddr); // === GLOBALS === +tMutex glPhysAlloc; Uint64 giPhysAlloc = 0; // Number of allocated pages Uint64 giPageCount = 0; // Total number of pages Uint64 giLastPossibleFree = 0; // Last possible free page (before all pages are used) @@ -130,7 +131,7 @@ tPAddr MM_AllocPhys(void) ENTER(""); - LOCK( &giPhysAlloc ); + Mutex_Acquire( &glPhysAlloc ); // Find free page // Scan downwards @@ -162,7 +163,7 @@ tPAddr MM_AllocPhys(void) LOG("a=%i,b=%i,c=%i", a, b, c); for( ; gaSuperBitmap[a] == -1 && a >= 0; a-- ); if(a < 0) { - RELEASE( &giPhysAlloc ); + Mutex_Release( &glPhysAlloc ); Warning("MM_AllocPhys - OUT OF MEMORY (Called by %p)", __builtin_return_address(0)); LEAVE('i', 0); return 0; @@ -188,7 +189,7 @@ tPAddr MM_AllocPhys(void) gaSuperBitmap[indx>>10] |= 1 << ((indx>>5)&31); // Release Spinlock - RELEASE( &giPhysAlloc ); + Mutex_Release( &glPhysAlloc ); LEAVE('X', ret); //Log("MM_AllocPhys: RETURN 0x%x", ret); @@ -217,7 +218,7 @@ tPAddr MM_AllocPhysRange(int Pages, int MaxBits) if(MaxBits > PHYS_BITS) MaxBits = PHYS_BITS; // Lock - LOCK( &giPhysAlloc ); + Mutex_Acquire( &glPhysAlloc ); // Set up search state if( giLastPossibleFree > ((tPAddr)1 << (MaxBits-12)) ) { @@ -237,7 +238,7 @@ tPAddr MM_AllocPhysRange(int Pages, int MaxBits) // Find free page for( ; gaSuperBitmap[a] == -1 && a --; ) b = 31; if(a < 0) { - RELEASE( &giPhysAlloc ); + Mutex_Release( &glPhysAlloc ); Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0)); LEAVE('i', 0); return 0; @@ -295,7 +296,7 @@ tPAddr MM_AllocPhysRange(int Pages, int MaxBits) // Check if an address was found if( idx < 0 ) { - RELEASE( &giPhysAlloc ); + Mutex_Release( &glPhysAlloc ); Warning("MM_AllocPhysRange - OUT OF MEMORY (Called by %p)", __builtin_return_address(0)); LEAVE('i', 0); return 0; @@ -318,7 +319,7 @@ tPAddr MM_AllocPhysRange(int Pages, int MaxBits) if(gaPageBitmap[ idx ] == -1) gaSuperBitmap[idx/32] |= 1 << (idx%32); // Release Spinlock - RELEASE( &giPhysAlloc ); + Mutex_Release( &glPhysAlloc ); LEAVE('X', ret); return ret; @@ -336,7 +337,7 @@ void MM_RefPhys(tPAddr PAddr) if(PAddr >= giPageCount) return; // Lock Structures - LOCK( &giPhysAlloc ); + Mutex_Acquire( &glPhysAlloc ); // Reference the page if(gaPageReferences) @@ -350,7 +351,7 @@ void MM_RefPhys(tPAddr PAddr) gaSuperBitmap[PAddr/1024] |= 1 << ((PAddr/32)&31); // Release Spinlock - RELEASE( &giPhysAlloc ); + Mutex_Release( &glPhysAlloc ); } /** @@ -372,7 +373,7 @@ void MM_DerefPhys(tPAddr PAddr) } // Lock Structures - LOCK( &giPhysAlloc ); + Mutex_Acquire( &glPhysAlloc ); if( giLastPossibleFree < PAddr ) giLastPossibleFree = PAddr; @@ -390,7 +391,7 @@ void MM_DerefPhys(tPAddr PAddr) } // Release spinlock - RELEASE( &giPhysAlloc ); + Mutex_Release( &glPhysAlloc ); } /** diff --git a/Kernel/arch/x86/mm_virt.c b/Kernel/arch/x86/mm_virt.c index 8902837c..dce47828 100644 --- a/Kernel/arch/x86/mm_virt.c +++ b/Kernel/arch/x86/mm_virt.c @@ -95,8 +95,8 @@ tPAddr MM_DuplicatePage(tVAddr VAddr); #define gaPAE_TmpDir ((tTabEnt*)PAE_TMP_DIR_ADDR) #define gaPAE_TmpPDPT ((tTabEnt*)PAE_TMP_PDPT_ADDR) int gbUsePAE = 0; - int gilTempMappings = 0; - int gilTempFractal = 0; +tMutex glTempMappings; +tMutex glTempFractal; Uint32 gWorkerStacks[(NUM_WORKER_STACKS+31)/32]; int giLastUsedWorker = 0; @@ -533,7 +533,7 @@ tPAddr MM_Clone(void) tVAddr kStackBase = Proc_GetCurThread()->KernelStack - KERNEL_STACK_SIZE; void *tmp; - LOCK( &gilTempFractal ); + Mutex_Acquire( &glTempFractal ); // Create Directory Table *gpTmpCR3 = MM_AllocPhys() | 3; @@ -641,7 +641,7 @@ tPAddr MM_Clone(void) } ret = *gpTmpCR3 & ~0xFFF; - RELEASE( &gilTempFractal ); + Mutex_Release( &glTempFractal ); //LEAVE('x', ret); return ret; @@ -713,7 +713,7 @@ tVAddr MM_NewWorkerStack() //Log(" MM_NewWorkerStack: base = 0x%x", base); // Acquire the lock for the temp fractal mappings - LOCK(&gilTempFractal); + Mutex_Acquire(&glTempFractal); // Set the temp fractals to TID0's address space *gpTmpCR3 = ((Uint)gaInitPageDir - KERNEL_BASE) | 3; @@ -737,7 +737,7 @@ tVAddr MM_NewWorkerStack() } *gpTmpCR3 = 0; // Release the temp mapping lock - RELEASE(&gilTempFractal); + Mutex_Release(&glTempFractal); // Copy the old stack oldstack = (esp + KERNEL_STACK_SIZE-1) & ~(KERNEL_STACK_SIZE-1); @@ -900,11 +900,11 @@ tVAddr MM_MapTemp(tPAddr PAddr) PAddr &= ~0xFFF; - //LOG("gilTempMappings = %i", gilTempMappings); + //LOG("glTempMappings = %i", glTempMappings); for(;;) { - LOCK( &gilTempMappings ); + Mutex_Acquire( &glTempMappings ); for( i = 0; i < NUM_TEMP_PAGES; i ++ ) { @@ -914,11 +914,11 @@ tVAddr MM_MapTemp(tPAddr PAddr) gaPageTable[ (TEMP_MAP_ADDR >> 12) + i ] = PAddr | 3; INVLPG( TEMP_MAP_ADDR + (i << 12) ); //LEAVE('p', TEMP_MAP_ADDR + (i << 12)); - RELEASE( &gilTempMappings ); + Mutex_Release( &glTempMappings ); return TEMP_MAP_ADDR + (i << 12); } - RELEASE( &gilTempMappings ); - Threads_Yield(); + Mutex_Release( &glTempMappings ); + Threads_Yield(); // TODO: Less expensive } } @@ -1053,8 +1053,7 @@ void MM_UnmapHWPages(tVAddr VAddr, Uint Number) i = VAddr >> 12; - LOCK( &gilTempMappings ); // Temp and HW share a directory, so they share a lock - + Mutex_Acquire( &glTempMappings ); // Temp and HW share a directory, so they share a lock for( j = 0; j < Number; j++ ) { @@ -1062,7 +1061,7 @@ void MM_UnmapHWPages(tVAddr VAddr, Uint Number) gaPageTable[ i + j ] = 0; } - RELEASE( &gilTempMappings ); + Mutex_Release( &glTempMappings ); } // --- EXPORTS --- diff --git a/Kernel/arch/x86/proc.c b/Kernel/arch/x86/proc.c index e9b4a681..16af0da8 100644 --- a/Kernel/arch/x86/proc.c +++ b/Kernel/arch/x86/proc.c @@ -42,7 +42,7 @@ extern Uint GetEIP(void); // start.asm extern int GetCPUNum(void); // start.asm extern Uint32 gaInitPageDir[1024]; // start.asm extern char Kernel_Stack_Top[]; -extern tSpinlock glThreadListLock; +extern tShortSpinlock glThreadListLock; extern int giNumCPUs; extern int giNextTID; extern tThread gThreadZero; diff --git a/Kernel/arch/x86/vm8086.c b/Kernel/arch/x86/vm8086.c index 86f440d1..5253a62d 100644 --- a/Kernel/arch/x86/vm8086.c +++ b/Kernel/arch/x86/vm8086.c @@ -44,7 +44,7 @@ tVM8086 *VM8086_Init(void); // === GLOBALS === MODULE_DEFINE(0, 0x100, VM8086, VM8086_Install, NULL, NULL); -tSpinlock glVM8086_Process; +tMutex glVM8086_Process; tPID gVM8086_WorkerPID; tTID gVM8086_CallingThread; tVM8086 volatile * volatile gpVM8086_State = (void*)-1; // Set to -1 to avoid race conditions @@ -55,7 +55,7 @@ int VM8086_Install(char **Arguments) tPID pid; // Lock to avoid race conditions - LOCK( &glVM8086_Process ); + Mutex_Acquire( &glVM8086_Process ); // Create BIOS Call process pid = Proc_Clone(NULL, CLONE_VM); @@ -147,7 +147,7 @@ void VM8086_GPF(tRegs *Regs) { if( gpVM8086_State == (void*)-1 ) { Log_Log("VM8086", "Worker thread ready and waiting"); - RELEASE( &glVM8086_Process ); // Release lock obtained in VM8086_Install + Mutex_Release( &glVM8086_Process ); // Release lock obtained in VM8086_Install gpVM8086_State = NULL; } //Log_Log("VM8086", "gpVM8086_State = %p, gVM8086_CallingThread = %i", @@ -401,7 +401,7 @@ void VM8086_Int(tVM8086 *State, Uint8 Interrupt) State->IP = *(Uint16*)(KERNEL_BASE+4*Interrupt); State->CS = *(Uint16*)(KERNEL_BASE+4*Interrupt+2); - LOCK( &glVM8086_Process ); + Mutex_Acquire( &glVM8086_Process ); gpVM8086_State = State; gVM8086_CallingThread = Threads_GetTID(); @@ -409,5 +409,5 @@ void VM8086_Int(tVM8086 *State, Uint8 Interrupt) while( gpVM8086_State != NULL ) Threads_Yield(); - RELEASE( &glVM8086_Process ); + Mutex_Release( &glVM8086_Process ); } diff --git a/Kernel/arch/x86_64/link.ld b/Kernel/arch/x86_64/link.ld index f5e73558..ce537f90 100644 --- a/Kernel/arch/x86_64/link.ld +++ b/Kernel/arch/x86_64/link.ld @@ -9,7 +9,7 @@ _kernel_base = 0xFFFFFFFF80000000; OUTPUT_FORMAT(elf32-i386) OUTPUT_ARCH(i386:x86-64) -ENTRY (start) +ENTRY(start) SECTIONS { . = 0x100000; diff --git a/Kernel/binary.c b/Kernel/binary.c index 72e8210a..b3040a5d 100644 --- a/Kernel/binary.c +++ b/Kernel/binary.c @@ -45,11 +45,11 @@ Uint Binary_GetSymbolEx(char *Name, Uint *Value); Uint Binary_FindSymbol(void *Base, char *Name, Uint *Val); // === GLOBALS === - int glBinListLock = 0; +tShortSpinlock glBinListLock; tBinary *glLoadedBinaries = NULL; char **gsaRegInterps = NULL; int giRegInterps = 0; - int glKBinListLock = 0; +tShortSpinlock glKBinListLock; tKernelBin *glLoadedKernelLibs; tBinaryType *gRegBinTypes = &gELF_Info; @@ -501,10 +501,10 @@ tBinary *Binary_DoLoad(char *truePath) VFS_Close(fp); // Add to the list - LOCK(&glBinListLock); + SHORTLOCK(&glBinListLock); pBinary->Next = glLoadedBinaries; glLoadedBinaries = pBinary; - RELEASE(&glBinListLock); + SHORTREL(&glBinListLock); // Return LEAVE('p', pBinary); @@ -745,10 +745,10 @@ void *Binary_LoadKernel(char *File) pKBinary = malloc(sizeof(*pKBinary)); pKBinary->Base = (void*)base; pKBinary->Info = pBinary; - LOCK( &glKBinListLock ); + SHORTLOCK( &glKBinListLock ); pKBinary->Next = glLoadedKernelLibs; glLoadedKernelLibs = pKBinary; - RELEASE( &glKBinListLock ); + SHORTREL( &glKBinListLock ); LEAVE('p', base); return (void*)base; diff --git a/Kernel/drv/iocache.c b/Kernel/drv/iocache.c index f42957c7..743dabc4 100644 --- a/Kernel/drv/iocache.c +++ b/Kernel/drv/iocache.c @@ -25,7 +25,7 @@ struct sIOCache { tIOCache *Next; int SectorSize; - int Lock; + tMutex Lock; int Mode; Uint32 ID; tIOCache_WriteCallback Write; @@ -35,7 +35,7 @@ struct sIOCache }; // === GLOBALS === - int glIOCache_Caches; +tShortSpinlock glIOCache_Caches; tIOCache *gIOCache_Caches = NULL; int giIOCache_NumCaches = 0; @@ -61,10 +61,10 @@ tIOCache *IOCache_Create( tIOCache_WriteCallback Write, Uint32 ID, int SectorSiz ret->Entries = 0; // Append to list - LOCK( &glIOCache_Caches ); + SHORTLOCK( &glIOCache_Caches ); ret->Next = gIOCache_Caches; gIOCache_Caches = ret; - RELEASE( &glIOCache_Caches ); + SHORTREL( &glIOCache_Caches ); // Return return ret; @@ -87,9 +87,9 @@ int IOCache_Read( tIOCache *Cache, Uint64 Sector, void *Buffer ) } // Lock - LOCK( &Cache->Lock ); + Mutex_Acquire( &Cache->Lock ); if(Cache->CacheSize == 0) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); LEAVE('i', -1); return -1; } @@ -101,7 +101,7 @@ int IOCache_Read( tIOCache *Cache, Uint64 Sector, void *Buffer ) if( ent->Num == Sector ) { memcpy(Buffer, ent->Data, Cache->SectorSize); ent->LastAccess = now(); - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); LEAVE('i', 1); return 1; } @@ -110,7 +110,7 @@ int IOCache_Read( tIOCache *Cache, Uint64 Sector, void *Buffer ) if(ent->Num > Sector) break; } - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); LEAVE('i', 0); return 0; } @@ -130,9 +130,9 @@ int IOCache_Add( tIOCache *Cache, Uint64 Sector, void *Buffer ) return -1; // Lock - LOCK( &Cache->Lock ); + Mutex_Acquire( &Cache->Lock ); if(Cache->CacheSize == 0) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return -1; } @@ -142,7 +142,7 @@ int IOCache_Add( tIOCache *Cache, Uint64 Sector, void *Buffer ) { // Is it already here? if( ent->Num == Sector ) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return 0; } @@ -197,7 +197,7 @@ int IOCache_Add( tIOCache *Cache, Uint64 Sector, void *Buffer ) Cache->CacheUsed ++; // Release Spinlock - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); // Return success return 1; @@ -215,9 +215,9 @@ int IOCache_Write( tIOCache *Cache, Uint64 Sector, void *Buffer ) if(!Cache || !Buffer) return -1; // Lock - LOCK( &Cache->Lock ); + Mutex_Acquire( &Cache->Lock ); if(Cache->CacheSize == 0) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return -1; } @@ -234,7 +234,7 @@ int IOCache_Write( tIOCache *Cache, Uint64 Sector, void *Buffer ) ent->LastWrite = 0; } - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return 1; } // It's a sorted list, so as soon as we go past `Sector` we know @@ -242,7 +242,7 @@ int IOCache_Write( tIOCache *Cache, Uint64 Sector, void *Buffer ) if(ent->Num > Sector) break; } - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return 0; } @@ -257,9 +257,9 @@ void IOCache_Flush( tIOCache *Cache ) if( Cache->Mode == IOCACHE_VIRTUAL ) return; // Lock - LOCK( &Cache->Lock ); + Mutex_Acquire( &Cache->Lock ); if(Cache->CacheSize == 0) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return; } @@ -270,7 +270,7 @@ void IOCache_Flush( tIOCache *Cache ) ent->LastWrite = 0; } - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); } /** @@ -282,9 +282,9 @@ void IOCache_Destroy( tIOCache *Cache ) tIOCache_Ent *ent, *prev = NULL; // Lock - LOCK( &Cache->Lock ); + Mutex_Acquire( &Cache->Lock ); if(Cache->CacheSize == 0) { - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); return; } @@ -302,10 +302,10 @@ void IOCache_Destroy( tIOCache *Cache ) Cache->CacheSize = 0; - RELEASE( &Cache->Lock ); + Mutex_Release( &Cache->Lock ); // Remove from list - LOCK( &glIOCache_Caches ); + SHORTLOCK( &glIOCache_Caches ); { tIOCache *ent; tIOCache *prev = (tIOCache*)&gIOCache_Caches; @@ -319,7 +319,7 @@ void IOCache_Destroy( tIOCache *Cache ) } } } - RELEASE( &glIOCache_Caches ); + SHORTREL( &glIOCache_Caches ); free(Cache); } diff --git a/Kernel/heap.c b/Kernel/heap.c index 3f767eb7..b55687b8 100644 --- a/Kernel/heap.c +++ b/Kernel/heap.c @@ -35,7 +35,7 @@ void Heap_Dump(void); void Heap_Stats(void); // === GLOBALS === -tSpinlock glHeap; +tMutex glHeap; void *gHeapStart; void *gHeapEnd; @@ -151,7 +151,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) #endif // Lock Heap - LOCK(&glHeap); + Mutex_Acquire(&glHeap); // Traverse Heap for( head = gHeapStart; @@ -165,7 +165,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) #else if( head->Size & (MIN_SIZE-1) ) { #endif - RELEASE(&glHeap); // Release spinlock + Mutex_Release(&glHeap); // Release spinlock #if WARNINGS Log_Warning("Heap", "Size of heap address %p is invalid not aligned (0x%x)", head, head->Size); Heap_Dump(); @@ -177,7 +177,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) if(head->Magic == MAGIC_USED) continue; // Error check if(head->Magic != MAGIC_FREE) { - RELEASE(&glHeap); // Release spinlock + Mutex_Release(&glHeap); // Release spinlock #if WARNINGS Log_Warning("Heap", "Magic of heap address %p is invalid (0x%x)", head, head->Magic); Heap_Dump(); @@ -193,7 +193,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) head->Magic = MAGIC_USED; head->File = File; head->Line = Line; - RELEASE(&glHeap); // Release spinlock + Mutex_Release(&glHeap); // Release spinlock #if DEBUG_TRACE Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", head->Data, head->Size, __builtin_return_address(0)); #endif @@ -220,7 +220,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) best = Heap_Extend( Bytes ); // Check for errors if(!best) { - RELEASE(&glHeap); // Release spinlock + Mutex_Release(&glHeap); // Release spinlock return NULL; } // Check size @@ -228,7 +228,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) best->Magic = MAGIC_USED; // Mark block as used best->File = File; best->Line = Line; - RELEASE(&glHeap); // Release spinlock + Mutex_Release(&glHeap); // Release spinlock #if DEBUG_TRACE Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0)); #endif @@ -251,7 +251,7 @@ void *Heap_Allocate(const char *File, int Line, size_t Bytes) best->File = File; best->Line = Line; - RELEASE(&glHeap); // Release spinlock + Mutex_Release(&glHeap); // Release spinlock #if DEBUG_TRACE Log_Debug("Heap", "newhead(%p)->Size = 0x%x", newhead, newhead->Size); Log_Debug("Heap", "Malloc'd %p (0x%x bytes), returning to %s:%i", @@ -314,7 +314,7 @@ void Heap_Deallocate(void *Ptr) } // Lock - LOCK( &glHeap ); + Mutex_Acquire( &glHeap ); // Mark as free head->Magic = MAGIC_FREE; @@ -325,7 +325,7 @@ void Heap_Deallocate(void *Ptr) Heap_Merge( head ); // Release - RELEASE( &glHeap ); + Mutex_Release( &glHeap ); } /** diff --git a/Kernel/include/acess.h b/Kernel/include/acess.h index f6f87f70..b8ba67a5 100644 --- a/Kernel/include/acess.h +++ b/Kernel/include/acess.h @@ -21,6 +21,15 @@ typedef int tTID; typedef Uint tUID; typedef Uint tGID; typedef Sint64 tTimestamp; +typedef struct sShortSpinlock tShortSpinlock; +typedef struct sMutex tMutex; + +struct sMutex { + tShortSpinlock Protector; //!< Protector for the lock strucure + struct sThread *volatile Owner; //!< Owner of the lock (set upon getting the lock) + struct sThread *Waiting; //!< Waiting threads + struct sThread *LastWaiting; //!< Waiting threads +}; // --- Helper Macros --- /** @@ -403,6 +412,9 @@ extern tGID Threads_GetGID(void); extern int SpawnTask(tThreadFunction Function, void *Arg); extern Uint *Threads_GetCfgPtr(int Id); extern int Threads_SetName(char *NewName); +extern void Mutex_Acquire(tMutex *Mutex); +extern void Mutex_Release(tMutex *Mutex); +extern int Mutex_IsLocked(tMutex *Mutex); /** * \} */ diff --git a/Kernel/include/threads.h b/Kernel/include/threads.h index 4a62166f..6c292307 100644 --- a/Kernel/include/threads.h +++ b/Kernel/include/threads.h @@ -19,7 +19,7 @@ typedef struct sThread { // --- threads.c's struct sThread *Next; //!< Next thread in list - tSpinlock IsLocked; //!< Thread's spinlock + tShortSpinlock IsLocked; //!< Thread's spinlock volatile int Status; //!< Thread Status int RetStatus; //!< Return Status diff --git a/Kernel/logging.c b/Kernel/logging.c index baca0aa8..225ce53e 100644 --- a/Kernel/logging.c +++ b/Kernel/logging.c @@ -66,12 +66,12 @@ EXPORT(Log_Log); EXPORT(Log_Debug); // === GLOBALS === -tSpinlock glLog; -tSpinlock glLogOutput; +tShortSpinlock glLogOutput; #if USE_RING_BUFFER Uint8 gaLog_RingBufferData[sizeof(tRingBuffer)+RING_BUFFER_SIZE]; tRingBuffer *gpLog_RingBuffer = (void*)gaLog_RingBufferData; #else +tMutex glLog; tLogList gLog; tLogList gLog_Levels[NUM_LOG_LEVELS]; #endif @@ -118,7 +118,7 @@ void Log_AddEvent(char *Ident, int Level, char *Format, va_list Args) RingBuffer_Write( gpLog_RingBuffer, newData, LOG_HDR_LEN + len + 2 ); } #else - LOCK( &glLog ); + Mutex_Acquire( &glLog ); ent->Next = gLog.Tail; if(gLog.Head) @@ -132,7 +132,7 @@ void Log_AddEvent(char *Ident, int Level, char *Format, va_list Args) else gLog_Levels[Level].Tail = gLog_Levels[Level].Head = ent; - RELEASE( &glLog ); + Mutex_Release( &glLog ); #endif #if PRINT_ON_APPEND @@ -149,7 +149,7 @@ void Log_AddEvent(char *Ident, int Level, char *Format, va_list Args) */ void Log_Int_PrintMessage(tLogEntry *Entry) { - LOCK( &glLogOutput ); + SHORTLOCK( &glLogOutput ); LogF("%s%014lli%s [%+8s] %s\x1B[0m\r\n", csaLevelColours[Entry->Level], Entry->Time, @@ -157,7 +157,7 @@ void Log_Int_PrintMessage(tLogEntry *Entry) Entry->Ident, Entry->Data ); - RELEASE( &glLogOutput ); + SHORTREL( &glLogOutput ); } /** diff --git a/Kernel/messages.c b/Kernel/messages.c index 32e579b3..4765d65e 100644 --- a/Kernel/messages.c +++ b/Kernel/messages.c @@ -34,10 +34,13 @@ int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data) if(!thread) { return -1; } // Get Spinlock - LOCK( &thread->IsLocked ); + SHORTLOCK( &thread->IsLocked ); // Check if thread is still alive - if(thread->Status == THREAD_STAT_DEAD) return -1; + if(thread->Status == THREAD_STAT_DEAD) { + SHORTREL( &thread->IsLocked ); + return -1; + } // Create message msg = malloc( sizeof(tMsg)+Length ); @@ -55,7 +58,7 @@ int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data) thread->LastMessage = msg; } - RELEASE(&thread->IsLocked); + SHORTREL(&thread->IsLocked); Threads_Wake( thread ); @@ -72,7 +75,7 @@ int Proc_SendMessage(Uint *Err, Uint Dest, int Length, void *Data) int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer) { int ret; - void *tmp; + void *tmp; tThread *cur = Proc_GetCurThread(); // Check if queue has any items @@ -80,7 +83,7 @@ int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer) return 0; } - LOCK( &cur->IsLocked ); + SHORTLOCK( &cur->IsLocked ); if(Source) *Source = cur->Messages->Source; @@ -88,7 +91,7 @@ int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer) // Get message length if( !Buffer ) { ret = cur->Messages->Length; - RELEASE( &cur->IsLocked ); + SHORTREL( &cur->IsLocked ); return ret; } @@ -98,7 +101,7 @@ int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer) if( !CheckMem( Buffer, cur->Messages->Length ) ) { *Err = -EINVAL; - RELEASE( &cur->IsLocked ); + SHORTREL( &cur->IsLocked ); return -1; } memcpy(Buffer, cur->Messages->Data, cur->Messages->Length); @@ -106,11 +109,12 @@ int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer) ret = cur->Messages->Length; // Remove from list - tmp = cur->Messages->Next; - free( (void*)cur->Messages ); - cur->Messages = tmp; + tmp = cur->Messages; + cur->Messages = cur->Messages->Next; + + SHORTREL( &cur->IsLocked ); - RELEASE( &cur->IsLocked ); + free(tmp); // Free outside of lock return ret; } diff --git a/Kernel/modules.c b/Kernel/modules.c index 0409711d..87511aa6 100644 --- a/Kernel/modules.c +++ b/Kernel/modules.c @@ -32,7 +32,7 @@ extern void gKernelModulesEnd; // === GLOBALS === int giNumBuiltinModules = 0; -tSpinlock glModuleSpinlock; +tShortSpinlock glModuleSpinlock; tModule *gLoadedModules = NULL; tModuleLoader *gModule_Loaders = NULL; tModule *gLoadingModules = NULL; @@ -180,10 +180,10 @@ int Module_int_Initialise(tModule *Module, char *ArgString) LOG("ret = %i", ret); // Add to loaded list - LOCK( &glModuleSpinlock ); + SHORTLOCK( &glModuleSpinlock ); Module->Next = gLoadedModules; gLoadedModules = Module; - RELEASE( &glModuleSpinlock ); + SHORTREL( &glModuleSpinlock ); LEAVE_RET('i', 0); } diff --git a/Kernel/threads.c b/Kernel/threads.c index 297470cd..8ec0e9fc 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -49,6 +49,9 @@ tUID Threads_GetUID(void); tGID Threads_GetGID(void); int Threads_SetGID(Uint *Errno, tUID ID); void Threads_Dump(void); +void Mutex_Acquire(tMutex *Mutex); +void Mutex_Release(tMutex *Mutex); + int Mutex_IsLocked(tMutex *Mutex); // === GLOBALS === // -- Core Thread -- @@ -62,7 +65,7 @@ tThread gThreadZero = { }; // -- Processes -- // --- Locks --- -tSpinlock glThreadListLock = 0; ///\note NEVER use a heap function while locked +tShortSpinlock glThreadListLock; ///\note NEVER use a heap function while locked // --- Current State --- volatile int giNumActiveThreads = 0; //volatile int giTotalTickets = 0; @@ -133,10 +136,10 @@ void Threads_SetTickets(tThread *Thread, int Num) if(Num > MAX_TICKETS) Num = MAX_TICKETS; if( Thread != Proc_GetCurThread() ) { - LOCK( &glThreadListLock ); + SHORTLOCK( &glThreadListLock ); giFreeTickets -= Thread->NumTickets - Num; Thread->NumTickets = Num; - RELEASE( &glThreadListLock ); + SHORTREL( &glThreadListLock ); } else Thread->NumTickets = Num; @@ -160,7 +163,7 @@ tThread *Threads_CloneTCB(Uint *Err, Uint Flags) new->CurCPU = -1; new->Next = NULL; - new->IsLocked = 0; + memset( &new->IsLocked, 0, sizeof(new->IsLocked)); new->Status = THREAD_STAT_ACTIVE; new->RetStatus = 0; @@ -386,15 +389,17 @@ void Threads_Kill(tThread *Thread, int Status) ///\note Double lock is needed due to overlap of lock areas // Lock thread (stop us recieving messages) - LOCK( &Thread->IsLocked ); + SHORTLOCK( &Thread->IsLocked ); // Lock thread list - LOCK( &glThreadListLock ); + SHORTLOCK( &glThreadListLock ); // Get previous thread on list prev = Threads_int_GetPrev( &gActiveThreads, Thread ); if(!prev) { Warning("Proc_Exit - Current thread is not on the active queue"); + Thread->IsLocked.Lock = 0; // We can't use SHORTREL as that starts IRQs again + SHORTREL( &glThreadListLock ); return; } @@ -428,8 +433,8 @@ void Threads_Kill(tThread *Thread, int Status) } // Release spinlocks - RELEASE( &Thread->IsLocked ); // Released first so that it IS released - RELEASE( &glThreadListLock ); + Thread->IsLocked.Lock = 0; // Released first so that it IS released + SHORTREL( &glThreadListLock ); //Log("Thread %i went *hurk*", Thread->TID); @@ -458,20 +463,20 @@ void Threads_Sleep(void) //Log_Log("Threads", "%i going to sleep", cur->TID); // Acquire Spinlock - LOCK( &glThreadListLock ); + SHORTLOCK( &glThreadListLock ); // Get thread before current thread thread = Threads_int_GetPrev( &gActiveThreads, cur ); if(!thread) { Warning("Threads_Sleep - Current thread is not on the active queue"); Threads_Dump(); - RELEASE( &glThreadListLock ); + SHORTREL( &glThreadListLock ); return; } // Don't sleep if there is a message waiting if( cur->Messages ) { - RELEASE( &glThreadListLock ); + SHORTREL( &glThreadListLock ); return; } @@ -487,12 +492,13 @@ void Threads_Sleep(void) // Reduce the active count & ticket count giNumActiveThreads --; + // - No need to alter giFreeTickets (we're being executed) // Mark thread as sleeping cur->Status = THREAD_STAT_SLEEPING; // Release Spinlock - RELEASE( &glThreadListLock ); + SHORTREL( &glThreadListLock ); while(cur->Status != THREAD_STAT_ACTIVE) HALT(); } @@ -516,10 +522,10 @@ int Threads_Wake(tThread *Thread) case THREAD_STAT_ACTIVE: Log("Thread_Wake: Waking awake thread (%i)", Thread->TID); return -EALREADY; - case THREAD_STAT_SLEEPING: + case THREAD_STAT_SLEEPING: // TODO: Comment better //Log_Log("Threads", "Waking %i (%p) from sleeping (CPU=%i)", // Thread->TID, Thread, Thread->CurCPU); - LOCK( &glThreadListLock ); + SHORTLOCK( &glThreadListLock ); prev = Threads_int_GetPrev(&gSleepingThreads, Thread); prev->Next = Thread->Next; // Remove from sleeping queue Thread->Next = gActiveThreads; // Add to active queue @@ -532,7 +538,7 @@ int Threads_Wake(tThread *Thread) Log("Threads_Wake: giFreeTickets = %i", giFreeTickets); #endif Thread->Status = THREAD_STAT_ACTIVE; - RELEASE( &glThreadListLock ); + SHORTREL( &glThreadListLock ); return -EOK; case THREAD_STAT_WAITING: Warning("Thread_Wake - Waiting threads are not currently supported"); @@ -565,7 +571,7 @@ int Threads_WakeTID(tTID TID) */ void Threads_AddActive(tThread *Thread) { - LOCK( &glThreadListLock ); + SHORTLOCK( &glThreadListLock ); Thread->Next = gActiveThreads; gActiveThreads = Thread; giNumActiveThreads ++; @@ -574,7 +580,7 @@ void Threads_AddActive(tThread *Thread) #if DEBUG_TRACE_TICKETS Log("Threads_AddActive: giFreeTickets = %i", giFreeTickets); #endif - RELEASE( &glThreadListLock ); + SHORTREL( &glThreadListLock ); } /** @@ -716,7 +722,7 @@ tThread *Threads_GetNextToRun(int CPU, tThread *Last) while(gDeleteThreads) { thread = gDeleteThreads->Next; - if(gDeleteThreads->IsLocked) { // Only free if structure is unused + if( IS_LOCKED(&gDeleteThreads->IsLocked) ) { // Only free if structure is unused gDeleteThreads->Status = THREAD_STAT_NULL; free( gDeleteThreads ); } @@ -736,13 +742,13 @@ tThread *Threads_GetNextToRun(int CPU, tThread *Last) // but it has a potentially long lock period) // - Well, this CPU can obtain the lock, but that is aliveviated by // the above. - TIGHTLOCK( &glThreadListLock ); + SHORTLOCK( &glThreadListLock ); // Special case: 1 thread if(giNumActiveThreads == 1) { if( gActiveThreads->CurCPU == -1 ) gActiveThreads->CurCPU = CPU; - RELEASE( &glThreadListLock ); + SHORTREL( &glThreadListLock ); if( gActiveThreads->CurCPU == CPU ) return gActiveThreads; return NULL; // CPU has nothing to do @@ -778,7 +784,7 @@ tThread *Threads_GetNextToRun(int CPU, tThread *Last) // No free tickets (all tasks delegated to cores) if( giFreeTickets == 0 ) { - RELEASE(&glThreadListLock); + SHORTREL(&glThreadListLock); return NULL; } @@ -817,7 +823,7 @@ tThread *Threads_GetNextToRun(int CPU, tThread *Last) CPU, giFreeTickets, thread, thread->ThreadName, thread->CurCPU); #endif - RELEASE( &glThreadListLock ); + SHORTREL( &glThreadListLock ); return thread; } @@ -833,5 +839,75 @@ void Threads_SegFault(tVAddr Addr) //Threads_Exit( 0, -1 ); } +/** + * \brief heavy mutex + */ +void Mutex_Acquire(tMutex *Mutex) +{ + tThread *us = Proc_GetCurThread(); + tThread *prev; + + // Get protector + SHORTLOCK( &Mutex->Protector ); + + //Log("Mutex_Acquire: (%p)", Mutex); + + // Check if the lock is already held + if( Mutex->Owner ) { + SHORTLOCK( &glThreadListLock ); + // - Remove from active list + us->Remaining = 0; + prev = Threads_int_GetPrev(&gActiveThreads, us); + prev->Next = us->Next; + giNumActiveThreads --; + us->Status = THREAD_STAT_SLEEPING; + + // - Add to waiting + if(Mutex->LastWaiting) { + Mutex->LastWaiting->Next = us; + Mutex->LastWaiting = us; + } + else { + Mutex->Waiting = us; + Mutex->LastWaiting = us; + } + SHORTREL( &glThreadListLock ); + SHORTREL( &Mutex->Protector ); + while(us->Status == THREAD_STAT_SLEEPING) HALT(); + // We're only woken when we get the lock + } + // Ooh, let's take it! + else { + Mutex->Owner = us; + SHORTREL( &Mutex->Protector ); + } +} + +/** + * \brief Release a held spinlock + */ +void Mutex_Release(tMutex *Mutex) +{ + SHORTLOCK( &Mutex->Protector ); + //Log("Mutex_Release: (%p)", Mutex); + if( Mutex->Waiting ) { + Mutex->Owner = Mutex->Waiting; // Set owner + Mutex->Waiting = Mutex->Waiting->Next; // Next! + // Wake new owner + Mutex->Owner->Status = THREAD_STAT_ACTIVE; + Threads_AddActive(Mutex->Owner); + Log("Mutex %p Woke %p", Mutex, Mutex->Owner); + } + else { + Mutex->Owner = NULL; + } + SHORTREL( &Mutex->Protector ); +} + +int Mutex_IsLocked(tMutex *Mutex) +{ + return Mutex->Owner != NULL; +} + // === EXPORTS === EXPORT(Threads_GetUID); diff --git a/Kernel/vfs/fs/devfs.c b/Kernel/vfs/fs/devfs.c index 13fe1099..098ac4f6 100644 --- a/Kernel/vfs/fs/devfs.c +++ b/Kernel/vfs/fs/devfs.c @@ -28,7 +28,7 @@ tVFS_Node gDevFS_RootNode = { }; tDevFS_Driver *gDevFS_Drivers = NULL; int giDevFS_NextID = 1; -tSpinlock glDevFS_ListLock; +tShortSpinlock glDevFS_ListLock; // === CODE === /** @@ -39,7 +39,7 @@ int DevFS_AddDevice(tDevFS_Driver *Device) int ret = 0; tDevFS_Driver *dev; - LOCK( &glDevFS_ListLock ); + SHORTLOCK( &glDevFS_ListLock ); // Check if the device is already registered or the name is taken for( dev = gDevFS_Drivers; dev; dev = dev->Next ) @@ -63,7 +63,7 @@ int DevFS_AddDevice(tDevFS_Driver *Device) gDevFS_RootNode.Size ++; ret = giDevFS_NextID ++; } - RELEASE( &glDevFS_ListLock ); + SHORTREL( &glDevFS_ListLock ); return ret; } @@ -75,7 +75,7 @@ void DevFS_DelDevice(tDevFS_Driver *Device) { tDevFS_Driver *prev = NULL, *dev; - LOCK( &glDevFS_ListLock ); + SHORTLOCK( &glDevFS_ListLock ); // Search list for device for(dev = gDevFS_Drivers; dev && dev != Device; @@ -94,7 +94,7 @@ void DevFS_DelDevice(tDevFS_Driver *Device) Log_Warning("DevFS", "Attempted to unregister device %p '%s' which was not registered", Device, Device->Name); - RELEASE( &glDevFS_ListLock ); + SHORTREL( &glDevFS_ListLock ); } /** diff --git a/Kernel/vfs/main.c b/Kernel/vfs/main.c index cd04680d..f072a285 100644 --- a/Kernel/vfs/main.c +++ b/Kernel/vfs/main.c @@ -25,7 +25,7 @@ EXPORT(VFS_AddDriver); // === GLOBALS === tVFS_Node NULLNode = {0}; -tSpinlock siDriverListLock = 0; +tShortSpinlock slDriverListLock; tVFS_Driver *gVFS_Drivers = NULL; char *gsVFS_DriverFile = NULL; int giVFS_DriverFileID = 0; @@ -118,10 +118,10 @@ int VFS_AddDriver(tVFS_Driver *Info) { if(!Info) return -1; - LOCK( &siDriverListLock ); + SHORTLOCK( &slDriverListLock ); Info->Next = gVFS_Drivers; gVFS_Drivers = Info; - RELEASE( &siDriverListLock ); + SHORTREL( &slDriverListLock ); VFS_UpdateDriverFile(); diff --git a/Kernel/vfs/mount.c b/Kernel/vfs/mount.c index 0c07bcb8..0436043b 100644 --- a/Kernel/vfs/mount.c +++ b/Kernel/vfs/mount.c @@ -15,7 +15,7 @@ extern char *gsVFS_MountFile; void VFS_UpdateMountFile(void); // === GLOBALS === - int glVFS_MountList = 0; +tMutex glVFS_MountList; tVFS_Mount *gVFS_Mounts; tVFS_Mount *gVFS_RootMount = NULL; @@ -82,7 +82,7 @@ int VFS_Mount(char *Device, char *MountPoint, char *Filesystem, char *Options) if(!gVFS_RootMount) gVFS_RootMount = mnt; // Add to mount list - LOCK( &glVFS_MountList ); + Mutex_Acquire( &glVFS_MountList ); { tVFS_Mount *tmp; mnt->Next = NULL; @@ -94,7 +94,7 @@ int VFS_Mount(char *Device, char *MountPoint, char *Filesystem, char *Options) gVFS_Mounts = mnt; } } - RELEASE( &glVFS_MountList ); + Mutex_Release( &glVFS_MountList ); Log_Log("VFS", "Mounted '%s' to '%s' ('%s')", Device, MountPoint, Filesystem); diff --git a/Kernel/vfs/nodecache.c b/Kernel/vfs/nodecache.c index a898d5d6..ad85ac32 100644 --- a/Kernel/vfs/nodecache.c +++ b/Kernel/vfs/nodecache.c @@ -23,7 +23,7 @@ tInodeCache *Inode_int_GetFSCache(int Handle); // === GLOBALS === int gVFS_NextInodeHandle = 1; - int gilVFS_InodeCache = 0; +tShortSpinlock glVFS_InodeCache; tInodeCache *gVFS_InodeCache = NULL; // === CODE === @@ -40,10 +40,10 @@ int Inode_GetHandle() ent->Next = NULL; ent->FirstNode = NULL; // Add to list - LOCK( &gilVFS_InodeCache ); + SHORTLOCK( &glVFS_InodeCache ); ent->Next = gVFS_InodeCache; gVFS_InodeCache = ent; - RELEASE( &gilVFS_InodeCache ); + SHORTREL( &glVFS_InodeCache ); return gVFS_NextInodeHandle-1; } diff --git a/Makefile.cfg b/Makefile.cfg index a2e7ccb0..834f9706 100644 --- a/Makefile.cfg +++ b/Makefile.cfg @@ -10,7 +10,7 @@ ACESSDIR := /home/tpg/Projects/Acess2 CC := gcc LD := ld AS := nasm -DISASM := objdump -d +DISASM := objdump -d -S RM := @rm -f STRIP := strip MKDIR := mkdir diff --git a/Modules/Display/VESA/main.c b/Modules/Display/VESA/main.c index ff9e5149..506a8b5a 100644 --- a/Modules/Display/VESA/main.c +++ b/Modules/Display/VESA/main.c @@ -44,7 +44,7 @@ tDevFS_Driver gVesa_DriverStruct = { .IOCtl = Vesa_Ioctl } }; -tSpinlock glVesa_Lock; +tMutex glVesa_Lock; tVM8086 *gpVesa_BiosState; int giVesaDriverId = -1; // --- Video Modes --- @@ -421,7 +421,7 @@ int Vesa_Int_SetMode(int mode) Time_RemoveTimer(giVesaCursorTimer); giVesaCursorTimer = -1; - LOCK( &glVesa_Lock ); + Mutex_Acquire( &glVesa_Lock ); gpVesa_BiosState->AX = 0x4F02; gpVesa_BiosState->BX = gVesa_Modes[mode].code; @@ -446,7 +446,7 @@ int Vesa_Int_SetMode(int mode) giVesaCurrentMode = mode; gpVesaCurMode = &gVesa_Modes[giVesaCurrentMode]; - RELEASE( &glVesa_Lock ); + Mutex_Release( &glVesa_Lock ); return 1; } diff --git a/Modules/Filesystems/FAT/fat.c b/Modules/Filesystems/FAT/fat.c index fbf4f71b..e14fad0b 100644 --- a/Modules/Filesystems/FAT/fat.c +++ b/Modules/Filesystems/FAT/fat.c @@ -393,7 +393,7 @@ Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster) Uint32 val = 0; Uint32 ofs; ENTER("pDisk xCluster", Disk, cluster); - LOCK( &Disk->lFAT ); + Mutex_Acquire( &Disk->lFAT ); #if CACHE_FAT if( Disk->ClusterCount <= giFAT_MaxCachedClusters ) { @@ -420,7 +420,7 @@ Uint32 FAT_int_GetFatValue(tFAT_VolInfo *Disk, Uint32 cluster) #if CACHE_FAT } #endif /*CACHE_FAT*/ - RELEASE( &Disk->lFAT ); + Mutex_Release( &Disk->lFAT ); LEAVE('x', val); return val; } diff --git a/Modules/Filesystems/FAT/fs_fat.h b/Modules/Filesystems/FAT/fs_fat.h index 99c82df3..6896945f 100644 --- a/Modules/Filesystems/FAT/fs_fat.h +++ b/Modules/Filesystems/FAT/fs_fat.h @@ -153,7 +153,7 @@ struct drv_fat_volinfo_s int fileHandle; //!< File Handle int type; //!< FAT Type. See eFatType char name[12]; //!< Volume Name (With NULL Terminator) - tSpinlock lFAT; //!< Lock to prevent double-writing to the FAT + tMutex lFAT; //!< Lock to prevent double-writing to the FAT Uint32 firstDataSect; //!< First data sector Uint32 rootOffset; //!< Root Offset (clusters) Uint32 ClusterCount; //!< Total Cluster Count diff --git a/Modules/IPStack/arp.c b/Modules/IPStack/arp.c index 8a18813d..f96c2d85 100644 --- a/Modules/IPStack/arp.c +++ b/Modules/IPStack/arp.c @@ -27,7 +27,7 @@ struct sARP_Cache4 { Sint64 LastUsed; } *gaARP_Cache4; int giARP_Cache4Space; -tSpinlock glARP_Cache4; +tMutex glARP_Cache4; struct sARP_Cache6 { tIPv6 IP; tMacAddr MAC; @@ -35,7 +35,7 @@ struct sARP_Cache6 { Sint64 LastUsed; } *gaARP_Cache6; int giARP_Cache6Space; -tSpinlock glARP_Cache6; +tMutex glARP_Cache6; int giARP_LastUpdateID = 0; // === CODE === @@ -68,7 +68,7 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address) ENTER("pInterface xAddress", Interface, Address); - LOCK( &glARP_Cache4 ); + Mutex_Acquire( &glARP_Cache4 ); for( i = 0; i < giARP_Cache4Space; i++ ) { if(gaARP_Cache4[i].IP.L != Address.L) continue; @@ -76,7 +76,7 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address) // Check if the entry needs to be refreshed if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE ) break; - RELEASE( &glARP_Cache4 ); + Mutex_Release( &glARP_Cache4 ); LOG("Return %x:%x:%x:%x:%x:%x", gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1], gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3], @@ -85,7 +85,7 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address) LEAVE('-'); return gaARP_Cache4[i].MAC; } - RELEASE( &glARP_Cache4 ); + Mutex_Release( &glARP_Cache4 ); lastID = giARP_LastUpdateID; @@ -112,15 +112,15 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address) while(lastID == giARP_LastUpdateID) Threads_Yield(); lastID = giARP_LastUpdateID; - LOCK( &glARP_Cache4 ); + Mutex_Acquire( &glARP_Cache4 ); for( i = 0; i < giARP_Cache4Space; i++ ) { if(gaARP_Cache4[i].IP.L != Address.L) continue; - RELEASE( &glARP_Cache4 ); + Mutex_Release( &glARP_Cache4 ); return gaARP_Cache4[i].MAC; } - RELEASE( &glARP_Cache4 ); + Mutex_Release( &glARP_Cache4 ); } } @@ -134,7 +134,7 @@ void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr) int oldest = 0; // Find an entry for the IP address in the cache - LOCK(&glARP_Cache4); + Mutex_Acquire(&glARP_Cache4); for( i = giARP_Cache4Space; i--; ) { if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) { @@ -161,7 +161,7 @@ void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr) gaARP_Cache4[i].MAC = HWAddr; gaARP_Cache4[i].LastUpdate = now(); giARP_LastUpdateID ++; - RELEASE(&glARP_Cache4); + Mutex_Release(&glARP_Cache4); } /** @@ -174,7 +174,7 @@ void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr) int oldest = 0; // Find an entry for the MAC address in the cache - LOCK(&glARP_Cache6); + Mutex_Acquire(&glARP_Cache6); for( i = giARP_Cache6Space; i--; ) { if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) { @@ -195,7 +195,7 @@ void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr) gaARP_Cache6[i].IP = SWAddr; gaARP_Cache6[i].LastUpdate = now(); giARP_LastUpdateID ++; - RELEASE(&glARP_Cache6); + Mutex_Release(&glARP_Cache6); } /** diff --git a/Modules/IPStack/main.c b/Modules/IPStack/main.c index d087a9bf..fe4bf0f6 100644 --- a/Modules/IPStack/main.c +++ b/Modules/IPStack/main.c @@ -50,11 +50,11 @@ tDevFS_Driver gIP_DriverInfo = { .IOCtl = IPStack_Root_IOCtl } }; -tSpinlock glIP_Interfaces = 0; +tShortSpinlock glIP_Interfaces; tInterface *gIP_Interfaces = NULL; tInterface *gIP_Interfaces_Last = NULL; int giIP_NextIfaceId = 1; -tSpinlock glIP_Adapters = 0; +tMutex glIP_Adapters; tAdapter *gIP_Adapters = NULL; tSocketFile *gIP_FileTemplates; @@ -561,7 +561,7 @@ int IPStack_AddInterface(char *Device) iface->Node.ImplInt = giIP_NextIfaceId++; // Append to list - LOCK( &glIP_Interfaces ); + SHORTLOCK( &glIP_Interfaces ); if( gIP_Interfaces ) { gIP_Interfaces_Last->Next = iface; gIP_Interfaces_Last = iface; @@ -570,7 +570,7 @@ int IPStack_AddInterface(char *Device) gIP_Interfaces = iface; gIP_Interfaces_Last = iface; } - RELEASE( &glIP_Interfaces ); + SHORTREL( &glIP_Interfaces ); gIP_DriverInfo.RootNode.Size ++; @@ -590,14 +590,14 @@ tAdapter *IPStack_GetAdapter(char *Path) ENTER("sPath", Path); - LOCK( &glIP_Adapters ); + Mutex_Acquire( &glIP_Adapters ); // Check if this adapter is already open for( dev = gIP_Adapters; dev; dev = dev->Next ) { if( strcmp(dev->Device, Path) == 0 ) { dev->NRef ++; - RELEASE( &glIP_Adapters ); + Mutex_Release( &glIP_Adapters ); LEAVE('p', dev); return dev; } @@ -606,7 +606,7 @@ tAdapter *IPStack_GetAdapter(char *Path) // Ok, so let's open it dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 ); if(!dev) { - RELEASE( &glIP_Adapters ); + Mutex_Release( &glIP_Adapters ); LEAVE('n'); return NULL; } @@ -620,7 +620,7 @@ tAdapter *IPStack_GetAdapter(char *Path) dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE ); if( dev->DeviceFD == -1 ) { free( dev ); - RELEASE( &glIP_Adapters ); + Mutex_Release( &glIP_Adapters ); LEAVE('n'); return NULL; } @@ -632,7 +632,7 @@ tAdapter *IPStack_GetAdapter(char *Path) Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device); VFS_Close( dev->DeviceFD ); free( dev ); - RELEASE( &glIP_Adapters ); + Mutex_Release( &glIP_Adapters ); LEAVE('n'); return NULL; } @@ -644,7 +644,7 @@ tAdapter *IPStack_GetAdapter(char *Path) dev->Next = gIP_Adapters; gIP_Adapters = dev; - RELEASE( &glIP_Adapters ); + Mutex_Release( &glIP_Adapters ); // Start watcher Link_WatchDevice( dev ); diff --git a/Modules/IPStack/tcp.c b/Modules/IPStack/tcp.c index 616e5d53..8b486d23 100644 --- a/Modules/IPStack/tcp.c +++ b/Modules/IPStack/tcp.c @@ -44,9 +44,9 @@ tSocketFile gTCP_ClientFile = {NULL, "tcpc", TCP_Client_Init}; // === GLOBALS === int giTCP_NumHalfopen = 0; -tSpinlock glTCP_Listeners; +tShortSpinlock glTCP_Listeners; tTCPListener *gTCP_Listeners; -tSpinlock glTCP_OutbountCons; +tShortSpinlock glTCP_OutbountCons; tTCPConnection *gTCP_OutbountCons; Uint32 gaTCP_PortBitmap[0x800]; int giTCP_NextOutPort = TCP_MIN_DYNPORT; @@ -223,7 +223,7 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe // it, just in case // Oh, wait, there is a case where a wildcard can be used // (srv->Interface == NULL) so having the lock is a good idea - LOCK(&srv->lConnections); + SHORTLOCK(&srv->lConnections); if( !srv->Connections ) srv->Connections = conn; else @@ -231,7 +231,7 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe srv->ConnectionsTail = conn; if(!srv->NewConnections) srv->NewConnections = conn; - RELEASE(&srv->lConnections); + SHORTLOCK(&srv->lConnections); // Send the SYN ACK hdr->Flags |= TCP_FLAG_ACK; @@ -357,7 +357,7 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head pkt->Sequence, Connection->NextSequenceRcv); // No? Well, let's cache it and look at it later - LOCK( &Connection->lFuturePackets ); + SHORTLOCK( &Connection->lFuturePackets ); for(tmp = Connection->FuturePackets; tmp; prev = tmp, tmp = tmp->Next) @@ -369,7 +369,7 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head else Connection->FuturePackets = pkt; pkt->Next = tmp; - RELEASE( &Connection->lFuturePackets ); + SHORTREL( &Connection->lFuturePackets ); } else { @@ -405,7 +405,7 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head */ void TCP_INT_AppendRecieved(tTCPConnection *Connection, tTCPStoredPacket *Pkt) { - LOCK( &Connection->lRecievedPackets ); + Mutex_Acquire( &Connection->lRecievedPackets ); if(Connection->RecievedBuffer->Length + Pkt->Length > Connection->RecievedBuffer->Space ) { Log_Error("TCP", "Buffer filled, packet dropped (%s)", @@ -417,7 +417,7 @@ void TCP_INT_AppendRecieved(tTCPConnection *Connection, tTCPStoredPacket *Pkt) RingBuffer_Write( Connection->RecievedBuffer, Pkt->Data, Pkt->Length ); - RELEASE( &Connection->lRecievedPackets ); + Mutex_Release( &Connection->lRecievedPackets ); } /** @@ -435,14 +435,14 @@ void TCP_INT_UpdateRecievedFromFuture(tTCPConnection *Connection) { prev = NULL; // Look for the next expected packet in the cache. - LOCK( &Connection->lFuturePackets ); + SHORTLOCK( &Connection->lFuturePackets ); for(pkt = Connection->FuturePackets; pkt && pkt->Sequence < Connection->NextSequenceRcv; prev = pkt, pkt = pkt->Next); // If we can't find the expected next packet, stop looking if(!pkt || pkt->Sequence > Connection->NextSequenceRcv) { - RELEASE( &Connection->lFuturePackets ); + SHORTREL( &Connection->lFuturePackets ); return; } @@ -453,7 +453,7 @@ void TCP_INT_UpdateRecievedFromFuture(tTCPConnection *Connection) Connection->FuturePackets = pkt->Next; // Release list - RELEASE( &Connection->lFuturePackets ); + SHORTREL( &Connection->lFuturePackets ); // Looks like we found one TCP_INT_AppendRecieved(Connection, pkt); @@ -548,10 +548,10 @@ tVFS_Node *TCP_Server_Init(tInterface *Interface) srv->Node.IOCtl = TCP_Server_IOCtl; srv->Node.Close = TCP_Server_Close; - LOCK(&glTCP_Listeners); + SHORTLOCK(&glTCP_Listeners); srv->Next = gTCP_Listeners; gTCP_Listeners = srv; - RELEASE(&glTCP_Listeners); + SHORTREL(&glTCP_Listeners); return &srv->Node; } @@ -573,10 +573,10 @@ char *TCP_Server_ReadDir(tVFS_Node *Node, int Pos) Log_Log("TCP", "Thread %i waiting for a connection", Threads_GetTID()); for(;;) { - LOCK( &srv->lConnections ); + SHORTLOCK( &srv->lConnections ); if( srv->NewConnections != NULL ) break; - RELEASE( &srv->lConnections ); - Threads_Yield(); + SHORTREL( &srv->lConnections ); + Threads_Yield(); // TODO: Sleep until poked continue; } @@ -586,12 +586,12 @@ char *TCP_Server_ReadDir(tVFS_Node *Node, int Pos) conn = srv->NewConnections; srv->NewConnections = conn->Next; + SHORTREL( &srv->lConnections ); + LOG("conn = %p", conn); LOG("srv->Connections = %p", srv->Connections); LOG("srv->NewConnections = %p", srv->NewConnections); LOG("srv->ConnectionsTail = %p", srv->ConnectionsTail); - - RELEASE( &srv->lConnections ); ret = malloc(9); itoa(ret, conn->Node.ImplInt, 16, 8, '0'); @@ -627,7 +627,7 @@ tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, const char *Name) Log_Debug("TCP", "srv->ConnectionsTail = %p", srv->ConnectionsTail); // Search - LOCK( &srv->lConnections ); + SHORTLOCK( &srv->lConnections ); for(conn = srv->Connections; conn; conn = conn->Next) @@ -635,7 +635,7 @@ tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, const char *Name) LOG("conn->Node.ImplInt = %i", conn->Node.ImplInt); if(conn->Node.ImplInt == id) break; } - RELEASE( &srv->lConnections ); + SHORTREL( &srv->lConnections ); // If not found, ret NULL if(!conn) { @@ -719,10 +719,10 @@ tVFS_Node *TCP_Client_Init(tInterface *Interface) conn->RecievedBuffer = RingBuffer_Create( TCP_RECIEVE_BUFFER_SIZE ); - LOCK(&glTCP_OutbountCons); + SHORTLOCK(&glTCP_OutbountCons); conn->Next = gTCP_OutbountCons; gTCP_OutbountCons = conn; - RELEASE(&glTCP_OutbountCons); + SHORTREL(&glTCP_OutbountCons); return &conn->Node; } @@ -754,11 +754,11 @@ Uint64 TCP_Client_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buff for(;;) { // Lock list and check if there is a packet - LOCK( &conn->lRecievedPackets ); + Mutex_Acquire( &conn->lRecievedPackets ); if( conn->RecievedBuffer->Length == 0 ) { // If not, release the lock, yield and try again - RELEASE( &conn->lRecievedPackets ); - Threads_Yield(); + Mutex_Release( &conn->lRecievedPackets ); + Threads_Yield(); // TODO: Less expensive wait continue; } @@ -766,7 +766,7 @@ Uint64 TCP_Client_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buff len = RingBuffer_Read( destbuf, conn->RecievedBuffer, Length ); // Release the lock (we don't need it any more) - RELEASE( &conn->lRecievedPackets ); + Mutex_Release( &conn->lRecievedPackets ); LEAVE('i', len); return len; diff --git a/Modules/IPStack/tcp.h b/Modules/IPStack/tcp.h index 492e035f..ab910fb9 100644 --- a/Modules/IPStack/tcp.h +++ b/Modules/IPStack/tcp.h @@ -67,7 +67,7 @@ struct sTCPListener tInterface *Interface; //!< Listening Interface tVFS_Node Node; //!< Server Directory node int NextID; //!< Name of the next connection - tSpinlock lConnections; //!< Spinlock for connections + tShortSpinlock lConnections; //!< Spinlock for connections tTCPConnection *Connections; //!< Connections (linked list) tTCPConnection *volatile NewConnections; tTCPConnection *ConnectionsTail; @@ -98,7 +98,7 @@ struct sTCPConnection * \note FIFO list * \{ */ - tSpinlock lQueuedPackets; + tMutex lQueuedPackets; tTCPStoredPacket *QueuedPackets; //!< Non-ACKed packets /** * \} @@ -109,7 +109,7 @@ struct sTCPConnection * \note Ring buffer * \{ */ - tSpinlock lRecievedPackets; + tMutex lRecievedPackets; tRingBuffer *RecievedBuffer; /** * \} @@ -120,7 +120,7 @@ struct sTCPConnection * \note Sorted list to improve times * \{ */ - tSpinlock lFuturePackets; //!< Future packets spinlock + tShortSpinlock lFuturePackets; //!< Future packets spinlock tTCPStoredPacket *FuturePackets; //!< Out of sequence packets /** * \} diff --git a/Modules/IPStack/udp.c b/Modules/IPStack/udp.c index 43dd023e..a721a7bb 100644 --- a/Modules/IPStack/udp.c +++ b/Modules/IPStack/udp.c @@ -31,13 +31,13 @@ Uint16 UDP_int_AllocatePort(); void UDP_int_FreePort(Uint16 Port); // === GLOBALS === -tSpinlock glUDP_Servers; +tMutex glUDP_Servers; tUDPServer *gpUDP_Servers; -tSpinlock glUDP_Channels; +tMutex glUDP_Channels; tUDPChannel *gpUDP_Channels; -tSpinlock glUDP_Ports; +tMutex glUDP_Ports; Uint32 gUDP_Ports[0x10000/32]; tSocketFile gUDP_ServerFile = {NULL, "udps", UDP_Server_Init}; @@ -57,7 +57,7 @@ void UDP_Initialise() } /** - * \brief Scan a list of tUDPChannel's and find process the first match + * \brief Scan a list of tUDPChannels and find process the first match * \return 0 if no match was found, -1 on error and 1 if a match was found */ int UDP_int_ScanList(tUDPChannel *List, tInterface *Interface, void *Address, int Length, void *Buffer) @@ -83,7 +83,7 @@ int UDP_int_ScanList(tUDPChannel *List, tInterface *Interface, void *Address, in } else { Warning("[UDP ] Address type %i unknown", Interface->Type); - RELEASE(&glUDP_Channels); + Mutex_Release(&glUDP_Channels); return -1; } @@ -96,13 +96,13 @@ int UDP_int_ScanList(tUDPChannel *List, tInterface *Interface, void *Address, in memcpy(pack->Data, hdr->Data, len); // Add the packet to the channel's queue - LOCK(&chan->lQueue); + SHORTLOCK(&chan->lQueue); if(chan->Queue) chan->QueueEnd->Next = pack; else chan->QueueEnd = chan->Queue = pack; - RELEASE(&chan->lQueue); - RELEASE(&glUDP_Channels); + SHORTREL(&chan->lQueue); + Mutex_Release(&glUDP_Channels); return 1; } return 0; @@ -124,14 +124,14 @@ void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe Log("[UDP ] hdr->Checksum = 0x%x", ntohs(hdr->Checksum)); // Check registered connections - LOCK(&glUDP_Channels); + Mutex_Acquire(&glUDP_Channels); ret = UDP_int_ScanList(gpUDP_Channels, Interface, Address, Length, Buffer); - RELEASE(&glUDP_Channels); + Mutex_Release(&glUDP_Channels); if(ret != 0) return ; // TODO: Server/Listener - LOCK(&glUDP_Servers); + Mutex_Acquire(&glUDP_Servers); for(srv = gpUDP_Servers; srv; srv = srv->Next) @@ -145,7 +145,7 @@ void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe Warning("[UDP ] TODO - Add channel on connection"); //TODO } - RELEASE(&glUDP_Servers); + Mutex_Release(&glUDP_Servers); } @@ -201,10 +201,10 @@ tVFS_Node *UDP_Server_Init(tInterface *Interface) new->Node.IOCtl = UDP_Server_IOCtl; new->Node.Close = UDP_Server_Close; - LOCK(&glUDP_Servers); + Mutex_Acquire(&glUDP_Servers); new->Next = gpUDP_Servers; gpUDP_Servers = new; - RELEASE(&glUDP_Servers); + Mutex_Release(&glUDP_Servers); return &new->Node; } @@ -221,13 +221,13 @@ char *UDP_Server_ReadDir(tVFS_Node *Node, int ID) if( srv->ListenPort == 0 ) return NULL; // Lock (so another thread can't collide with us here) and wait for a connection - LOCK( &srv->Lock ); + Mutex_Acquire( &srv->Lock ); while( srv->NewChannels == NULL ) Threads_Yield(); // Pop the connection off the new list chan = srv->NewChannels; srv->NewChannels = chan->Next; // Release the lock - RELEASE( &srv->Lock ); + Mutex_Release( &srv->Lock ); // Create the ID string and return it ret = malloc(11+1); @@ -324,7 +324,7 @@ void UDP_Server_Close(tVFS_Node *Node) // Remove from the main list first - LOCK(&glUDP_Servers); + Mutex_Acquire(&glUDP_Servers); if(gpUDP_Servers == srv) gpUDP_Servers = gpUDP_Servers->Next; else @@ -337,28 +337,28 @@ void UDP_Server_Close(tVFS_Node *Node) else prev->Next = prev->Next->Next; } - RELEASE(&glUDP_Servers); + Mutex_Release(&glUDP_Servers); - LOCK(&srv->Lock); + Mutex_Acquire(&srv->Lock); for(chan = srv->Channels; chan; chan = chan->Next) { // Clear Queue - LOCK(&chan->lQueue); + SHORTLOCK(&chan->lQueue); while(chan->Queue) { tmp = chan->Queue; chan->Queue = tmp->Next; free(tmp); } - RELEASE(&chan->lQueue); + SHORTREL(&chan->lQueue); // Free channel structure free(chan); } - RELEASE(&srv->Lock); + Mutex_Release(&srv->Lock); free(srv); } @@ -377,10 +377,10 @@ tVFS_Node *UDP_Channel_Init(tInterface *Interface) new->Node.IOCtl = UDP_Channel_IOCtl; new->Node.Close = UDP_Channel_Close; - LOCK(&glUDP_Channels); + Mutex_Acquire(&glUDP_Channels); new->Next = gpUDP_Channels; gpUDP_Channels = new; - RELEASE(&glUDP_Channels); + Mutex_Release(&glUDP_Channels); return &new->Node; } @@ -400,15 +400,15 @@ Uint64 UDP_Channel_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buf for(;;) { - LOCK(&chan->lQueue); + SHORTLOCK(&chan->lQueue); if(chan->Queue == NULL) { - RELEASE(&chan->lQueue); + SHORTREL(&chan->lQueue); continue; } pack = chan->Queue; chan->Queue = pack->Next; if(!chan->Queue) chan->QueueEnd = NULL; - RELEASE(&chan->lQueue); + SHORTREL(&chan->lQueue); break; } @@ -521,7 +521,7 @@ void UDP_Channel_Close(tVFS_Node *Node) tUDPChannel *prev; // Remove from the main list first - LOCK(&glUDP_Channels); + Mutex_Acquire(&glUDP_Channels); if(gpUDP_Channels == chan) gpUDP_Channels = gpUDP_Channels->Next; else @@ -534,10 +534,10 @@ void UDP_Channel_Close(tVFS_Node *Node) else prev->Next = prev->Next->Next; } - RELEASE(&glUDP_Channels); + Mutex_Release(&glUDP_Channels); // Clear Queue - LOCK(&chan->lQueue); + SHORTLOCK(&chan->lQueue); while(chan->Queue) { tUDPPacket *tmp; @@ -545,7 +545,7 @@ void UDP_Channel_Close(tVFS_Node *Node) chan->Queue = tmp->Next; free(tmp); } - RELEASE(&chan->lQueue); + SHORTREL(&chan->lQueue); // Free channel structure free(chan); @@ -557,7 +557,7 @@ void UDP_Channel_Close(tVFS_Node *Node) Uint16 UDP_int_AllocatePort() { int i; - LOCK(&glUDP_Ports); + Mutex_Acquire(&glUDP_Ports); // Fast Search for( i = UDP_ALLOC_BASE; i < 0x10000; i += 32 ) if( gUDP_Ports[i/32] != 0xFFFFFFFF ) @@ -568,7 +568,7 @@ Uint16 UDP_int_AllocatePort() if( !(gUDP_Ports[i/32] & (1 << (i%32))) ) return i; } - RELEASE(&glUDP_Ports); + Mutex_Release(&glUDP_Ports); } /** @@ -577,13 +577,13 @@ Uint16 UDP_int_AllocatePort() */ int UDP_int_MarkPortAsUsed(Uint16 Port) { - LOCK(&glUDP_Ports); + Mutex_Acquire(&glUDP_Ports); if( gUDP_Ports[Port/32] & (1 << (Port%32)) ) { return 0; - RELEASE(&glUDP_Ports); + Mutex_Release(&glUDP_Ports); } gUDP_Ports[Port/32] |= 1 << (Port%32); - RELEASE(&glUDP_Ports); + Mutex_Release(&glUDP_Ports); return 1; } @@ -592,7 +592,7 @@ int UDP_int_MarkPortAsUsed(Uint16 Port) */ void UDP_int_FreePort(Uint16 Port) { - LOCK(&glUDP_Ports); + Mutex_Acquire(&glUDP_Ports); gUDP_Ports[Port/32] &= ~(1 << (Port%32)); - RELEASE(&glUDP_Ports); + Mutex_Release(&glUDP_Ports); } diff --git a/Modules/IPStack/udp.h b/Modules/IPStack/udp.h index 2cc743ab..657bce5a 100644 --- a/Modules/IPStack/udp.h +++ b/Modules/IPStack/udp.h @@ -40,7 +40,7 @@ struct sUDPChannel } RemoteAddr; Uint16 RemotePort; tVFS_Node Node; - tSpinlock lQueue; + tShortSpinlock lQueue; tUDPPacket * volatile Queue; tUDPPacket *QueueEnd; }; @@ -56,7 +56,7 @@ struct sUDPServer int NextID; int NumChannels; tUDPChannel *Channels; - tSpinlock Lock; + tMutex Lock; tUDPChannel * volatile NewChannels; }; diff --git a/Modules/Storage/ATA/io.c b/Modules/Storage/ATA/io.c index 89fcdb9f..0f442acd 100644 --- a/Modules/Storage/ATA/io.c +++ b/Modules/Storage/ATA/io.c @@ -94,7 +94,7 @@ Uint8 *gATA_BusMasterBasePtr; //!< Paging Mapped MMIO (If needed) int gATA_IRQSec = 15; volatile int gaATA_IRQs[2] = {0}; // - Locks to avoid tripping -tSpinlock giaATA_ControllerLock[2]; +tMutex glaATA_ControllerLock[2]; // - Buffers! Uint8 gATA_Buffers[2][(MAX_DMA_SECTORS+0xFFF)&~0xFFF] __attribute__ ((section(".padata"))); // - PRDTs @@ -283,7 +283,7 @@ int ATA_ReadDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer) } // Get exclusive access to the disk controller - LOCK( &giaATA_ControllerLock[ cont ] ); + Mutex_Acquire( &glaATA_ControllerLock[ cont ] ); // Set Size gATA_PRDTs[ cont ].Bytes = Count * SECTOR_SIZE; @@ -345,7 +345,7 @@ int ATA_ReadDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer) if( gaATA_IRQs[cont] == 0 ) { // Release controller lock - RELEASE( &giaATA_ControllerLock[ cont ] ); + Mutex_Release( &glaATA_ControllerLock[ cont ] ); Log_Warning("ATA", "Read timeout on disk %i (Reading sector 0x%llx)\n", Disk, Address); @@ -357,7 +357,7 @@ int ATA_ReadDMA(Uint8 Disk, Uint64 Address, Uint Count, void *Buffer) // Copy to destination buffer memcpy( Buffer, gATA_Buffers[cont], Count*SECTOR_SIZE ); // Release controller lock - RELEASE( &giaATA_ControllerLock[ cont ] ); + Mutex_Release( &glaATA_ControllerLock[ cont ] ); LEAVE('i', 0); return 0; @@ -384,7 +384,7 @@ int ATA_WriteDMA(Uint8 Disk, Uint64 Address, Uint Count, const void *Buffer) if(Count > MAX_DMA_SECTORS) return 1; // Get exclusive access to the disk controller - LOCK( &giaATA_ControllerLock[ cont ] ); + Mutex_Acquire( &glaATA_ControllerLock[ cont ] ); // Set Size gATA_PRDTs[ cont ].Bytes = Count * SECTOR_SIZE; @@ -437,11 +437,11 @@ int ATA_WriteDMA(Uint8 Disk, Uint64 Address, Uint Count, const void *Buffer) // If the IRQ is unset, return error if( gaATA_IRQs[cont] == 0 ) { // Release controller lock - RELEASE( &giaATA_ControllerLock[ cont ] ); + Mutex_Release( &glaATA_ControllerLock[ cont ] ); return 1; // Error } else { - RELEASE( &giaATA_ControllerLock[ cont ] ); + Mutex_Release( &glaATA_ControllerLock[ cont ] ); return 0; } } diff --git a/Modules/Storage/FDD/fdd.c b/Modules/Storage/FDD/fdd.c index 149465e3..974e5295 100644 --- a/Modules/Storage/FDD/fdd.c +++ b/Modules/Storage/FDD/fdd.c @@ -113,7 +113,7 @@ void FDD_int_StartMotor(int Disk); // === GLOBALS === MODULE_DEFINE(0, FDD_VERSION, FDD, FDD_Install, NULL, "ISADMA", NULL); t_floppyDevice gFDD_Devices[2]; -tSpinlock glFDD; +tMutex glFDD; volatile int gbFDD_IrqFired = 0; tDevFS_Driver gFDD_DriverInfo = { NULL, "fdd", @@ -214,12 +214,12 @@ void FDD_UnloadModule() { int i; //DevFS_DelDevice( &gFDD_DriverInfo ); - LOCK(&glFDD); + Mutex_Acquire(&glFDD); for(i=0;i<4;i++) { Time_RemoveTimer(gFDD_Devices[i].timer); FDD_int_StopMotor((void *)(Uint)i); } - RELEASE(&glFDD); + Mutex_Release(&glFDD); //IRQ_Clear(6); } @@ -370,11 +370,11 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf } LOG("Cyl=%i, Head=%i, Sector=%i", cyl, head, sec); - LOCK(&glFDD); // Lock to stop the motor stopping on us + Mutex_Acquire(&glFDD); // Lock to stop the motor stopping on us Time_RemoveTimer(gFDD_Devices[Disk].timer); // Remove Old Timer // Start motor if needed if(gFDD_Devices[Disk].motorState != 2) FDD_int_StartMotor(Disk); - RELEASE(&glFDD); + Mutex_Release(&glFDD); LOG("Wait for the motor to spin up"); @@ -382,7 +382,7 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf while(gFDD_Devices[Disk].motorState == 1) Threads_Yield(); LOG("Acquire Spinlock"); - LOCK(&glFDD); + Mutex_Acquire(&glFDD); // Seek to track outb(base + CALIBRATE_DRIVE, 0); @@ -390,7 +390,7 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf while(FDD_int_SeekTrack(Disk, head, (Uint8)cyl) == 0 && i++ < FDD_SEEK_TIMEOUT ) Threads_Yield(); if( i > FDD_SEEK_TIMEOUT ) { - RELEASE(&glFDD); + Mutex_Release(&glFDD); LEAVE('i', 0); return 0; } @@ -487,7 +487,7 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf // Release Spinlock LOG("Realeasing Spinlock and setting motor to stop"); - RELEASE(&glFDD); + Mutex_Release(&glFDD); if(i == FDD_MAX_READWRITE_ATTEMPTS) { Log_Warning("FDD", "Exceeded %i attempts in %s the disk", @@ -804,7 +804,7 @@ void FDD_int_StartMotor(int disk) void FDD_int_StopMotor(void *Arg) { Uint8 state, disk = (Uint)Arg; - if( IS_LOCKED(&glFDD) ) return ; + if( Mutex_IsLocked(&glFDD) ) return ; ENTER("iDisk", disk); state = inb( cPORTBASE[ disk>>1 ] + PORT_DIGOUTPUT ); -- 2.20.1