Home | History | Annotate | Download | only in pthread_rwlock_wrlock
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * This file is licensed under the GPL license.  For the full content
      4  * of this license, see the COPYING file at the top level of this
      5  * source tree.
      6 
      7  * Test pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
      8  *
      9  *	It may fail if:
     10  *	[EDEADLK] The current thread already owns the rwlock for writing or reading.
     11  *
     12  * Steps:
     13  * 1. Create and initialize an rwlock
     14  * 2. Perform a write lock via pthread_rwlock_wrlock()
     15  * 3. Perform a write lock _again_ with pthread_rwlock_wrlock without first unlocking rwlock()
     16  * 4. Test if returns EDEADLK or not.  Note the standard states "may" fail, so the test always
     17  *    passes even if 0 is returned.
     18  *
     19  */
     20 #include <pthread.h>
     21 #include <stdio.h>
     22 #include <stdlib.h>
     23 #include <unistd.h>
     24 #include <errno.h>
     25 #include "posixtest.h"
     26 
     27 int main(void)
     28 {
     29 	static pthread_rwlock_t rwlock;
     30 	int rc;
     31 
     32 	if (pthread_rwlock_init(&rwlock, NULL) != 0) {
     33 		printf("Error at pthread_rwlock_init()\n");
     34 		return PTS_UNRESOLVED;
     35 	}
     36 
     37 	/* Attempt to write lock rwlock, it should return successfully */
     38 
     39 	printf("main: attempt write lock\n");
     40 	if (pthread_rwlock_wrlock(&rwlock) != 0) {
     41 		printf("Error at pthread_rwlock_wrlock()\n");
     42 		return PTS_UNRESOLVED;
     43 	}
     44 	printf("main: acquired write lock\n");
     45 
     46 	/* Now attempt to write lock again without first unlocking rwlock. It may return
     47 	 * EDEADLK, but it may also return successfully. */
     48 
     49 	printf("main: attempt write lock\n");
     50 	rc = pthread_rwlock_wrlock(&rwlock);
     51 
     52 	/* Clean up before we test the return value of pthread_rwlock_wrlock() */
     53 	if (pthread_rwlock_unlock(&rwlock) != 0) {
     54 		printf("Error releasing write lock\n");
     55 		exit(PTS_UNRESOLVED);
     56 	}
     57 
     58 	if (pthread_rwlock_destroy(&rwlock) != 0) {
     59 		printf("Error at pthread_rwlock_destroy()\n");
     60 		return PTS_UNRESOLVED;
     61 	}
     62 
     63 	if (rc != 0) {
     64 		if (rc == EDEADLK) {
     65 			printf("main: correctly got EDEADLK\n");
     66 			printf("Test PASSED\n");
     67 			return PTS_PASS;
     68 		} else {
     69 			printf("Test FAILED: Incorrect return code %d\n", rc);
     70 			return PTS_FAIL;
     71 		}
     72 	}
     73 
     74 	printf("main: acquired write lock\n");
     75 	printf("main: unlock write lock\n");
     76 
     77 	printf
     78 	    ("Test PASSED: Note*: Returned 0 instead of EDEADLK, but standard specified _may_ fail.\n");
     79 	return PTS_PASS;
     80 }
     81