9 #include "../refcount.h"
19 void *data_object_delete(DataObject *obj)
21 refcount_dst(&obj->rc);
31 int data_object_compare(const void *pa, const void *pb)
34 ((DataObject *) ((const Refcount *) pa)->obj)->id -
35 ((DataObject *) ((const Refcount *) pb)->obj)->id;
38 int data_object_compare_id(const void *pa, const void *pb)
41 ((DataObject *) ((const Refcount *) pa)->obj)->id -
45 static void *thread_create(unsigned int *result)
48 DataObject *obj = malloc(sizeof *obj);
51 refcount_ini(&obj->rc, obj, (void *) &data_object_delete);
53 if (map_add(&map, &obj->rc, &data_object_compare, &refcount_inc) == &obj->rc)
56 refcount_drp(&obj->rc);
64 static void *thread_access(unsigned int *result)
66 DataObject *objs[NUM_OBJS] = {NULL};
69 int i = rand() % NUM_OBJS;
72 refcount_drp(&objs[i]->rc);
76 while (! objs[i] && ! cancel) {
78 objs[i] = map_get(&map, &id, &data_object_compare_id, &refcount_grb);
85 for (int i = 0; i < NUM_OBJS; i++)
87 refcount_drp(&objs[i]->rc);
94 static void *thread_delete(unsigned int *result)
97 unsigned int id = rand_id();
99 if (map_del(&map, &id, &data_object_compare_id, &refcount_drp))
110 printf("------------------------\n");
111 printf("Testing Map and Refcount\n");
112 printf("------------------------\n");
116 void *(*funcs[3])(void *) = {
117 (void *) &thread_create,
118 (void *) &thread_access,
119 (void *) &thread_delete,
122 unsigned int results[3][5] = {0};
123 pthread_t threads[3][5] = {0};
125 for (int i = 0; i < 3; i++)
126 for (int j = 0; j < 5; j++)
127 pthread_create(&threads[i][j], NULL, funcs[i], &results[i][j]);
132 for (int i = 0; i < 3; i++)
133 for (int j = 0; j < 5; j++) {
134 pthread_join(threads[i][j], NULL);
137 results[i][0] += results[i][j];
140 map_cnl(&map, (void *) &refcount_drp, NULL, NULL, 0);
143 printf("Time: 10 seconds\n");
144 printf("Created objects: %u\n", results[0][0]);
145 printf("Accessed objects: %u\n", results[1][0]);
146 printf("Deleted objects: %u\n", results[2][0]);