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 #define SIGC_THREAD_IMPL
20 #include <sigc++/thread.h>
23 #ifdef SIGC_CXX_NAMESPACES
30 Mutex::Mutex(const MutexAttr attr)
32 pthread_mutex_init(&mutex_,attr.impl_);
39 int Mutex::lock() {return pthread_mutex_lock(&mutex_);}
40 int Mutex::trylock() {return pthread_mutex_trylock(&mutex_);}
41 int Mutex::unlock() {return pthread_mutex_unlock(&mutex_);}
42 int Mutex::destroy() {return pthread_mutex_destroy(&mutex_);}
45 Condition::Condition(const CondAttr &attr)
47 pthread_cond_init(&cond_,attr.impl_);
50 Condition::~Condition()
53 int Condition::signal() {return pthread_cond_signal(&cond_);}
54 int Condition::broadcast() {return pthread_cond_broadcast(&cond_);}
55 int Condition::wait(Mutex &m) {return pthread_cond_wait(&cond_,m);}
56 int Condition::wait(Mutex &m,timespec* spec)
57 {return pthread_cond_timedwait(&cond_,m,spec);}
58 int Condition::destroy() {return pthread_cond_destroy(&cond_);}
69 void Semaphore::down()
78 Semaphore::Semaphore(int value):value_(value) {}
79 Semaphore::~Semaphore() {}
83 void* Thread::call_main_(void* obj)
85 Thread *thread=(Thread*)obj;
86 return thread->main(thread->arg_);
89 Thread::Thread(const ThreadAttr &attr):attr_(attr) {}
93 #ifdef SIGC_PTHREAD_DCE
94 {return pthread_detach(&thread_);}
96 {return pthread_detach(thread_);}
99 int Thread::start(void* arg)
103 return pthread_create(&thread_,attr_.impl_,call_main_,t);
106 void* Private_::get()
109 #ifdef SIGC_PTHREAD_DCE
110 pthread_getspecific(key_,(pthread_addr_t*)(&value));
112 value=(void*)(pthread_getspecific(key_));
117 void Private_::set(void *value)
119 pthread_setspecific(key_,value);
122 void Private_::create(void (*dtor)(void*))
124 #ifdef SIGC_PTHREAD_DCE
125 pthread_keycreate(&key_,dtor);
127 pthread_key_create(&key_,dtor);
131 void Private_::destroy()
133 #ifndef SIGC_PTHREAD_DCE
134 pthread_key_delete(key_);
138 Private<int>::operator int&()
140 int * value = static_cast<int*>(get());
142 set(static_cast<void*>(value = new int(0)));
146 #ifdef SIGC_PTHREAD_DCE
147 MutexAttr Mutex::Default={pthread_mutexattr_default};
148 CondAttr Condition::Default={pthread_condattr_default};
149 ThreadAttr Thread::Default={pthread_attr_default};
151 MutexAttr Mutex::Default={0};
152 CondAttr Condition::Default={0};
153 ThreadAttr Thread::Default={0};
157 #ifdef SIGC_CXX_NAMESPACES