Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

threads_win32.cpp

Go to the documentation of this file.
00001 #include <windows.h>
00002 #include <process.h>
00003 #include "threads.hpp"
00004 
00005 
00006 struct ThreadInternal
00007 {
00008   AI_ThreadRoutine routine;
00009   void*         opaque;
00010   int           priority;
00011 };
00012 
00013 struct AI_CriticalSectionStruct
00014 {
00015   CRITICAL_SECTION cs;
00016 };
00017 
00018 
00019 static void __cdecl InternalThreadRoutine(void* opaque);
00020 
00021 
00023 
00024 bool AI_CreateThread(AI_ThreadRoutine routine, void* opaque, int priority)
00025 {
00026   // create internal thread data
00027   ThreadInternal* internal = new ThreadInternal;
00028   internal->routine  = routine;
00029   internal->opaque   = opaque;
00030   internal->priority = priority;
00031 
00032   // create the actual thread
00033   // use _beginthread because it makes a thread-safe C library
00034   unsigned long handle = ::_beginthread(InternalThreadRoutine, 0, internal);
00035   return (handle != -1);
00036 }
00037 
00039 
00040 void __cdecl InternalThreadRoutine(void* opaque)
00041 {
00042   ThreadInternal* internal = (ThreadInternal*)opaque;
00043 
00044   int priority;
00045   if (internal->priority < -2) {
00046     priority = THREAD_PRIORITY_IDLE;
00047   } else if (internal->priority == -2) {
00048     priority = THREAD_PRIORITY_LOWEST;
00049   } else if (internal->priority == -1) {
00050     priority = THREAD_PRIORITY_BELOW_NORMAL;
00051   } else if (internal->priority == 0) {
00052     priority = THREAD_PRIORITY_NORMAL;
00053   } else if (internal->priority == 1) {
00054     priority = THREAD_PRIORITY_ABOVE_NORMAL;
00055   } else {
00056     priority = THREAD_PRIORITY_HIGHEST;
00057   }
00058 
00059   // set the priority
00060   SetThreadPriority(GetCurrentThread(), priority);
00061 
00062   // call the function passed 
00063   internal->routine(internal->opaque);
00064   delete internal;
00065 }
00066 
00068 
00069 void AI_Sleep(unsigned milliseconds)
00070 {
00071   ::Sleep(milliseconds);
00072 }
00073 
00075 
00076 AI_CriticalSection AI_CreateCriticalSection()
00077 {
00078   AI_CriticalSectionStruct* cs = new AI_CriticalSectionStruct;
00079   ::InitializeCriticalSection(&cs->cs);
00080   return cs;
00081 }
00082 
00084 
00085 void AI_DestroyCriticalSection(AI_CriticalSection cs)
00086 {
00087   ::DeleteCriticalSection(&cs->cs);
00088   delete cs;
00089 }
00090 
00092 
00093 void AI_EnterCriticalSection(AI_CriticalSection cs)
00094 {
00095   ::EnterCriticalSection(&cs->cs);
00096 }
00097 
00099 
00100 void AI_LeaveCriticalSection(AI_CriticalSection cs)
00101 {
00102   ::LeaveCriticalSection(&cs->cs);
00103 }
00104 

Generated at Mon Jun 10 02:55:13 2002 for audiere by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001