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

threads_posix.cpp

Go to the documentation of this file.
00001 #include <unistd.h>
00002 #include <pthread.h>
00003 #include "threads.h"
00004 
00005 
00006 namespace audiere {
00007 
00008   struct ThreadInternal {
00009     AI_ThreadRoutine routine;
00010     void* opaque;
00011   };
00012 
00013 
00014   struct AI_CriticalSectionStruct {
00015     pthread_mutex_t mutex;
00016   };
00017 
00018 
00019   void* ThreadRoutine(void* arg) {
00020     ThreadInternal* ti = (ThreadInternal*)arg;
00021     ti->routine(ti->opaque);
00022     delete ti;
00023     return NULL;
00024   }
00025 
00026 
00027   bool AI_CreateThread(AI_ThreadRoutine routine, void* opaque, int priority) {
00028     ThreadInternal* ti = new ThreadInternal;
00029     ti->routine = routine;
00030     ti->opaque  = opaque;
00031 
00032     pthread_t thread;
00033     int result = pthread_create(&thread, NULL, ThreadRoutine, ti);
00034     if (result != 0) {
00035       delete ti;
00036       return false;
00037     } else {
00038       return true;
00039     }
00040   }
00041 
00042 
00043   void AI_Sleep(unsigned milliseconds) {
00044     int seconds = milliseconds / 1000;
00045     int useconds = (milliseconds % 1000) * 1000;
00046 
00047     sleep(seconds);
00048     usleep(useconds);
00049   }
00050 
00051 
00052   AI_CriticalSection AI_CreateCriticalSection() {
00053     AI_CriticalSectionStruct* cs = new AI_CriticalSectionStruct;
00054     int result = pthread_mutex_init(&cs->mutex, 0);
00055     if (result != 0) {
00056       delete cs;
00057       return NULL;
00058     }
00059 
00060     return cs;
00061   }
00062 
00063   void AI_DestroyCriticalSection(AI_CriticalSection cs) {
00064     pthread_mutex_destroy(&cs->mutex);
00065   }
00066 
00067   void AI_EnterCriticalSection(AI_CriticalSection cs) {
00068     pthread_mutex_lock(&cs->mutex);
00069   }
00070 
00071   void AI_LeaveCriticalSection(AI_CriticalSection cs) {
00072     pthread_mutex_unlock(&cs->mutex);
00073   }
00074 
00075 }

Generated on Sat Oct 12 01:43:04 2002 for audiere by doxygen1.2.17