annotate test/testatomic.c @ 5003:3a95a2b93eb3

Updated the atomic API for better use cases
author Sam Lantinga <slouken@libsdl.org>
date Sat, 15 Jan 2011 12:41:59 -0800
parents 9de326b3099c
children 0c72ae7b7cb2
rev   line source
3338
9de326b3099c Fixed bug #817
Sam Lantinga <slouken@libsdl.org>
parents: 3327
diff changeset
1 #include <stdio.h>
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
2 #include "SDL.h"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
3
3201
c297230efc75 Disabling 64 bit atomics operations until I figure out why they do not link.
Bob Pendleton <bob@pendleton.com>
parents: 3199
diff changeset
4 /*
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
5 Absolutely basic tests just to see if we get the expected value
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
6 after calling each function.
3201
c297230efc75 Disabling 64 bit atomics operations until I figure out why they do not link.
Bob Pendleton <bob@pendleton.com>
parents: 3199
diff changeset
7 */
c297230efc75 Disabling 64 bit atomics operations until I figure out why they do not link.
Bob Pendleton <bob@pendleton.com>
parents: 3199
diff changeset
8
3338
9de326b3099c Fixed bug #817
Sam Lantinga <slouken@libsdl.org>
parents: 3327
diff changeset
9 char *
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
10 tf(SDL_bool tf)
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
11 {
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
12 static char *t = "TRUE";
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
13 static char *f = "FALSE";
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
14
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
15 if (tf)
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
16 {
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
17 return t;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
18 }
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
19
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
20 return f;
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
21 }
3338
9de326b3099c Fixed bug #817
Sam Lantinga <slouken@libsdl.org>
parents: 3327
diff changeset
22
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
23 int
3299
975fd903466b int testmmousetable.c the arguments to main() are not compatible with what is required in SDL_main.h
Bob Pendleton <bob@pendleton.com>
parents: 3261
diff changeset
24 main(int argc, char *argv[])
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
25 {
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
26 int value;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
27 SDL_SpinLock lock = 0;
3261
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
28
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
29 SDL_bool tfret = SDL_FALSE;
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
30
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
31 printf("\nspin lock---------------------------------------\n\n");
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
32
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
33 SDL_AtomicLock(&lock);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
34 printf("AtomicLock lock=%d\n", lock);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
35 SDL_AtomicUnlock(&lock);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
36 printf("AtomicUnlock lock=%d\n", lock);
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
37
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
38 printf("\natomic -----------------------------------------\n\n");
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
39
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
40 SDL_atomic_t v;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
41
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
42 SDL_AtomicSet(&v, 0);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
43 tfret = SDL_AtomicSet(&v, 10) == 0;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
44 printf("AtomicSet(10) tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
45 tfret = SDL_AtomicAdd(&v, 10) == 10;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
46 printf("AtomicAdd(10) tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
47
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
48 SDL_AtomicSet(&v, 0);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
49 SDL_AtomicIncRef(&v);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
50 tfret = (SDL_AtomicGet(&v) == 1);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
51 printf("AtomicIncRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
52 SDL_AtomicIncRef(&v);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
53 tfret = (SDL_AtomicGet(&v) == 2);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
54 printf("AtomicIncRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
55 tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
56 printf("AtomicDecRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
57 tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
58 printf("AtomicDecRef() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
59
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
60 SDL_AtomicSet(&v, 10);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
61 tfret = (SDL_AtomicCAS(&v, 0, 20) != 0);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
62 printf("AtomicCAS() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
63 value = SDL_AtomicGet(&v);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
64 tfret = (SDL_AtomicCAS(&v, value, 20) == value);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
65 printf("AtomicCAS() tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
66
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
67 return 0;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
68 }