3 * Copyright 1999 Karl Nelson <kenelson@ece.ucdavis.edu>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #ifndef SIGCXX_THREAD_H
20 #define SIGCXX_THREAD_H
21 #include <sigc++/sigc++config.h>
25 #ifdef SIGC_THREAD_IMPL
31 #ifdef SIGC_CXX_NAMESPACES
40 #ifdef SIGC_THREAD_IMPL
41 #ifdef SIGC_PTHREAD_DCE
42 struct CondAttr { pthread_condattr_t impl_;};
43 struct MutexAttr { pthread_mutexattr_t impl_;};
44 struct ThreadAttr { pthread_attr_t impl_;};
46 struct CondAttr { pthread_condattr_t* impl_;};
47 struct MutexAttr { pthread_mutexattr_t* impl_;};
48 struct ThreadAttr { pthread_attr_t* impl_;};
50 typedef pthread_mutex_t MutexImpl;
51 typedef pthread_cond_t CondImpl;
52 typedef pthread_key_t KeyImpl;
53 typedef pthread_t ThreadImpl;
55 class CondAttr {unsigned char dummy[SIGC_PTHREAD_COND_ATTR];};
56 class CondImpl {unsigned char dummy[SIGC_PTHREAD_COND_IMPL];};
57 class MutexAttr {unsigned char dummy[SIGC_PTHREAD_MUTEX_ATTR];};
58 class MutexImpl {unsigned char dummy[SIGC_PTHREAD_MUTEX_IMPL];};
59 class ThreadAttr {unsigned char dummy[SIGC_PTHREAD_THREAD_ATTR];};
60 class ThreadImpl {unsigned char dummy[SIGC_PTHREAD_THREAD_IMPL];};
61 class KeyImpl {unsigned char dummy[SIGC_PTHREAD_KEY_IMPL];};
67 typedef MutexImpl Impl;
73 static MutexAttr Default;
74 #ifdef SIGC_THREAD_IMPL
75 operator Impl* () {return (Impl*)(&mutex_);}
78 Mutex(const MutexAttr attr=Default);
88 // A lazy way to unlock at end of scope
92 MLock(Mutex& mutex):mutex_(mutex) {mutex_.lock();}
93 ~MLock() {mutex_.unlock();}
99 typedef CondImpl Impl;
105 static CondAttr Default;
106 #ifdef SIGC_THREAD_IMPL
107 operator Impl* () {return (Impl*)(&cond_);}
110 Condition(const CondAttr &attr=Default);
113 // restarts exactly one thread hung on condition
116 // restarts all threads waiting on condition
119 // unlocks a mutex while waiting on a condition, then reaquires lock.
122 // unlocks a mutex while waiting on a condition, then reaquires lock
123 // with a fixed maximum duration.
124 int wait(Mutex &m,struct timespec* spec);
138 Semaphore(int value=1);
146 void set(void *value);
147 void create(void (*dtor)(void*));
151 // Private is a thread split static.
153 class Private : private Private_
156 static void dtor(void* v)
164 T& operator =(const T& t)
165 {return (((T&)*this)=t);}
171 set((void*)(value=new T()));
175 Private() { create(&dtor); }
176 ~Private() { destroy(); }
179 // int needs to initialized
181 class Private<int> : private Private_
184 static void dtor(void* v)
191 int& operator =(const int& t)
192 {return (((int&)*this)=t);}
196 int *value=(int*)get();
198 set((void*)(value=new int(0)));
202 Private() { create(&dtor); }
203 ~Private() { destroy(); }
209 typedef ThreadImpl Impl;
214 static void* call_main_(void* obj);
217 #ifdef SIGC_THREAD_IMPL
218 operator Impl* () {return &thread_;}
221 virtual void* main(void*)=0;
224 static ThreadAttr Default;
226 // arg is for passing extra data to main, but never pass a
227 // local variable or address of local variable. Arg must
228 // be available throughout life of program.
229 int start(void* arg=0);
231 Thread(const ThreadAttr &attr=Default);
236 #ifdef SIGC_CXX_NAMESPACES
237 } /* namespace Threads */
238 } /* namespace SigC */
241 #endif /* SIGC_PTHREADS */
242 #endif /* SIGCXX_THREAD_H */