Home | History | Annotate | Download | only in pthread_spin_destroy
      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_spin_destroy(pthread_spinlock_t *lock) may fail if:
      8  *
      9  * [EBUSY] The implementation has detected an attempt to
     10  * initialize or destroy a spin lock while it is in use
     11  * (for example, while being used in a pthread_spin_lock()
     12  * call) by another thread.
     13  *
     14  * Note: This test will always pass
     15  *
     16  * Steps:
     17  * 1.  Initialize a pthread_spinlock_t object 'spinlock' with
     18  *     pthread_spin_init()
     19  * 2.  Main thread lock 'spinlock', should get the lock
     20  * 3.  Create a child thread. The thread call pthread_spin_destroy()
     21  */
     22 
     23 #define _XOPEN_SOURCE 600
     24 #include <pthread.h>
     25 #include <stdio.h>
     26 #include <stdlib.h>
     27 #include <unistd.h>
     28 #include <signal.h>
     29 #include <errno.h>
     30 #include <string.h>
     31 #include "posixtest.h"
     32 
     33 static pthread_spinlock_t spinlock;
     34 
     35 static void *fn_chld(void *arg)
     36 {
     37 	int rc = 0;
     38 
     39 	printf("child: destroy spin lock\n");
     40 	rc = pthread_spin_destroy(&spinlock);
     41 	if (rc == EBUSY) {
     42 		printf("child: correctly got EBUSY\n");
     43 		printf("Test PASSED\n");
     44 	} else {
     45 		printf("child: got return code %d, %s\n", rc, strerror(rc));
     46 		printf
     47 		    ("Test PASSED: *Note: Did not return EBUSY when destroying a spinlock already in use, but standard says 'may' fail\n");
     48 	}
     49 	exit(PTS_PASS);
     50 }
     51 
     52 int main(void)
     53 {
     54 	pthread_t child_thread;
     55 
     56 	if (pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE) != 0) {
     57 		printf("main: Error at pthread_spin_init()\n");
     58 		return PTS_UNRESOLVED;
     59 	}
     60 
     61 	printf("main: attempt spin lock\n");
     62 
     63 	/* We should get the lock */
     64 	if (pthread_spin_lock(&spinlock) != 0) {
     65 		printf("main cannot get spin lock when no one owns the lock\n");
     66 		return PTS_UNRESOLVED;
     67 	}
     68 	printf("main: acquired spin lock\n");
     69 
     70 	printf("main: create thread\n");
     71 	if (pthread_create(&child_thread, NULL, fn_chld, NULL) != 0) {
     72 		printf("main: Error creating child thread\n");
     73 		return PTS_UNRESOLVED;
     74 	}
     75 
     76 	/* Wait for thread to end execution */
     77 	pthread_join(child_thread, NULL);
     78 
     79 	return PTS_PASS;
     80 }
     81