Panda3D
 All Classes Functions Variables Enumerations
conditionVarSimpleImpl.cxx
1 // Filename: conditionVarSimpleImpl.cxx
2 // Created by: drose (19Jun07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "selectThreadImpl.h"
16 
17 #ifdef THREAD_SIMPLE_IMPL
18 
19 #include "conditionVarSimpleImpl.h"
20 #include "threadSimpleImpl.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: ConditionVarSimpleImpl::wait
24 // Access: Public
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 void ConditionVarSimpleImpl::
28 wait() {
29  _mutex.release_quietly();
30 
31  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
32  ThreadSimpleImpl *thread = manager->get_current_thread();
33  manager->enqueue_block(thread, this);
34  manager->next_context();
35 
36  _mutex.acquire();
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: ConditionVarSimpleImpl::wait
41 // Access: Public
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 void ConditionVarSimpleImpl::
45 wait(double timeout) {
46  _mutex.release_quietly();
47 
48  // TODO. For now this will release every frame, since we don't have
49  // an interface yet on ThreadSimpleManager to do a timed wait.
50  // Maybe that's good enough forever (it does satisfy the condition
51  // variable semantics, after all).
52  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
53  ThreadSimpleImpl *thread = manager->get_current_thread();
54  manager->enqueue_ready(thread, true);
55  manager->next_context();
56 
57  _mutex.acquire();
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: ConditionVarSimpleImpl::do_notify
62 // Access: Private
63 // Description:
64 ////////////////////////////////////////////////////////////////////
65 void ConditionVarSimpleImpl::
66 do_notify() {
67  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
68  if (manager->unblock_one(this)) {
69  // There had been a thread waiting on this condition variable.
70  // Switch contexts immediately, to make fairness more likely.
71  ThreadSimpleImpl *thread = manager->get_current_thread();
72  manager->enqueue_ready(thread, false);
73  manager->next_context();
74  }
75 }
76 
77 ////////////////////////////////////////////////////////////////////
78 // Function: ConditionVarSimpleImpl::do_notify_all
79 // Access: Private
80 // Description:
81 ////////////////////////////////////////////////////////////////////
82 void ConditionVarSimpleImpl::
83 do_notify_all() {
84  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
85  if (manager->unblock_all(this)) {
86  // There had been a thread waiting on this condition variable.
87  // Switch contexts immediately, to make fairness more likely.
88  ThreadSimpleImpl *thread = manager->get_current_thread();
89  manager->enqueue_ready(thread, false);
90  manager->next_context();
91  }
92 }
93 
94 #endif // THREAD_SIMPLE_IMPL