Panda3D
 All Classes Functions Variables Enumerations
conditionVarSimpleImpl.cxx
00001 // Filename: conditionVarSimpleImpl.cxx
00002 // Created by:  drose (19Jun07)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "selectThreadImpl.h"
00016 
00017 #ifdef THREAD_SIMPLE_IMPL
00018 
00019 #include "conditionVarSimpleImpl.h"
00020 #include "threadSimpleImpl.h"
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: ConditionVarSimpleImpl::wait
00024 //       Access: Public
00025 //  Description: 
00026 ////////////////////////////////////////////////////////////////////
00027 void ConditionVarSimpleImpl::
00028 wait() {
00029   _mutex.release_quietly();
00030 
00031   ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
00032   ThreadSimpleImpl *thread = manager->get_current_thread();
00033   manager->enqueue_block(thread, this);
00034   manager->next_context();
00035 
00036   _mutex.acquire();
00037 }
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //     Function: ConditionVarSimpleImpl::wait
00041 //       Access: Public
00042 //  Description: 
00043 ////////////////////////////////////////////////////////////////////
00044 void ConditionVarSimpleImpl::
00045 wait(double timeout) {
00046   _mutex.release_quietly();
00047 
00048   // TODO.  For now this will release every frame, since we don't have
00049   // an interface yet on ThreadSimpleManager to do a timed wait.
00050   // Maybe that's good enough forever (it does satisfy the condition
00051   // variable semantics, after all).
00052   ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
00053   ThreadSimpleImpl *thread = manager->get_current_thread();
00054   manager->enqueue_ready(thread, true);
00055   manager->next_context();
00056 
00057   _mutex.acquire();
00058 }
00059 
00060 ////////////////////////////////////////////////////////////////////
00061 //     Function: ConditionVarSimpleImpl::do_notify
00062 //       Access: Private
00063 //  Description: 
00064 ////////////////////////////////////////////////////////////////////
00065 void ConditionVarSimpleImpl::
00066 do_notify() {
00067   ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
00068   if (manager->unblock_one(this)) {
00069     // There had been a thread waiting on this condition variable.
00070     // Switch contexts immediately, to make fairness more likely.
00071     ThreadSimpleImpl *thread = manager->get_current_thread();
00072     manager->enqueue_ready(thread, false);
00073     manager->next_context();
00074   }
00075 }
00076 
00077 ////////////////////////////////////////////////////////////////////
00078 //     Function: ConditionVarSimpleImpl::do_notify_all
00079 //       Access: Private
00080 //  Description: 
00081 ////////////////////////////////////////////////////////////////////
00082 void ConditionVarSimpleImpl::
00083 do_notify_all() {
00084   ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
00085   if (manager->unblock_all(this)) {
00086     // There had been a thread waiting on this condition variable.
00087     // Switch contexts immediately, to make fairness more likely.
00088     ThreadSimpleImpl *thread = manager->get_current_thread();
00089     manager->enqueue_ready(thread, false);
00090     manager->next_context();
00091   }
00092 }
00093 
00094 #endif  // THREAD_SIMPLE_IMPL
 All Classes Functions Variables Enumerations