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
29
30 sem_t psem, csem;
31 int n;
32
main()33 int main()
34 {
35 pthread_t prod, cons;
36 void *producer(void *);
37 void *consumer(void *);
38 long cnt = 3;
39
40 n = 0;
41 if (sem_init(&csem, 0, 0) < 0) {
42 perror("sem_init");
43 return PTS_UNRESOLVED;
44 }
45 if (sem_init(&psem, 0, 1) < 0) {
46 perror("sem_init");
47 return PTS_UNRESOLVED;
48 }
49 if (pthread_create(&prod, NULL, producer, (void *)cnt) != 0) {
50 perror("pthread_create");
51 return PTS_UNRESOLVED;
52 }
53 if (pthread_create(&cons, NULL, consumer, (void *)cnt) != 0) {
54 perror("pthread_create");
55 return PTS_UNRESOLVED;
56 }
57
58 if (( pthread_join(prod, NULL) == 0) && ( pthread_join(cons, NULL) == 0)) {
59 puts("TEST PASS");
60 pthread_exit(NULL);
61 if (( sem_destroy(&psem) == 0) &&( sem_destroy (&csem)) == 0 )
62 return PTS_PASS;
63 } else {
64 puts("TEST FAILED");
65 return PTS_FAIL;
66 }
67 }
68
69
producer(void * arg)70 void * producer(void *arg)
71 {
72 int i, cnt;
73 cnt = (long)arg;
74 for (i=0; i<cnt; i++) {
75 sem_wait(&psem);
76 n++;
77 sem_post(&csem);
78 }
79 return NULL;
80 }
81
consumer(void * arg)82 void * consumer(void *arg)
83 {
84 int i, cnt;
85 cnt = (long)arg;
86 for (i=0; i<cnt; i++) {
87 sem_wait(&csem);
88 sem_post(&psem);
89 }
90 return NULL;
91 }
92
93