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_destroy(pthread_spinlock_t *lock) may fail if:
8 *
9 * [EBUSY] The implementation has detected an attempt to
10 * initialize or destroy a spin lock while it is in use
11 * (for example, while being used in a pthread_spin_lock( )
12 * call) by another thread.
13 *
14 * Note: This test will always pass
15 *
16 * Steps:
17 * 1.  Initialize a pthread_spinlock_t object 'spinlock' with
18 *     pthread_spin_init()
19 * 2.  Main thread lock 'spinlock', should get the lock
20 * 3.  Create a child thread. The thread call pthread_spin_destroy()
21 */
22
23#define _XOPEN_SOURCE 600
24#include <pthread.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <signal.h>
29#include <errno.h>
30#include <string.h>
31#include "posixtest.h"
32
33static pthread_spinlock_t spinlock;
34
35static void* fn_chld(void *arg)
36{
37	int rc = 0;
38
39	printf("child: destroy spin lock\n");
40	rc = pthread_spin_destroy(&spinlock);
41	if(rc == EBUSY)
42	{
43		printf("child: correctly got EBUSY\n");
44		printf("Test PASSED\n");
45	}
46	else
47	{
48		printf("child: got return code %d, %s\n", rc, strerror(rc));
49		printf("Test PASSED: *Note: Did not return EBUSY when destroying a spinlock already in use, but standard says 'may' fail\n");
50	}
51	exit(PTS_PASS);
52}
53
54int main()
55{
56	pthread_t child_thread;
57
58	if(pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE) != 0)
59	{
60		printf("main: Error at pthread_spin_init()\n");
61		return PTS_UNRESOLVED;
62	}
63
64	printf("main: attempt spin lock\n");
65
66	/* We should get the lock */
67	if(pthread_spin_lock(&spinlock) != 0)
68	{
69		printf("main cannot get spin lock when no one owns the lock\n");
70		return PTS_UNRESOLVED;
71	}
72	printf("main: acquired spin lock\n");
73
74	printf("main: create thread\n");
75	if(pthread_create(&child_thread, NULL, fn_chld, NULL) != 0)
76	{
77		printf("main: Error creating child thread\n");
78		return PTS_UNRESOLVED;
79	}
80
81	/* Wait for thread to end execution */
82	pthread_join(child_thread, NULL);
83
84	return PTS_PASS;
85}
86