]> git.lyx.org Git - lyx.git/blob - sigc++/thread.cc
fix thinko in 'double space suppression'
[lyx.git] / sigc++ / thread.cc
1 // -*- c++ -*-
2 /* 
3  * Copyright 1999 Karl Nelson <kenelson@ece.ucdavis.edu>
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 #define SIGC_THREAD_IMPL
20 #include <sigc++/thread.h>
21
22 #ifdef SIGC_PTHREADS
23 #ifdef SIGC_CXX_NAMESPACES
24 namespace SigC
25 {
26 namespace Threads
27 {
28 #endif
29
30 Mutex::Mutex(const MutexAttr attr)
31   {
32     pthread_mutex_init(&mutex_,attr.impl_);
33   }
34
35 // (needs work)
36 Mutex::~Mutex()
37   {destroy();}
38
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_);}
43
44
45 Condition::Condition(const CondAttr &attr)
46   {
47    pthread_cond_init(&cond_,attr.impl_);
48   }
49
50 Condition::~Condition()
51   {destroy();}
52
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,struct timespec* spec)
57   {return pthread_cond_timedwait(&cond_,m,spec);}
58 int Condition::destroy()      {return pthread_cond_destroy(&cond_);}
59
60
61 void Semaphore::up()
62   {
63    access_.lock();
64    value_++;
65    access_.unlock();
66    sig_.signal();
67   }
68
69 void Semaphore::down()
70   {
71    access_.lock();
72    while (value_<1)
73      {sig_.wait(access_);}
74    value_--;
75    access_.unlock();
76   }
77
78 Semaphore::Semaphore(int value):value_(value) {}
79 Semaphore::~Semaphore() {}
80
81
82
83 void* Thread::call_main_(void* obj)
84   {
85     Thread *thread=(Thread*)obj;
86     return thread->main(thread->arg_);
87   }
88
89 Thread::Thread(const ThreadAttr &attr):attr_(attr) {}
90 Thread::~Thread() {}
91
92 int Thread::detach()
93 #ifdef SIGC_PTHREAD_DCE
94   {return pthread_detach(&thread_);}
95 #else
96   {return pthread_detach(thread_);}
97 #endif
98
99 int Thread::start(void* arg)
100   {
101    arg_=arg;
102    Thread *t=this;
103    return pthread_create(&thread_,attr_.impl_,call_main_,t);
104   }
105
106 void* Private_::get()
107   {
108     void* value;
109 #ifdef SIGC_PTHREAD_DCE
110     pthread_getspecific(key_,(pthread_addr_t*)(&value));
111 #else
112     value=(void*)(pthread_getspecific(key_));
113 #endif
114     return value;
115   }
116
117 void Private_::set(void *value)
118   {
119     pthread_setspecific(key_,value);
120   }
121
122 void Private_::create(void (*dtor)(void*))
123   {
124 #ifdef SIGC_PTHREAD_DCE
125     pthread_keycreate(&key_,dtor);
126 #else
127     pthread_key_create(&key_,dtor);
128 #endif
129   }
130
131 void Private_::destroy()
132   {
133 #ifndef SIGC_PTHREAD_DCE
134     pthread_key_delete(key_);
135 #endif
136   }
137
138
139 #ifdef SIGC_PTHREAD_DCE
140 MutexAttr Mutex::Default={pthread_mutexattr_default};
141 CondAttr Condition::Default={pthread_condattr_default};
142 ThreadAttr Thread::Default={pthread_attr_default};
143 #else
144 MutexAttr Mutex::Default={0};
145 CondAttr Condition::Default={0};
146 ThreadAttr Thread::Default={0};
147 #endif
148
149
150 #ifdef SIGC_CXX_NAMESPACES
151 }
152 }
153 #endif
154
155 #endif