[ipc/ipcdev.git] / qnx / src / ipc3x_dev / ti / syslink / samples / hlos / hwspinlock / usr / HWSpinlock.c
1 /*
2 * @file HWSpinlock.c
3 *
4 * @brief Sample app demonstrating Hardware SpinLocks API usage.
5 *
6 *
7 * @ver 02.00.00.46_alpha1
8 *
9 * ============================================================================
10 *
11 * Copyright (c) 2008-20012, Texas Instruments Incorporated
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * * Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * * Neither the name of Texas Instruments Incorporated nor the names of
25 * its contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
32 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
35 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
37 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
38 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * Contact information for paper mail:
40 * Texas Instruments
41 * Post Office Box 655303
42 * Dallas, Texas 75265
43 * Contact information:
44 * http://www-k.ext.ti.com/sc/technical-support/product-information-centers.htm?
45 * DCMP=TIHomeTracking&HQS=Other+OT+home_d_contact
46 * ============================================================================
47 *
48 */
51 #include <stdio.h>
52 #include "GateHWSpinlock.h"
53 #include "HwSpinLockUsr.h"
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include <string.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <pthread.h>
60 #include <semaphore.h>
61 #include <sys/procmgr.h>
62 #include <stdint.h>
63 #include <fcntl.h>
64 #include <stdbool.h>
66 #define MAX_LOCKS 128
67 struct{
68 int handleID;
69 int* key;
70 }locks[MAX_LOCKS];
72 void testHWSpinlock(char* str, int token)
73 {
74 int status, *key;
75 int count = 100;
76 key = HwSpinlock_enter(token);
77 if(key != (int *)(-1)){
78 printf( "\n%s=> lock (%d) entered\n", str, token);
79 /*execute some instruction instead of sleep()*/
80 while(--count);
81 status = HwSpinlock_leave(key, token);
82 if(!status){
83 printf( "\n%s=> return from HwSpinlock_leave(%d)\n", str, token);
84 }
85 else {
86 printf( "\n%s=> HwSpinlock_leave(%d) FAILED\n", str, token);
87 }
88 }
89 else {
90 printf( "\n%s=> HwSpinlock_enter(%d) FAILED\n", str, token);
91 }
92 }
94 int main()
95 {
96 int token0, token1, token2;
97 int status;
99 token0 = HwSpinlock_create(0, GateHWSpinlock_LocalProtect_NONE);
100 if(token0 != -1) {
101 printf( "\nPROC=> HwSpinlock_create(0) CREATED\n");
102 testHWSpinlock("PROC", token0);
103 status = HwSpinlock_delete(token0);
104 printf("HwSpinlock_delete(token0): %s\n", status?"FAILED":"PASSED");
105 }
106 else {
107 printf( "\nPROC=> HwSpinlock_create(0) FAIELD\n");
108 }
109 token1 = HwSpinlock_create(1, GateHWSpinlock_LocalProtect_INTERRUPT);
110 if(token1 != -1) {
111 printf( "\nPROC=> HwSpinlock_create(1) CREATED\n");
112 testHWSpinlock("PROC", token1);
113 status = HwSpinlock_delete(token1);
114 printf("HwSpinlock_delete(token1): %s\n", status?"FAILED":"PASSED");
115 }
116 else {
117 printf( "\nPROC=> HwSpinlock_create(1) FAIELD\n");
118 }
119 token2 = HwSpinlock_create(2, GateHWSpinlock_LocalProtect_THREAD);
120 if(token2 != -1) {
121 printf( "\nPROC=> HwSpinlock_create(1) CREATED\n");
122 testHWSpinlock("PROC", token2);
123 status = HwSpinlock_delete(token2);
124 printf("HwSpinlock_delete(token2): %s\n", status?"FAILED":"PASSED");
125 }
126 else {
127 printf( "\nPROC=> HwSpinlock_create(1) FAIELD\n");
128 }
129 return 0;
130 }