1 // Package : omnithread
2 // omnithread/mach.cc Created : 7/97 lars immisch lars@ibp.de
4 // Copyright (C) 1997 Immisch, Becker & Partner
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
25 // Implementation of OMNI thread abstraction for mach threads
27 // to the author's pleasure, mach cthreads are very similar to posix threads
33 #include <mach/cthreads.h>
34 #include "gnuradio/omnithread.h"
37 // #include <iostream> or #include <iostream.h> if DB is on.
45 int omni_thread::init_t::count = 0;
47 omni_mutex* omni_thread::next_id_mutex;
48 int omni_thread::next_id = 0;
50 static int normal_priority;
51 static int highest_priority;
53 static size_t stack_size = 0;
55 ///////////////////////////////////////////////////////////////////////////
59 ///////////////////////////////////////////////////////////////////////////
62 omni_mutex::omni_mutex(void)
64 mutex_init(&mach_mutex);
68 omni_mutex::~omni_mutex(void)
70 mutex_clear(&mach_mutex);
74 void omni_mutex::lock(void)
76 mutex_lock(&mach_mutex);
80 void omni_mutex::unlock(void)
82 mutex_unlock(&mach_mutex);
87 ///////////////////////////////////////////////////////////////////////////
91 ///////////////////////////////////////////////////////////////////////////
94 omni_condition::omni_condition(omni_mutex* m) : mutex(m)
96 condition_init(&mach_cond);
100 omni_condition::~omni_condition(void)
102 condition_clear(&mach_cond);
106 omni_condition::wait(void)
108 condition_wait(&mach_cond, &mutex->mach_mutex);
111 typedef struct alarmclock_args {
115 condition_t condition;
119 any_t alarmclock(any_t arg)
121 alarmclock_args* alarm = (alarmclock_args*)arg;
123 omni_thread::sleep(alarm->secs, alarm->nsecs);
125 mutex_lock(alarm->mutex);
127 alarm->wakeup = TRUE;
129 condition_signal(alarm->condition);
131 mutex_unlock(alarm->mutex);
136 int omni_condition::timedwait(unsigned long abs_secs, unsigned long abs_nsecs)
138 alarmclock_args alarm;
140 omni_thread::get_time(&alarm.secs, &alarm.nsecs, 0, 0);
142 if (abs_secs < alarm.secs || (abs_secs == alarm.secs && abs_nsecs <= alarm.nsecs))
145 alarm.secs = abs_secs - alarm.secs;
146 if (abs_nsecs <= alarm.nsecs) {
147 alarm.nsecs = 1000000 - alarm.nsecs + abs_nsecs;
151 alarm.nsecs = abs_nsecs - alarm.nsecs;
154 alarm.mutex = &mutex->mach_mutex;
155 alarm.condition = &mach_cond;
156 alarm.wakeup = FALSE;
158 cthread_t ct = cthread_fork((cthread_fn_t)alarmclock, (any_t)&alarm);
161 condition_wait(&mach_cond, &mutex->mach_mutex);
167 // interrupt the alarmclock thread sleep
170 // wait until it has signalled the condition
171 condition_wait(&mach_cond, &mutex->mach_mutex);
177 void omni_condition::signal(void)
179 condition_signal(&mach_cond);
183 void omni_condition::broadcast(void)
185 condition_signal(&mach_cond);
190 ///////////////////////////////////////////////////////////////////////////
192 // Counting semaphore
194 ///////////////////////////////////////////////////////////////////////////
197 omni_semaphore::omni_semaphore(unsigned int initial) : c(&m)
203 omni_semaphore::~omni_semaphore(void)
209 omni_semaphore::wait(void)
211 omni_mutex_lock l(m);
221 omni_semaphore::trywait(void)
223 omni_mutex_lock l(m);
234 omni_semaphore::post(void)
236 omni_mutex_lock l(m);
246 ///////////////////////////////////////////////////////////////////////////
250 ///////////////////////////////////////////////////////////////////////////
255 // Initialisation function (gets called before any user code).
258 omni_thread::init_t::init_t(void)
260 if (count++ != 0) // only do it once however many objects get created.
264 // find base and max priority.
265 // This is the initial thread, so the max priority of this
266 // thread also applies to any newly created thread.
270 struct thread_sched_info info;
271 unsigned int info_count = THREAD_SCHED_INFO_COUNT;
273 error = thread_info(thread_self(), THREAD_SCHED_INFO, (thread_info_t)&info, &info_count);
274 if (error != KERN_SUCCESS) {
275 DB(cerr << "omni_thread::init: error determining thread_info" << endl);
279 normal_priority = info.base_priority;
280 highest_priority = info.max_priority;
283 next_id_mutex = new omni_mutex;
286 // Create object for this (i.e. initial) thread.
289 omni_thread* t = new omni_thread;
291 if (t->_state != STATE_NEW) {
292 DB(cerr << "omni_thread::init: problem creating initial thread object\n");
296 t->_state = STATE_RUNNING;
298 t->mach_thread = cthread_self();
300 DB(cerr << "initial thread " << t->id() << endl);
302 cthread_set_data(t->mach_thread, (any_t)t);
307 // Wrapper for thread creation.
311 omni_thread_wrapper(void* ptr)
313 omni_thread* me = (omni_thread*)ptr;
315 DB(cerr << "omni_thread::wrapper: thread " << me->id()
318 cthread_set_data(cthread_self(), (any_t)me);
321 // Now invoke the thread function with the given argument.
324 if (me->fn_void != NULL) {
325 (*me->fn_void)(me->thread_arg);
329 if (me->fn_ret != NULL) {
330 void* return_value = (*me->fn_ret)(me->thread_arg);
331 omni_thread::exit(return_value);
335 me->run(me->thread_arg);
338 void* return_value = me->run_undetached(me->thread_arg);
339 omni_thread::exit(return_value);
342 // should never get here.
349 // Constructors for omni_thread - set up the thread object but don't
353 // construct a detached thread running a given function.
355 omni_thread::omni_thread(void (*fn)(void*), void* arg, priority_t pri)
357 common_constructor(arg, pri, 1);
362 // construct an undetached thread running a given function.
364 omni_thread::omni_thread(void* (*fn)(void*), void* arg, priority_t pri)
366 common_constructor(arg, pri, 0);
371 // construct a thread which will run either run() or run_undetached().
373 omni_thread::omni_thread(void* arg, priority_t pri)
375 common_constructor(arg, pri, 1);
380 // common part of all constructors.
382 void omni_thread::common_constructor(void* arg, priority_t pri, int det)
387 next_id_mutex->lock();
389 next_id_mutex->unlock();
392 detached = det; // may be altered in start_undetached()
397 // posix_thread is set up in initialisation routine or start().
402 // Destructor for omni_thread.
405 omni_thread::~omni_thread(void)
407 DB(cerr << "destructor called for thread " << id() << endl);
409 for (key_t i=0; i < _value_alloc; i++) {
424 omni_thread::start(void)
426 omni_mutex_lock l(mutex);
430 if (_state != STATE_NEW)
431 throw omni_thread_invalid();
433 mach_thread = cthread_fork(omni_thread_wrapper, (any_t)this);
435 _state = STATE_RUNNING;
438 cthread_detach(mach_thread);
443 // Start a thread which will run the member function run_undetached().
447 omni_thread::start_undetached(void)
449 if ((fn_void != NULL) || (fn_ret != NULL))
450 throw omni_thread_invalid();
458 // join - simply check error conditions & call cthread_join.
462 omni_thread::join(void** status)
466 if ((_state != STATE_RUNNING) && (_state != STATE_TERMINATED)) {
468 throw omni_thread_invalid();
474 throw omni_thread_invalid();
477 throw omni_thread_invalid();
479 DB(cerr << "omni_thread::join: doing cthread_join\n");
481 *status = cthread_join(mach_thread);
488 // Change this thread's priority.
492 omni_thread::set_priority(priority_t pri)
494 omni_mutex_lock l(mutex);
496 if (_state != STATE_RUNNING)
497 throw omni_thread_invalid();
501 kern_return_t rc = cthread_priority(mach_thread, mach_priority(pri), FALSE);
503 if (rc != KERN_SUCCESS)
504 throw omni_thread_fatal(errno);
508 // create - construct a new thread object and start it running. Returns thread
509 // object if successful, null pointer if not.
515 omni_thread::create(void (*fn)(void*), void* arg, priority_t pri)
517 omni_thread* t = new omni_thread(fn, arg, pri);
524 // undetached version
527 omni_thread::create(void* (*fn)(void*), void* arg, priority_t pri)
529 omni_thread* t = new omni_thread(fn, arg, pri);
537 // exit() _must_ lock the mutex even in the case of a detached thread. This is
538 // because a thread may run to completion before the thread that created it has
539 // had a chance to get out of start(). By locking the mutex we ensure that the
540 // creating thread must have reached the end of start() before we delete the
541 // thread object. Of course, once the call to start() returns, the user can
542 // still incorrectly refer to the thread object, but that's their problem.
545 void omni_thread::exit(void* return_value)
547 omni_thread* me = self();
553 if (me->_state != STATE_RUNNING)
554 DB(cerr << "omni_thread::exit: thread not in \"running\" state\n");
556 me->_state = STATE_TERMINATED;
560 DB(cerr << "omni_thread::exit: thread " << me->id() << " detached "
561 << me->detached << " return value " << return_value << endl);
568 DB(cerr << "omni_thread::exit: called with a non-omnithread. Exit quietly." << endl);
570 cthread_exit(return_value);
573 omni_thread* omni_thread::self(void)
577 me = (omni_thread*)cthread_data(cthread_self());
580 // This thread is not created by omni_thread::start because it
581 // doesn't has a class omni_thread instance attached to its key.
582 DB(cerr << "omni_thread::self: called with a non-ominthread. NULL is returned." << endl);
588 void omni_thread::yield(void)
593 #define MAX_SLEEP_SECONDS (unsigned)4294966 // (2**32-2)/1000
596 omni_thread::sleep(unsigned long secs, unsigned long nanosecs)
598 if (secs <= MAX_SLEEP_SECONDS) {
599 thread_switch(THREAD_NULL, SWITCH_OPTION_WAIT, secs * 1000 + nanosecs / 1000000);
603 unsigned no_of_max_sleeps = secs / MAX_SLEEP_SECONDS;
605 for (unsigned i = 0; i < no_of_max_sleeps; i++)
606 thread_switch(THREAD_NULL, SWITCH_OPTION_WAIT, MAX_SLEEP_SECONDS * 1000);
608 thread_switch(THREAD_NULL, SWITCH_OPTION_WAIT,
609 (secs % MAX_SLEEP_SECONDS) * 1000 + nanosecs / 1000000);
615 omni_thread::get_time(unsigned long* abs_sec, unsigned long* abs_nsec,
616 unsigned long rel_sec, unsigned long rel_nsec)
619 unsigned long tv_sec;
620 unsigned long tv_nsec;
623 rc = gettimeofday(&tv, NULL);
624 if (rc) throw omni_thread_fatal(rc);
627 tv_nsec = tv.tv_usec * 1000;
630 tv_sec += rel_sec + tv_nsec / 1000000000;
631 tv_nsec = tv_nsec % 1000000000;
639 omni_thread::mach_priority(priority_t pri)
646 case PRIORITY_NORMAL:
647 return normal_priority;
650 return highest_priority;
658 omni_thread::stacksize(unsigned long sz)
664 omni_thread::stacksize()
674 #error This dummy thread code is not tested. It might work if you're lucky.
676 class omni_thread_dummy : public omni_thread {
678 inline omni_thread_dummy() : omni_thread()
681 _state = STATE_RUNNING;
682 mach_thread = cthread_self();
683 cthread_set_data(mach_thread, (any_t)this));
685 inline ~omni_thread_dummy()
687 cthread_set_data(mach_thread, (any_t)0));
692 omni_thread::create_dummy()
694 if (omni_thread::self())
695 throw omni_thread_invalid();
697 return new omni_thread_dummy;
701 omni_thread::release_dummy()
703 omni_thread* self = omni_thread::self();
704 if (!self || !self->_dummy)
705 throw omni_thread_invalid();
707 omni_thread_dummy* dummy = (omni_thread_dummy*)self;
712 #define INSIDE_THREAD_IMPL_CC
713 #include "threaddata.cc"
714 #undef INSIDE_THREAD_IMPL_CC