?? win32_critical.h
字號:
/* * Copyright (C) 2005-2007 gulikoza * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* $Id$ *//* Mutex functions using the Win32 Critical Section */#ifndef CRITICAL_H#define CRITICAL_H#define MODULE "Critical"#include "log.h"#ifdef WIN32#define MUTEX WIN_mutex#ifdef SEM_SILENT#define MUTEX_LOCK(x) WIN_LockMutex(x)#else#define MUTEX_LOCK(x) WIN_LockMutex(x, #x)#endif#define MUTEX_UNLOCK(x) WIN_UnlockMutex(x)#define MUTEX_CREATE WIN_CreateMutex#define MUTEX_DESTROY(x) WIN_DestroyMutex(x)struct WIN_mutex { CRITICAL_SECTION cs;#ifndef SEM_SILENT unsigned int thread; HANDLE hSemaphore;#endif};#include "SDL_Error.h"#include "SDL_timer.h"#include "SDL_thread.h"/* Create a mutex */static inline WIN_mutex *WIN_CreateMutex(void){ WIN_mutex *mutex; /* Allocate mutex memory */ mutex = (WIN_mutex *)malloc(sizeof(*mutex)); if(mutex) {#ifndef SEM_SILENT // Create waiting semaphore mutex->hSemaphore = CreateSemaphore(NULL, 0, 1, NULL); if(mutex->hSemaphore == NULL) { ERROR_MSG("Error creating mutex semaphore"); free(mutex); return NULL; } mutex->thread = 0;#endif /* Create the mutex */ InitializeCriticalSection(&mutex->cs); } else { SDL_OutOfMemory(); } return mutex;}/* Free the mutex */static inline void WIN_DestroyMutex(WIN_mutex *mutex){ if(mutex) {#ifndef SEM_SILENT if(mutex->thread != 0) { ERROR_MSG("WARNING: Destroying locked mutex (%d)!", mutex->thread); } CloseHandle(mutex->hSemaphore);#endif DeleteCriticalSection(&mutex->cs); free(mutex); }}/* Lock the mutex */#ifdef SEM_SILENTstatic inline int WIN_LockMutex(WIN_mutex *mutex){ EnterCriticalSection(&mutex->cs);#elsestatic inline int WIN_LockMutex(WIN_mutex *mutex, const char * name){ while(!TryEnterCriticalSection(&mutex->cs)) { if(WaitForSingleObject(mutex->hSemaphore, 1000L)) { ERROR_MSG("Thread %u mutex timeout %s, owning thread: %u", SDL_ThreadID(), name, mutex->thread); } } if(mutex->thread != 0) { ERROR_MSG("WARNING: Thread %u (=%u) re-entering mutex %s", SDL_ThreadID(), mutex->thread, name); } mutex->thread = SDL_ThreadID();#endif return 0;}/* Unlock the mutex */static inline int WIN_UnlockMutex(WIN_mutex *mutex){#ifndef SEM_SILENT mutex->thread = 0;#endif LeaveCriticalSection(&mutex->cs);#ifndef SEM_SILENT ReleaseSemaphore(mutex->hSemaphore, 1, NULL);#endif return 0;}#endif // WIN32#undef MODULE#endif // CRITICAL_H
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -