1 /* 2 * Copyright (c) 2003, Intel Corporation. All rights reserved. 3 * Created by: majid.awad 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 9 /* 10 * Test case vrifies sem_destroy shall destroy on intialized semaphore 11 * upon which no threads are currently blocked. 12 */ 13 14 #include <sys/types.h> 15 #include <stdio.h> 16 #include <errno.h> 17 #include <unistd.h> 18 #include <semaphore.h> 19 #include <sys/stat.h> 20 #include <fcntl.h> 21 #include <pthread.h> 22 #include "posixtest.h" 23 24 #define TEST "3-1" 25 #define FUNCTION "sem_destroy" 26 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": " 27 28 sem_t psem, csem; 29 int n; 30 31 int main(void) 32 { 33 pthread_t prod, cons; 34 void *producer(void *); 35 void *consumer(void *); 36 long cnt = 3; 37 38 n = 0; 39 if (sem_init(&csem, 0, 0) < 0) { 40 perror("sem_init"); 41 return PTS_UNRESOLVED; 42 } 43 if (sem_init(&psem, 0, 1) < 0) { 44 perror("sem_init"); 45 return PTS_UNRESOLVED; 46 } 47 if (pthread_create(&prod, NULL, producer, (void *)cnt) != 0) { 48 perror("pthread_create"); 49 return PTS_UNRESOLVED; 50 } 51 if (pthread_create(&cons, NULL, consumer, (void *)cnt) != 0) { 52 perror("pthread_create"); 53 return PTS_UNRESOLVED; 54 } 55 56 if (pthread_join(prod, NULL) == 0 && pthread_join(cons, NULL) == 0) { 57 puts("TEST PASS"); 58 pthread_exit(NULL); 59 if ((sem_destroy(&psem) == 0) && sem_destroy(&csem) == 0) { 60 return PTS_PASS; 61 } else { 62 puts("TEST FAILED"); 63 return PTS_FAIL; 64 } 65 } 66 } 67 68 void *producer(void *arg) 69 { 70 int i, cnt; 71 cnt = (long)arg; 72 for (i = 0; i < cnt; i++) { 73 sem_wait(&psem); 74 n++; 75 sem_post(&csem); 76 } 77 return NULL; 78 } 79 80 void *consumer(void *arg) 81 { 82 int i, cnt; 83 cnt = (long)arg; 84 for (i = 0; i < cnt; i++) { 85 sem_wait(&csem); 86 sem_post(&psem); 87 } 88 return NULL; 89 } 90