3 #ifndef __ARC_THREAD_H__ 4 #define __ARC_THREAD_H__ 8 #include <glibmm/thread.h> 15 class SimpleCondition;
29 const size_t thread_stacksize = (16 * 1024 * 1024);
45 std::map<unsigned long int, unsigned long int> thread_ids;
46 unsigned long int thread_no;
49 static ThreadId& getInstance();
55 unsigned long int get();
67 friend class ThreadData;
86 void Attach(std::string& key);
90 void Attach(
const std::string& key);
99 virtual void Dup(
void);
102 class ThreadArgument;
106 friend class ThreadArgument;
117 virtual void thread(
void) = 0;
123 AutoLock(T& olock,
bool olocked =
true) : lock_(olock), locked_(0) {
128 if(locked_ != 0) lock_.unlock();
133 if(locked_ == 0) lock_.lock();
138 if(locked_ == 1) lock_.unlock();
155 unsigned int waiting_;
158 : flag_(0), waiting_(0) {}
188 flag_ = waiting_?waiting_:1;
196 while (!flag_) cond_.wait(lock_);
205 while (!flag_) cond_.wait(lock_);
214 etime.assign_current_time();
215 etime.add_milliseconds(t);
219 res = cond_.timed_wait(lock_, etime);
254 virtual int inc(
void);
257 virtual int dec(
void);
259 virtual int get(void)
const;
262 virtual int set(
int v);
264 virtual void wait(
void)
const;
268 virtual bool wait(
int t)
const;
297 etime.assign_current_time();
298 etime.add_milliseconds(t);
300 if(!cond_.timed_wait(lock_, etime))
break;
337 unsigned int exclusive_;
338 Glib::Thread* thread_;
339 typedef std::map<Glib::Thread*,unsigned int> shared_list;
341 void add_shared_lock(
void);
342 void remove_shared_lock(
void);
343 bool have_shared_lock(
void);
344 inline bool have_exclusive_lock(
void) {
345 if(!exclusive_)
return false;
346 if(thread_ == Glib::Thread::self())
return false;
358 return (shared_.size() > 0);
366 return (exclusive_ > 0);
404 class ThreadedPointerBase {
411 ThreadedPointerBase(ThreadedPointerBase&);
412 ~ThreadedPointerBase(
void);
414 ThreadedPointerBase(
void *p);
415 ThreadedPointerBase* add(
void);
417 void* ptr(
void)
const {
return ptr_; };
418 void rel(
void) { released_ =
true; };
419 unsigned int cnt(
void)
const {
return cnt_; };
420 void lock(
void) { lock_.lock(); };
421 void unlock(
void) { lock_.unlock(); };
422 void wait(
void) { cond_.wait(lock_); };
423 bool wait(Glib::TimeVal etime) {
424 return cond_.timed_wait(lock_,etime);
438 ThreadedPointerBase *object_;
441 : object_(
new ThreadedPointerBase(p)) {}
443 : object_(p.object_->add()) {}
445 : object_(
new ThreadedPointerBase(NULL)) {}
447 delete((T*)(object_->rem()));
451 if (p != object_->ptr()) {
452 delete((T*)(object_->rem()));
453 object_ =
new ThreadedPointerBase(p);
459 if (p.object_->ptr() != object_->ptr()) {
460 delete((T*)(object_->rem()));
461 object_ = p.object_->add();
467 return *(T*)(object_->ptr());
471 return (T*)(object_->ptr());
474 operator bool(
void)
const {
475 return ((object_->ptr()) != NULL);
479 return ((object_->ptr()) == NULL);
483 return ((T*)(object_->ptr()) == (T*)(p.object_->ptr()));
487 return ((T*)(object_->ptr()) != (T*)(p.object_->ptr()));
491 return ((T*)(object_->ptr()) < (T*)(p.object_->ptr()));
495 return (T*)(object_->ptr());
501 T* tmp = (T*)(object_->ptr());
507 return object_->cnt();
516 if(r <= minThr)
break;
517 if(r >= maxThr)
break;
526 unsigned int WaitOutRange(
unsigned int minThr,
unsigned int maxThr,
int timeout) {
531 etime.assign_current_time();
532 etime.add_milliseconds(timeout);
535 if(r <= minThr)
break;
536 if(r >= maxThr)
break;
537 if(!object_->wait(etime))
break;
544 unsigned int WaitInRange(
unsigned int minThr,
unsigned int maxThr) {
549 if((r >= minThr) && (r <= maxThr))
break;
558 unsigned int WaitInRange(
unsigned int minThr,
unsigned int maxThr,
int timeout) {
559 if(timeout < 0)
return WaitInRange(minThr, maxThr);
563 etime.assign_current_time();
564 etime.add_milliseconds(timeout);
567 if((r >= minThr) && (r <= maxThr))
break;
568 if(!object_->wait(etime))
break;
611 void GlibThreadInitialize(
void);
614 class ThreadInitializer {
619 GlibThreadInitialize();
638 static ThreadInitializer _local_thread_initializer;
bool operator!=(const ThreadedPointer &p) const
Returns true if pointers are not equal.
Definition: Thread.h:486
Arc namespace contains all core ARC classes.
Definition: ArcConfig.h:11
void lock(void)
Acquire semaphor.
Definition: Thread.h:164
void Attach(std::string &key)
Attaches object to current thread under key.
bool wait(int t)
Wait for condition no longer than t milliseconds.
Definition: Thread.h:211
Simple triggered condition.
Definition: Thread.h:150
void unlockShared(void)
Release a shared lock.
void forceReset(void)
This method is meant to be used only after fork.
Definition: Thread.h:603
bool operator!(void) const
Returns true if pointer is NULL and false otherwise.
Definition: Thread.h:478
Base class for per-thread object.
Definition: Thread.h:66
void broadcast(void)
Signal about condition to all waiting threads.
Definition: Thread.h:186
Exclusive lock for SharedMutex.
Definition: Thread.h:390
Thread-safe counter with capability to wait for zero value.
Definition: Thread.h:244
bool unlock(void)
Release mutex.
Definition: Thread.h:313
bool isLockExclusive(void)
Returns true if the exclusive lock is held.
Definition: Thread.h:365
T * Release(void)
Release referred object so that it can be passed to other container.
Definition: Thread.h:500
virtual void wait(void) const
Wait for zero condition.
void signal_nonblock(void)
Signal about condition without using semaphor.
Definition: Thread.h:180
virtual void thread(void)=0
Implement this method and put thread functionality into it.
void unlockExclusive(void)
Release exclusive lock.
void unlock(void)
Release semaphor.
Definition: Thread.h:168
void UnregisterThread(void)
Report thread as exited.
void RegisterThread(void)
Register thread as started/starting into this instance.
bool start(SimpleCounter *count=NULL)
Start thread.
void wait_nonblock(void)
Wait for condition without using semaphor.
Definition: Thread.h:203
void forceReset(void)
This method is meant to be used only after fork.
ThreadDataItem(void)
Dummy constructor which does nothing.
Mutex which allows shared and exclusive locking.
Definition: Thread.h:333
bool lock(int t=-1)
Lock mutex, but wait no longer than t milliseconds.
Definition: Thread.h:289
T * operator->(void) const
For referring to wrapped object.
Definition: Thread.h:470
unsigned int Holders(void)
Returns number of ThreadedPointer instances referring to underlying object.
Definition: Thread.h:506
bool CreateThreadFunction(void(*func)(void *), void *arg, SimpleCounter *count=NULL)
Helper function to create simple thread.
void reset(void)
Reset object to initial state.
Definition: Thread.h:228
T & operator*(void) const
For referring to wrapped object.
Definition: Thread.h:466
bool operator==(const ThreadedPointer &p) const
Returns true if pointers are equal.
Definition: Thread.h:482
bool isLockShared(void)
Returns true if at least one shared lock is held.
Definition: Thread.h:357
Base class for simple object associated thread.
Definition: Thread.h:105
unsigned int WaitInRange(unsigned int minThr, unsigned int maxThr, int timeout)
Waits till number of ThreadedPointer instances >= minThr and <= maxThr.
Definition: Thread.h:558
void signal(void)
Definition: Thread.h:172
unsigned int WaitOutRange(unsigned int minThr, unsigned int maxThr, int timeout)
Waits till number of ThreadedPointer instances <= minThr or >= maxThr.
Definition: Thread.h:526
bool WaitOrCancel(int timeout)
Wait for timeout milliseconds or cancel request.
unsigned int WaitInRange(unsigned int minThr, unsigned int maxThr)
Waits till number of ThreadedPointer instances >= minThr and <= maxThr.
Definition: Thread.h:544
virtual void forceReset(void)
This method is meant to be used only after fork.
Definition: Thread.h:271
A set of conditions, mutexes, etc. conveniently exposed to monitor running child threads and to wait ...
Definition: Thread.h:579
T * Ptr(void) const
Cast to original pointer.
Definition: Thread.h:494
unsigned int WaitOutRange(unsigned int minThr, unsigned int maxThr)
Waits till number of ThreadedPointer instances <= minThr or >= maxThr.
Definition: Thread.h:511
void forceReset(void)
This method is meant to be used only after fork.
Definition: Thread.h:235
virtual int inc(void)
Increment value of counter.
ThreadedPointer< T > & operator=(const ThreadedPointer< T > &p)
Assign a new ThreadedPointer from another ThreadedPointer.
Definition: Thread.h:458
virtual int dec(void)
Decrement value of counter.
void forceReset(void)
This method is meant to be used only after fork.
Definition: Thread.h:370
bool WaitForExit(int timeout=-1)
Wait for registered threads to exit.
ThreadedPointer< T > & operator=(T *p)
Assign a new ThreadedPointer from a pointer to an object.
Definition: Thread.h:450
void wait(void)
Wait for condition.
Definition: Thread.h:193
ThreadInitializer(void)
Initialise the thread system.
Definition: Thread.h:618
void lockExclusive(void)
Acquire an exclusive lock. Blocks until all shared and exclusive locks are released.
Mutex which allows a timeout on locking.
Definition: Thread.h:279
virtual void Dup(void)
Creates copy of object.
void forceReset(void)
This method is meant to be used only after fork.
Definition: Thread.h:325
Shared lock for SharedMutex.
Definition: Thread.h:377
void RequestCancel(void)
Send cancel request to registered threads.
void waitExit(void)
Wait for all known threads to exit.
bool operator<(const ThreadedPointer &p) const
Comparison operator.
Definition: Thread.h:490
bool trylock(void)
Returns true if mutex is currently locked, but does not attempt to acquire lock.
Definition: Thread.h:309
static ThreadDataItem * Get(const std::string &key)
Retrieves object attached to thread under key.
Wrapper for pointer with automatic destruction and multiple references.
Definition: Thread.h:436
void lockShared(void)
Acquire a shared lock. Blocks until exclusive lock is released.