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.
24 #include <sys/types.h>
29 #define BUFFER_SIZE 16
31 /* Circular buffer of integers. */
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 */
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
);
53 /* Store an integer in the buffer */
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
;
67 if (b
->writepos
>= BUFFER_SIZE
)
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 */
76 get (struct prodcons
*b
)
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
];
88 if (b
->readpos
>= BUFFER_SIZE
)
90 /* Signal that the buffer is now not full */
91 pthread_cond_signal (&b
->notfull
);
92 pthread_mutex_unlock (&b
->lock
);
96 /* A test program: one thread inserts integers from 1 to 10000,
97 the other reads them and prints them. */
101 struct prodcons buffer
;
104 producer (void *data
)
108 long double a3
= 100.5678943, b3
= 200.578435698;
109 long double c3
, d3
, e3
, f3
;
113 printf("producer pid=%d\n", pid
);
115 for (n
= 0; n
< 10000; n
++)
117 printf ("%d --->\n", n
);
121 system("ps ax | grep ex");
128 sprintf(buf
, "%d%d\n", pid
, pid
);
129 asm volatile ("movups (%0), %%xmm1;"::"r" (buf
):"memory");
139 consumer (void *data
)
144 long double a2
= 10002.5, b2
= 2888883.5;
145 long double c2
, d2
, e2
, f2
;
148 printf("consumer pid=%d\n", pid
);
155 printf ("---> %d\n", d
);
157 system("ps ax | grep ex");
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");
178 pthread_t th_a
, th_b
;
180 double a1
= 1.5, b1
= 2.5;
181 long double c1
, d1
, e1
;
188 /* Create the threads */
189 pthread_create (&th_a
, NULL
, producer
, 0);
190 pthread_create (&th_b
, NULL
, consumer
, 0);
193 /* Wait until producer and consumer finish. */
194 pthread_join (th_a
, &retval
);
195 pthread_join (th_b
, &retval
);