Branch: Tag:

2019-10-14

2019-10-14 12:44:56 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Thread.RWMutex: Complain about recursive READ-READ locks too.

A recursive READ-READ lock would lead to a deadlock if
another thread started an attempt to get a write lock
between the two read locks.

2798:   {    struct object *rwmutex_obj = NULL;    struct rwmutex_storage *rwmutex = NULL; +  struct rwmutex_key_storage *m;       get_all_args("create", args, "%o", &rwmutex_obj);    if (!(rwmutex = get_storage(rwmutex_obj, rwmutex_program))) {    SIMPLE_ARG_TYPE_ERROR("create", 1, "RWMutex");    }    -  if (rwmutex->writers == 1) { -  struct rwmutex_key_storage *m; +     for (m = rwmutex->first_key; m; m = m->next) { -  if (m->owner && (m->owner = Pike_interpreter.thread_state)) { -  /* NB: The only way we can come here is if -  * it is we that have the write lock. +  if ((m->owner == Pike_interpreter.thread_state) && (m->owner)) { +  /* NB: We have a lock already. +  * +  * This is a bad idea, as it may lead to dead-locks +  * if another thread has started an attempt to get +  * a write lock between our two read locks, or if +  * we already hold the write lock.    */    Pike_error("Recursive mutex locks!\n");    }    } -  } +        while (THIS_RWKEY->rwmutex) {    /* Not really a supported operation, but... */