Changeset c77cfd8 in mainline


Ignore:
Timestamp:
2022-10-04T19:55:25Z (2 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e0e612b
Parents:
3fd38b2
Message:

Proper timer cleanup, unit tests

Destroying a timer that sets itself again (i.e., runs periodically) is
a little tricky. It requires a handshake (similar to destroying a worker
thread). Realizing that this handshake works correctly is a little bit
mind boggling.

Location:
uspace/app/taskbar
Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/taskbar/clock.c

    r3fd38b2 rc77cfd8  
    8989        }
    9090
     91        fibril_mutex_initialize(&clock->lock);
     92        fibril_condvar_initialize(&clock->timer_done_cv);
    9193        fibril_timer_set(clock->timer, 1000000, taskbar_clock_timer, clock);
    9294
     
    106108void taskbar_clock_destroy(taskbar_clock_t *clock)
    107109{
     110        /*
     111         * Signal to the timer that we are cleaning up. If the timer handler
     112         * misses it and sets the timer again, we will clear that active
     113         * timer and be done (and if we were even slower and the timer
     114         * fired again, it's the same situation as before.
     115         */
     116        fibril_mutex_lock(&clock->lock);
     117        clock->timer_cleanup = true;
     118        fibril_mutex_unlock(&clock->lock);
     119
     120        /* If we catch the timer while it's active, there's nothing to do. */
     121        if (fibril_timer_clear(clock->timer) != fts_active) {
     122                /* Need to wait for timer handler to finish */
     123                fibril_mutex_lock(&clock->lock);
     124                while (clock->timer_done == false)
     125                        fibril_condvar_wait(&clock->timer_done_cv, &clock->lock);
     126                fibril_mutex_unlock(&clock->lock);
     127        }
     128
    108129        fibril_timer_destroy(clock->timer);
    109130        ui_control_delete(clock->control);
     
    302323        taskbar_clock_t *clock = (taskbar_clock_t *) arg;
    303324
     325        fibril_mutex_lock(&clock->lock);
    304326        (void) taskbar_clock_paint(clock);
    305         fibril_timer_set(clock->timer, 1000000, taskbar_clock_timer, clock);
     327
     328        if (!clock->timer_cleanup) {
     329                fibril_timer_set(clock->timer, 1000000, taskbar_clock_timer,
     330                    clock);
     331        } else {
     332                /* Acknowledge timer cleanup */
     333                clock->timer_done = true;
     334                fibril_condvar_signal(&clock->timer_done_cv);
     335        }
     336
     337        fibril_mutex_unlock(&clock->lock);
    306338}
    307339
  • uspace/app/taskbar/meson.build

    r3fd38b2 rc77cfd8  
    3333        'taskbar.c',
    3434)
     35
     36test_src = files(
     37        'clock.c',
     38        'taskbar.c',
     39        'test/clock.c',
     40        'test/main.c',
     41        'test/taskbar.c',
     42)
  • uspace/app/taskbar/taskbar.c

    r3fd38b2 rc77cfd8  
    9292        rc = ui_get_rect(taskbar->ui, &scr_rect);
    9393        if (rc != EOK) {
    94                 printf("Error getting screen dimensions.\n");
    95                 goto error;
     94                if (str_cmp(display_spec, UI_DISPLAY_NULL) != 0) {
     95                        printf("Error getting screen dimensions.\n");
     96                        goto error;
     97                }
     98
     99                /* For the sake of unit tests */
     100                scr_rect.p0.x = 0;
     101                scr_rect.p0.y = 0;
     102                scr_rect.p1.x = 100;
     103                scr_rect.p1.y = 100;
    96104        }
    97105
     
    194202void taskbar_destroy(taskbar_t *taskbar)
    195203{
     204        ui_fixed_remove(taskbar->fixed,  taskbar_clock_ctl(taskbar->clock));
    196205        taskbar_clock_destroy(taskbar->clock);
    197206        ui_window_destroy(taskbar->window);
  • uspace/app/taskbar/types/clock.h

    r3fd38b2 rc77cfd8  
    5555        gfx_rect_t rect;
    5656
     57        /** Clock lock */
     58        fibril_mutex_t lock;
     59
    5760        /** Clock update timer */
    5861        fibril_timer_t *timer;
     62
     63        /** Signal to timer that we are cleaning up */
     64        bool timer_cleanup;
     65
     66        /** Signal to maih thread that timer is done */
     67        bool timer_done;
     68
     69        /** Timer done condition variable */
     70        fibril_condvar_t timer_done_cv;
    5971} taskbar_clock_t;
    6072
Note: See TracChangeset for help on using the changeset viewer.