Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / omnithread / posix.cc
1 //                              Package : omnithread
2 // omnithread/posix.cc          Created : 7/94 tjr
3 //
4 //    Copyright (C) 2006 Free Software Foundation, Inc.
5 //    Copyright (C) 1994-1999 AT&T Laboratories Cambridge
6 //
7 //    This file is part of the omnithread library
8 //
9 //    The omnithread library is free software; you can redistribute it and/or
10 //    modify it under the terms of the GNU Library General Public
11 //    License as published by the Free Software Foundation; either
12 //    version 2 of the License, or (at your option) any later version.
13 //
14 //    This library is distributed in the hope that it will be useful,
15 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 //    Library General Public License for more details.
18 //
19 //    You should have received a copy of the GNU Library General Public
20 //    License along with this library; if not, write to the Free
21 //    Software Foundation, Inc., 51 Franklin Street, Boston, MA  
22 //    02110-1301, USA
23 //
24
25 //
26 // Implementation of OMNI thread abstraction for posix threads
27 //
28 // The source below tests for the definition of the macros:
29 //     PthreadDraftVersion
30 //     PthreadSupportThreadPriority
31 //     NoNanoSleep
32 //     NeedPthreadInit
33 //
34 // As different draft versions of the pthread standard P1003.4a/P1003.1c
35 // define slightly different APIs, the macro 'PthreadDraftVersion'
36 // identifies the draft version supported by this particular platform.
37 //
38 // Some unix variants do not support thread priority unless a real-time
39 // kernel option is installed. The macro 'PthreadSupportThreadPriority',
40 // if defined, enables the use of thread priority. If it is not defined,
41 // setting or changing thread priority will be silently ignored.
42 //
43 // nanosleep() is defined in Posix P1003.4 since Draft 9 (?).
44 // Not all platforms support this standard. The macro 'NoNanoSleep'
45 // identifies platform that don't.
46 //
47
48 #include <config.h>
49 #include <stdlib.h>
50 #include <errno.h>
51 #include <time.h>
52 #include <omnithread.h>
53
54 #ifdef HAVE_NANOSLEEP
55 #undef NoNanoSleep
56 #else
57 #define NoNanoSleep
58 #endif
59
60 #ifdef HAVE_SYS_TIME_H
61 // typedef of struct timeval and gettimeofday();
62 #include <sys/time.h>
63 #include <unistd.h>
64 #endif
65
66 #if defined(__linux__) && defined(_MIT_POSIX_THREADS)
67 #include <pthread/mit/sys/timers.h>
68 #endif
69
70 #if defined(__irix__) && defined(PthreadSupportThreadPriority)
71 #if _POSIX_THREAD_PRIORITY_SCHEDULING
72 #include <sched.h>
73 #endif
74 #endif
75
76 #define DB(x) // x
77 //#include <iostream.h> or #include <iostream> if DB is on.
78
79 #if (PthreadDraftVersion <= 6)
80 #define ERRNO(x) (((x) != 0) ? (errno) : 0)
81 #ifdef __VMS
82 // pthread_setprio returns old priority on success (draft version 4:
83 // OpenVms version < 7)
84 #define THROW_ERRORS(x) { if ((x) == -1) throw omni_thread_fatal(errno); }
85 #else
86 #define THROW_ERRORS(x) { if ((x) != 0) throw omni_thread_fatal(errno); }
87 #endif
88 #else
89 #define ERRNO(x) (x)
90 #define THROW_ERRORS(x) { int rc = (x); \
91                           if (rc != 0) throw omni_thread_fatal(rc); }
92 #endif
93
94
95
96 ///////////////////////////////////////////////////////////////////////////
97 //
98 // Mutex
99 //
100 ///////////////////////////////////////////////////////////////////////////
101
102
103 omni_mutex::omni_mutex(void)
104 {
105 #if (PthreadDraftVersion == 4)
106     THROW_ERRORS(pthread_mutex_init(&posix_mutex, pthread_mutexattr_default));
107 #else
108     THROW_ERRORS(pthread_mutex_init(&posix_mutex, 0));
109 #endif
110 }
111
112 omni_mutex::~omni_mutex(void)
113 {
114     THROW_ERRORS(pthread_mutex_destroy(&posix_mutex));
115 }
116
117
118 ///////////////////////////////////////////////////////////////////////////
119 //
120 // Condition variable
121 //
122 ///////////////////////////////////////////////////////////////////////////
123
124
125 omni_condition::omni_condition(omni_mutex* m) : mutex(m)
126 {
127 #if (PthreadDraftVersion == 4)
128     THROW_ERRORS(pthread_cond_init(&posix_cond, pthread_condattr_default));
129 #else
130     THROW_ERRORS(pthread_cond_init(&posix_cond, 0));
131 #endif
132 }
133
134 omni_condition::~omni_condition(void)
135 {
136     THROW_ERRORS(pthread_cond_destroy(&posix_cond));
137 }
138
139 void
140 omni_condition::wait(void)
141 {
142     THROW_ERRORS(pthread_cond_wait(&posix_cond, &mutex->posix_mutex));
143 }
144
145 int
146 omni_condition::timedwait(unsigned long secs, unsigned long nanosecs)
147 {
148     timespec rqts = { secs, nanosecs };
149
150 again:
151     int rc = ERRNO(pthread_cond_timedwait(&posix_cond,
152                                           &mutex->posix_mutex, &rqts));
153     if (rc == 0)
154         return 1;
155
156 #if (PthreadDraftVersion <= 6)
157     if (rc == EAGAIN)
158         return 0;
159 #endif
160
161     // Some versions of unix produces this errno when the wait was
162     // interrupted by a unix signal or fork.
163     // Some versions of the glibc 2.0.x produces this errno when the 
164     // program is debugged under gdb. Straightly speaking this is non-posix
165     // compliant. We catch this here to make debugging possible.
166     if (rc == EINTR)
167       goto again;
168
169     if (rc == ETIMEDOUT)
170         return 0;
171
172     throw omni_thread_fatal(rc);
173 #ifdef _MSC_VER
174     return 0;
175 #endif
176 }
177
178 void
179 omni_condition::signal(void)
180 {
181     THROW_ERRORS(pthread_cond_signal(&posix_cond));
182 }
183
184 void
185 omni_condition::broadcast(void)
186 {
187     THROW_ERRORS(pthread_cond_broadcast(&posix_cond));
188 }
189
190
191
192 ///////////////////////////////////////////////////////////////////////////
193 //
194 // Counting (or binary) semaphore
195 //
196 ///////////////////////////////////////////////////////////////////////////
197
198
199 omni_semaphore::omni_semaphore(unsigned int initial, unsigned int _max_count) : c(&m)
200 {
201     value = initial;
202     max_count = _max_count;
203     if (value < 0 || max_count < 1)
204       throw omni_thread_fatal(0);
205 }
206
207 omni_semaphore::~omni_semaphore(void)
208 {
209 }
210
211 void
212 omni_semaphore::wait(void)
213 {
214     omni_mutex_lock l(m);
215
216     while (value == 0)
217         c.wait();
218
219     value--;
220 }
221
222 int
223 omni_semaphore::trywait(void)
224 {
225     omni_mutex_lock l(m);
226
227     if (value == 0)
228         return 0;
229
230     value--;
231     return 1;
232 }
233
234 void
235 omni_semaphore::post(void)
236 {
237     {
238         omni_mutex_lock l(m);
239         if (value < max_count)
240           value++;
241     }
242
243     c.signal();
244 }
245
246
247
248 ///////////////////////////////////////////////////////////////////////////
249 //
250 // Thread
251 //
252 ///////////////////////////////////////////////////////////////////////////
253
254
255 //
256 // static variables
257 //
258
259 omni_mutex* omni_thread::next_id_mutex;
260 int omni_thread::next_id = 0;
261
262 static pthread_key_t self_key;
263
264 #ifdef PthreadSupportThreadPriority
265 static int lowest_priority;
266 static int normal_priority;
267 static int highest_priority;
268 #endif
269
270 #if defined(__osf1__) && defined(__alpha__) || defined(__VMS)
271 // omniORB requires a larger stack size than the default (21120) on OSF/1
272 static size_t stack_size = 32768;
273 #elif defined(__rtems__)
274 static size_t stack_size = ThreadStackSize;
275 #elif defined(__aix__)
276 static size_t stack_size = 262144;
277 #else
278 static size_t stack_size = 0;
279 #endif
280
281 //
282 // Initialisation function (gets called before any user code).
283 //
284
285 static int& count() {
286   static int the_count = 0;
287   return the_count;
288 }
289
290 omni_thread::init_t::init_t(void)
291 {
292     if (count()++ != 0) // only do it once however many objects get created.
293         return;
294
295     DB(cerr << "omni_thread::init: posix 1003.4a/1003.1c (draft "
296        << PthreadDraftVersion << ") implementation initialising\n");
297
298 #ifdef NeedPthreadInit
299
300     pthread_init();
301
302 #endif
303
304 #if (PthreadDraftVersion == 4)
305     THROW_ERRORS(pthread_keycreate(&self_key, NULL));
306 #else
307     THROW_ERRORS(pthread_key_create(&self_key, NULL));
308 #endif
309
310 #ifdef PthreadSupportThreadPriority
311
312 #if defined(__osf1__) && defined(__alpha__) || defined(__VMS)
313
314     lowest_priority = PRI_OTHER_MIN;
315     highest_priority = PRI_OTHER_MAX;
316
317 #elif defined(__hpux__)
318
319     lowest_priority = PRI_OTHER_MIN;
320     highest_priority = PRI_OTHER_MAX;
321
322 #elif defined(__sunos__) && (__OSVERSION__ == 5)
323
324     // a bug in pthread_attr_setschedparam means lowest priority is 1 not 0
325
326     lowest_priority  = 1;
327     highest_priority = 3;
328
329 #else
330
331     lowest_priority = sched_get_priority_min(SCHED_FIFO);
332     highest_priority = sched_get_priority_max(SCHED_FIFO);
333
334 #endif
335
336     switch (highest_priority - lowest_priority) {
337
338     case 0:
339     case 1:
340         normal_priority = lowest_priority;
341         break;
342
343     default:
344         normal_priority = lowest_priority + 1;
345         break;
346     }
347
348 #endif   /* PthreadSupportThreadPriority */
349
350     next_id_mutex = new omni_mutex;
351
352     //
353     // Create object for this (i.e. initial) thread.
354     //
355
356     omni_thread* t = new omni_thread;
357
358     t->_state = STATE_RUNNING;
359
360     t->posix_thread = pthread_self ();
361
362     DB(cerr << "initial thread " << t->id() << endl);
363
364     THROW_ERRORS(pthread_setspecific(self_key, (void*)t));
365
366 #ifdef PthreadSupportThreadPriority
367
368 #if (PthreadDraftVersion == 4)
369
370     THROW_ERRORS(pthread_setprio(t->posix_thread,
371                                  posix_priority(PRIORITY_NORMAL)));
372
373 #elif (PthreadDraftVersion == 6)
374
375     pthread_attr_t attr;
376     pthread_attr_init(&attr);
377
378     THROW_ERRORS(pthread_attr_setprio(&attr, posix_priority(PRIORITY_NORMAL)));
379
380     THROW_ERRORS(pthread_setschedattr(t->posix_thread, attr));
381
382 #else
383
384     struct sched_param sparam;
385
386     sparam.sched_priority = posix_priority(PRIORITY_NORMAL);
387
388     THROW_ERRORS(pthread_setschedparam(t->posix_thread, SCHED_OTHER, &sparam));
389
390 #endif   /* PthreadDraftVersion */
391
392 #endif   /* PthreadSupportThreadPriority */
393 }
394
395 omni_thread::init_t::~init_t(void)
396 {
397     if (--count() != 0) return;
398
399     omni_thread* self = omni_thread::self();
400     if (!self) return;
401
402     pthread_setspecific(self_key, 0);
403     delete self;
404
405     delete next_id_mutex;
406 }
407
408 //
409 // Wrapper for thread creation.
410 //
411
412 extern "C" void* 
413 omni_thread_wrapper(void* ptr)
414 {
415     omni_thread* me = (omni_thread*)ptr;
416
417     DB(cerr << "omni_thread_wrapper: thread " << me->id()
418        << " started\n");
419
420     THROW_ERRORS(pthread_setspecific(self_key, me));
421
422     //
423     // Now invoke the thread function with the given argument.
424     //
425
426     if (me->fn_void != NULL) {
427         (*me->fn_void)(me->thread_arg);
428         omni_thread::exit();
429     }
430
431     if (me->fn_ret != NULL) {
432         void* return_value = (*me->fn_ret)(me->thread_arg);
433         omni_thread::exit(return_value);
434     }
435
436     if (me->detached) {
437         me->run(me->thread_arg);
438         omni_thread::exit();
439     } else {
440         void* return_value = me->run_undetached(me->thread_arg);
441         omni_thread::exit(return_value);
442     }
443
444     // should never get here.
445
446     return NULL;
447 }
448
449
450 //
451 // Constructors for omni_thread - set up the thread object but don't
452 // start it running.
453 //
454
455 // construct a detached thread running a given function.
456
457 omni_thread::omni_thread(void (*fn)(void*), void* arg, priority_t pri)
458 {
459     common_constructor(arg, pri, 1);
460     fn_void = fn;
461     fn_ret = NULL;
462 }
463
464 // construct an undetached thread running a given function.
465
466 omni_thread::omni_thread(void* (*fn)(void*), void* arg, priority_t pri)
467 {
468     common_constructor(arg, pri, 0);
469     fn_void = NULL;
470     fn_ret = fn;
471 }
472
473 // construct a thread which will run either run() or run_undetached().
474
475 omni_thread::omni_thread(void* arg, priority_t pri)
476 {
477     common_constructor(arg, pri, 1);
478     fn_void = NULL;
479     fn_ret = NULL;
480 }
481
482 // common part of all constructors.
483
484 void
485 omni_thread::common_constructor(void* arg, priority_t pri, int det)
486 {
487     _state = STATE_NEW;
488     _priority = pri;
489
490     next_id_mutex->lock();
491     _id = next_id++;
492     next_id_mutex->unlock();
493
494     thread_arg = arg;
495     detached = det;     // may be altered in start_undetached()
496
497     _dummy       = 0;
498     _values      = 0;
499     _value_alloc = 0;
500     // posix_thread is set up in initialisation routine or start().
501 }
502
503
504 //
505 // Destructor for omni_thread.
506 //
507
508 omni_thread::~omni_thread(void)
509 {
510     DB(cerr << "destructor called for thread " << id() << endl);
511     if (_values) {
512         for (key_t i=0; i < _value_alloc; i++) {
513             if (_values[i]) {
514                 delete _values[i];
515             }
516         }
517         delete [] _values;
518     }
519 }
520
521
522 //
523 // Start the thread
524 //
525
526 void
527 omni_thread::start(void)
528 {
529     omni_mutex_lock l(mutex);
530
531     if (_state != STATE_NEW)
532         throw omni_thread_invalid();
533
534     pthread_attr_t attr;
535
536 #if (PthreadDraftVersion == 4)
537     pthread_attr_create(&attr);
538 #else
539     pthread_attr_init(&attr);
540 #endif
541
542 #if (PthreadDraftVersion == 8)
543     pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_UNDETACHED);
544 #endif
545
546 #ifdef PthreadSupportThreadPriority
547
548 #if (PthreadDraftVersion <= 6)
549
550     THROW_ERRORS(pthread_attr_setprio(&attr, posix_priority(_priority)));
551
552 #else
553
554     struct sched_param sparam;
555
556     sparam.sched_priority = posix_priority(_priority);
557
558     THROW_ERRORS(pthread_attr_setschedparam(&attr, &sparam));
559
560 #endif  /* PthreadDraftVersion */
561
562 #endif  /* PthreadSupportThreadPriority */
563
564 #if !defined(__linux__)
565     if (stack_size) {
566       THROW_ERRORS(pthread_attr_setstacksize(&attr, stack_size));
567     }
568 #endif
569
570
571 #if (PthreadDraftVersion == 4)
572     THROW_ERRORS(pthread_create(&posix_thread, attr, omni_thread_wrapper,
573                                 (void*)this));
574     pthread_attr_delete(&attr);
575 #else
576     THROW_ERRORS(pthread_create(&posix_thread, &attr, omni_thread_wrapper,
577                                 (void*)this));
578     pthread_attr_destroy(&attr);
579 #endif
580
581     _state = STATE_RUNNING;
582
583     if (detached) {
584
585 #if (PthreadDraftVersion <= 6)
586         THROW_ERRORS(pthread_detach(&posix_thread));
587 #else
588         THROW_ERRORS(pthread_detach(posix_thread));
589 #endif
590     }
591 }
592
593
594 //
595 // Start a thread which will run the member function run_undetached().
596 //
597
598 void
599 omni_thread::start_undetached(void)
600 {
601     if ((fn_void != NULL) || (fn_ret != NULL))
602         throw omni_thread_invalid();
603
604     detached = 0;
605     start();
606 }
607
608
609 //
610 // join - simply check error conditions & call pthread_join.
611 //
612
613 void
614 omni_thread::join(void** status)
615 {
616     mutex.lock();
617
618     if ((_state != STATE_RUNNING) && (_state != STATE_TERMINATED)) {
619         mutex.unlock();
620         throw omni_thread_invalid();
621     }
622
623     mutex.unlock();
624
625     if (this == self())
626         throw omni_thread_invalid();
627
628     if (detached)
629         throw omni_thread_invalid();
630
631     DB(cerr << "omni_thread::join: doing pthread_join\n");
632
633     THROW_ERRORS(pthread_join(posix_thread, status));
634
635     DB(cerr << "omni_thread::join: pthread_join succeeded\n");
636
637 #if (PthreadDraftVersion == 4)
638     // With draft 4 pthreads implementations (HPUX 10.x and
639     // Digital Unix 3.2), have to detach the thread after 
640     // join. If not, the storage for the thread will not be
641     // be reclaimed.
642     THROW_ERRORS(pthread_detach(&posix_thread));
643 #endif
644
645     delete this;
646 }
647
648
649 //
650 // Change this thread's priority.
651 //
652
653 void
654 omni_thread::set_priority(priority_t pri)
655 {
656     omni_mutex_lock l(mutex);
657
658     if (_state != STATE_RUNNING)
659         throw omni_thread_invalid();
660
661     _priority = pri;
662
663 #ifdef PthreadSupportThreadPriority
664
665 #if (PthreadDraftVersion == 4)
666
667     THROW_ERRORS(pthread_setprio(posix_thread, posix_priority(pri)));
668
669 #elif (PthreadDraftVersion == 6)
670
671     pthread_attr_t attr;
672     pthread_attr_init(&attr);
673
674     THROW_ERRORS(pthread_attr_setprio(&attr, posix_priority(pri)));
675
676     THROW_ERRORS(pthread_setschedattr(posix_thread, attr));
677
678 #else
679
680     struct sched_param sparam;
681
682     sparam.sched_priority = posix_priority(pri);
683
684     THROW_ERRORS(pthread_setschedparam(posix_thread, SCHED_OTHER, &sparam));
685
686 #endif   /* PthreadDraftVersion */
687
688 #endif   /* PthreadSupportThreadPriority */
689 }
690
691
692 //
693 // create - construct a new thread object and start it running.  Returns thread
694 // object if successful, null pointer if not.
695 //
696
697 // detached version
698
699 omni_thread*
700 omni_thread::create(void (*fn)(void*), void* arg, priority_t pri)
701 {
702     omni_thread* t = new omni_thread(fn, arg, pri);
703
704     t->start();
705
706     return t;
707 }
708
709 // undetached version
710
711 omni_thread*
712 omni_thread::create(void* (*fn)(void*), void* arg, priority_t pri)
713 {
714     omni_thread* t = new omni_thread(fn, arg, pri);
715
716     t->start();
717
718     return t;
719 }
720
721
722 //
723 // exit() _must_ lock the mutex even in the case of a detached thread.  This is
724 // because a thread may run to completion before the thread that created it has
725 // had a chance to get out of start().  By locking the mutex we ensure that the
726 // creating thread must have reached the end of start() before we delete the
727 // thread object.  Of course, once the call to start() returns, the user can
728 // still incorrectly refer to the thread object, but that's their problem.
729 //
730
731 void
732 omni_thread::exit(void* return_value)
733 {
734     omni_thread* me = self();
735
736     if (me)
737       {
738         me->mutex.lock();
739
740         me->_state = STATE_TERMINATED;
741
742         me->mutex.unlock();
743
744         DB(cerr << "omni_thread::exit: thread " << me->id() << " detached "
745            << me->detached << " return value " << return_value << endl);
746
747         if (me->detached)
748           delete me;
749       }
750     else
751       {
752         DB(cerr << "omni_thread::exit: called with a non-omnithread. Exit quietly." << endl);
753       }
754
755     pthread_exit(return_value);
756 }
757
758
759 omni_thread*
760 omni_thread::self(void)
761 {
762     omni_thread* me;
763
764 #if (PthreadDraftVersion <= 6)
765
766     THROW_ERRORS(pthread_getspecific(self_key, (void**)&me));
767
768 #else
769
770     me = (omni_thread *)pthread_getspecific(self_key);
771
772 #endif
773
774     if (!me) {
775       // This thread is not created by omni_thread::start because it
776       // doesn't has a class omni_thread instance attached to its key.
777       DB(cerr << "omni_thread::self: called with a non-omnithread. NULL is returned." << endl);
778     }
779
780     return me;
781 }
782
783
784 void
785 omni_thread::yield(void)
786 {
787 #if (PthreadDraftVersion == 6)
788
789     pthread_yield(NULL);
790
791 #elif (PthreadDraftVersion < 9)
792
793     pthread_yield();
794
795 #else
796
797     THROW_ERRORS(sched_yield());
798
799 #endif
800 }
801
802
803 void
804 omni_thread::sleep(unsigned long secs, unsigned long nanosecs)
805 {
806     timespec rqts = { secs, nanosecs };
807
808 #ifndef NoNanoSleep
809
810     timespec remain;
811     while (nanosleep(&rqts, &remain)) {
812       if (errno == EINTR) {
813         rqts.tv_sec  = remain.tv_sec;
814         rqts.tv_nsec = remain.tv_nsec;
815         continue;
816       }
817       else
818         throw omni_thread_fatal(errno);
819     }
820 #else
821
822 #if defined(__osf1__) && defined(__alpha__) || defined(__hpux__) && (__OSVERSION__ == 10) || defined(__VMS) || defined(__SINIX__) || defined (__POSIX_NT__)
823
824     if (pthread_delay_np(&rqts) != 0)
825         throw omni_thread_fatal(errno);
826
827 #elif defined(__linux__) || defined(__aix__)
828
829     if (secs > 2000) {
830       while ((secs = ::sleep(secs))) ;
831     } else {
832         usleep(secs * 1000000 + (nanosecs / 1000));
833     }
834
835 #elif defined(__darwin__) || defined(__macos__)
836
837     // Single UNIX Specification says argument of usleep() must be
838     // less than 1,000,000.
839     secs += nanosecs / 1000000000;
840     nanosecs %= 1000000000;
841     while ((secs = ::sleep(secs))) ;
842     usleep(nanosecs / 1000);
843
844 #else
845
846     throw omni_thread_invalid();
847
848 #endif
849 #endif  /* NoNanoSleep */
850 }
851
852
853 void
854 omni_thread::get_time(unsigned long* abs_sec, unsigned long* abs_nsec,
855                       unsigned long rel_sec, unsigned long rel_nsec)
856 {
857     timespec abs;
858
859 #if defined(__osf1__) && defined(__alpha__) || defined(__hpux__) && (__OSVERSION__ == 10) || defined(__VMS) || defined(__SINIX__) || defined(__POSIX_NT__)
860
861     timespec rel;
862     rel.tv_sec = rel_sec;
863     rel.tv_nsec = rel_nsec;
864     THROW_ERRORS(pthread_get_expiration_np(&rel, &abs));
865
866 #else
867
868 #ifdef HAVE_CLOCK_GETTIME       /* __linux__ || __aix__ */
869
870     clock_gettime(CLOCK_REALTIME, &abs);
871
872 #elif defined(HAVE_GETTIMEOFDAY)        /* defined(__linux__) || defined(__aix__) || defined(__SCO_VERSION__) || defined(__darwin__) || defined(__macos__) */
873
874     struct timeval tv;
875     gettimeofday(&tv, NULL); 
876     abs.tv_sec = tv.tv_sec;
877     abs.tv_nsec = tv.tv_usec * 1000;
878
879 #else
880 #error no get time support
881 #endif  /* __linux__ || __aix__ */
882
883     abs.tv_nsec += rel_nsec;
884     abs.tv_sec += rel_sec + abs.tv_nsec / 1000000000;
885     abs.tv_nsec = abs.tv_nsec % 1000000000;
886
887 #endif  /* __osf1__ && __alpha__ */
888
889     *abs_sec = abs.tv_sec;
890     *abs_nsec = abs.tv_nsec;
891 }
892
893
894 int
895 omni_thread::posix_priority(priority_t pri)
896 {
897 #ifdef PthreadSupportThreadPriority
898     switch (pri) {
899
900     case PRIORITY_LOW:
901         return lowest_priority;
902
903     case PRIORITY_NORMAL:
904         return normal_priority;
905
906     case PRIORITY_HIGH:
907         return highest_priority;
908
909     }
910 #endif
911
912     throw omni_thread_invalid();
913 #ifdef _MSC_VER
914     return 0;
915 #endif
916 }
917
918 void
919 omni_thread::stacksize(unsigned long sz)
920 {
921   stack_size = sz;
922 }
923
924 unsigned long
925 omni_thread::stacksize()
926 {
927   return stack_size;
928 }
929
930 //
931 // Dummy thread
932 //
933
934 class omni_thread_dummy : public omni_thread {
935 public:
936   inline omni_thread_dummy() : omni_thread()
937   {
938     _dummy = 1;
939     _state = STATE_RUNNING;
940     posix_thread = pthread_self();
941     THROW_ERRORS(pthread_setspecific(self_key, (void*)this));
942   }
943   inline ~omni_thread_dummy()
944   {
945     THROW_ERRORS(pthread_setspecific(self_key, 0));
946   }
947 };
948
949 omni_thread*
950 omni_thread::create_dummy()
951 {
952   if (omni_thread::self())
953     throw omni_thread_invalid();
954
955   return new omni_thread_dummy;
956 }
957
958 void
959 omni_thread::release_dummy()
960 {
961   omni_thread* self = omni_thread::self();
962   if (!self || !self->_dummy)
963     throw omni_thread_invalid();
964
965   omni_thread_dummy* dummy = (omni_thread_dummy*)self;
966   delete dummy;
967 }
968
969
970 #define INSIDE_THREAD_IMPL_CC
971 #include "threaddata.cc"
972 #undef INSIDE_THREAD_IMPL_CC