1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
2 /*
3  * Copyright (c) 2012-2016 Cisco Systems, Inc.  All rights reserved.
4  * Copyright (c) 2012      Los Alamos National Security, LLC. All rights reserved
5  * Copyright (c) 2015-2019 Intel, Inc.  All rights reserved.
6  * $COPYRIGHT$
7  *
8  * Additional copyrights may follow
9  *
10  * $HEADER$
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 
local_eviction_callback(int fd,short flags,void * arg)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     /* Remove the occurpant from the room.
29 
30        Do not change this logic without also changing the same logic
31        in pmix_hotel_checkout() and
32        pmix_hotel_checkout_and_return_occupant(). */
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     /* Invoke the user callback to tell them that they were evicted */
41     hotel->evict_callback_fn(hotel,
42                              eargs->room_num,
43                              occupant);
44 }
45 
46 
pmix_hotel_init(pmix_hotel_t * h,int num_rooms,pmix_event_base_t * evbase,uint32_t eviction_timeout,pmix_hotel_eviction_callback_fn_t evict_callback_fn)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     /* Bozo check */
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         /* Mark this room as unoccupied */
75         h->rooms[i].occupant = NULL;
76 
77         /* Setup this room in the unoccupied index array */
78         h->unoccupied_rooms[i] = i;
79 
80         /* Setup the eviction callback args */
81         h->eviction_args[i].hotel = h;
82         h->eviction_args[i].room_num = i;
83 
84         /* Create this room's event (but don't add it) */
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 
constructor(pmix_hotel_t * h)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 
destructor(pmix_hotel_t * h)109 static void destructor(pmix_hotel_t *h)
110 {
111     int i;
112 
113     /* Go through all occupied rooms and destroy their events */
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);
137