9 #include "../refcount.h"
24 void delete_obj(DataObject *obj)
26 refcount_dst(&obj->rc);
30 int cmp_obj(const Refcount *rc, const int *id)
32 return ((DataObject *) rc->obj)->id - *id;
35 static void *thread_create(unsigned int *result)
38 DataObject *obj = malloc(sizeof *obj);
41 refcount_ini(&obj->rc, obj, &delete_obj);
43 if (map_add(&map, &obj->id, &obj->rc, &cmp_obj, &refcount_inc))
46 refcount_drp(&obj->rc);
54 static void *thread_access(unsigned int *result)
56 DataObject *objs[NUM_OBJS] = {NULL};
59 int i = rand() % NUM_OBJS;
62 refcount_drp(&objs[i]->rc);
66 while (!objs[i] && !cancel) {
68 objs[i] = map_get(&map, &id, &cmp_obj, &refcount_grb);
75 for (int i = 0; i < NUM_OBJS; i++)
77 refcount_drp(&objs[i]->rc);
84 static void *thread_delete(unsigned int *result)
87 unsigned int id = rand_id();
89 if (map_del(&map, &id, &cmp_obj, &refcount_drp, NULL, NULL))
100 printf("------------------------\n");
101 printf("Testing Map and Refcount\n");
102 printf("------------------------\n");
106 void *(*funcs[3])(void *) = {
107 (void *) &thread_create,
108 (void *) &thread_access,
109 (void *) &thread_delete,
112 unsigned int results[3][5] = {0};
113 pthread_t threads[3][5] = {0};
115 for (int i = 0; i < 3; i++)
116 for (int j = 0; j < 5; j++)
117 pthread_create(&threads[i][j], NULL, funcs[i], &results[i][j]);
122 for (int i = 0; i < 3; i++)
123 for (int j = 0; j < 5; j++) {
124 pthread_join(threads[i][j], NULL);
127 results[i][0] += results[i][j];
130 map_cnl(&map, &refcount_drp, NULL, NULL, 0);
133 printf("Time: 1 second\n");
134 printf("Created objects: %u\n", results[0][0]);
135 printf("Accessed objects: %u\n", results[1][0]);
136 printf("Deleted objects: %u\n", results[2][0]);