00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00037 #include "rc_mutex_object.h"
00038
00039 namespace ReChannel {
00040
00041 rc_mutex_object::rc_mutex_object()
00042 : m_lock_count(0)
00043 { }
00044
00045 bool rc_mutex_object::trylock()
00046 {
00047 const sc_process_handle hproc = sc_get_current_process_handle();
00048
00049 if (m_lock_count == 0 || this->is_lock_owner(hproc)) {
00050
00051 m_lock_owner = hproc;
00052 m_lock_count++;
00053 return true;
00054 } else {
00055 return false;
00056 }
00057 }
00058
00059 void rc_mutex_object::lock()
00060 {
00061 const sc_process_handle hproc = sc_get_current_process_handle();
00062
00063 while(!this->is_lock_owner(hproc)) {
00064
00065 if (m_lock_count == 0) {
00066
00067 m_lock_owner = hproc;
00068 break;
00069 }
00070
00071 ::sc_core::wait(m_lock_release_event);
00072 }
00073
00074 m_lock_count++;
00075 }
00076
00077 bool rc_mutex_object::lock(sc_time timeout)
00078 {
00079 const sc_time stoptime = sc_time_stamp() + timeout;
00080 const sc_process_handle hproc = sc_get_current_process_handle();
00081
00082 while(this->is_lock_owner(hproc)) {
00083
00084 if (m_lock_count == 0) {
00085
00086 m_lock_owner = hproc;
00087 break;
00088 }
00089 const sc_time curtime = sc_time_stamp();
00090 if (curtime >= stoptime) {
00091 return false;
00092 }
00093
00094 ::sc_core::wait(stoptime - curtime, m_lock_release_event);
00095 }
00096
00097 m_lock_count++;
00098 return true;
00099 }
00100
00101 bool rc_mutex_object::unlock()
00102 {
00103 if (m_lock_count == 0) {
00104 return false;
00105 }
00106 const sc_process_handle hproc = sc_get_current_process_handle();
00107
00108 if (this->is_lock_owner(hproc)) {
00109 m_lock_count--;
00110 if (m_lock_count == 0) {
00111
00112 m_lock_owner = sc_process_handle();
00113 m_lock_release_event.notify();
00114 }
00115 return true;
00116 } else {
00117 return false;
00118 }
00119 }
00120
00121 bool rc_mutex_object::has_lock() const
00122 {
00123 const sc_process_handle hproc = sc_get_current_process_handle();
00124 return (m_lock_count > 0 && this->is_lock_owner(hproc));
00125 }
00126
00127 }
00128
00129
00130
00131
00132