123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- #include "test.h"
- #include "implement.h"
- #include <sched.h>
- enum
- {
- NUM_THREADS = OS_MAX_SIMUL_THREADS
- };
- static pthread_key_t key = NULL;
- static int accesscount[NUM_THREADS];
- static int thread_set[NUM_THREADS];
- static int thread_destroyed[NUM_THREADS];
- static pthread_barrier_t startBarrier;
- static void
- destroy_key(void * arg)
- {
- int * j = (int *) arg;
- (*j)++;
- assert(*j == 2);
- thread_destroyed[j - accesscount] = 1;
- }
- static void
- setkey(void * arg)
- {
- int * j = (int *) arg;
- thread_set[j - accesscount] = 1;
- assert(*j == 0);
- assert(pthread_setspecific(key, arg) == 0);
- assert(pthread_setspecific(key, arg) == 0);
- assert(pthread_setspecific(key, arg) == 0);
- assert(pthread_getspecific(key) == arg);
- (*j)++;
- assert(*j == 1);
- }
- static void *
- mythread(void * arg)
- {
- (void) pthread_barrier_wait(&startBarrier);
-
-
-
-
-
- assert(pthread_getspecific(key) == NULL);
- setkey(arg);
- return 0;
-
- }
- int pthread_test_tsd1()
- {
- int i;
- int fail = 0;
- pthread_t thread[NUM_THREADS];
-
- thread_set[0] = thread_set[0];
- thread_destroyed[0] = thread_destroyed[0];
- assert(pthread_barrier_init(&startBarrier, NULL, NUM_THREADS/2) == 0);
- for (i = 1; i < NUM_THREADS/2; i++)
- {
- accesscount[i] = thread_set[i] = thread_destroyed[i] = 0;
- assert(pthread_create(&thread[i], NULL, mythread, (void *)&accesscount[i]) == 0);
- }
-
- assert(pthread_key_create(&key, destroy_key) == 0);
- (void) pthread_barrier_wait(&startBarrier);
-
- accesscount[0] = 0;
- setkey((void *) &accesscount[0]);
-
- for (i = NUM_THREADS/2; i < NUM_THREADS; i++)
- {
- accesscount[i] = thread_set[i] = thread_destroyed[i] = 0;
- assert(pthread_create(&thread[i], NULL, mythread, (void *)&accesscount[i]) == 0);
- }
-
- for (i = 1; i < NUM_THREADS; i++)
- {
- int result = 0;
- assert(pthread_join(thread[i], (void **) &result) == 0);
- }
- assert(pthread_barrier_destroy(&startBarrier) == 0);
- for (i = 1; i < NUM_THREADS; i++)
- {
-
- if (accesscount[i] != 2)
- {
- fail++;
- }
- }
- assert(fail == 0);
- return (fail);
- }
|