1/*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * This file is licensed under the GPL license.  For the full content
4 * of this license, see the COPYING file at the top level of this
5 * source tree.
6 *
7 * Test pthread_spin_init(pthread_spinlock_t * lock, int pshared)
8 *
9 * pthread_spin_init() shall allocate any resources required to use
10 * the spin lock referenced by 'lock' and initialize the lock to an
11 * unlocked state.
12 *
13 * Steps:
14 * 1.  Initialize a pthread_spinlock_t object 'spinlock' with
15 *     pthread_spin_init()
16 * 2.  Main thread lock 'spinlock', should get the lock
17 * 3.  Main thread unlock 'spinlock'
18 * 4.  Main thread destroy the 'spinlock'
19 */
20
21#define _XOPEN_SOURCE 600
22
23#include <pthread.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <signal.h>
28#include "posixtest.h"
29
30static pthread_spinlock_t spinlock;
31
32int main()
33{
34	int rc = 0;
35	int pshared;
36
37	#ifdef PTHREAD_PROCESS_PRIVATE
38	pshared = PTHREAD_PROCESS_PRIVATE;
39	#else
40 	pshared = -1;
41	#endif
42
43	rc = pthread_spin_init(&spinlock, pshared);
44	if(rc != 0)
45	{
46		printf("Test FAILED:  Error at pthread_spin_init(): %d\n", rc);
47		return PTS_FAIL;
48	}
49
50	printf("main: attempt spin lock\n");
51
52	/* We should get the lock */
53	if(pthread_spin_lock(&spinlock) != 0)
54	{
55		perror("Error: main cannot get spin lock when no one owns the lock\n");
56		return PTS_UNRESOLVED;
57	}
58
59	printf("main: acquired spin lock\n");
60
61	if(pthread_spin_unlock(&spinlock) != 0)
62	{
63		perror("main: Error at pthread_spin_unlock()\n");
64		return PTS_UNRESOLVED;
65	}
66
67	rc = pthread_spin_destroy(&spinlock);
68	if(rc != 0)
69	{
70		printf("Error at pthread_spin_destroy(): %d\n", rc);
71		return PTS_UNRESOLVED;
72	}
73
74	printf("Test PASSED\n");
75	return PTS_PASS;
76}
77