mce-wltimer.c 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
/**
 * @file mce-wltimer.c
 *
 * Mode Control Entity - Timers that block suspend until triggered
 *
 * <p>
 *
 * Copyright (C) 2015 Jolla Ltd.
 *
 * <p>
 *
 * @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
 *
 * mce is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * mce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mce.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mce-wltimer.h"

#include "mce-log.h"
#include "mce-wakelock.h"

#include <stdlib.h>
#include <string.h>

/* ========================================================================= *
 * Types and functions
 * ========================================================================= */

/* ------------------------------------------------------------------------- *
 * TIMER_METHODS
 * ------------------------------------------------------------------------- */

/** State data for mce wakelock timers */
struct mce_wltimer_t
{
    /** Timer name, used as wakelock name too */
    char       *wlt_name;

    /** Timer delay in milliseconds */
    int         wlt_period;

    /** Underlying glib timeout id */
    guint       wlt_timer_id;

    /** Timer callback function */
    GSourceFunc wlt_notify;

    /** User data to pass to wlt_notify() */
    void       *wlt_user_data;
};

mce_wltimer_t * mce_wltimer_create         (const char *name, int period, GSourceFunc notify, void *user_data);
void            mce_wltimer_delete         (mce_wltimer_t *self);

static void     mce_wltimer_eval_wakelock  (const mce_wltimer_t *self);

bool            mce_wltimer_is_active      (const mce_wltimer_t *self);
const char     *mce_wltimer_get_name       (const mce_wltimer_t *self);
void            mce_wltimer_set_period     (mce_wltimer_t *self, int period);
void            mce_wltimer_start          (mce_wltimer_t *self);
void            mce_wltimer_stop           (mce_wltimer_t *self);

static gboolean mce_wltimer_gate_cb        (gpointer aptr);

/* ------------------------------------------------------------------------- *
 * QUEUE_MANAGEMENT
 * ------------------------------------------------------------------------- */

/** Idle callback id for delayed garbage collect */
static guint mwt_queue_compact_id = 0;

/** List of registered timers */
static GSList *mwt_queue_timer_list = 0;

static void     mwt_queue_compact          (void);
static gboolean mwt_queue_compact_cb       (gpointer aptr);
static void     mwt_queue_schedule_compact (void);
static void     mwt_queue_cancel_compact   (void);

static void     mwt_queue_add_timer        (mce_wltimer_t *self);
static void     mwt_queue_remove_timer     (mce_wltimer_t *self);
static bool     mwt_queue_has_timer        (const mce_wltimer_t *self);

/* ------------------------------------------------------------------------- *
 * MODULE_INIT
 * ------------------------------------------------------------------------- */

/** Flag for: timers can be started */
static bool     mce_wltimer_ready = true;

void            mce_wltimer_init          (void);
void            mce_wltimer_quit          (void);

/* ========================================================================= *
 * TIMER_METHODS
 * ========================================================================= */

/** Create wakelock timer
 *
 * @param name      timer name
 * @param period    timer delay [ms]
 * @param notify    function to be called when triggered
 * @param user_data pointer to be passed to notify function
 *
 * @return wakelock timer object
 */
mce_wltimer_t *
mce_wltimer_create(const char  *name,
                   int          period,
                   GSourceFunc  notify,
                   void        *user_data)
{
    mce_wltimer_t *self = calloc(1, sizeof *self);

    self->wlt_name      = name ? strdup(name) : 0;
    self->wlt_period    = period;
    self->wlt_timer_id  = 0;
    self->wlt_notify    = notify;
    self->wlt_user_data = user_data;

    mwt_queue_add_timer(self);

    return self;
}

/** Delete wakelock timer
 *
 * @param self wakelock timer object, or NULL
 */
void
mce_wltimer_delete(mce_wltimer_t *self)
{
    if( !self )
        goto EXIT;

    mce_wltimer_stop(self);
    mwt_queue_remove_timer(self);

    free(self->wlt_name),
        self->wlt_name = 0;

    free(self);

EXIT:
    return;
}

