X-Git-Url: https://pd.if.org/git/?p=uuid;a=blobdiff_plain;f=internal.c;fp=internal.c;h=02f3e8720b58bfc2cc3a6ab95f6b168355bcaf69;hp=ef0115b86db6e6e383d019d981ac1c88425077b3;hb=6c2a66816de51f195e4cae40406834f519768099;hpb=04180d5e69c3d727e5837fff1224fbf4b244fe20 diff --git a/internal.c b/internal.c index ef0115b..02f3e87 100644 --- a/internal.c +++ b/internal.c @@ -370,30 +370,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; }