This source file includes following definitions.
- local_eviction_callback
- pmix_hotel_init
- constructor
- destructor
1
2
3
4
5
6
7
8
9
10
11
12
13 #include <src/include/pmix_config.h>
14
15 #include <stdio.h>
16 #include <stddef.h>
17
18 #include PMIX_EVENT_HEADER
19 #include "src/class/pmix_hotel.h"
20
21
22 static void local_eviction_callback(int fd, short flags, void *arg)
23 {
24 pmix_hotel_room_eviction_callback_arg_t *eargs =
25 (pmix_hotel_room_eviction_callback_arg_t*) arg;
26 void *occupant = eargs->hotel->rooms[eargs->room_num].occupant;
27
28
29
30
31
32
33 pmix_hotel_t *hotel = eargs->hotel;
34 pmix_hotel_room_t *room = &(hotel->rooms[eargs->room_num]);
35 room->occupant = NULL;
36 hotel->last_unoccupied_room++;
37 assert(hotel->last_unoccupied_room < hotel->num_rooms);
38 hotel->unoccupied_rooms[hotel->last_unoccupied_room] = eargs->room_num;
39
40
41 hotel->evict_callback_fn(hotel,
42 eargs->room_num,
43 occupant);
44 }
45
46
47 pmix_status_t pmix_hotel_init(pmix_hotel_t *h, int num_rooms,
48 pmix_event_base_t *evbase,
49 uint32_t eviction_timeout,
50 pmix_hotel_eviction_callback_fn_t evict_callback_fn)
51 {
52 int i;
53
54
55 if (num_rooms <= 0 ||
56 NULL == evict_callback_fn) {
57 return PMIX_ERR_BAD_PARAM;
58 }
59
60 h->num_rooms = num_rooms;
61 h->evbase = evbase;
62 h->eviction_timeout.tv_usec = eviction_timeout % 1000000;
63 h->eviction_timeout.tv_sec = eviction_timeout / 1000000;
64 h->evict_callback_fn = evict_callback_fn;
65 h->rooms = (pmix_hotel_room_t*)malloc(num_rooms * sizeof(pmix_hotel_room_t));
66 if (NULL != evict_callback_fn) {
67 h->eviction_args =
68 (pmix_hotel_room_eviction_callback_arg_t*)malloc(num_rooms * sizeof(pmix_hotel_room_eviction_callback_arg_t));
69 }
70 h->unoccupied_rooms = (int*) malloc(num_rooms * sizeof(int));
71 h->last_unoccupied_room = num_rooms - 1;
72
73 for (i = 0; i < num_rooms; ++i) {
74
75 h->rooms[i].occupant = NULL;
76
77
78 h->unoccupied_rooms[i] = i;
79
80
81 h->eviction_args[i].hotel = h;
82 h->eviction_args[i].room_num = i;
83
84
85 if (NULL != h->evbase) {
86 pmix_event_assign(&(h->rooms[i].eviction_timer_event),
87 h->evbase,
88 -1, 0, local_eviction_callback,
89 &(h->eviction_args[i]));
90 }
91 }
92
93 return PMIX_SUCCESS;
94 }
95
96 static void constructor(pmix_hotel_t *h)
97 {
98 h->num_rooms = 0;
99 h->evbase = NULL;
100 h->eviction_timeout.tv_sec = 0;
101 h->eviction_timeout.tv_usec = 0;
102 h->evict_callback_fn = NULL;
103 h->rooms = NULL;
104 h->eviction_args = NULL;
105 h->unoccupied_rooms = NULL;
106 h->last_unoccupied_room = -1;
107 }
108
109 static void destructor(pmix_hotel_t *h)
110 {
111 int i;
112
113
114 if (NULL != h->evbase) {
115 for (i = 0; i < h->num_rooms; ++i) {
116 if (NULL != h->rooms[i].occupant) {
117 pmix_event_del(&(h->rooms[i].eviction_timer_event));
118 }
119 }
120 }
121
122 if (NULL != h->rooms) {
123 free(h->rooms);
124 }
125 if (NULL != h->eviction_args) {
126 free(h->eviction_args);
127 }
128 if (NULL != h->unoccupied_rooms) {
129 free(h->unoccupied_rooms);
130 }
131 }
132
133 PMIX_CLASS_INSTANCE(pmix_hotel_t,
134 pmix_object_t,
135 constructor,
136 destructor);