Home | History | Annotate | Download | only in pthread_spin_init
      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 
     30 static pthread_spinlock_t spinlock;
     31 
     32 int main(void)
     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 		printf("Test FAILED:  Error at pthread_spin_init(): %d\n", rc);
     46 		return PTS_FAIL;
     47 	}
     48 
     49 	printf("main: attempt spin lock\n");
     50 
     51 	/* We should get the lock */
     52 	if (pthread_spin_lock(&spinlock) != 0) {
     53 		perror
     54 		    ("Error: main cannot get spin lock when no one owns the lock\n");
     55 		return PTS_UNRESOLVED;
     56 	}
     57 
     58 	printf("main: acquired spin lock\n");
     59 
     60 	if (pthread_spin_unlock(&spinlock) != 0) {
     61 		perror("main: Error at pthread_spin_unlock()\n");
     62 		return PTS_UNRESOLVED;
     63 	}
     64 
     65 	rc = pthread_spin_destroy(&spinlock);
     66 	if (rc != 0) {
     67 		printf("Error at pthread_spin_destroy(): %d\n", rc);
     68 		return PTS_UNRESOLVED;
     69 	}
     70 
     71 	printf("Test PASSED\n");
     72 	return PTS_PASS;
     73 }
     74