9 #include "../refcount.h"
19 void data_object_delete(DataObject *obj)
21 refcount_dst(&obj->rc);
30 int data_object_compare(const void *pa, const void *pb)
33 ((DataObject *) ((const Refcount *) pa)->obj)->id -
34 ((DataObject *) ((const Refcount *) pb)->obj)->id;
37 int data_object_compare_id(const void *pa, const void *pb)
40 ((DataObject *) ((const Refcount *) pa)->obj)->id -
44 static void *thread_create(unsigned int *result)
47 DataObject *obj = malloc(sizeof *obj);
50 refcount_ini(&obj->rc, obj, (void *) &data_object_delete);
52 if (map_add(&map, &obj->rc, &data_object_compare, (void *) &refcount_inc))
55 refcount_drp(&obj->rc);
63 static void *thread_access(unsigned int *result)
65 DataObject *objs[NUM_OBJS] = {NULL};
68 int i = rand() % NUM_OBJS;
71 refcount_drp(&objs[i]->rc);
75 while (!objs[i] && !cancel) {
77 objs[i] = map_get(&map, &id, &data_object_compare_id, (void *) &refcount_grb);
84 for (int i = 0; i < NUM_OBJS; i++)
86 refcount_drp(&objs[i]->rc);
93 static void *thread_delete(unsigned int *result)
96 unsigned int id = rand_id();
98 if (map_del(&map, &id, &data_object_compare_id, (void *) &refcount_drp, NULL, NULL))
109 printf("------------------------\n");
110 printf("Testing Map and Refcount\n");
111 printf("------------------------\n");
115 void *(*funcs[3])(void *) = {
116 (void *) &thread_create,
117 (void *) &thread_access,
118 (void *) &thread_delete,
121 unsigned int results[3][5] = {0};
122 pthread_t threads[3][5] = {0};
124 for (int i = 0; i < 3; i++)
125 for (int j = 0; j < 5; j++)
126 pthread_create(&threads[i][j], NULL, funcs[i], &results[i][j]);
131 for (int i = 0; i < 3; i++)
132 for (int j = 0; j < 5; j++) {
133 pthread_join(threads[i][j], NULL);
136 results[i][0] += results[i][j];
139 map_cnl(&map, (void *) &refcount_drp, NULL, NULL, 0);
142 printf("Time: 1 second\n");
143 printf("Created objects: %u\n", results[0][0]);
144 printf("Accessed objects: %u\n", results[1][0]);
145 printf("Deleted objects: %u\n", results[2][0]);