1 // Package : omnithread
2 // omnithread/solaris.cc Created : 7/94 tjr
4 // Copyright (C) 1994-1999 AT&T Laboratories Cambridge
6 // This file is part of the omnithread library
8 // The omnithread library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free
20 // Software Foundation, Inc., 51 Franklin Street, Boston, MA
24 // Implementation of OMNI thread abstraction for solaris threads.
29 #include <gnuradio/omnithread.h>
32 // #include <iostream> or #include <iostream.h> if DB is on.
34 #define THROW_ERRORS(x) { int rc = (x); \
35 if (rc != 0) throw omni_thread_fatal(rc); }
39 ///////////////////////////////////////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////////////
46 omni_mutex::omni_mutex(void)
48 THROW_ERRORS(mutex_init(&sol_mutex, USYNC_THREAD, 0));
51 omni_mutex::~omni_mutex(void)
53 THROW_ERRORS(mutex_destroy(&sol_mutex));
57 omni_mutex::lock(void)
59 THROW_ERRORS(mutex_lock(&sol_mutex));
63 omni_mutex::unlock(void)
65 THROW_ERRORS(mutex_unlock(&sol_mutex));
70 ///////////////////////////////////////////////////////////////////////////
74 ///////////////////////////////////////////////////////////////////////////
77 omni_condition::omni_condition(omni_mutex* m) : mutex(m)
79 THROW_ERRORS(cond_init(&sol_cond, USYNC_THREAD, 0));
82 omni_condition::~omni_condition(void)
84 THROW_ERRORS(cond_destroy(&sol_cond));
88 omni_condition::wait(void)
90 THROW_ERRORS(cond_wait(&sol_cond, &mutex->sol_mutex));
94 omni_condition::timedwait(unsigned long secs, unsigned long nanosecs)
96 timespec rqts = { secs, nanosecs };
99 int rc = cond_timedwait(&sol_cond, &mutex->sol_mutex, &rqts);
110 throw omni_thread_fatal(rc);
114 omni_condition::signal(void)
116 THROW_ERRORS(cond_signal(&sol_cond));
120 omni_condition::broadcast(void)
122 THROW_ERRORS(cond_broadcast(&sol_cond));
127 ///////////////////////////////////////////////////////////////////////////
129 // Counting semaphore
131 ///////////////////////////////////////////////////////////////////////////
134 omni_semaphore::omni_semaphore(unsigned int initial)
136 THROW_ERRORS(sema_init(&sol_sem, initial, USYNC_THREAD, NULL));
139 omni_semaphore::~omni_semaphore(void)
141 THROW_ERRORS(sema_destroy(&sol_sem));
145 omni_semaphore::wait(void)
147 THROW_ERRORS(sema_wait(&sol_sem));
151 omni_semaphore::post(void)
153 THROW_ERRORS(sema_post(&sol_sem));
158 ///////////////////////////////////////////////////////////////////////////
162 ///////////////////////////////////////////////////////////////////////////
169 int omni_thread::init_t::count = 0;
171 omni_mutex* omni_thread::next_id_mutex;
172 int omni_thread::next_id = 0;
174 static thread_key_t self_key;
176 static size_t stack_size = 0;
179 // Initialisation function (gets called before any user code).
182 omni_thread::init_t::init_t(void)
184 if (count++ != 0) // only do it once however many objects get created.
187 DB(cerr << "omni_thread::init: solaris implementation initialising\n");
189 THROW_ERRORS(thr_keycreate(&self_key, NULL));
191 next_id_mutex = new omni_mutex;
194 // Create object for this (i.e. initial) thread.
197 omni_thread* t = new omni_thread;
199 t->_state = STATE_RUNNING;
201 t->sol_thread = thr_self();
203 DB(cerr << "initial thread " << t->id() << " sol_thread " << t->sol_thread
206 THROW_ERRORS(thr_setspecific(self_key, (void*)t));
208 THROW_ERRORS(thr_setprio(t->sol_thread, sol_priority(PRIORITY_NORMAL)));
213 // Wrapper for thread creation.
217 omni_thread_wrapper(void* ptr)
219 omni_thread* me = (omni_thread*)ptr;
221 DB(cerr << "omni_thread::wrapper: thread " << me->id()
224 THROW_ERRORS(thr_setspecific(self_key, me));
227 // Now invoke the thread function with the given argument.
230 if (me->fn_void != NULL) {
231 (*me->fn_void)(me->thread_arg);
235 if (me->fn_ret != NULL) {
236 void* return_value = (*me->fn_ret)(me->thread_arg);
237 omni_thread::exit(return_value);
241 me->run(me->thread_arg);
244 void* return_value = me->run_undetached(me->thread_arg);
245 omni_thread::exit(return_value);
248 // should never get here.
255 // Constructors for omni_thread - set up the thread object but don't
259 // construct a detached thread running a given function.
261 omni_thread::omni_thread(void (*fn)(void*), void* arg, priority_t pri)
263 common_constructor(arg, pri, 1);
268 // construct an undetached thread running a given function.
270 omni_thread::omni_thread(void* (*fn)(void*), void* arg, priority_t pri)
272 common_constructor(arg, pri, 0);
277 // construct a thread which will run either run() or run_undetached().
279 omni_thread::omni_thread(void* arg, priority_t pri)
281 common_constructor(arg, pri, 1);
286 // common part of all constructors.
289 omni_thread::common_constructor(void* arg, priority_t pri, int det)
294 next_id_mutex->lock();
296 next_id_mutex->unlock();
299 detached = det; // may be altered in start_undetached()
304 // sol_thread is set up in initialisation routine or start().
309 // Destructor for omni_thread.
312 omni_thread::~omni_thread(void)
314 DB(cerr << "destructor called for thread " << id() << endl);
316 for (key_t i=0; i < _value_alloc; i++) {
331 omni_thread::start(void)
336 flags |= THR_DETACHED;
338 omni_mutex_lock l(mutex);
340 if (_state != STATE_NEW)
341 throw omni_thread_invalid();
343 THROW_ERRORS(thr_create(0, stack_size, omni_thread_wrapper, (void*)this, flags,
346 _state = STATE_RUNNING;
348 THROW_ERRORS(thr_setprio(sol_thread, sol_priority(_priority)));
353 // Start a thread which will run the member function run_undetached().
357 omni_thread::start_undetached(void)
359 if ((fn_void != NULL) || (fn_ret != NULL))
360 throw omni_thread_invalid();
368 // join - simply check error conditions & call thr_join.
372 omni_thread::join(void** status)
376 if ((_state != STATE_RUNNING) && (_state != STATE_TERMINATED)) {
378 throw omni_thread_invalid();
384 throw omni_thread_invalid();
387 throw omni_thread_invalid();
389 DB(cerr << "omni_thread::join: doing thr_join\n");
391 THROW_ERRORS(thr_join(sol_thread, (thread_t *)NULL, status));
393 DB(cerr << "omni_thread::join: thr_join succeeded\n");
400 // Change this thread's priority.
404 omni_thread::set_priority(priority_t pri)
406 omni_mutex_lock l(mutex);
408 if (_state != STATE_RUNNING)
409 throw omni_thread_invalid();
413 THROW_ERRORS(thr_setprio(sol_thread, sol_priority(pri)));
418 // create - construct a new thread object and start it running. Returns thread
419 // object if successful, null pointer if not.
425 omni_thread::create(void (*fn)(void*), void* arg, priority_t pri)
427 omni_thread* t = new omni_thread(fn, arg, pri);
434 // undetached version
437 omni_thread::create(void* (*fn)(void*), void* arg, priority_t pri)
439 omni_thread* t = new omni_thread(fn, arg, pri);
448 // exit() _must_ lock the mutex even in the case of a detached thread. This is
449 // because a thread may run to completion before the thread that created it has
450 // had a chance to get out of start(). By locking the mutex we ensure that the
451 // creating thread must have reached the end of start() before we delete the
452 // thread object. Of course, once the call to start() returns, the user can
453 // still incorrectly refer to the thread object, but that's their problem.
457 omni_thread::exit(void* return_value)
459 omni_thread* me = self();
465 me->_state = STATE_TERMINATED;
469 DB(cerr << "omni_thread::exit: thread " << me->id() << " detached "
470 << me->detached << " return value " << return_value << endl);
477 DB(cerr << "omni_thread::exit: called with a non-omnithread. Exit quietly." << endl);
480 thr_exit(return_value);
485 omni_thread::self(void)
489 THROW_ERRORS(thr_getspecific(self_key, (void**)&me));
492 // This thread is not created by omni_thread::start because it
493 // doesn't has a class omni_thread instance attached to its key.
494 DB(cerr << "omni_thread::self: called with a non-ominthread. NULL is returned." << endl);
502 omni_thread::yield(void)
509 omni_thread::sleep(unsigned long secs, unsigned long nanosecs)
511 timespec rqts = { secs, nanosecs };
513 while (nanosleep(&rqts, &remain)) {
514 if (errno == EINTR) {
515 rqts.tv_sec = remain.tv_sec;
516 rqts.tv_nsec = remain.tv_nsec;
520 throw omni_thread_fatal(errno);
526 omni_thread::get_time(unsigned long* abs_sec, unsigned long* abs_nsec,
527 unsigned long rel_sec, unsigned long rel_nsec)
530 clock_gettime(CLOCK_REALTIME, &abs);
531 abs.tv_nsec += rel_nsec;
532 abs.tv_sec += rel_sec + abs.tv_nsec / 1000000000;
533 abs.tv_nsec = abs.tv_nsec % 1000000000;
534 *abs_sec = abs.tv_sec;
535 *abs_nsec = abs.tv_nsec;
540 omni_thread::sol_priority(priority_t pri)
547 case PRIORITY_NORMAL:
554 throw omni_thread_invalid();
559 omni_thread::stacksize(unsigned long sz)
565 omni_thread::stacksize()
575 #error This dummy thread code is not tested. It might work if you're lucky.
577 class omni_thread_dummy : public omni_thread {
579 inline omni_thread_dummy() : omni_thread()
582 _state = STATE_RUNNING;
583 sol_thread = thr_self();
584 THROW_ERRORS(thr_setspecific(self_key, (void*)this));
586 inline ~omni_thread_dummy()
588 THROW_ERRORS(thr_setspecific(self_key, 0));
593 omni_thread::create_dummy()
595 if (omni_thread::self())
596 throw omni_thread_invalid();
598 return new omni_thread_dummy;
602 omni_thread::release_dummy()
604 omni_thread* self = omni_thread::self();
605 if (!self || !self->_dummy)
606 throw omni_thread_invalid();
608 omni_thread_dummy* dummy = (omni_thread_dummy*)self;
613 #define INSIDE_THREAD_IMPL_CC
614 #include "threaddata.cc"
615 #undef INSIDE_THREAD_IMPL_CC