/** Evaluate need for wakelock
 *
 * @param self   wakelock timer object, or NULL
 */
static void
mce_wltimer_eval_wakelock(const mce_wltimer_t *self)
{
    if( !self )
        goto EXIT;

    if( !self->wlt_name)
        goto EXIT;

    if( self->wlt_timer_id )
        mce_wakelock_obtain(self->wlt_name, -1);
    else
        mce_wakelock_release(self->wlt_name);

EXIT:
    return;
}

/** Predicate for: wakelock timer has been started
 *
 * @param self wakelock timer object, or NULL
 *
 * @return true if wakelock timer has been started, false otherwise
 */
bool
mce_wltimer_is_active(const mce_wltimer_t *self)
{
    bool active = false;
    if( self )
        active = (self->wlt_timer_id != 0);
    return active;
}

/** Get wakelock timer name
 *
 * @param self wakelock timer object, or NULL
 *
 * @return "invalid" if object is not valid,
 *         "unknown" if object has no name, or
 *         object name
 */

const char *
mce_wltimer_get_name(const mce_wltimer_t *self)
{
    const char *name = "invalid";
    if( self )
        name = self->wlt_name;
    return name ?: "unknown";
}

/** Set wakelock timer period
 *
 * @param self   wakelock timer object, or NULL
 * @param period timer delay [ms]
 */
void
mce_wltimer_set_period(mce_wltimer_t *self, int period)
{
    if( self )
        self->wlt_period = period;
}

/** Call wakelock timer notification functiom
 *
 * @param aptr   wakelock timer object (as void pointer)
 */
static gboolean
mce_wltimer_gate_cb(gpointer aptr)
{
    gboolean repeat = FALSE;

    mce_wltimer_t *self = aptr;

    if( !self->wlt_timer_id )
        goto EXIT;

    if( self->wlt_notify ) {
        bool res = self->wlt_notify(self->wlt_user_data);

        if( !mwt_queue_has_timer(self) ) {
            /* The notify callback managed to delete the timer
             * object, invalidate the pointer */
            self = 0;
        }
        else {
            /* Repeat/stop according to the callback return value */
            repeat = res;
        }
    }

EXIT:

    if( self ) {
        if( !repeat &&  self->wlt_timer_id )
            self->wlt_timer_id = 0;
        mce_wltimer_eval_wakelock(self);
    }

    return repeat;
}

/** Start wakelock timer
 *
 * @param self   wakelock timer object, or NULL
 */
void
mce_wltimer_start(mce_wltimer_t *self)
{
    if( !self )
        goto EXIT;

    if( self->wlt_timer_id ) {
        g_source_remove(self->wlt_timer_id),
            self->wlt_timer_id = 0;
    }

    if( !mce_wltimer_ready )
        goto EXIT;

    if( self->wlt_period < 0 )
        goto EXIT;

    mce_log(LL_DEBUG, "start %s %d", mce_wltimer_get_name(self),
            self->wlt_period);

    if( self->wlt_period > 0 ) {
        self->wlt_timer_id = g_timeout_add(self->wlt_period,
                                           mce_wltimer_gate_cb,
                                           self);
    }
    else {
        self->wlt_timer_id = g_idle_add(mce_wltimer_gate_cb,
                                        self);
    }

EXIT:
    mce_wltimer_eval_wakelock(self);
    return;
}

/** Stop wakelock timer
 *
 * @param self   wakelock timer object, or NULL
 */
void
mce_wltimer_stop(mce_wltimer_t *self)
{
    if( !self )
        goto EXIT;

    if( !self->wlt_timer_id )
        goto EXIT;

    mce_log(LL_DEBUG, "stop %s", mce_wltimer_get_name(self));

    g_source_remove(self->wlt_timer_id),
        self->wlt_timer_id = 0;

EXIT:
    mce_wltimer_eval_wakelock(self);

    return;
}

/* ========================================================================= *
 * QUEUE_MANAGEMENT
 * ========================================================================= */

