diff --git a/third_party/shared_mutex/shared_mutex.cc b/third_party/shared_mutex/shared_mutex.cc index ae211a81c3..b1b8dade38 100755 --- a/third_party/shared_mutex/shared_mutex.cc +++ b/third_party/shared_mutex/shared_mutex.cc @@ -78,7 +78,7 @@ static std::vector lsof(const char* filename) { return matched_process; } -shared_mutex_t shared_mutex_init(const char *name, mode_t mode) { +shared_mutex_t shared_mutex_init(const char *name, mode_t mode, bool retried) { shared_mutex_t mutex = {NULL, 0, NULL, 0}; errno = 0; @@ -123,15 +123,6 @@ shared_mutex_t shared_mutex_init(const char *name, mode_t mode) { pthread_mutex_t *mutex_ptr = reinterpret_cast(addr); - // When process crash before unlock the mutex, the mutex is in bad status. - // reset the mutex if no process is using it - std::vector ids = lsof(name); - if (ids.size() == 0) { // no process is using it - memset(mutex_ptr, 0, sizeof(pthread_mutex_t)); - // Set mutex.created == 1 so that it can be initialized latter. - mutex.created = 1; - } - // Make sure the mutex wasn't left in a locked state. If we can't // acquire it in 5 sec., re-do everything. struct timespec expireTime; @@ -161,6 +152,20 @@ shared_mutex_t shared_mutex_init(const char *name, mode_t mode) { } else if (ret || (mutex.created == 0 && reinterpret_cast(addr)->ptr == NULL)) { // Something is out of sync. + + // When process crash before unlock the mutex, the mutex is in bad status. + // reset the mutex if no process is using it, and then retry lock + if (!retried) { + std::vector ids = lsof(name); + if (ids.size() == 0) { // no process is using it + memset(mutex_ptr, 0, sizeof(pthread_mutex_t)); + // Set mutex.created == 1 so that it can be initialized latter. + mutex.created = 1; + free(mutex.name); + return shared_mutex_init(name, mode, true); + } + } + fprintf(stderr, "pthread_mutex_timedlock() returned %d\n", ret); perror("Failed to initialize RSMI device mutex after 5 seconds. Previous " "execution may not have shutdown cleanly. To fix problem, stop all " diff --git a/third_party/shared_mutex/shared_mutex.h b/third_party/shared_mutex/shared_mutex.h index 7fc5ce845e..d04d1d0984 100755 --- a/third_party/shared_mutex/shared_mutex.h +++ b/third_party/shared_mutex/shared_mutex.h @@ -61,7 +61,7 @@ typedef struct shared_mutex_t { // There is no workaround currently, except to run first // initialization only before multi-threaded or multi-process // functionality. -shared_mutex_t shared_mutex_init(const char *name, mode_t mode); +shared_mutex_t shared_mutex_init(const char *name, mode_t mode, bool retried=false); // Close access to the shared mutex and free all the resources, // used by the structure.