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/* This tests case will open a locked semaphore.  The time will tick 5 times
10 * until the absolute time passes.  The sempahore will unlock, then the
11 * sem_timedwait call will immediately lock again.
12 */
13
14#define _XOPEN_SOURCE 600
15
16#include <stdio.h>
17#include <fcntl.h>
18#include <sys/stat.h>
19#include <sys/types.h>
20#include <strings.h>
21#include <semaphore.h>
22#include <time.h>
23#include <unistd.h>
24#include "posixtest.h"
25
26
27#define TEST "3-1"
28#define FUNCTION "sem_timedwait"
29#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
30
31
32int main(){
33
34    struct timespec ts;
35    sem_t mysemp;
36    int i=0;
37    int val;
38
39	if ( sem_init (&mysemp, 0, 0) == -1 ) {
40		perror(ERROR_PREFIX "sem_init");
41		return PTS_UNRESOLVED;
42	}
43
44	ts.tv_sec=time(NULL);
45	ts.tv_nsec=0;
46
47	while (sem_timedwait(&mysemp, &ts) == -1 )
48	{
49		ts.tv_sec += 1;
50//		printf("%s \n", asctime(localtime(&ts.tv_sec)));
51		i++;
52//		printf("i=%d\n",i);
53		if (i==5) {
54			sem_post(&mysemp);
55		}
56	}
57
58	/* Value of Semaphore */
59	if( sem_getvalue(&mysemp, &val) == -1 ) {
60		perror(ERROR_PREFIX "sem_getvalue");
61		return PTS_UNRESOLVED;
62	}
63
64	/* Checking if the value of the Semaphore after lock & unlock */
65	if( val == 0 )  {
66		puts("TEST PASSED: Sem unlocked after 5 timeouts");
67		sem_destroy(&mysemp);
68		return PTS_PASS;
69	} else {
70		puts("TEST FAILED");
71		return PTS_FAIL;
72	}
73}
74