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 #ifndef _thread
00027 #define _thread
00028
00029
00030
00031
00032
00033 namespace Basics
00034 {
00035 class thread;
00036
00037
00038
00039
00040
00041 class mutex {
00042 friend class cond;
00043 public:
00044
00045 mutex() throw ();
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 mutex(int type) throw();
00057
00058 ~mutex() throw ();
00059
00060 void lock() throw ();
00061 void unlock() throw ();
00062
00063 private:
00064 pthread_mutex_t m;
00065
00066
00067 char pad[64];
00068 };
00069
00070
00071
00072
00073
00074
00075
00076
00077 #ifdef signal
00078 #undef signal
00079 #endif
00080
00081 class cond {
00082 public:
00083 cond() throw ();
00084 ~cond() throw ();
00085
00086 void wait(mutex &m) throw ();
00087 int timedwait(mutex &m, struct timespec *abstime) throw ();
00088
00089
00090
00091
00092 void signal() throw ();
00093 void broadcast() throw ();
00094
00095 private:
00096 pthread_cond_t c;
00097
00098
00099 char pad[64];
00100 };
00101
00102 class thread_attr
00103 {
00104 public:
00105 thread_attr() throw ();
00106
00107
00108 thread_attr(size_t stacksize) throw ();
00109 thread_attr(const thread_attr &other) throw();
00110 ~thread_attr() throw ();
00111
00112 void set_guardsize(size_t guardsize) throw();
00113 size_t get_guardsize() const throw();
00114 void set_stacksize(size_t stacksize) throw();
00115 size_t get_stacksize() const throw();
00116
00117 #if defined (_POSIX_THREAD_PRIORITY_SCHEDULING)
00118 void set_schedpolicy(int policy) throw();
00119 int get_schedpolicy() const throw();
00120 void set_inheritsched(int inheritsched) throw();
00121 int get_inheritsched() const throw();
00122 void set_sched_priority(int sched_priority) throw();
00123 int get_sched_priority() const throw();
00124 #endif
00125
00126 thread_attr &operator=(const thread_attr &other) throw();
00127
00128 private:
00129 pthread_attr_t attr;
00130
00131
00132
00133 friend class thread;
00134 };
00135
00136
00137
00138
00139
00140 class thread {
00141 public:
00142 thread() throw () { }
00143 thread(const thread &other) throw() : t(other.t) { }
00144 ~thread() throw () { }
00145
00146 typedef void* (*StartRoutine)(void *arg) ;
00147
00148
00149
00150 void fork(StartRoutine proc, void *arg, const thread_attr &attr) throw ();
00151 void fork(StartRoutine proc, void *arg) throw ()
00152 {
00153 thread_attr attr;
00154 fork(proc, arg, attr);
00155 }
00156 void fork(StartRoutine proc, void *arg, long stacksize) throw ()
00157 {
00158 thread_attr attr;
00159 if(stacksize > 0)
00160 attr.set_stacksize(stacksize);
00161 fork(proc, arg, attr);
00162 }
00163
00164
00165
00166 void detach() throw ();
00167
00168 void fork_and_detach(StartRoutine proc, void *arg, const thread_attr &attr)
00169 throw ();
00170 void fork_and_detach(StartRoutine proc, void *arg) throw ()
00171 {
00172 thread_attr attr;
00173 fork_and_detach(proc, arg, attr);
00174 }
00175 void fork_and_detach(StartRoutine proc, void *arg, long stacksize) throw ()
00176 {
00177 thread_attr attr;
00178 if(stacksize > 0)
00179 attr.set_stacksize(stacksize);
00180 fork_and_detach(proc, arg, attr);
00181 }
00182
00183
00184
00185 void *join() throw ();
00186
00187 thread &operator=(const thread &other) throw()
00188 {
00189 t = other.t;
00190 return *this;
00191 }
00192
00193 void get_sched_param( int &policy,
00194 int &sched_priority) throw();
00195 void set_sched_param(int policy, int sched_priority) throw();
00196
00197 static void pause(int secs, int usecs = 0) throw ();
00198
00199
00200
00201 static thread self() throw();
00202
00203
00204 private:
00205 thread(pthread_t existing) throw () : t(existing) { }
00206 pthread_t t;
00207 void fork_inner(StartRoutine proc, void *arg, const thread_attr &attr)
00208 throw ();
00209 };
00210 }
00211
00212 #endif