Panda3D
 All Classes Functions Variables Enumerations
mutexSimpleImpl.cxx
1 // Filename: mutexSimpleImpl.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 "mutexSimpleImpl.h"
20 #include "threadSimpleImpl.h"
21 #include "threadSimpleManager.h"
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: MutexSimpleImpl::do_acquire
25 // Access: Private
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 void MutexSimpleImpl::
29 do_acquire() {
30  // By the time we get here, we already know that someone else is
31  // holding the lock: (_flags & F_lock_count) != 0.
32  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
33  ThreadSimpleImpl *thread = manager->get_current_thread();
34 
35  while ((_flags & F_lock_count) != 0) {
36  manager->enqueue_block(thread, this);
37  manager->next_context();
38  }
39 
40  _flags |= F_lock_count;
41 }
42 
43 ////////////////////////////////////////////////////////////////////
44 // Function: MutexSimpleImpl::do_release
45 // Access: Private
46 // Description:
47 ////////////////////////////////////////////////////////////////////
48 void MutexSimpleImpl::
49 do_release() {
50  // By the time we get here, we already know that someone else is
51  // blocked on this mutex: (_flags & F_waiters) != 0.
52  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
53  if (manager->unblock_one(this)) {
54  // There had been a thread waiting on this mutex. Switch contexts
55  // immediately, to make fairness more likely.
56  ThreadSimpleImpl *thread = manager->get_current_thread();
57  manager->enqueue_ready(thread, false);
58  manager->next_context();
59  }
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: MutexSimpleImpl::do_release_quietly
64 // Access: Private
65 // Description:
66 ////////////////////////////////////////////////////////////////////
67 void MutexSimpleImpl::
68 do_release_quietly() {
69  ThreadSimpleManager *manager = ThreadSimpleManager::get_global_ptr();
70  manager->unblock_one(this);
71 }
72 
73 #endif // THREAD_SIMPLE_IMPL