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_lock(pthread_spinlock_t *lock) 8 * 9 * The pthread_spin_lock() function may fail if: 10 * [EDEADLK] The calling thread already holds the lock. 11 * 12 * This case will always pass. The thread might keep spin 13 * when re-lock the spin lock. 14 * 15 */ 16 17 #define _XOPEN_SOURCE 600 18 #include <pthread.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <unistd.h> 22 #include <signal.h> 23 #include <errno.h> 24 #include <string.h> 25 #include "posixtest.h" 26 27 static void sig_handler() 28 { 29 printf("main: interrupted by SIGALARM\n"); 30 printf 31 ("Test PASSED: *Note: Did not return EDEADLK when re-locking a spinlock already in use, but standard says 'may' fail\n"); 32 pthread_exit((void *)PTS_PASS); 33 } 34 35 int main(void) 36 { 37 int rc; 38 pthread_spinlock_t spinlock; 39 struct sigaction act; 40 41 /* Set up child thread to handle SIGALRM */ 42 act.sa_flags = 0; 43 act.sa_handler = sig_handler; 44 sigfillset(&act.sa_mask); 45 sigaction(SIGALRM, &act, 0); 46 47 if (pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE) != 0) { 48 printf("main: Error at pthread_spin_init()\n"); 49 return PTS_UNRESOLVED; 50 } 51 52 printf("main: attempt spin lock\n"); 53 54 /* We should get the lock */ 55 if (pthread_spin_lock(&spinlock) != 0) { 56 printf 57 ("Test FAILED: main cannot get spin lock when no one owns the lock\n"); 58 return PTS_FAIL; 59 } 60 printf("main: acquired spin lock\n"); 61 62 printf("main: send SIGALRM to me after 2 secs\n"); 63 alarm(2); 64 65 printf("main: re-lock spin lock\n"); 66 rc = pthread_spin_lock(&spinlock); 67 68 if (rc == EDEADLK) { 69 printf 70 ("main: correctly got EDEADLK when re-locking the spin lock\n"); 71 printf("Test PASSED\n"); 72 } else { 73 printf("main: get return code: %d , %s\n", rc, strerror(rc)); 74 printf 75 ("Test PASSED: *Note: Did not return EDEADLK when re-locking a spinlock already in use, but standard says 'may' fail\n"); 76 } 77 78 /* Unlock spinlock */ 79 pthread_spin_unlock(&spinlock); 80 81 /* Destroy spinlock */ 82 pthread_spin_destroy(&spinlock); 83 84 return PTS_PASS; 85 } 86