X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=internal.c;h=59e1bbf402d05b3416b1c1323e625f1f095b9e16;hb=86b30d90a32ec2f409ddfff4fb8f1a2b6a77413f;hp=0ad2b81fe611a237a4a63da078df08c67d52971e;hpb=c784264fc1aaa6a22eb718020ef5886087abd525;p=uuid diff --git a/internal.c b/internal.c index 0ad2b81..59e1bbf 100644 --- a/internal.c +++ b/internal.c @@ -17,6 +17,7 @@ #include #include #include +#include #endif #ifdef __APPLE__ @@ -186,7 +187,6 @@ static uint64_t current_node(struct pd_uuid_state *st) { continue; } if (ioctl(s, SIOCGIFHWADDR, req) == 0) { - int j; data = (unsigned char *)req->ifr_hwaddr.sa_data; node = data[0]; node = node << 8; node += data[1]; @@ -194,10 +194,12 @@ static uint64_t current_node(struct pd_uuid_state *st) { node = node << 8; node += data[3]; node = node << 8; node += data[4]; node = node << 8; node += data[5]; + close(s); return node; } } } + close(s); } #endif @@ -344,6 +346,7 @@ int pd_uuid_make_v1(struct pd_uuid_state *s, pd_uuid_t *uuid) { if (!s->available || s->node != node) { s->clock_sequence = random_clock_sequence(s); } + s->node = node; if (s->available && s->timestamp > now) { @@ -371,30 +374,60 @@ int pd_uuid_make_v1(struct pd_uuid_state *s, pd_uuid_t *uuid) { return 1; } -int pd_uuid_make_v1mc(struct pd_uuid_state *ps, pd_uuid_t *uuid) { - struct pd_uuid_state s; +int pd_uuid_make_v1mc(struct pd_uuid_state *s, pd_uuid_t *uuid) { + struct pd_uuid_state ls; uint64_t now; uint64_t node; + int err; + + if (!s) { + s = &ls; + pd_uuid_init_state(s); + } + + if (s->get_lock) { + if ((err = s->get_lock(s->lock_data)) != 0) { + /* TODO set uuid to nil ? */ + /* be cute and have an "error" uuid? */ + return 0; + } + } + + if (s->read_state) { + if ((err = s->read_state(s)) != 0) { + return 0; + } + } - obtain_global_lock(0); - read_state(&s); now = current_time(); - node = random_mc_mac(&s); - if (!s.available) { - s.clock_sequence = random_clock_sequence(&s); + node = random_mc_mac(s); + + if (!s->available || s->node != node) { + s->clock_sequence = random_clock_sequence(s); } + s->node = node; - if (s.available && s.timestamp > now) { - s.clock_sequence++; + if (s->available && s->timestamp > now) { + s->clock_sequence++; } else { - s.timestamp = now; + s->timestamp = now; } - save_state(&s); - release_global_lock(0); - s.node = node; + if (s->save_state) { + if ((err = s->save_state(s)) != 0) { + return 0; + } + } + + if (s->release_lock) { + if ((err = s->release_lock(s->lock_data)) != 0) { + /* TODO set uuid to nil ? */ + /* be cute and have an "error" uuid? */ + return 0; + } + } - format_uuid(uuid, &s, 1); + format_uuid(uuid, s, 1); return 1; }