/** Clean up unused timer list slots
 *
 * When timers are deleted, the associated node in mwt_queue_timer_list
 * is left in place with data pointer set to zero.
 *
 * This function can be used to remove such stale entries in suitable
 * point outside the dispatch iteration loop.
 */
static void
mwt_queue_compact(void)
{
    mwt_queue_cancel_compact();

    GSList **tail = &mwt_queue_timer_list;

    GSList *item;

    while( (item = *tail) ) {
        if( item->data ) {
            tail = &item->next;
        }
        else {
            *tail = item->next;
            item->next = 0;
            g_slist_free(item);
        }
    }
}

/** Idle callback function for delayed garbage collect
 *
 * @param aptr user data pointer (unused)
 *
 * @return FALSE, to stop repeats
 */
static gboolean
mwt_queue_compact_cb(gpointer aptr)
{
    (void)aptr;

    if( !mwt_queue_compact_id )
        goto EXIT;

    mwt_queue_compact_id = 0;

    mwt_queue_compact();

EXIT:
    return FALSE;
}

/** Schedule delayed garbage collection
 */
static void mwt_queue_schedule_compact(void)
{
    if( mwt_queue_compact_id )
        goto EXIT;

    mwt_queue_compact_id = g_idle_add(mwt_queue_compact_cb, 0);

EXIT:
    return;
}

/** Cancel delayed garbage collection
 */
static void mwt_queue_cancel_compact(void)
{
    if( !mwt_queue_compact_id )
        goto EXIT;

    g_source_remove(mwt_queue_compact_id),
        mwt_queue_compact_id = 0;

EXIT:
    return;
}

/** Predicate for: wakelock timer is registered
 *
 * @param self   wakelock timer object, or NULL
 *
 * return true if timer is registered, false otherwise
 */
static bool
mwt_queue_has_timer(const mce_wltimer_t *self)
{
    bool has_timer = false;

    if( !self )
        goto EXIT;

    has_timer = g_slist_find(mwt_queue_timer_list, self) != 0;

EXIT:
    return has_timer;
}

/** Register wakelock timer
 *
 * @param self   wakelock timer object, or NULL
 */
static void
mwt_queue_add_timer(mce_wltimer_t *self)
{
    if( !self )
        goto EXIT;

    /* Try to find recyclable vacated timer slot */
    GSList *item = g_slist_find(mwt_queue_timer_list, 0);

    if( item )
        item->data = self;
    else
        mwt_queue_timer_list = g_slist_prepend(mwt_queue_timer_list, self);

EXIT:
    return;
}

/** Unregister wakelock timer
 *
 * @param self   wakelock timer object, or NULL
 */
static void
mwt_queue_remove_timer(mce_wltimer_t *self)
{
    if( !self )
        goto EXIT;

    GSList *item = g_slist_find(mwt_queue_timer_list, self);

    /* Vacate the slot by clearing the data link */
    if( item )
        item->data = 0;

    /* Schedule removal of the link it self */
    mwt_queue_schedule_compact();

EXIT:
    return;
}

/* ========================================================================= *
 * MODULE_INIT
 * ========================================================================= */

void
mce_wltimer_init(void)
{
    /* nop */
}

void
mce_wltimer_quit(void)
{
    mce_log(LL_DEBUG, "deny suspend block timers");

    /* Deny starting of timers */
    mce_wltimer_ready = false;

    /* Disable left-behind timer objects */
    for( GSList *item = mwt_queue_timer_list; item; item = item->next ) {
        mce_wltimer_t *timer = item->data;
        if( !timer )
            continue;

        /* Note: What we have here is effectively a resource leak
         *       somewhere else. But all that can be done is to make
         *       sure we do not leave behind active timeouts that
         *       might then trigger callbacks in unexpected manner.
         */

        mce_log(LL_WARN, "timer '%s' exists at deinit",
               mce_wltimer_get_name(timer));

        mce_wltimer_stop(timer);
        item->data = 0;
    }

    /* Flush timer list */
    mwt_queue_compact();

}