Ticket #11852: 0001-BTimeSource-Cleanup-code.patch

File 0001-BTimeSource-Cleanup-code.patch, 11.5 KB (added by Barrett, 5 years ago)
  • src/kits/media/TimeSource.cpp

    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  
    77
    88
    99#include <TimeSource.h>
     10
    1011#include <Autolock.h>
     12
    1113#include <string.h>
     14
    1215#include "debug.h"
    1316#include "DataExchange.h"
    1417#include "ServerInterface.h"
    namespace BPrivate { namespace media {  
    2629
    2730#define _atomic_read(p)     atomic_or((p), 0)
    2831
    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
    3136
    32 struct TimeSourceTransmit // sizeof(TimeSourceTransmit) must be <= TS_AREA_SIZE
     37// sizeof(TimeSourceTransmit) must be <= TS_AREA_SIZE
     38struct TimeSourceTransmit
    3339{
    3440    int32 readindex;
    3541    int32 writeindex;
    BTimeSource::~BTimeSource()  
    9298
    9399status_t
    94100BTimeSource::SnoozeUntil(bigtime_t performance_time,
    95                          bigtime_t with_latency,
    96                          bool retry_signals)
     101    bigtime_t with_latency, bool retry_signals)
    97102{
    98103    CALLED();
    99104    bigtime_t time;
    BTimeSource::PerformanceTimeFor(bigtime_t real_time)  
    125130    if (GetTime(&last_perf_time, &last_real_time, &last_drift) != B_OK)
    126131        debugger("BTimeSource::PerformanceTimeFor: GetTime failed");
    127132
    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);
    129135}
    130136
    131137
    132138bigtime_t
    133139BTimeSource::RealTimeFor(bigtime_t performance_time,
    134                          bigtime_t with_latency)
     140    bigtime_t with_latency)
    135141{
    136142    PRINT(8, "CALLED BTimeSource::RealTimeFor()\n");
    137143
    BTimeSource::RealTimeFor(bigtime_t performance_time,  
    146152    if (GetTime(&last_perf_time, &last_real_time, &last_drift) != B_OK)
    147153        debugger("BTimeSource::RealTimeFor: GetTime failed");
    148154
    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);
    150157}
    151158
    152159
    BTimeSource::IsRunning()  
    157164
    158165    bool isrunning;
    159166
     167    // The system time source is always running
    160168    if (fIsRealtime)
    161         isrunning = true; // The system time source is always running :)
     169        isrunning = true;
    162170    else
    163171        isrunning = fBuf ? atomic_add(&fBuf->isrunning, 0) : fStarted;
    164172
    BTimeSource::IsRunning()  
    169177
    170178
    171179status_t
    172 BTimeSource::GetTime(bigtime_t *performance_time,
    173                      bigtime_t *real_time,
    174                      float *drift)
     180BTimeSource::GetTime(bigtime_t* performance_time,
     181    bigtime_t* real_time, float* drift)
    175182{
    176183    PRINT(8, "CALLED BTimeSource::GetTime()\n");
    177184
    BTimeSource::GetTime(bigtime_t *performance_time,  
    180187        *drift = 1.0f;
    181188        return B_OK;
    182189    }
    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 //  }
    189190
    190191    int32 index;
    191192    index = _atomic_read(&fBuf->readindex);
    BTimeSource::GetTime(bigtime_t *performance_time,  
    194195    *performance_time = fBuf->perftime[index];
    195196    *drift = fBuf->drift[index];
    196197
    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 
    204198    TRACE_TIMESOURCE("BTimeSource::GetTime     timesource %" B_PRId32
    205199        ", perf %16" B_PRId64 ", real %16" B_PRId64 ", drift %2.2f\n", ID(),
    206200        *performance_time, *real_time, *drift);
    BTimeSource::RealTime()  
    217211
    218212
    219213status_t
    220 BTimeSource::GetStartLatency(bigtime_t *out_latency)
     214BTimeSource::GetStartLatency(bigtime_t* out_latency)
    221215{
    222216    CALLED();
    223217    *out_latency = 0;
    BTimeSource::GetStartLatency(bigtime_t *out_latency)  
    229223 *************************************************************/
    230224
    231225
    232 BTimeSource::BTimeSource() :
     226BTimeSource::BTimeSource()
     227    :
    233228    BMediaNode("This one is never called"),
    234229    fStarted(false),
    235230    fArea(-1),
    BTimeSource::BTimeSource() :  
    239234{
    240235    CALLED();
    241236    AddNodeKind(B_TIME_SOURCE);
    242 //  printf("##### BTimeSource::BTimeSource() name %s, id %ld\n", Name(), ID());
    243 
    244237    // This constructor is only called by real time sources that inherit
    245238    // BTimeSource. We create the communication area in FinishCreate(),
    246239    // since we don't have a correct ID() until this node is registered.
    BTimeSource::BTimeSource() :  
    248241
    249242
    250243status_t
    251 BTimeSource::HandleMessage(int32 message,
    252                            const void *rawdata,
    253                            size_t size)
     244BTimeSource::HandleMessage(int32 message, const void* rawdata,
     245    size_t size)
    254246{
    255247    PRINT(4, "BTimeSource::HandleMessage %#" B_PRIx32 ", node %" B_PRId32 "\n",
    256248        message, fNodeID);
    BTimeSource::HandleMessage(int32 message,  
    258250    switch (message) {
    259251        case TIMESOURCE_OP:
    260252        {
    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);
    262255
    263256            status_t result;
    264257            result = TimeSourceOp(*data, NULL);
    BTimeSource::HandleMessage(int32 message,  
    285278
    286279        case TIMESOURCE_ADD_SLAVE_NODE:
    287280        {
    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);
    289283            DirectAddMe(data->node);
    290284            return B_OK;
    291285        }
    292286
    293287        case TIMESOURCE_REMOVE_SLAVE_NODE:
    294288        {
    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);
    296291            DirectRemoveMe(data->node);
    297292            return B_OK;
    298293        }
    299294
    300295        case TIMESOURCE_GET_START_LATENCY:
    301296        {
    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);
    303299            timesource_get_start_latency_reply reply;
    304300            rv = GetStartLatency(&reply.start_latency);
    305301            request->SendReply(rv, &reply, sizeof(reply));
    BTimeSource::HandleMessage(int32 message,  
    312308
    313309void
    314310BTimeSource::PublishTime(bigtime_t performance_time,
    315                          bigtime_t real_time,
    316                          float drift)
     311    bigtime_t real_time, float drift)
    317312{
    318313    TRACE_TIMESOURCE("BTimeSource::PublishTime timesource %" B_PRId32
    319314        ", perf %16" B_PRId64 ", real %16" B_PRId64 ", drift %2.2f\n", ID(),
    BTimeSource::PublishTime(bigtime_t performance_time,  
    332327    fBuf->perftime[index] = performance_time;
    333328    fBuf->drift[index] = drift;
    334329    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);
    337330}
    338331
    339332
    340333void
    341334BTimeSource::BroadcastTimeWarp(bigtime_t at_real_time,
    342                                bigtime_t new_performance_time)
     335    bigtime_t new_performance_time)
    343336{
    344337    CALLED();
    345338    ASSERT(fSlaveNodes != NULL);
    status_t BTimeSource::_Reserved_TimeSource_4(void *) { return B_ERROR; }  
    410403status_t BTimeSource::_Reserved_TimeSource_5(void *) { return B_ERROR; }
    411404
    412405/* explicit */
    413 BTimeSource::BTimeSource(media_node_id id) :
     406BTimeSource::BTimeSource(media_node_id id)
     407    :
    414408    BMediaNode("This one is never called"),
    415409    fStarted(false),
    416410    fArea(-1),
    BTimeSource::BTimeSource(media_node_id id) :  
    421415    CALLED();
    422416    AddNodeKind(B_TIME_SOURCE);
    423417    ASSERT(id > 0);
    424 //  printf("###### explicit BTimeSource::BTimeSource() id %ld, name %s\n", id, Name());
    425418
    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.
    428422    char name[32];
    429423    area_id area;
    430424    sprintf(name, "__timesource_buf_%" B_PRId32, id);
    BTimeSource::BTimeSource(media_node_id id) :  
    435429        return;
    436430    }
    437431    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
    439439    if (fArea <= 0) {
    440440        ERROR("BTimeSource::BTimeSource couldn't clone area, node %" B_PRId32
    441441            "\n", id);
    void  
    448448BTimeSource::FinishCreate()
    449449{
    450450    CALLED();
    451     //printf("BTimeSource::FinishCreate(), id %ld\n", ID());
    452451
    453452    char name[32];
    454453    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
    456461    if (fArea <= 0) {
    457462        ERROR("BTimeSource::BTimeSource couldn't create area, node %" B_PRId32
    458463            "\n", ID());
    BTimeSource::FinishCreate()  
    469474
    470475
    471476status_t
    472 BTimeSource::RemoveMe(BMediaNode *node)
     477BTimeSource::RemoveMe(BMediaNode* node)
    473478{
    474479    CALLED();
    475480    if (fKinds & NODE_KIND_SHADOW_TIMESOURCE) {
    476481        timesource_remove_slave_node_command cmd;
    477482        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));
    479485    } else {
    480486        DirectRemoveMe(node->Node());
    481487    }
    BTimeSource::RemoveMe(BMediaNode *node)  
    484490
    485491
    486492status_t
    487 BTimeSource::AddMe(BMediaNode *node)
     493BTimeSource::AddMe(BMediaNode* node)
    488494{
    489495    CALLED();
    490496    if (fKinds & NODE_KIND_SHADOW_TIMESOURCE) {
    BTimeSource::AddMe(BMediaNode *node)  
    499505
    500506
    501507void
    502 BTimeSource::DirectAddMe(const media_node &node)
     508BTimeSource::DirectAddMe(const media_node& node)
    503509{
    504510    // XXX this code has race conditions and is pretty dumb, and it
    505511    // XXX won't detect nodes that crash and don't remove themself.
    BTimeSource::DirectAddMe(const media_node &node)  
    536542}
    537543
    538544void
    539 BTimeSource::DirectRemoveMe(const media_node &node)
     545BTimeSource::DirectRemoveMe(const media_node& node)
    540546{
    541547    // XXX this code has race conditions and is pretty dumb, and it
    542548    // XXX won't detect nodes that crash and don't remove themself.
    BTimeSource::DirectStart(bigtime_t at)  
    580586
    581587
    582588void
    583 BTimeSource::DirectStop(bigtime_t at,
    584                         bool immediate)
     589BTimeSource::DirectStop(bigtime_t at, bool immediate)
    585590{
    586591    CALLED();
    587592    if (fBuf)
    BTimeSource::DirectStop(bigtime_t at,  
    592597
    593598
    594599void
    595 BTimeSource::DirectSeek(bigtime_t to,
    596                         bigtime_t at)
     600BTimeSource::DirectSeek(bigtime_t to, bigtime_t at)
    597601{
    598602    UNIMPLEMENTED();
    599603}