From 346f64a337e71a96eb05bed32b868cc09920fd0f Mon Sep 17 00:00:00 2001
From: Dario Casalinuovo <b.vitruvio@gmail.com>
Date: Mon, 16 Mar 2015 21:11:32 +0100
Subject: [PATCH 1/3] BTimeSource: Cleanup code.
---
src/kits/media/TimeSource.cpp | 118 ++++++++++++++++++++++--------------------
1 file changed, 61 insertions(+), 57 deletions(-)
diff --git a/src/kits/media/TimeSource.cpp b/src/kits/media/TimeSource.cpp
index dae0244..3ceee37 100644
a
|
b
|
|
7 | 7 | |
8 | 8 | |
9 | 9 | #include <TimeSource.h> |
| 10 | |
10 | 11 | #include <Autolock.h> |
| 12 | |
11 | 13 | #include <string.h> |
| 14 | |
12 | 15 | #include "debug.h" |
13 | 16 | #include "DataExchange.h" |
14 | 17 | #include "ServerInterface.h" |
… |
… |
namespace BPrivate { namespace media {
|
26 | 29 | |
27 | 30 | #define _atomic_read(p) atomic_or((p), 0) |
28 | 31 | |
29 | | #define TS_AREA_SIZE B_PAGE_SIZE // must be multiple of page size |
30 | | #define TS_INDEX_COUNT 128 // must be power of two |
| 32 | // must be multiple of page size |
| 33 | #define TS_AREA_SIZE B_PAGE_SIZE |
| 34 | // must be power of two |
| 35 | #define TS_INDEX_COUNT 128 |
31 | 36 | |
32 | | struct TimeSourceTransmit // sizeof(TimeSourceTransmit) must be <= TS_AREA_SIZE |
| 37 | // sizeof(TimeSourceTransmit) must be <= TS_AREA_SIZE |
| 38 | struct TimeSourceTransmit |
33 | 39 | { |
34 | 40 | int32 readindex; |
35 | 41 | int32 writeindex; |
… |
… |
BTimeSource::~BTimeSource()
|
92 | 98 | |
93 | 99 | status_t |
94 | 100 | BTimeSource::SnoozeUntil(bigtime_t performance_time, |
95 | | bigtime_t with_latency, |
96 | | bool retry_signals) |
| 101 | bigtime_t with_latency, bool retry_signals) |
97 | 102 | { |
98 | 103 | CALLED(); |
99 | 104 | bigtime_t time; |
… |
… |
BTimeSource::PerformanceTimeFor(bigtime_t real_time)
|
125 | 130 | if (GetTime(&last_perf_time, &last_real_time, &last_drift) != B_OK) |
126 | 131 | debugger("BTimeSource::PerformanceTimeFor: GetTime failed"); |
127 | 132 | |
128 | | return last_perf_time + (bigtime_t)((real_time - last_real_time) * last_drift); |
| 133 | return last_perf_time |
| 134 | + (bigtime_t)((real_time - last_real_time) * last_drift); |
129 | 135 | } |
130 | 136 | |
131 | 137 | |
132 | 138 | bigtime_t |
133 | 139 | BTimeSource::RealTimeFor(bigtime_t performance_time, |
134 | | bigtime_t with_latency) |
| 140 | bigtime_t with_latency) |
135 | 141 | { |
136 | 142 | PRINT(8, "CALLED BTimeSource::RealTimeFor()\n"); |
137 | 143 | |
… |
… |
BTimeSource::RealTimeFor(bigtime_t performance_time,
|
146 | 152 | if (GetTime(&last_perf_time, &last_real_time, &last_drift) != B_OK) |
147 | 153 | debugger("BTimeSource::RealTimeFor: GetTime failed"); |
148 | 154 | |
149 | | return last_real_time - with_latency + (bigtime_t)((performance_time - last_perf_time) / last_drift); |
| 155 | return last_real_time - with_latency |
| 156 | + (bigtime_t)((performance_time - last_perf_time) / last_drift); |
150 | 157 | } |
151 | 158 | |
152 | 159 | |
… |
… |
BTimeSource::IsRunning()
|
157 | 164 | |
158 | 165 | bool isrunning; |
159 | 166 | |
| 167 | // The system time source is always running |
160 | 168 | if (fIsRealtime) |
161 | | isrunning = true; // The system time source is always running :) |
| 169 | isrunning = true; |
162 | 170 | else |
163 | 171 | isrunning = fBuf ? atomic_add(&fBuf->isrunning, 0) : fStarted; |
164 | 172 | |
… |
… |
BTimeSource::IsRunning()
|
169 | 177 | |
170 | 178 | |
171 | 179 | status_t |
172 | | BTimeSource::GetTime(bigtime_t *performance_time, |
173 | | bigtime_t *real_time, |
174 | | float *drift) |
| 180 | BTimeSource::GetTime(bigtime_t* performance_time, |
| 181 | bigtime_t* real_time, float* drift) |
175 | 182 | { |
176 | 183 | PRINT(8, "CALLED BTimeSource::GetTime()\n"); |
177 | 184 | |
… |
… |
BTimeSource::GetTime(bigtime_t *performance_time,
|
180 | 187 | *drift = 1.0f; |
181 | 188 | return B_OK; |
182 | 189 | } |
183 | | // if (fBuf == 0) { |
184 | | // PRINT(1, "BTimeSource::GetTime: fBuf == 0, name %s, id %ld\n",Name(),ID()); |
185 | | // *performance_time = *real_time = system_time(); |
186 | | // *drift = 1.0f; |
187 | | // return B_OK; |
188 | | // } |
189 | 190 | |
190 | 191 | int32 index; |
191 | 192 | index = _atomic_read(&fBuf->readindex); |
… |
… |
BTimeSource::GetTime(bigtime_t *performance_time,
|
194 | 195 | *performance_time = fBuf->perftime[index]; |
195 | 196 | *drift = fBuf->drift[index]; |
196 | 197 | |
197 | | // if (*real_time == 0) { |
198 | | // *performance_time = *real_time = system_time(); |
199 | | // *drift = 1.0f; |
200 | | // return B_OK; |
201 | | // } |
202 | | // printf("BTimeSource::GetTime timesource %ld, index %ld, perf %16Ld, real %16Ld, drift %2.2f\n", ID(), index, *performance_time, *real_time, *drift); |
203 | | |
204 | 198 | TRACE_TIMESOURCE("BTimeSource::GetTime timesource %" B_PRId32 |
205 | 199 | ", perf %16" B_PRId64 ", real %16" B_PRId64 ", drift %2.2f\n", ID(), |
206 | 200 | *performance_time, *real_time, *drift); |
… |
… |
BTimeSource::RealTime()
|
217 | 211 | |
218 | 212 | |
219 | 213 | status_t |
220 | | BTimeSource::GetStartLatency(bigtime_t *out_latency) |
| 214 | BTimeSource::GetStartLatency(bigtime_t* out_latency) |
221 | 215 | { |
222 | 216 | CALLED(); |
223 | 217 | *out_latency = 0; |
… |
… |
BTimeSource::GetStartLatency(bigtime_t *out_latency)
|
229 | 223 | *************************************************************/ |
230 | 224 | |
231 | 225 | |
232 | | BTimeSource::BTimeSource() : |
| 226 | BTimeSource::BTimeSource() |
| 227 | : |
233 | 228 | BMediaNode("This one is never called"), |
234 | 229 | fStarted(false), |
235 | 230 | fArea(-1), |
… |
… |
BTimeSource::BTimeSource() :
|
239 | 234 | { |
240 | 235 | CALLED(); |
241 | 236 | AddNodeKind(B_TIME_SOURCE); |
242 | | // printf("##### BTimeSource::BTimeSource() name %s, id %ld\n", Name(), ID()); |
243 | | |
244 | 237 | // This constructor is only called by real time sources that inherit |
245 | 238 | // BTimeSource. We create the communication area in FinishCreate(), |
246 | 239 | // since we don't have a correct ID() until this node is registered. |
… |
… |
BTimeSource::BTimeSource() :
|
248 | 241 | |
249 | 242 | |
250 | 243 | status_t |
251 | | BTimeSource::HandleMessage(int32 message, |
252 | | const void *rawdata, |
253 | | size_t size) |
| 244 | BTimeSource::HandleMessage(int32 message, const void* rawdata, |
| 245 | size_t size) |
254 | 246 | { |
255 | 247 | PRINT(4, "BTimeSource::HandleMessage %#" B_PRIx32 ", node %" B_PRId32 "\n", |
256 | 248 | message, fNodeID); |
… |
… |
BTimeSource::HandleMessage(int32 message,
|
258 | 250 | switch (message) { |
259 | 251 | case TIMESOURCE_OP: |
260 | 252 | { |
261 | | const time_source_op_info *data = static_cast<const time_source_op_info *>(rawdata); |
| 253 | const time_source_op_info* data |
| 254 | = static_cast<const time_source_op_info*>(rawdata); |
262 | 255 | |
263 | 256 | status_t result; |
264 | 257 | result = TimeSourceOp(*data, NULL); |
… |
… |
BTimeSource::HandleMessage(int32 message,
|
285 | 278 | |
286 | 279 | case TIMESOURCE_ADD_SLAVE_NODE: |
287 | 280 | { |
288 | | const timesource_add_slave_node_command *data = static_cast<const timesource_add_slave_node_command *>(rawdata); |
| 281 | const timesource_add_slave_node_command* data |
| 282 | = static_cast<const timesource_add_slave_node_command*>(rawdata); |
289 | 283 | DirectAddMe(data->node); |
290 | 284 | return B_OK; |
291 | 285 | } |
292 | 286 | |
293 | 287 | case TIMESOURCE_REMOVE_SLAVE_NODE: |
294 | 288 | { |
295 | | const timesource_remove_slave_node_command *data = static_cast<const timesource_remove_slave_node_command *>(rawdata); |
| 289 | const timesource_remove_slave_node_command* data |
| 290 | = static_cast<const timesource_remove_slave_node_command*>(rawdata); |
296 | 291 | DirectRemoveMe(data->node); |
297 | 292 | return B_OK; |
298 | 293 | } |
299 | 294 | |
300 | 295 | case TIMESOURCE_GET_START_LATENCY: |
301 | 296 | { |
302 | | const timesource_get_start_latency_request *request = static_cast<const timesource_get_start_latency_request *>(rawdata); |
| 297 | const timesource_get_start_latency_request* request |
| 298 | = static_cast<const timesource_get_start_latency_request*>(rawdata); |
303 | 299 | timesource_get_start_latency_reply reply; |
304 | 300 | rv = GetStartLatency(&reply.start_latency); |
305 | 301 | request->SendReply(rv, &reply, sizeof(reply)); |
… |
… |
BTimeSource::HandleMessage(int32 message,
|
312 | 308 | |
313 | 309 | void |
314 | 310 | BTimeSource::PublishTime(bigtime_t performance_time, |
315 | | bigtime_t real_time, |
316 | | float drift) |
| 311 | bigtime_t real_time, float drift) |
317 | 312 | { |
318 | 313 | TRACE_TIMESOURCE("BTimeSource::PublishTime timesource %" B_PRId32 |
319 | 314 | ", perf %16" B_PRId64 ", real %16" B_PRId64 ", drift %2.2f\n", ID(), |
… |
… |
BTimeSource::PublishTime(bigtime_t performance_time,
|
332 | 327 | fBuf->perftime[index] = performance_time; |
333 | 328 | fBuf->drift[index] = drift; |
334 | 329 | atomic_add(&fBuf->readindex, 1); |
335 | | |
336 | | // printf("BTimeSource::PublishTime timesource %ld, write index %ld, perf %16Ld, real %16Ld, drift %2.2f\n", ID(), index, performance_time, real_time, drift); |
337 | 330 | } |
338 | 331 | |
339 | 332 | |
340 | 333 | void |
341 | 334 | BTimeSource::BroadcastTimeWarp(bigtime_t at_real_time, |
342 | | bigtime_t new_performance_time) |
| 335 | bigtime_t new_performance_time) |
343 | 336 | { |
344 | 337 | CALLED(); |
345 | 338 | ASSERT(fSlaveNodes != NULL); |
… |
… |
status_t BTimeSource::_Reserved_TimeSource_4(void *) { return B_ERROR; }
|
410 | 403 | status_t BTimeSource::_Reserved_TimeSource_5(void *) { return B_ERROR; } |
411 | 404 | |
412 | 405 | /* explicit */ |
413 | | BTimeSource::BTimeSource(media_node_id id) : |
| 406 | BTimeSource::BTimeSource(media_node_id id) |
| 407 | : |
414 | 408 | BMediaNode("This one is never called"), |
415 | 409 | fStarted(false), |
416 | 410 | fArea(-1), |
… |
… |
BTimeSource::BTimeSource(media_node_id id) :
|
421 | 415 | CALLED(); |
422 | 416 | AddNodeKind(B_TIME_SOURCE); |
423 | 417 | ASSERT(id > 0); |
424 | | // printf("###### explicit BTimeSource::BTimeSource() id %ld, name %s\n", id, Name()); |
425 | 418 | |
426 | | // This constructor is only called by the derived BPrivate::media::TimeSourceObject objects |
427 | | // We create a clone of the communication area |
| 419 | // This constructor is only called by the derived |
| 420 | // BPrivate::media::TimeSourceObject objects |
| 421 | // We create a clone of the communication area. |
428 | 422 | char name[32]; |
429 | 423 | area_id area; |
430 | 424 | sprintf(name, "__timesource_buf_%" B_PRId32, id); |
… |
… |
BTimeSource::BTimeSource(media_node_id id) :
|
435 | 429 | return; |
436 | 430 | } |
437 | 431 | sprintf(name, "__cloned_timesource_buf_%" B_PRId32, id); |
438 | | fArea = clone_area(name, reinterpret_cast<void **>(const_cast<BPrivate::media::TimeSourceTransmit **>(&fBuf)), B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, area); |
| 432 | |
| 433 | void** buf = reinterpret_cast<void**> |
| 434 | (const_cast<BPrivate::media::TimeSourceTransmit**>(&fBuf)); |
| 435 | |
| 436 | fArea = clone_area(name, buf, B_ANY_ADDRESS, |
| 437 | B_READ_AREA | B_WRITE_AREA, area); |
| 438 | |
439 | 439 | if (fArea <= 0) { |
440 | 440 | ERROR("BTimeSource::BTimeSource couldn't clone area, node %" B_PRId32 |
441 | 441 | "\n", id); |
… |
… |
void
|
448 | 448 | BTimeSource::FinishCreate() |
449 | 449 | { |
450 | 450 | CALLED(); |
451 | | //printf("BTimeSource::FinishCreate(), id %ld\n", ID()); |
452 | 451 | |
453 | 452 | char name[32]; |
454 | 453 | sprintf(name, "__timesource_buf_%" B_PRId32, ID()); |
455 | | fArea = create_area(name, reinterpret_cast<void **>(const_cast<BPrivate::media::TimeSourceTransmit **>(&fBuf)), B_ANY_ADDRESS, TS_AREA_SIZE, B_FULL_LOCK, B_READ_AREA | B_WRITE_AREA); |
| 454 | |
| 455 | void** buf = reinterpret_cast<void**> |
| 456 | (const_cast<BPrivate::media::TimeSourceTransmit**>(&fBuf)); |
| 457 | |
| 458 | fArea = create_area(name, buf, B_ANY_ADDRESS, TS_AREA_SIZE, |
| 459 | B_FULL_LOCK, B_READ_AREA | B_WRITE_AREA); |
| 460 | |
456 | 461 | if (fArea <= 0) { |
457 | 462 | ERROR("BTimeSource::BTimeSource couldn't create area, node %" B_PRId32 |
458 | 463 | "\n", ID()); |
… |
… |
BTimeSource::FinishCreate()
|
469 | 474 | |
470 | 475 | |
471 | 476 | status_t |
472 | | BTimeSource::RemoveMe(BMediaNode *node) |
| 477 | BTimeSource::RemoveMe(BMediaNode* node) |
473 | 478 | { |
474 | 479 | CALLED(); |
475 | 480 | if (fKinds & NODE_KIND_SHADOW_TIMESOURCE) { |
476 | 481 | timesource_remove_slave_node_command cmd; |
477 | 482 | cmd.node = node->Node(); |
478 | | SendToPort(fControlPort, TIMESOURCE_REMOVE_SLAVE_NODE, &cmd, sizeof(cmd)); |
| 483 | SendToPort(fControlPort, TIMESOURCE_REMOVE_SLAVE_NODE, |
| 484 | &cmd, sizeof(cmd)); |
479 | 485 | } else { |
480 | 486 | DirectRemoveMe(node->Node()); |
481 | 487 | } |
… |
… |
BTimeSource::RemoveMe(BMediaNode *node)
|
484 | 490 | |
485 | 491 | |
486 | 492 | status_t |
487 | | BTimeSource::AddMe(BMediaNode *node) |
| 493 | BTimeSource::AddMe(BMediaNode* node) |
488 | 494 | { |
489 | 495 | CALLED(); |
490 | 496 | if (fKinds & NODE_KIND_SHADOW_TIMESOURCE) { |
… |
… |
BTimeSource::AddMe(BMediaNode *node)
|
499 | 505 | |
500 | 506 | |
501 | 507 | void |
502 | | BTimeSource::DirectAddMe(const media_node &node) |
| 508 | BTimeSource::DirectAddMe(const media_node& node) |
503 | 509 | { |
504 | 510 | // XXX this code has race conditions and is pretty dumb, and it |
505 | 511 | // XXX won't detect nodes that crash and don't remove themself. |
… |
… |
BTimeSource::DirectAddMe(const media_node &node)
|
536 | 542 | } |
537 | 543 | |
538 | 544 | void |
539 | | BTimeSource::DirectRemoveMe(const media_node &node) |
| 545 | BTimeSource::DirectRemoveMe(const media_node& node) |
540 | 546 | { |
541 | 547 | // XXX this code has race conditions and is pretty dumb, and it |
542 | 548 | // XXX won't detect nodes that crash and don't remove themself. |
… |
… |
BTimeSource::DirectStart(bigtime_t at)
|
580 | 586 | |
581 | 587 | |
582 | 588 | void |
583 | | BTimeSource::DirectStop(bigtime_t at, |
584 | | bool immediate) |
| 589 | BTimeSource::DirectStop(bigtime_t at, bool immediate) |
585 | 590 | { |
586 | 591 | CALLED(); |
587 | 592 | if (fBuf) |
… |
… |
BTimeSource::DirectStop(bigtime_t at,
|
592 | 597 | |
593 | 598 | |
594 | 599 | void |
595 | | BTimeSource::DirectSeek(bigtime_t to, |
596 | | bigtime_t at) |
| 600 | BTimeSource::DirectSeek(bigtime_t to, bigtime_t at) |
597 | 601 | { |
598 | 602 | UNIMPLEMENTED(); |
599 | 603 | } |