multithread_win32.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:4k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*****************************************************************************
  2.  *
  3.  * This program is free software ; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  16.  *
  17.  * $Id: multithread_win32.c 343 2005-11-16 20:11:07Z picard $
  18.  *
  19.  * The Core Pocket Media Player
  20.  * Copyright (c) 2004-2005 Gabor Kovacs
  21.  *
  22.  ****************************************************************************/
  23. #include "../common.h"
  24. #if defined(TARGET_WIN32) || defined(TARGET_WINCE)
  25. //#define LOCK_TIMEOUT
  26. #ifndef STRICT
  27. #define STRICT
  28. #endif
  29. #include <windows.h>
  30. #if defined(TARGET_WINCE)
  31. extern BOOL (WINAPI* FuncCeSetThreadQuantum)(HANDLE, DWORD);
  32. #endif
  33. void* LockCreate()
  34. {
  35. #ifndef LOCK_TIMEOUT
  36. void* p = malloc(sizeof(CRITICAL_SECTION));
  37. InitializeCriticalSection((LPCRITICAL_SECTION)p);
  38. return p;
  39. #else
  40. return CreateMutex(NULL,FALSE,NULL);
  41. #endif
  42. }
  43. void LockDelete(void* p)
  44. {
  45. if (p)
  46. {
  47. #ifndef LOCK_TIMEOUT
  48. DeleteCriticalSection((LPCRITICAL_SECTION)p);
  49. free(p);
  50. #else
  51. CloseHandle(p);
  52. #endif
  53. }
  54. }
  55. void LockEnter(void* p)
  56. {
  57. if (p)
  58. #ifndef LOCK_TIMEOUT
  59. EnterCriticalSection((LPCRITICAL_SECTION)p);
  60. #else
  61. while (WaitForSingleObject(p,3000)==WAIT_TIMEOUT)
  62. DebugBreak();
  63. #endif
  64. }
  65. void LockLeave(void* p)
  66. {
  67. if (p)
  68. #ifndef LOCK_TIMEOUT
  69. LeaveCriticalSection((LPCRITICAL_SECTION)p);
  70. #else
  71. ReleaseMutex(p);
  72. #endif
  73. }
  74. int ThreadPriority(void* Thread,int Priority)
  75. {
  76. int Old;
  77. if (!Thread) Thread = GetCurrentThread();
  78. Old = GetThreadPriority(Thread);
  79. #if defined(TARGET_WINCE)
  80. Priority = THREAD_PRIORITY_NORMAL + Priority;
  81. if (Priority > THREAD_PRIORITY_IDLE)
  82. Priority = THREAD_PRIORITY_IDLE;
  83. if (Priority < THREAD_PRIORITY_TIME_CRITICAL)
  84. Priority = THREAD_PRIORITY_TIME_CRITICAL;
  85. SetThreadPriority(Thread,Priority);
  86. if (FuncCeSetThreadQuantum)
  87. {
  88. if (Old == THREAD_PRIORITY_TIME_CRITICAL && Priority != THREAD_PRIORITY_TIME_CRITICAL)
  89. FuncCeSetThreadQuantum(Thread,25);
  90. if (Old != THREAD_PRIORITY_TIME_CRITICAL && Priority == THREAD_PRIORITY_TIME_CRITICAL)
  91. FuncCeSetThreadQuantum(Thread,250);
  92. }
  93. return Old - THREAD_PRIORITY_NORMAL;
  94. #else
  95. Priority = THREAD_PRIORITY_NORMAL - Priority;
  96. if (Priority < THREAD_PRIORITY_IDLE)
  97. Priority = THREAD_PRIORITY_IDLE;
  98. if (Priority > THREAD_PRIORITY_TIME_CRITICAL)
  99. Priority = THREAD_PRIORITY_TIME_CRITICAL;
  100. SetThreadPriority(Thread,Priority);
  101. return -(Old - THREAD_PRIORITY_NORMAL);
  102. #endif
  103. }
  104. bool_t EventWait(void* Handle,int Time) { return WaitForSingleObject(Handle,Time) == WAIT_OBJECT_0; } // -1 = INFINITE
  105. void* EventCreate(bool_t ManualReset,bool_t InitState) { return CreateEvent(NULL,ManualReset,InitState,NULL); }
  106. void EventSet(void* Handle) { SetEvent(Handle); }
  107. void EventReset(void* Handle) { ResetEvent(Handle); }
  108. void EventClose(void* Handle) { CloseHandle(Handle); }
  109. int ThreadId() { return GetCurrentThreadId(); }
  110. void ThreadSleep(int Time) { Sleep(Time); }
  111. void ThreadTerminate(void* Handle)
  112. {
  113. if (Handle && WaitForSingleObject(Handle,5000) == WAIT_TIMEOUT)
  114. TerminateThread(Handle,0);
  115. }
  116. void* ThreadCreate(int(*Start)(void*),void* Parameter,int Quantum)
  117. {
  118. DWORD Id;
  119. HANDLE Handle = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Start,Parameter,0,&Id);
  120. #if defined(TARGET_WINCE)
  121. if (FuncCeSetThreadQuantum)
  122. FuncCeSetThreadQuantum(Handle,Quantum);
  123. #endif
  124. return Handle;
  125. }
  126. #endif