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 int cmp_obj_sym(const Refcount *rc1, const Refcount *rc2)
37 return cmp_obj(rc1, &((DataObject *) rc2->obj)->id);
40 static void *thread_create(unsigned int *result)
43 DataObject *obj = malloc(sizeof *obj);
46 refcount_ini(&obj->rc, obj, &delete_obj);
48 if (map_add(&map, &obj->rc, &cmp_obj_sym, &refcount_inc))
51 refcount_drp(&obj->rc);
59 static void *thread_access(unsigned int *result)
61 DataObject *objs[NUM_OBJS] = {NULL};
64 int i = rand() % NUM_OBJS;
67 refcount_drp(&objs[i]->rc);
71 while (!objs[i] && !cancel) {
73 objs[i] = map_get(&map, &id, &cmp_obj, &refcount_grb);
80 for (int i = 0; i < NUM_OBJS; i++)
82 refcount_drp(&objs[i]->rc);
89 static void *thread_delete(unsigned int *result)
92 unsigned int id = rand_id();
94 if (map_del(&map, &id, &cmp_obj, &refcount_drp, NULL, NULL))
105 printf("------------------------\n");
106 printf("Testing Map and Refcount\n");
107 printf("------------------------\n");
111 void *(*funcs[3])(void *) = {
112 (void *) &thread_create,
113 (void *) &thread_access,
114 (void *) &thread_delete,
117 unsigned int results[3][5] = {0};
118 pthread_t threads[3][5] = {0};
120 for (int i = 0; i < 3; i++)
121 for (int j = 0; j < 5; j++)
122 pthread_create(&threads[i][j], NULL, funcs[i], &results[i][j]);
127 for (int i = 0; i < 3; i++)
128 for (int j = 0; j < 5; j++) {
129 pthread_join(threads[i][j], NULL);
132 results[i][0] += results[i][j];
135 map_cnl(&map, &refcount_drp, NULL, NULL, 0);
138 printf("Time: 1 second\n");
139 printf("Created objects: %u\n", results[0][0]);
140 printf("Accessed objects: %u\n", results[1][0]);
141 printf("Deleted objects: %u\n", results[2][0]);