00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 #ifndef multi_h_
00093 #define multi_h_
00094
00095 #include <agent_pp/agent++.h>
00096
00097 #ifdef _THREADS
00098 #ifdef _WIN32THREADS
00099 #include <winbase.h>
00100 #else
00101 #include <pthread.h>
00102 #endif
00103 #endif
00104
00105 #include <time.h>
00106 #include <sys/types.h>
00107
00108 #include <agent_pp/List.h>
00109
00110
00111 #define MULTI_THREADED TRUE
00112 #define SINGLE_THREADED FALSE
00113 #define AGENTPP_DEFAULT_STACKSIZE 0x10000
00114
00115 #ifdef AGENTPP_NAMESPACE
00116 namespace Agentpp {
00117 #endif
00118
00119 class MibEntry;
00120 class Request;
00121 class Mib;
00122
00123
00124 typedef enum { STANDARD_CB, SHADOW_CB } cb_type;
00125
00126 typedef void (Mib::*mib_method_t)(Request*);
00127
00128
00129
00130
00131 class AGENTPP_DECL MibMethodCall {
00132
00133 public:
00134 MibMethodCall(Mib* c,
00135 void (Mib::*m) (Request *),
00136 Request* r):
00137
00138 called_class(c), method(m), req(r) {
00139 }
00140
00141 MibMethodCall(const MibMethodCall& other) {
00142
00143 called_class = other.called_class;
00144 method = other.method;
00145 req = other.req;
00146 }
00147
00148 Mib* called_class;
00149 mib_method_t method;
00150 Request* req;
00151 };
00152
00153
00154 void* method_routine_caller(void *);
00155
00156
00157
00158 #define TS_SYNCHRONIZED(x) { ThreadSynchronize _ts_synchronize(*this); x }
00159
00160 #ifdef _THREADS
00161 #ifndef WIN32
00162 #ifndef POSIX_THREADS
00163 #error "This SYSTEM does not support threads. Undefine _THREADS in agent++.h"
00164 #endif
00165 #endif
00166
00167
00191 class AGENTPP_DECL Runnable {
00192
00193 public:
00194 Runnable() { }
00195 virtual ~Runnable() { }
00196
00202 virtual void run() = 0;
00203 };
00204
00212 class AGENTPP_DECL Synchronized {
00213 public:
00214 Synchronized();
00215 ~Synchronized();
00216
00222 void wait();
00223
00235 boolean wait(unsigned long timeout);
00236
00241 void notify();
00246 void notify_all();
00247
00251 void lock();
00252
00259 boolean trylock();
00260
00264 void unlock();
00265
00266 private:
00267 #ifdef POSIX_THREADS
00268 int cond_timed_wait(const timespec*);
00269 pthread_cond_t cond;
00270 pthread_mutex_t monitor;
00271 #else
00272 #ifdef WIN32
00273 char numNotifies;
00274 HANDLE semEvent;
00275 HANDLE semMutex;
00276 boolean isLocked;
00277 #endif
00278 #endif
00279
00280 };
00281
00292 class AGENTPP_DECL Lock {
00293 public:
00301 Lock (Synchronized &s):sync(s) { sync.lock(); }
00302
00307 ~Lock() { sync.unlock(); }
00308
00309
00319 void wait (long timeout)
00320 { if (timeout<0) sync.wait(); else sync.wait(timeout);}
00321
00326 void notify () { sync.notify (); }
00327
00328 private:
00329 Synchronized &sync;
00330 };
00331
00332 class AGENTPP_DECL ThreadList;
00333
00350 class AGENTPP_DECL Thread : public Synchronized, public Runnable {
00351
00352 enum ThreadStatus { IDLE, RUNNING, FINISHED };
00353
00354 friend class Synchronized;
00355 #ifdef WIN32
00356 friend DWORD thread_starter(LPDWORD lpdwParam);
00357 #else
00358 friend void* thread_starter(void*);
00359 #endif
00360 public:
00364 Thread();
00365
00372 Thread(Runnable &r);
00373
00378 virtual ~Thread();
00379
00387 static void sleep(long millis);
00388
00399 static void sleep(long millis, int nanos);
00400
00408 virtual void run();
00409
00418 Runnable& get_runnable();
00419
00423 void join();
00424
00429 void start();
00430
00438 void set_stack_size(long s) { stackSize = s; }
00439
00446 boolean is_alive() { return (status == RUNNING); }
00447
00452 Thread* clone() { return new Thread(get_runnable()); }
00453
00454 private:
00455 Runnable* runnable;
00456 ThreadStatus status;
00457 long stackSize;
00458 #ifdef POSIX_THREADS
00459 pthread_t tid;
00460 #else
00461 #if WIN32
00462 HANDLE threadHandle;
00463 DWORD tid;
00464 HANDLE threadEndEvent;
00465 #endif
00466 #endif
00467 static ThreadList threadList;
00468 static void nsleep(int secs, long nanos);
00469 };
00470
00471
00479 #if !defined (AGENTPP_DECL_TEMPL_ARRAY_THREAD)
00480 #define AGENTPP_DECL_TEMPL_ARRAY_THREAD
00481 AGENTPP_DECL_TEMPL template class AGENTPP_DECL Array<Thread>;
00482 #endif
00483
00484 class AGENTPP_DECL ThreadList : public Synchronized {
00485 public:
00486 ThreadList() { }
00487 ~ThreadList() { list.clear(); }
00488
00489 void add(Thread* t) { lock(); list.add(t); unlock(); }
00490 void remove(Thread* t) { lock(); list.remove(t); unlock(); }
00491 int size() const { return list.size(); }
00492 Thread* last() { lock(); Thread* t = list.last(); unlock(); return t; }
00493
00494 protected:
00495 Array<Thread> list;
00496 };
00497
00498
00499 class TaskManager;
00500
00501 #ifdef AGENTPP_USE_THREAD_POOL
00502
00503 #if !defined (AGENTPP_DECL_TEMPL_ARRAY_TASKMANAGER)
00504 #define AGENTPP_DECL_TEMPL_ARRAY_TASKMANAGER
00505 AGENTPP_DECL_TEMPL template class AGENTPP_DECL Array<TaskManager>;
00506 #endif
00507
00515 class AGENTPP_DECL ThreadPool : public Synchronized {
00516
00517 protected:
00518 Array<TaskManager> taskList;
00519 int stackSize;
00520 public:
00528 ThreadPool(int size = 4);
00529
00530
00541 ThreadPool(int size, int stackSize);
00542
00546 virtual ~ThreadPool();
00547
00552 virtual void execute(Runnable*);
00553
00561 boolean is_idle();
00562
00568 unsigned int size() { return taskList.size(); }
00569
00576 int stack_size() { return stackSize; }
00577
00581 virtual void idle_notification() { lock(); notify(); unlock(); }
00582 };
00583
00584
00585
00586 #if !defined (AGENTPP_DECL_TEMPL_LIST_RUNNABLE)
00587 #define AGENTPP_DECL_TEMPL_LIST_RUNNABLE
00588 AGENTPP_DECL_TEMPL template class AGENTPP_DECL List<Runnable>;
00589 #endif
00590
00604 class AGENTPP_DECL QueuedThreadPool : public ThreadPool, public Thread {
00605
00606 List<Runnable> queue;
00607 boolean go;
00608
00609 public:
00617 QueuedThreadPool(int size = 4);
00618
00619
00630 QueuedThreadPool(int size, int stackSize);
00631
00635 virtual ~QueuedThreadPool();
00636
00641 void execute(Runnable*);
00642
00649 unsigned int queue_length() { return queue.size(); }
00650
00651
00655 void run();
00656
00660 void stop() { go = FALSE; }
00661
00665 virtual void idle_notification();
00666
00667 private:
00668 void assign(Runnable* task);
00669 };
00670
00671
00679 class AGENTPP_DECL TaskManager : public Synchronized, public Runnable {
00680 public:
00690 TaskManager(ThreadPool*,
00691 int stackSize = AGENTPP_DEFAULT_STACKSIZE);
00692
00696 virtual ~TaskManager();
00697
00698
00706 boolean is_idle() { return (!task); }
00707
00711 void start() { thread.start(); }
00712
00716 void stop() { go = FALSE; }
00717
00729 boolean set_task(Runnable*);
00730
00734 TaskManager* clone()
00735 { return new TaskManager(
00736 new ThreadPool(threadPool->size(), threadPool->stack_size()));}
00737
00738 protected:
00739 Thread thread;
00740 ThreadPool* threadPool;
00741 Runnable* task;
00742 void run();
00743 boolean go;
00744 };
00745
00746 #endif // AGENTPP_USE_THREAD_POOL
00747
00755 class AGENTPP_DECL MibTask: public Runnable {
00756 public:
00757 MibTask(MibMethodCall* call) { task = call; }
00758 virtual ~MibTask() { delete task; }
00759
00760 virtual void run();
00761 protected:
00762 MibMethodCall* task;
00763 };
00764
00765 #ifdef NO_FAST_MUTEXES
00766
00775 class AGENTPP_DECL LockRequest: public Synchronized {
00776 public:
00784 LockRequest(Synchronized*);
00785 ~LockRequest();
00786
00787 Synchronized* target;
00788 };
00789
00801 #if !defined (AGENTPP_DECL_TEMPL_LIST_LOCKREQUEST)
00802 #define AGENTPP_DECL_TEMPL_LIST_LOCKREQUEST
00803 AGENTPP_DECL_TEMPL template class AGENTPP_DECL List<LockRequest>;
00804 #endif
00805
00806 class AGENTPP_DECL LockQueue: public Thread {
00807 public:
00808 LockQueue();
00809 virtual ~LockQueue();
00810 virtual void run();
00811
00820 void acquire(LockRequest*);
00821
00830 void release(LockRequest*);
00831
00832 protected:
00833 List<LockRequest> pendingLock;
00834 List<LockRequest> pendingRelease;
00835 boolean go;
00836 };
00837 #endif
00838
00839 #endif
00840
00849 #ifdef _THREADS
00850 class AGENTPP_DECL ThreadManager: public Synchronized {
00851 #else
00852 class AGENTPP_DECL ThreadManager {
00853 #endif
00854
00855 public:
00856
00860 ThreadManager();
00861
00865 virtual ~ThreadManager();
00866
00870 void start_synch();
00874 void end_synch();
00875
00879 static void start_global_synch();
00883 static void end_global_synch();
00884
00885 private:
00886 #ifdef _THREADS
00887 static Synchronized global_lock;
00888 #endif
00889 };
00890
00891
00892 class AGENTPP_DECL ThreadSynchronize {
00893 public:
00894 ThreadSynchronize(ThreadManager&);
00895 ~ThreadSynchronize();
00896 protected:
00897 ThreadManager& s;
00898 };
00899
00900 class AGENTPP_DECL SingleThreadObject: public ThreadManager
00901 {
00902 public:
00903 SingleThreadObject();
00904 virtual ~SingleThreadObject();
00905 };
00906
00907
00908 #ifdef AGENTPP_NAMESPACE
00909 }
00910 #endif
00911
00912 #endif