1/* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * Created by: rolla.n.selbak REMOVE-THIS AT intel DOT com 4 * This file is licensed under the GPL license. For the full content 5 * of this license, see the COPYING file at the top level of this 6 * source tree. 7 8 * Test that pthread_self() 9 * 10 * Shall return the thread ID of the calling thread. No errors are defined. 11 * 12 * Steps: 13 * 1. Create a new thread. 14 * 2. The function that the thread calls will call pthread_self() and store 15 * the return value (which is the thread ID of the thread calling it) into 16 * a global variable. 17 * 3. Call pthread_equal and verify that the thread IDs are the same. 18 * 4. Verify that the new thread ID is not the same as main thread ID. 19 * 20 */ 21 22#include <pthread.h> 23#include <stdio.h> 24#include "posixtest.h" 25 26pthread_t new_th2; /* Global thread to hold the value of when pthread_self 27 returns from the thread function. */ 28 29void *a_thread_func() 30{ 31 new_th2=pthread_self(); 32 pthread_exit(0); 33 return NULL; 34} 35 36int main() 37{ 38 pthread_t new_th1; 39 40 /* Create a new thread. */ 41 if(pthread_create(&new_th1, NULL, a_thread_func, NULL) != 0) 42 { 43 perror("Error creating thread\n"); 44 return PTS_UNRESOLVED; 45 } 46 47 /* Wait for thread to return */ 48 if(pthread_join(new_th1, NULL) != 0) 49 { 50 perror("Error in pthread_join()\n"); 51 return PTS_UNRESOLVED; 52 } 53 54 /* Call pthread_equal() and pass to it the new thread ID in both 55 * parameters. It should return a non-zero value, indicating that 56 * both thread IDs are equal, and therefore refer to the same 57 * thread. */ 58 if(pthread_equal(new_th1, new_th2) == 0) 59 { 60 printf("Test FAILED\n"); 61 return PTS_FAIL; 62 } 63 if (pthread_equal(new_th1, pthread_self()) != 0) 64 { 65 printf("Test FAILED -- 2 threads have the same ID\n"); 66 return PTS_FAIL; 67 } 68 printf("Test PASSED\n"); 69 return PTS_PASS; 70} 71 72 73