00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _DECAF_INTERNAL_UTIL_CONCURRENT_THREADING_H_
00019 #define _DECAF_INTERNAL_UTIL_CONCURRENT_THREADING_H_
00020
00021 #include <decaf/util/Config.h>
00022
00023 #include <decaf/lang/Thread.h>
00024
00025 namespace decaf {
00026 namespace internal {
00027 namespace util {
00028 namespace concurrent {
00029
00030 using decaf::lang::Thread;
00031
00032 class ThreadLocalImpl;
00033 struct ThreadHandle;
00034 struct MonitorHandle;
00035
00036 class DECAF_API Threading {
00037 private:
00038
00039 Threading();
00040 Threading(const Threading&);
00041 Threading& operator= (const Threading&);
00042
00043 public:
00044
00050 static void initialize();
00051
00057 static void shutdown();
00058
00063 static void lockThreadsLib();
00064
00068 static void unlockThreadsLib();
00069
00073 static void dumpRunningThreads();
00074
00075 public:
00076
00084 static MonitorHandle* takeMonitor(bool alreadyLocked = false);
00085
00094 static void returnMonitor(MonitorHandle* monitor, bool alreadyLocked = false);
00095
00104 static void enterMonitor(MonitorHandle* monitor);
00105
00118 static bool tryEnterMonitor(MonitorHandle* monitor);
00119
00130 static void exitMonitor(MonitorHandle* monitor);
00131
00151 static bool waitOnMonitor(MonitorHandle* monitor, long long mills, int nanos);
00152
00163 static void notifyWaiter(MonitorHandle* monitor);
00164
00175 static void notifyAllWaiters(MonitorHandle* monitor);
00176
00182 static bool isMonitorLocked(MonitorHandle* monitor);
00183
00184 public:
00185
00201 static ThreadHandle* createNewThread(Thread* parant, const char* name,
00202 long long stackSize);
00203
00211 static void start(ThreadHandle* thread);
00212
00230 static bool join(ThreadHandle* thread, long long mills, int nanos);
00231
00232 static void interrupt(ThreadHandle* thread);
00233 static bool interrupted();
00234 static bool isInterrupted(ThreadHandle* thread, bool reset);
00235
00236 static void yeild();
00237 static bool sleep(long long mills, int nanos);
00238
00239 static long long getThreadId(ThreadHandle* thread);
00240
00241 static int getThreadPriority(ThreadHandle* thread);
00242
00243 static void setThreadPriority(ThreadHandle* thread, int priority);
00244
00245 static const char* getThreadName(ThreadHandle* thread);
00246
00247 static void setThreadName(ThreadHandle* thread, const char* name);
00248
00249 static Thread::State getThreadState(ThreadHandle* thread);
00250
00251 static bool isThreadAlive(ThreadHandle* thread);
00252
00253 static void destroyThread(ThreadHandle* thread);
00254
00270 static ThreadHandle* createThreadWrapper(decaf::lang::Thread* parent, const char* name);
00271
00275 static Thread* getCurrentThread();
00276
00280 static ThreadHandle* getCurrentThreadHandle();
00281
00290 static void park(Thread* thread);
00291
00304 static bool park(Thread* thread, long long mills, int nanos);
00305
00314 static void unpark(Thread* thread);
00315
00316 public:
00317
00329 static int createThreadLocalSlot(ThreadLocalImpl* threadLocal);
00330
00331 static void* getThreadLocalValue(int slot);
00332
00333 static void setThreadLocalValue(int slot, void* value);
00334
00335 static void destoryThreadLocalSlot(int slot);
00336
00337 private:
00338
00339 static ThreadHandle* attachToCurrentThread();
00340
00341 static void monitorEnterUsingThreadId(MonitorHandle* monitor, ThreadHandle* thread);
00342 static bool monitorTryEnterUsingThreadId(MonitorHandle* monitor, ThreadHandle* thread);
00343 static void monitorExitUsingThreadId(MonitorHandle* monitor, ThreadHandle* thread);
00344 };
00345
00346 }}}}
00347
00348 #endif