Changeset 3ede440 in mainline


Ignore:
Timestamp:
2019-02-01T22:30:53Z (6 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Parents:
1a37496
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2019-02-01 22:12:04)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2019-02-01 22:30:53)
Message:

Change the meaning of counter in fibril mutex

Previously analogous to semaphore tokens, initially 1 and
counting downward into negative numbers.

Now the number of fibrils currently accessing or waiting for access
to the critical sections. Initially 0 (unlocked mutex), and counting
up. Never negative when used correctly. Also consistent with
readers/writers counts in rwlock.

This way is IMO easier to understand.

Location:
uspace/lib/c
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/thread/fibril_synch.c

    r1a37496 r3ede440  
    165165{
    166166        fm->oi.owned_by = NULL;
    167         fm->counter = 1;
     167        fm->counter = 0;
    168168        list_initialize(&fm->waiters);
    169169}
     
    175175        futex_lock(&fibril_synch_futex);
    176176
    177         if (fm->counter-- > 0) {
     177        fm->counter++;
     178
     179        if (fm->counter == 1) {
     180                /* I am the only one here. */
    178181                fm->oi.owned_by = f;
    179182                futex_unlock(&fibril_synch_futex);
     
    196199
    197200        futex_lock(&fibril_synch_futex);
    198         if (fm->counter > 0) {
    199                 fm->counter--;
     201        if (fm->counter == 0) {
     202                fm->counter++;
    200203                fm->oi.owned_by = (fibril_t *) fibril_get_id();
    201204                locked = true;
     
    209212{
    210213        assert(fm->oi.owned_by == (fibril_t *) fibril_get_id());
    211 
    212         if (fm->counter++ < 0) {
     214        assert(fm->counter > 0);
     215
     216        fm->counter--;
     217
     218        if (fm->counter > 0) {
    213219                awaiter_t *wdp = list_pop(&fm->waiters, awaiter_t, link);
    214220                assert(wdp);
  • uspace/lib/c/include/fibril_synch.h

    r1a37496 r3ede440  
    4444typedef struct {
    4545        fibril_owner_info_t oi;  /**< Keep this the first thing. */
    46         int counter;
     46        int counter;  /**< # of fibrils currently waiting for or running in the critical section. */
    4747        list_t waiters;
    4848} fibril_mutex_t;
     
    5353                        .owned_by = NULL \
    5454                }, \
    55                 .counter = 1, \
     55                .counter = 0, \
    5656                .waiters = LIST_INITIALIZER((name).waiters), \
    5757        }
Note: See TracChangeset for help on using the changeset viewer.