sem_d.c revision 256281
1169695Skan/**************************************************************************** 2169695Skan * 3169695Skan * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>. 4169695Skan * All rights reserved. 5169695Skan * 6169695Skan * Redistribution and use in source and binary forms, with or without 7169695Skan * modification, are permitted provided that the following conditions 8169695Skan * are met: 9169695Skan * 1. Redistributions of source code must retain the above copyright 10169695Skan * notice(s), this list of conditions and the following disclaimer as 11169695Skan * the first lines of this file unmodified other than the possible 12169695Skan * addition of one or more copyright notices. 13169695Skan * 2. Redistributions in binary form must reproduce the above copyright 14169695Skan * notice(s), this list of conditions and the following disclaimer in 15169695Skan * the documentation and/or other materials provided with the 16169695Skan * distribution. 17169695Skan * 18169695Skan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 19169695Skan * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20169695Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21169695Skan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE 22169695Skan * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23169695Skan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24169695Skan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 25169695Skan * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26169695Skan * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 27169695Skan * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 28169695Skan * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29169695Skan * 30169695Skan **************************************************************************** 31169695Skan * 32169695Skan * sem test. 33169695Skan * 34169695Skan * $FreeBSD: stable/10/lib/libkse/test/sem_d.c 76911 2001-05-20 23:11:09Z jasone $ 35169695Skan * 36169695Skan ****************************************************************************/ 37169695Skan 38169695Skan#include <assert.h> 39169695Skan#include <stdio.h> 40169695Skan#include <fcntl.h> 41169695Skan#include <errno.h> 42169695Skan#include <semaphore.h> 43169695Skan#include <pthread.h> 44169695Skan 45169695Skan#define NTHREADS 10 46169695Skan 47169695Skanvoid * 48169695Skanentry(void * a_arg) 49169695Skan{ 50169695Skan sem_t * sem = (sem_t *) a_arg; 51169695Skan 52169695Skan sem_wait(sem); 53169695Skan fprintf(stderr, "Got semaphore\n"); 54169695Skan 55169695Skan return NULL; 56169695Skan} 57169695Skan 58169695Skanint 59169695Skanmain() 60169695Skan{ 61169695Skan sem_t sem_a, sem_b; 62169695Skan pthread_t threads[NTHREADS]; 63169695Skan unsigned i; 64169695Skan int val; 65169695Skan 66169695Skan fprintf(stderr, "Test begin\n"); 67169695Skan 68169695Skan#ifdef _LIBC_R_ 69169695Skan assert(-1 == sem_init(&sem_b, 1, 0)); 70169695Skan assert(EPERM == errno); 71169695Skan#endif 72169695Skan 73169695Skan assert(0 == sem_init(&sem_b, 0, 0)); 74169695Skan assert(0 == sem_getvalue(&sem_b, &val)); 75169695Skan assert(0 == val); 76169695Skan 77169695Skan assert(0 == sem_post(&sem_b)); 78169695Skan assert(0 == sem_getvalue(&sem_b, &val)); 79 assert(1 == val); 80 81 assert(0 == sem_wait(&sem_b)); 82 assert(-1 == sem_trywait(&sem_b)); 83 assert(EAGAIN == errno); 84 assert(0 == sem_post(&sem_b)); 85 assert(0 == sem_trywait(&sem_b)); 86 assert(0 == sem_post(&sem_b)); 87 assert(0 == sem_wait(&sem_b)); 88 assert(0 == sem_post(&sem_b)); 89 90#ifdef _LIBC_R_ 91 assert(SEM_FAILED == sem_open("/foo", O_CREAT | O_EXCL, 0644, 0)); 92 assert(ENOSYS == errno); 93 94 assert(-1 == sem_close(&sem_b)); 95 assert(ENOSYS == errno); 96 97 assert(-1 == sem_unlink("/foo")); 98 assert(ENOSYS == errno); 99#endif 100 101 assert(0 == sem_destroy(&sem_b)); 102 103 assert(0 == sem_init(&sem_a, 0, 0)); 104 105 for (i = 0; i < NTHREADS; i++) { 106 pthread_create(&threads[i], NULL, entry, (void *) &sem_a); 107 } 108 109 for (i = 0; i < NTHREADS; i++) { 110 assert(0 == sem_post(&sem_a)); 111 } 112 113 for (i = 0; i < NTHREADS; i++) { 114 pthread_join(threads[i], NULL); 115 } 116 117 for (i = 0; i < NTHREADS; i++) { 118 pthread_create(&threads[i], NULL, entry, (void *) &sem_a); 119 } 120 121 for (i = 0; i < NTHREADS; i++) { 122 assert(0 == sem_post(&sem_a)); 123 } 124 125 for (i = 0; i < NTHREADS; i++) { 126 pthread_join(threads[i], NULL); 127 } 128 129 assert(0 == sem_destroy(&sem_a)); 130 131 fprintf(stderr, "Test end\n"); 132 return 0; 133} 134