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 /*
     10  * sem_timedwait shall lock the unlocked semaphore and decrement the
     11  * semaphore * value by one.
     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 "1-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 val, sts;
     34 
     35 	if (sem_init(&mysemp, 0, 1) == -1) {
     36 		perror(ERROR_PREFIX "sem_init");
     37 		return PTS_UNRESOLVED;
     38 	}
     39 
     40 	ts.tv_sec = time(NULL);
     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 		return PTS_UNRESOLVED;
     48 	}
     49 
     50 	/* Value of Semaphore */
     51 	if (sem_getvalue(&mysemp, &val) == -1) {
     52 		perror(ERROR_PREFIX "sem_getvalue");
     53 		return PTS_UNRESOLVED;
     54 	}
     55 
     56 	/* Checking if the value of the Semaphore decremented by one */
     57 	if (val == 0 && sts == 0) {
     58 		puts("TEST PASSED");
     59 		sem_destroy(&mysemp);
     60 		return PTS_PASS;
     61 	} else {
     62 		puts("TEST FAILED");
     63 		return PTS_FAIL;
     64 	}
     65 }
     66