Imported Upstream version 20081219
[ltp-debian.git] / testcases / misc / tcore_patch_test_suites / tcore.c
blob5b3959d39586bfd4cbdf719d52181f3639b717fa
1 /*
3 * Copyright (c) Min Guo <min.guo@intel.com>., 2003
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 // Use gcc -o xmm xmm.c -pthread -lm to compile.
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <math.h>
27 #include <pthread.h>
29 #define BUFFER_SIZE 16
31 /* Circular buffer of integers. */
33 struct prodcons
35 int buffer[BUFFER_SIZE]; /* the actual data */
36 pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */
37 int readpos, writepos; /* positions for reading and writing */
38 pthread_cond_t notempty; /* signaled when buffer is not empty */
39 pthread_cond_t notfull; /* signaled when buffer is not full */
42 /* Initialize a buffer */
43 static void
44 init (struct prodcons *b)
46 pthread_mutex_init (&b->lock, NULL);
47 pthread_cond_init (&b->notempty, NULL);
48 pthread_cond_init (&b->notfull, NULL);
49 b->readpos = 0;
50 b->writepos = 0;
53 /* Store an integer in the buffer */
54 static void
55 put (struct prodcons *b, int data)
57 pthread_mutex_lock (&b->lock);
58 /* Wait until buffer is not full */
59 while ((b->writepos + 1) % BUFFER_SIZE == b->readpos)
61 pthread_cond_wait (&b->notfull, &b->lock);
62 /* pthread_cond_wait reacquired b->lock before returning */
64 /* Write the data and advance write pointer */
65 b->buffer[b->writepos] = data;
66 b->writepos++;
67 if (b->writepos >= BUFFER_SIZE)
68 b->writepos = 0;
69 /* Signal that the buffer is now not empty */
70 pthread_cond_signal (&b->notempty);
71 pthread_mutex_unlock (&b->lock);
74 /* Read and remove an integer from the buffer */
75 static int
76 get (struct prodcons *b)
78 int data;
79 pthread_mutex_lock (&b->lock);
80 /* Wait until buffer is not empty */
81 while (b->writepos == b->readpos)
83 pthread_cond_wait (&b->notempty, &b->lock);
85 /* Read the data and advance read pointer */
86 data = b->buffer[b->readpos];
87 b->readpos++;
88 if (b->readpos >= BUFFER_SIZE)
89 b->readpos = 0;
90 /* Signal that the buffer is now not full */
91 pthread_cond_signal (&b->notfull);
92 pthread_mutex_unlock (&b->lock);
93 return data;
96 /* A test program: one thread inserts integers from 1 to 10000,
97 the other reads them and prints them. */
99 #define OVER (-1)
101 struct prodcons buffer;
103 static void *
104 producer (void *data)
106 int n;
107 pid_t pid;
108 long double a3 = 100.5678943, b3 = 200.578435698;
109 long double c3, d3, e3, f3;
110 a3 += b3;
111 a3 *= pow(b3, 2);
112 pid = getpid();
113 printf("producer pid=%d\n", pid);
114 sleep(1);
115 for (n = 0; n < 10000; n++)
117 printf ("%d --->\n", n);
118 put (&buffer, n);
120 if (n==7686) {
121 system("ps ax | grep ex");
122 c3 = pow(pid, pid);
123 d3 = log(pid);
124 e3 = c3 * d3;
125 f3 = c3 / d3;
127 char buf[16];
128 sprintf(buf, "%d%d\n", pid, pid);
129 asm volatile ("movups (%0), %%xmm1;"::"r" (buf):"memory");
131 sleep(1);
134 put (&buffer, OVER);
135 return NULL;
138 static void *
139 consumer (void *data)
141 int d;
142 char *junk = NULL;
143 pid_t pid;
144 long double a2 = 10002.5, b2 = 2888883.5;
145 long double c2, d2, e2, f2;
146 a2 += b2;
147 pid = getpid();
148 printf("consumer pid=%d\n", pid);
149 sleep(1);
150 while (1)
152 d = get (&buffer);
153 if (d == OVER)
154 break;
155 printf ("---> %d\n", d);
156 if (d==7688) {
157 system("ps ax | grep ex");
158 d2 = pid * a2 / b2;
159 e2 = tan(pid);
160 f2 = cos (pid)/d2;
162 char buf[16];
163 char buf1[16];
164 sprintf(buf, "%d%d\n", pid, pid);
165 sprintf(buf1,"%d",d2);
166 asm volatile ("movups (%0), %%xmm2;":: "r" (buf):"memory");
167 asm volatile ("movups (%0), %%xmm5;":: "r" (buf):"memory");
169 *junk = 0;
172 return NULL;
176 main (void)
178 pthread_t th_a, th_b;
179 void *retval;
180 double a1 = 1.5, b1 = 2.5;
181 long double c1, d1, e1;
182 pid_t pid;
183 a1 += b1;
185 pid = getpid();
187 init (&buffer);
188 /* Create the threads */
189 pthread_create (&th_a, NULL, producer, 0);
190 pthread_create (&th_b, NULL, consumer, 0);
192 c1 = exp(pid);
193 /* Wait until producer and consumer finish. */
194 pthread_join (th_a, &retval);
195 pthread_join (th_b, &retval);
196 return 0;