]>
Dogcows Code - chaz/yoink/blob - src/moof/runloop.cc
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
31 static uint32_t call_registry(runloop
*& runloop
, registry_action action
)
34 typedef stlplus::hash
<uint32_t,moof::runloop
*,hash_function
> table_t
;
37 uint32_t thread_id
= thread::current_identifier();
39 MOOF_DECLARE_STATIC_MUTEX(table_mutex
);
40 MOOF_MUTEX_LOCK(table_mutex
);
46 log_info("registering runloop", runloop
, "for thread", thread_id
);
47 if (runloop
) table
[thread_id
] = runloop
;
48 else table
.erase(thread_id
);
54 table_t::iterator it
= table
.find(thread_id
);
55 if (it
!= table
.end()) runloop
= (*it
).second
;
56 else log_warning("runloop is not in registry for thread", thread_id
);
63 return thread::current_identifier();
67 bool comp(timer
* a
, timer
* b
)
69 return a
->expiration() < b
->expiration();
75 runloop
* runloop
= this;
76 thread_id_
= call_registry(runloop
, set
);
82 log_debug("------------------------------------");
83 scalar next_event
= SCALAR(0.0);
85 MOOF_MUTEX_LOCK(timers_mutex_
);
87 for (timers_it_
= timers_
.begin();
88 timers_it_
!= timers_
.end();
91 (*timers_it_
)->fire_if_expired();
94 std::sort(timers_
.begin(), timers_
.end(), comp
);
95 next_event
= timers_
[0]->expiration();
97 //for (timers_it_ = timers_.begin();
98 //timers_it_ != timers_.end();
101 //scalar absolute = (*timers_it_)->fire_if_expired();
102 //if (next_event == SCALAR(0.0) ||
103 //(absolute != SCALAR(0.0) && absolute < next_event))
105 //next_event = absolute;
109 timer::sleep(next_event
, timer::absolute
);
118 runloop
* runloop
= 0;
119 call_registry(runloop
, set
);
123 void runloop::stop(int code
)
130 runloop
* runloop::current()
133 call_registry(runloop
, lookup
);
138 void runloop::add_timer(timer
& timer
)
141 if (thread_id_
!= thread::current_identifier())
143 MOOF_MUTEX_LOCK(timers_mutex_
);
144 timers_
.push_back(&timer
);
145 timers_it_
= timers_
.end();
150 timers_
.push_back(&timer
);
151 timers_it_
= timers_
.end();
155 void runloop::remove_timer(timer
& timer
)
158 if (thread_id_
!= thread::current_identifier())
160 MOOF_MUTEX_LOCK(timers_mutex_
);
161 timers_
.erase(std::find(timers_
.begin(), timers_
.end(), &timer
));
162 timers_it_
= timers_
.end();
167 timers_
.erase(std::find(timers_
.begin(), timers_
.end(), &timer
));
168 timers_it_
= timers_
.end();
This page took 0.036386 seconds and 4 git commands to generate.