From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 39992 invoked by alias); 21 Aug 2017 02:31:15 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 7498 invoked by uid 89); 21 Aug 2017 02:30:06 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-8.4 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,GIT_PATCH_2,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,SPF_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:8284, commented, ALSO, kindly X-HELO: mail-wr0-f195.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=2Qr5zFAtTPRflrHrgsgMk2+wH4oR/OwNk9iWckMi+P0=; b=eZuokk9ybIzKL3h1ZZACJD0SkXqQgBXhZcMsHV5f0PXXGvEkhv2sDkkk+IBaoJaMqb zRBWDWjWHj0DJcDhyx+cqKm5svKA+ZcrryErEj4b2wxTY6wfvmgZOk4A/gSbYBL6MAK2 eu1V/cRgS5uCDuJbw6B5YSgJ1NeXea5u9wpVby0QMUSmQHnEp3rQm/dZO1ylh7IW/344 whB2zLzzBW9Nh1gIb+Gt/3ZXZpQrp3elPsCe4/RcCq/49jYLzv2SJF3G9cGDIKrPz6sF immL2b3pNnYOANJj4zzg+PGMIpDoBBR07IDw+XBCBPSVc0rDebQhbBPXCUQEAAK3rM6O IT+g== X-Gm-Message-State: AHYfb5hTihr5/VGW7Fk/PoqeFF50JXhl/l2seoa5zExCCwmkh8Spmyda AW4SoGPh5t0rDyX3kfF7zjrpu0G4LA== X-Received: by 10.80.147.197 with SMTP id o63mr10839708eda.194.1503282602350; Sun, 20 Aug 2017 19:30:02 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Yubin Ruan Date: Mon, 21 Aug 2017 02:31:00 -0000 Message-ID: Subject: Re: [PATCTH 0/2] pthread_mutexattr_setrobust() and pthread_mutex_consistent() To: "Michael Kerrisk (man-pages)" Cc: linux-man@vger.kernel.org, libc-alpha@sourceware.org Content-Type: multipart/mixed; boundary="f403045c809e82af3305573a417f" X-SW-Source: 2017-08/txt/msg00945.txt.bz2 --f403045c809e82af3305573a417f Content-Type: text/plain; charset="UTF-8" Content-length: 6073 Here are the man page in plain text, in case you want to read it directly in the email. FYI, I obtain information about these API mainly from reading the source and testing. If you find anything wrong, please kindly reply and point it out. Many thanks! Yubin --- pthread_mutexattr_setrobust() and pthread_mutexattr_getrobust() --- Name pthread_mutexattr_getrobust, pthread_mutexattr_setrobust, pthread_mutexattr_getrobust_np, pthread_mutexattr_setrobust_np -- get and set the robustness attribute of a mutex attribute object SYNOPSIS #include int pthread_mutexattr_getrobust(const pthread_mutexattr_t *attr, int *robustness); int pthread_mutexattr_setrobust(const pthread_mutexattr_t *attr, int *robustness); int pthread_mutexattr_getrobust_np(const pthread_mutexattr_t *attr, int *robustness); int pthread_mutexattr_setrobust_np(const pthread_mutexattr_t *attr, int *robustness); Compile and link with -pthread. DESCRIPTION The pthread_mutexattr_getrobust() and pthread_mutexattr_setrobust() functions get and set the robustness attribute of a initialized mutex attribute object, respectively. The robustness attribute specify the behavior of the mutex when its owner dies without unlocking it. These two functions are specified in POSIX. Glibc's NPTL has pthread_mutexattr_getrobust_np() and pthread_mutexattr_setrobust_np() respectively but they are just aliases, with the "np" standing for "Native Posix". See NPTL(7). Currently there are only two possible values for the robustness attribute: PTHREAD_MUTEX_STALLED is the default value for a mutex attribute object. If a mutex is initialized with a PTHREAD_MUTEX_STALLED attribute object and its owner dies without unlocking it, it is kept locked afterwards and any future attempts to call pthread_mutex_lock() on this mutex will block indefinitely. PTHREAD_MUTEX_ROBUST can be set on a mutex attribute object so that when the owner of the mutex dies or when the process containing such a locked mutex performs execve(2), any future attempts to call pthread_mutex_lock() on this mutex will suceed and return EOWNERDEAD to indicate that the original owner no longer exists and the mutex is left in an inconsisten state. After EOWNERDEAD is returned, the next owner should call pthread_mutex_consistent(3) on the acquired mutex to make it consistent again before using it any further. If the next owner calls pthread_mutex_unlock(3) before making it consistent, the mutex will be unusable permanently and any subsequent attempts to lock it using pthread_mutex_lock(3) will return ENORECOVERABLE. If the next owner terminates before calling pthread_mutex_consistent(3), furture pthread_mutex_lock(3) on this mutex will still return EOWNERDEAD. Glibc defined PTHREAD_MUTEX_STALLED_NP and PTHREAD_MUTEX_ROBUST_NP as aliases of PTHREAD_MUTEX_STALLED and PTHREAD_MUTEX_ROBUST respectively. Note that the *attr* argument of pthread_mutexattr_getrobust() and pthread_mutexattr_setrobust() should refer to a mutex attribute object that was initialized by pthread_mutexattr_init(3), otherwise the behavior is undefined. RETURN VALUE On success, zero is returned by pthread_mutexattr_getrobust() and the value pointed to by the *robustness* parameter is set to the robustness attribute of *attr*. Otherwise, an error number shall be returned. On success pthread_mutexattr_setrobust() set the robustness attribute into the mutex attribute object *attr* and return zero, otherwise a error number is returned to indicate the error. [Glibc-specificed features] In glibc's implementation, pthread_mutexattr_getrobust() always return zero. ERRORS EINVAL A value other than PTHREAD_MUTEX_STALLED or PTHREAD_MUTEX_ROBUST is passed into pthread_mutexattr_setrobust(). EXAMPLES #include #include #include #include pthread_mutex_t lock; void *original_owner_thread(void *ptr) { printf("[original owner] Setting lock...\n"); pthread_mutex_lock(&lock); printf("[original owner] Locked. Now exiting without unlocking.\n"); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t lock_getter; pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); /* initialize the attribute object */ pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST); /* set robustness */ pthread_mutex_init(&lock, &attr); /* initialize the lock */ pthread_create(&lock_getter, NULL, original_owner_thread, NULL); sleep(2); /* original_owner_thread should have exited now */ printf("Attempting to acquire unlock robust mutex.\n"); int ret_code = pthread_mutex_lock(&lock); if(EOWNERDEAD == ret_code) { printf("EOWNERDEAD returned. Make the mutex consistent now\n"); pthread_mutex_consistent(&lock); } pthread_mutex_unlock(&lock); return 0; } SEE ALSO pthread_mutexattr_init(3), pthread_mutex_consistent(3), pthread_mutex_lock(3) --- END --- --- pthread_mutex_consistent() --- NAME pthread_mutex_consistent - make the robust mutex consistent SYNOPSIS #include int pthread_mutex_consistent(pthread_mutex_t *mutex) Compile and link with -pthread. DESCRIPTION This function make a robust mutex consistent if it is in a inconsistent state. A mutex can be left in a inconsistent state if its owner terminate while holding the mutex, in which situation the next owner who acquire the mutex will succeed and be notified by the return value of EOWNERDEAD. RETURN VALUE On success, pthread_mutex_consistent() return 0. Otherwise an error value is returned to indicate the error. ERRORS EINVAL The mutex is either not robust or is not in a inconsistent state. SEE ALSO pthread_mutex_lock(3), pthread_mutexattr_init(3), pthread_mutexattr_setrobust(3), pthread_mutexattr_getrobust(3) ---- END --- --f403045c809e82af3305573a417f Content-Type: text/x-csrc; charset="US-ASCII"; name="verify_robustness.1.c" Content-Disposition: attachment; filename="verify_robustness.1.c" Content-Transfer-Encoding: base64 X-Attachment-Id: f_j6lyfpsp0 Content-length: 1741 LyoKICogVGhpcyBwcm9ncmFtIGlzIHVzZWQgdG8gdmVyaWZ5IHRoYXQgd2hl biB0aGUgb3duZXIgb2YgYSBub24tcm9idXN0IGxvY2sgZXhpdAogKiBwcmVt YXR1cmVseSB3aXRob3V0IHVubG9ja2luZyB0aGUgbXV0ZXgsIHRoZSBuZXh0 IG93bmVyIHdpbGwgYmUgYmxvY2sKICogaW5kZWZpbnRlbHkuCiAqCiAqIENv bXBpbGUgYW5kIGxpbmsgd2l0aCAtcHRocmVhZAogKi8KCiNpbmNsdWRlIDxz dGRpby5oPgojaW5jbHVkZSA8dW5pc3RkLmg+CiNpbmNsdWRlIDxwdGhyZWFk Lmg+CiNpbmNsdWRlIDxlcnJuby5oPg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AgICBwcmlu dGYoIkVPV05FUkRFQUQgcmV0dXJuZWQuIE1ha2UgdGhlIG11dGV4IGNvbnNp c3RlbnQgbm93XG4iKTsKICAgICAgICBwdGhyZWFkX211dGV4X2NvbnNpc3Rl bnQoJmxvY2spOwogICAgfQoKICAgIHB0aHJlYWRfbXV0ZXhfdW5sb2NrKCZs b2NrKTsKCiAgICByZXR1cm4gMDsKfQo= --f403045c809e82af3305573a417f Content-Type: text/x-csrc; charset="US-ASCII"; name="verify_robustness.2.c" Content-Disposition: attachment; filename="verify_robustness.2.c" Content-Transfer-Encoding: base64 X-Attachment-Id: f_j6lyfpt31 Content-length: 1879 LyoKICogVGhpcyBwcm9ncmFtIGlzIHVzZWQgdG8gdmVyaWZ5IHRoYXQgd2hl biB0aGUgb3duZXIgb2YgYSBub24tcm9idXN0IGxvY2sgZXhpdAogKiBwcmVt YXR1cmVseSB3aXRob3V0IHVubG9ja2luZyB0aGUgbXV0ZXgsIHRoZSBuZXh0 IG93bmVyIHdpbGwgYmUgYmxvY2sKICogaW5kZWZpbnRlbHkuCiAqLwoKI2lu Y2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDx1bmlzdGQuaD4KI2luY2x1ZGUg PHB0aHJlYWQuaD4KI2luY2x1ZGUgPGVycm5vLmg+CgpwdGhyZWFkX211dGV4 X3QgbG9jazsKCnZvaWQgKm9yaWdpbmFsX293bmVyX3RocmVhZCh2b2lkICpw dHIpCnsKICAgIHByaW50ZigiW29yaWdpbmFsIG93bmVyXSBTZXR0aW5nIGxv Y2suLi5cbiIpOwogICAgcHRocmVhZF9tdXRleF9sb2NrKCZsb2NrKTsKICAg IHByaW50ZigiW29yaWdpbmFsIG93bmVyXSBMb2NrZWQuIE5vdyBleGl0aW5n IHdpdGhvdXQgdW5sb2NraW5nLlxuIik7CiAgICBwdGhyZWFkX2V4aXQoTlVM TCk7Cn0KCmludCBtYWluKGludCBhcmdjLCBjaGFyICphcmd2W10pIAp7CiAg ICBwdGhyZWF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gIHJldHVybiAwOwp9Cg== --f403045c809e82af3305573a417f--