Home | History | Annotate | Download | only in sem_timedwait
      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 /* sem_timedwait shall return zero if the calling process successfully
     10  * performed the semaphore lock operation on the semaphore designated
     11  * by sem.
     12  */
     13 
     14 #define _XOPEN_SOURCE 600
     15 
     16 #include <stdio.h>
     17 #include <errno.h>
     18 #include <unistd.h>
     19 #include <semaphore.h>
     20 #include <sys/stat.h>
     21 #include <fcntl.h>
     22 #include <time.h>
     23 #include "posixtest.h"
     24 
     25 #define TEST "4-1"
     26 #define FUNCTION "sem_timedwait"
     27 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     28 
     29 int main(void)
     30 {
     31 	sem_t mysemp;
     32 	struct timespec ts;
     33 	int unresolved = 0, sts;
     34 
     35 	if (sem_init(&mysemp, 0, 1) == -1) {
     36 		perror(ERROR_PREFIX "sem_init");
     37 		unresolved = 1;
     38 	}
     39 
     40 	ts.tv_sec = time(NULL) + 1;
     41 	ts.tv_nsec = 0;
     42 
     43 	/* Lock Semaphore */
     44 	sts = sem_timedwait(&mysemp, &ts);
     45 	if (sts == -1) {
     46 		perror(ERROR_PREFIX "sem_timedwait");
     47 		unresolved = 1;
     48 	}
     49 
     50 	/* unlock Semaphore */
     51 	if (sem_post(&mysemp) == -1) {
     52 		perror(ERROR_PREFIX "sem_post");
     53 		unresolved = 1;
     54 	}
     55 
     56 	if ((sts == 0) && (unresolved == 0)) {
     57 		puts("TEST PASSED");
     58 		sem_destroy(&mysemp);
     59 		return PTS_PASS;
     60 	} else {
     61 		puts("TEST FAILED");
     62 		return PTS_FAIL;
     63 	}
     64 }
     65