int get_resource_id(struct resource *resource);
int get_resource_type(struct resource *resource);
int get_resource_ts(struct resource *resource, int64_t *ts_start, int64_t *ts_end);
+void init_resource_id(void);
int set_resource_privdata(struct resource *resource, void *priv);
#include <glib.h>
#include <stdio.h>
#include <json.h>
+#include <sys/time.h>
#include <util/common.h>
#include <util/resource.h>
do_delete_resource(resource);
}
+static int clear_sign_bit(unsigned int val)
+{
+ return (int)((val << 1) >> 1);
+}
+
+void init_resource_id(void)
+{
+ int val;
+ struct timeval tv;
+
+ /*
+ * Initial g_resource_id is set by a combination of
+ * tv_sec and tv_usec values of gettimeofday().
+ * It ensures that PASS assigns a unique resource id
+ * for each create_resource request.
+ *
+ * Even PASS is restarted, a newly initialized value
+ * of g_resource_id reflects the current value of
+ * gettimeofday(). Thus, the uniqueness in resource
+ * id is kept unless the number of created resources
+ * crosses the time consumed (in msecs) before
+ * restarted.
+ */
+ gettimeofday(&tv, NULL);
+ val = tv.tv_sec * 1000 + tv.tv_usec / 1000;
+ g_resource_id = clear_sign_bit(val);
+}
+
struct resource *create_resource(int resource_type)
{
const struct resource_driver *driver = NULL;
if (!resource)
return NULL;
- resource->id = __sync_fetch_and_add(&g_resource_id, 1);
+ resource->id = clear_sign_bit(
+ (unsigned int)__sync_fetch_and_add(&g_resource_id, 1));
resource->type = resource_type;
resource->name = g_strdup(driver->name);
resource->driver = driver;