00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __SYNC_H__
00012 #define __SYNC_H__
00013
00014 #ifdef _WIN32
00015 #include "sync_w32.h"
00016 #else // Unix
00017 #include "sync_unix.h"
00018 #endif
00019
00020 BEGIN_FASTDB_NAMESPACE
00021
00022 class FASTDB_DLL_ENTRY dbSystem {
00023 public:
00024 static unsigned getCurrentTimeMsec();
00025 };
00026
00027
00028 class FASTDB_DLL_ENTRY dbCriticalSection {
00029 private:
00030 dbMutex& mutex;
00031 public:
00032 dbCriticalSection(dbMutex& guard) : mutex(guard) {
00033 mutex.lock();
00034 }
00035 ~dbCriticalSection() {
00036 mutex.unlock();
00037 }
00038 };
00039
00040 #define SMALL_BUF_SIZE 512
00041
00042 class FASTDB_DLL_ENTRY dbSmallBuffer {
00043 protected:
00044 char smallBuf[SMALL_BUF_SIZE];
00045 char* buf;
00046 size_t used;
00047
00048 public:
00049 dbSmallBuffer(size_t size) {
00050 if (size > SMALL_BUF_SIZE) {
00051 buf = new char[size];
00052 } else {
00053 buf = smallBuf;
00054 }
00055 used = size;
00056 }
00057
00058 dbSmallBuffer() {
00059 used = 0;
00060 buf = smallBuf;
00061 }
00062
00063 void put(size_t size) {
00064 if (size > SMALL_BUF_SIZE && size > used) {
00065 if (buf != smallBuf) {
00066 delete[] buf;
00067 }
00068 buf = new char[size];
00069 used = size;
00070 }
00071 }
00072
00073 operator char*() { return buf; }
00074 char* base() { return buf; }
00075
00076 ~dbSmallBuffer() {
00077 if (buf != smallBuf) {
00078 delete[] buf;
00079 }
00080 }
00081 };
00082
00083 class dbThreadPool;
00084
00085 class FASTDB_DLL_ENTRY dbPooledThread {
00086 private:
00087 friend class dbThreadPool;
00088
00089 dbThread thread;
00090 dbThreadPool* pool;
00091 dbPooledThread* next;
00092 dbThread::thread_proc_t f;
00093 void* arg;
00094 bool running;
00095 dbLocalSemaphore startSem;
00096 dbLocalSemaphore readySem;
00097
00098 static void thread_proc pooledThreadFunc(void* arg);
00099
00100 void run();
00101 void stop();
00102
00103 dbPooledThread(dbThreadPool* threadPool);
00104 ~dbPooledThread();
00105 };
00106
00107 class FASTDB_DLL_ENTRY dbThreadPool {
00108 friend class dbPooledThread;
00109 dbPooledThread* freeThreads;
00110 dbMutex mutex;
00111
00112 public:
00113 dbPooledThread* create(dbThread::thread_proc_t f, void* arg);
00114 void join(dbPooledThread* thr);
00115 dbThreadPool();
00116 ~dbThreadPool();
00117 };
00118
00119 END_FASTDB_NAMESPACE
00120
00121 #endif // __SYNC_H__
00122
00123